Module: UPM::Tool::DSL

Included in:
UPM::Tool
Defined in:
lib/upm/tool_dsl.rb

Instance Method Summary collapse

Instance Method Details

#cache_dir(dir) ⇒ Object



27
28
29
30
# File 'lib/upm/tool_dsl.rb', line 27

def cache_dir(dir)
  @cache_dir = Pathname.new(dir).expand_path
  @cache_dir.mkpath unless @cache_dir.exist?
end

#call_command(name, *args) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/upm/tool_dsl.rb', line 153

def call_command(name, *args)
  if block = (@cmds[name] || @cmds[ALIASES[name]])
    block.call args
  else
    puts "Command #{name.inspect} not supported by #{@name.inspect}"
  end
end

#command(name, shell_command = nil, root: false, paged: false, highlight: nil, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/upm/tool_dsl.rb', line 41

def command(name, shell_command=nil, root: false, paged: false, highlight: nil, &block)
  @cmds ||= {}

  if block_given?

    if root and Process.uid != 0
      @cmds[name] = proc { exec("sudo", $PROGRAM_NAME, *ARGV) }
    else
      @cmds[name] = block
    end

  elsif shell_command

    if shell_command.is_a? String
      shell_command = shell_command.split
    elsif not shell_command.is_a? Array
      raise "Error: command argument must be a String or an Array; it was a #{cmd.class}"
    end

    @cmds[name] = proc do |args|
      query = highlight ? args.join("\\s+") : nil
      run(*shell_command, *args, paged: paged, root: root, highlight: query)
    end

  else
    raise "Error: Must supply a block or shell command"
  end
end

#config_dir(dir) ⇒ Object



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

def config_dir(dir)
  @config_dir = Pathname.new(dir).expand_path
  @config_dir.mkpath unless @config_dir.exist?
end

#curl(url) ⇒ Object



131
132
133
134
135
136
# File 'lib/upm/tool_dsl.rb', line 131

def curl(url)
  IO.popen(["curl", "-Ss", url], &:read)
rescue Errno::ENOENT
  puts "Error: 'curl' isn't installed. You need this!"
  exit 1
end

#database_ageObject



175
176
177
# File 'lib/upm/tool_dsl.rb', line 175

def database_age
  Time.now.to_i - database_lastupdate.to_i
end

#database_lastupdateObject



171
172
173
# File 'lib/upm/tool_dsl.rb', line 171

def database_lastupdate
  database_lastupdate_file.exist? ? File.mtime(database_lastupdate_file) : 0
end

#database_lastupdate_fileObject



161
162
163
164
165
# File 'lib/upm/tool_dsl.rb', line 161

def database_lastupdate_file
  raise "Error: Tool 'name' is not set" unless @name
  raise "Error: 'cache_dir' is not set" unless @cache_dir
  @cache_dir/"#{@name}-last-update"
end

#database_needs_updating?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/upm/tool_dsl.rb', line 179

def database_needs_updating?
  database_age > @max_database_age
end

#database_updated!Object



167
168
169
# File 'lib/upm/tool_dsl.rb', line 167

def database_updated!
  FileUtils.touch(database_lastupdate_file)
end

#helpObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/upm/tool_dsl.rb', line 183

def help
  puts "    UPM version: #{File.read("#{__dir__}/../../VERSION")}"
  if osname = Tool.nice_os_name
    puts "    Detected OS: #{osname}"
  end

  puts "Package manager: #{@name}"
  puts
  puts "Available commands:"
  available = COMMAND_HELP.select do |name, desc|
    names = name.split("/")
    names.any? { |name| @cmds[name] }
  end

  max_width = available.map(&:first).map(&:size).max
  available.each do |name, desc|
    puts "  #{name.rjust(max_width)} | #{desc}"
  end
end

#identifying_binary(id_bin = nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/upm/tool_dsl.rb', line 7

def identifying_binary(id_bin=nil)
  if id_bin
    @id_bin = id_bin
  else
    @id_bin || @name
  end
end

#max_database_age(age) ⇒ Object



23
24
25
# File 'lib/upm/tool_dsl.rb', line 23

def max_database_age(age)
  @max_database_age = age.to_i
end

#os(*args) ⇒ Object



19
20
21
# File 'lib/upm/tool_dsl.rb', line 19

def os(*args)
  args.any? ? @os = args : @os
end

#prefix(name) ⇒ Object



15
16
17
# File 'lib/upm/tool_dsl.rb', line 15

def prefix(name)
  @prefix = name
end


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/upm/tool_dsl.rb', line 138

def print_files(*paths, include: nil, exclude: nil)
  lesspipe do |less|
    paths.each do |path|
      less.puts "<8>=== <11>#{path} <8>========".colorize
      open(path) do |io|
        enum = io.each_line
        enum = enum.grep(include) if include
        enum = enum.reject { |line| line[exclude] } if exclude
        enum.each { |line| less.puts line }
      end
      less.puts
    end
  end
end

#run(*args, root: false, paged: false, grep: nil, highlight: nil, sort: false) ⇒ Object

Helpers



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/upm/tool_dsl.rb', line 72

def run(*args, root: false, paged: false, grep: nil, highlight: nil, sort: false)
  if root
    if Process.uid != 0
      if File.which("sudo")
        args.unshift "sudo"
      elsif File.which("su")
        args = ["su", "-c"] + args
      else
        raise "Error: You must be root to run this command. (And I couldn't find the 'sudo' *or* 'su' commands.)"
      end
    end
  end


  unless paged or grep or sort
    system(*args)
  else
    IO.popen(args, err: [:child, :out]) do |command_io|

      # if grep
      #   pattern = grep.is_a?(Regexp) ? grep.source : grep.to_s
      #   grep_io = IO.popen(["grep", "--color=always", "-Ei", pattern], "w+")
      #   IO.copy_stream(command_io, grep_io)
      #   grep_io.close_write
      #   command_io = grep_io
      # end

      # if paged
      #   lesspipe do |less|
      #     IO.copy_stream(command_io, less)
      #   end
      # else
      #   IO.copy_stream(command_io, STDOUT)
      # end

      # highlight_proc = if highlight
      #   proc { |line| line.gsub(highlight) { |m| "\e[33;1m#{m}\e[0m" } }
      # else
      #   proc { |line| line }
      # end

      lesspipe(disabled: !paged, search: highlight, always: false) do |less|
        each_proc = if grep
          proc { |line| less.puts line if line[grep] }
        else
          proc { |line| less.puts line }
        end

        lines = command_io.each_line
        lines = lines.to_a.sort if sort
        lines.each(&each_proc)
      end

    end

    $?.to_i == 0
  end
end

#set_default(key, value) ⇒ Object



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

def set_default(key, value)
  send(key, value)
end