Class: UPM::Tool

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/upm/tool.rb,
lib/upm/tool_dsl.rb,
lib/upm/tool_class_methods.rb

Defined Under Namespace

Modules: DSL

Constant Summary collapse

COMMAND_HELP =

TODO: Show unlisted commands

{
  "install"          => "install a package",
  "remove/uninstall" => "remove a package",
  "search"           => "search packages",
  "search-sources"   => "search package source (for use with 'build' command)",
  "build"            => "build a package from source and install it",
  "list"             => "list installed packages (or search their names if extra arguments are supplied)",
  "selection/manual" => "list manually installed packages", # this should probably be a `list` option ("upm list --manually-added" or smth (would be nice: rewrite in go and use ipfs' arg parsing library))
  "files"            => "list files in a package",
  "rdeps/depends"    => "reverse dependencies (which packages depend on this package?)",
  "locate"           => "search contents of packages (local or remote)",
  "info/show"        => "show metadata about a package",
  "update/sync"      => "retrieve the latest package list or manifest",
  "upgrade"          => "update package list and install updates",
  "selfupdate"       => "update the package manager",
  "download"         => "download package list and updates, but don't insatall them",
  "pin"              => "pinning a package means it won't be automatically upgraded",
  "rollback"         => "revert to an earlier version of a package (including its dependencies)",
  "verify/check"     => "verify the integrity of packages' files on the filesystem",
  "repair"           => "fix corrupted packages",
  "audit/vulns"      => "show known vulnerabilities in installed packages",
  "log"              => "show history of package installs",
  "packagers"        => "detect installed package managers, and pick which ones upm should wrap",
  "clean"            => "clear out the local package cache",
  "monitor"          => "ad-hoc package manager for custom installations (like instmon)",
  "keys"             => "keyrings and package authentication",
  "default"          => "configure the action to take when no arguments are passed to 'upm' (defaults to 'os:update')",
  "stats"            => "show statistics about package database(s)",
  "repos/mirrors/sources/channels" => "manage subscriptions to remote repositories/mirrors/channels",
}
ALIASES =
{
  "u"             => "upgrade",
  "i"             => "install",
  "d"             => "download",
  "s"             => "search",
  "f"             => "files",
  "r"             => "remove",
  "m"             => "mirrors",
  "file"          => "files",
  "vuln"          => "audit",
  "source-search" => "search-sources",
}
@@tools =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#cache_dir, #call_command, #command, #config_dir, #curl, #database_age, #database_lastupdate, #database_lastupdate_file, #database_needs_updating?, #database_updated!, #help, #identifying_binary, #max_database_age, #os, #prefix, #print_files, #run, #set_default

Constructor Details

#initialize(name, &block) ⇒ Tool

Returns a new instance of Tool.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/upm/tool.rb', line 72

def initialize(name, &block)
  @name = name

  set_default :cache_dir, "~/.cache/upm"
  set_default :config_dir, "~/.cache/upm"
  set_default :max_database_age, 15*60 # 15 minutes

  instance_eval(&block)

  @@tools[name] = self
end

Class Method Details

.current_os_namesObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/upm/tool_class_methods.rb', line 27

def current_os_names
  # eg: ID=ubuntu, ID_LIKE=debian
  if os_release
    os_release.values_at("ID", "ID_LIKE").compact
  else
    # `uname -s` => Darwin|FreeBSD|OpenBSD
    # `uname -o` => Android|Cygwin
    names = [`uname -s`]
    names << `uname -o` unless names.first =~ /OpenBSD/
    names.map(&:chomp).uniq
  end
end

.error(message) ⇒ Object



5
6
7
8
# File 'lib/upm/tool_class_methods.rb', line 5

def error(message)
  $stderr.puts message
  exit 1
end

.for_os(os_names = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/upm/tool_class_methods.rb', line 52

def for_os(os_names=nil)
  os_names = os_names ? [os_names].flatten : current_os_names

  tool = nil

  if os_names.any?
    tool = @@tools.find { |name, tool| os_names.any? { |name| tool.os&.include? name } }
  end

  if tool.nil?
    tool = @@tools.find { |name, tool| File.which(tool.identifying_binary) }
  end

  tool&.last
end

.installedObject



48
49
50
# File 'lib/upm/tool_class_methods.rb', line 48

def installed
  @@tools.select { |tool| File.which(tool.identifying_binary) }
end

.nice_os_nameObject



40
41
42
43
44
45
46
# File 'lib/upm/tool_class_methods.rb', line 40

def nice_os_name
  if os_release
    os_release.values_at("PRETTY_NAME", "NAME", "ID", "ID_LIKE").first
  else
    (`uname -o 2> /dev/null`.chomp rescue nil)
  end
end

.os_releaseObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/upm/tool_class_methods.rb', line 16

def os_release
  @os_release ||= begin
    pairs = open("/etc/os-release") do |io|
      io.read.scan(/^(\w+)="?(.+?)"?$/)
    end
    Hash[pairs]
  rescue Errno::ENOENT
    nil
  end
end

.register_tools!Object



12
13
14
# File 'lib/upm/tool_class_methods.rb', line 12

def register_tools!
  Dir["#{__dir__}/tools/*.rb"].each { |lib| require_relative(lib) }
end

.toolsObject



10
# File 'lib/upm/tool_class_methods.rb', line 10

def tools; @@tools; end