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 =
{
  "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)",
  "info"             => "show metadata about a package",
  "update/sync"      => "retrieve the latest package list or manifest",
  "upgrade"          => "update package list and install updates",
  "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",
  "audit/vuln"       => "show known vulnerabilities in installed packages",
  "log"              => "show history of package installs",
  "packagers"        => "detect installed package managers, and pick which ones upm should wrap",
  "mirrors/sources"  => "manage remote repositories and mirrors",
  "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)",
}
ALIASES =
{
  "file"    => "files",
  "sync"    => "update",
  "sources" => "mirrors",
  "show"    => "info",
  "vuln"    => "audit",
  "vulns"   => "audit",
  "check"   => "verify",
  "u"       => "upgrade",
  "i"       => "install",
  "d"       => "download",
  "s"       => "search",
  "f"       => "files",
  "r"       => "remove",
  "source-search" => "search-sources",
}
@@tools =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#call_command, #command, #curl, #help, #identifying_binary, #os, #prefix, #print_files, #run

Constructor Details

#initialize(name, &block) ⇒ Tool

Returns a new instance of Tool.



61
62
63
64
65
66
# File 'lib/upm/tool.rb', line 61

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

  @@tools[name] = self
end

Class Method Details

.current_os_namesObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/upm/tool_class_methods.rb', line 21

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
    [`uname -s`, `uname -o`].map(&:chomp).uniq
  end
end

.for_os(os_names = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/upm/tool_class_methods.rb', line 41

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 } }&.last
  end

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

  tool
end

.installedObject



37
38
39
# File 'lib/upm/tool_class_methods.rb', line 37

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

.nice_os_nameObject



32
33
34
35
# File 'lib/upm/tool_class_methods.rb', line 32

def nice_os_name
  os_release.values_at("PRETTY_NAME", "NAME", "ID", "ID_LIKE").first || 
    (`uname -o`.chomp rescue nil)
end

.os_releaseObject



11
12
13
14
15
16
17
18
19
# File 'lib/upm/tool_class_methods.rb', line 11

def os_release
  @os_release ||= begin
    open("/etc/os-release") do |io|
      io.read.scan(/^(\w+)="?(.+?)"?$/)
    end.to_h
  rescue Errno::ENOENT
    {}
  end
end

.register_tools!Object



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

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

.toolsObject



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

def tools; @@tools; end