Commit 942dc67905f52cd6f0e0392ca510be22932ce805

Thomas de Grivel 2023-02-23T22:42:02

required_by

diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 00b0da4..4fe69d6 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -240,9 +240,9 @@ module Rbpkg
 
   @@sha256 = nil
   def self.sha256
-    @@sha256 || @@sha256 = if cmd "which sha256"
+    @@sha256 || @@sha256 = if system("which sha256 >/dev/null 2>&1")
                              "sha256"
-                           elsif cmd "which sha256sum"
+                           elsif system("which sha256sum >/dev/null 2>&1")
                              "sha256sum"
                            else
                              raise "sha256 executable not found"
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index 1753b4f..083e3c3 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -118,10 +118,14 @@ class Rbpkg::Repo
     end
   end
 
-  def dependencies
+  def self.dependencies
     nil
   end
 
+  def dependencies
+    self.class.dependencies
+  end
+
   def dir
     "#{File.basename(File.dirname(git_url))}/#{name}"
   end
@@ -196,7 +200,9 @@ class Rbpkg::Repo
  head: #{head.inspect},
  hash: #{hash.inspect},
  version:           #{version.inspect},
- installed_version: #{installed_version.inspect}
+ installed_version: #{installed_version.inspect},
+ dependencies: #{dependencies.inspect},
+ required_by: #{required_by.inspect}
 }"""
   end
 
@@ -325,6 +331,20 @@ class Rbpkg::Repo
     cmd! "cd #{sh_quote(src_dir!)} && git pull && git submodule update"
   end
 
+  @@required_by = nil
+  def required_by
+    @@required_by || @@required_by =
+      begin
+        r = []
+        Rbpkg::Repos.repos.each do |repo_name, repo_class|
+          if repo_class.dependencies&.include?(name)
+            r << repo_name
+          end
+        end
+        r
+      end
+  end
+
   def src_dir
     "#{Rbpkg.src_dir}/#{dir}"
   end
@@ -382,6 +402,7 @@ class Rbpkg::Repo
   def uninstall
     verbose 3, "repo(#{name}).uninstall"
     if installed?
+      uninstall_required_by
       File.open(installed, "r") do |input|
         while line = input.gets
           if (scan = line.scan(/^(SHA256 \((.*)\) = ([0-9A-Fa-f]*))$/)[0])
@@ -400,6 +421,13 @@ class Rbpkg::Repo
     end
   end
 
+  def uninstall_required_by
+    verbose 3, "repo(#{name}).uninstall_required_by"
+    required_by.each do |req|
+      Rbpkg.Repos.repo(req).uninstall
+    end
+  end
+
   def upgrade
     verbose 3, "repo(#{name}).upgrade"
     pull
diff --git a/lib/rbpkg/repos.rb b/lib/rbpkg/repos.rb
index e3a96eb..6fa88c7 100644
--- a/lib/rbpkg/repos.rb
+++ b/lib/rbpkg/repos.rb
@@ -20,6 +20,9 @@ module Rbpkg
       end
     end
 
+    def self.repos
+      @@repo_class.sort
+    end
   end
 end
 
diff --git a/lib/rbpkg/repos/c3.rb b/lib/rbpkg/repos/c3.rb
index f135215..aa241ce 100644
--- a/lib/rbpkg/repos/c3.rb
+++ b/lib/rbpkg/repos/c3.rb
@@ -2,7 +2,7 @@ class Rbpkg::Repos::C3 < Rbpkg::Repo
 
   def_name "c3"
 
-  def dependencies
+  def self.dependencies
     ["libbsd", "libmd"]
   end
 
diff --git a/lib/rbpkg/repos/libbsd.rb b/lib/rbpkg/repos/libbsd.rb
index 4142f9f..bbfc966 100644
--- a/lib/rbpkg/repos/libbsd.rb
+++ b/lib/rbpkg/repos/libbsd.rb
@@ -6,7 +6,7 @@ class Rbpkg::Repos::Libbsd < Rbpkg::Repo
     "main"
   end
 
-  def dependencies
+  def self.dependencies
     ["libmd"]
   end