Commit b1ff8994927da6fdbd0e3f31a026edf58e3ab9cb

Thomas de Grivel 2023-02-20T01:54:53

rbpkg clone c3

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4f00cd9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/src/
diff --git a/bin/rbpkg b/bin/rbpkg
index 974764d..7cceb0a 100755
--- a/bin/rbpkg
+++ b/bin/rbpkg
@@ -1,11 +1,8 @@
 #!/usr/bin/env ruby
-require '../lib/rbpkg'
-
-$me = ARGV[0]
-$args = ARGV[1..]
+require __FILE__ + '/../../lib/rbpkg'
 
 def usage()
-  STDERR.puts """Usage: #{$me} COMMAND PKG ...
+  STDERR.puts """Usage: #{File.basename($0)} COMMAND PKG ...
 
 Source directory commands :
  clone            shortcut for git clone
@@ -14,8 +11,43 @@ Source directory commands :
   1
 end
 
-case $args[0]
-when "-h"
-when "--help"
-  exit usage
+def cmd(string)
+  verbose(1, string)
+  `#{string}` || exit(1)
+end
+
+def rbpkg(args)
+  case args[0]
+  when nil
+    usage()
+  when "-h"
+    usage()
+  when "--help"
+    usage()
+  when "clone"
+    Rbpkg.git_clone(args[1..])
+  when "clean-sources"
+    Rbpkg.clean_sources(args[1..])
+  else
+    usage
+  end
 end
+
+def sh_quote (str)
+  if str.scan("\n") == []
+    if str.match?(/^[-+\/=.,:^_0-9A-Za-z]*$/)
+      str
+    else
+      "\"" + str.gsub(/([$`\\\"])/, "\\\1") + "\""
+    end
+  end
+end
+      
+def verbose(level, msg)
+  case level
+  when 1
+    puts "\33[0;34m#{msg}\33[0m\n"
+  end
+end
+
+rbpkg ARGV
diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 03bd322..8307584 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -1,3 +1,24 @@
+require __FILE__ + '/../rbpkg/repos'
+
 module Rbpkg
 
+  def self.clean_sources(repos)
+    repos.each do |name|
+      Repos.repo(name).clean_sources()
+    end
+  end
+
+  def self.dir
+    ENV["RBPKG_DIR"] || "#{ENV["HOME"]}/rbpkg"
+  end
+
+  def self.git_clone(repos)
+    repos.each do |name|
+      Repos.repo(name).git_clone()
+    end
+  end
+
+  def self.src_dir
+    "#{dir}/src"
+  end
 end
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
new file mode 100644
index 0000000..6edd265
--- /dev/null
+++ b/lib/rbpkg/repo.rb
@@ -0,0 +1,40 @@
+require 'fileutils'
+
+class Rbpkg::Repo
+
+  def dir
+    "#{File.basename(File.dirname(git_url))}/#{name}"
+  end
+
+  def git_clone
+    puts "name: #{name.inspect}"
+    puts "dir: #{dir.inspect}"
+    puts "git_url: #{git_url.inspect}"
+    puts "src_dir: #{src_dir.inspect}"
+    puts "src_parent_dir: #{src_parent_dir.inspect}"
+    if File.directory?(src_dir)
+      false
+    else
+      if ! File.directory?(src_parent_dir)
+        cmd "mkdir -p #{sh_quote(src_parent_dir)}"
+      end
+      cmd "cd #{sh_quote(src_parent_dir)} && git clone #{sh_quote(git_url)} #{sh_quote(name)}"
+    end
+  end
+
+  def git_url
+    raise "no git url for #{self}"
+  end
+
+  def name
+    self.class.name.to_s.downcase.scan(/::([^:]*)$/)[0][0]
+  end
+
+  def src_dir
+    "#{Rbpkg.src_dir}/#{dir}"
+  end
+
+  def src_parent_dir
+    File.dirname(src_dir)
+  end
+end
diff --git a/lib/rbpkg/repos.rb b/lib/rbpkg/repos.rb
new file mode 100644
index 0000000..b1d3d05
--- /dev/null
+++ b/lib/rbpkg/repos.rb
@@ -0,0 +1,28 @@
+module Rbpkg
+  module Repos
+
+    @@repo_class = {}
+    @@repo = {}
+
+    def self.define_repo(name, klass)
+      raise "repo already exists: #{name.inspect}" if @@repo_class[name]
+      @@repo_class[name] = klass
+    end
+
+    def self.repo(name)
+      r = @@repo[name]
+      if r
+        r
+      else
+        klass = @@repo_class[name]
+        raise "unknown repo: #{name.inspect}" unless klass
+        @@repo[name] = klass.new
+      end
+    end
+
+  end
+end
+
+Dir.glob("*.rb", base: "#{File.dirname(__FILE__)}/repos").each do |r|
+  require "#{File.dirname(__FILE__)}/repos/#{r}"
+end
diff --git a/lib/rbpkg/repos/c3.rb b/lib/rbpkg/repos/c3.rb
new file mode 100644
index 0000000..8a6340c
--- /dev/null
+++ b/lib/rbpkg/repos/c3.rb
@@ -0,0 +1,11 @@
+require "#{__FILE__}/../../repo"
+
+class Rbpkg::Repos::C3 < Rbpkg::Repo
+
+  def git_url
+    "https://git.kmx.io/c3-lang/c3.git"
+  end
+
+end
+
+Rbpkg::Repos.define_repo("c3", Rbpkg::Repos::C3)
diff --git a/rbpkg.gemspec b/rbpkg.gemspec
new file mode 100644
index 0000000..d511a02
--- /dev/null
+++ b/rbpkg.gemspec
@@ -0,0 +1,11 @@
+Gem::Specification.new do |s|
+  s.name        = "rbpkg"
+  s.version     = "0.1.0"
+  s.summary     = "Hola!"
+  s.description = "Ruby package manager"
+  s.authors     = ["Thomas de Grivel"]
+  s.email       = "thodg@kmx.io"
+  s.files       = ["lib/rbpkg.rb"]
+  s.homepage    = "https://git.kmx.io/kmx.io/rbpkg"
+  s.license     = "BSD-2"
+end