Class: Ergane::Tool

Inherits:
CommandDefinition show all
Defined in:
lib/ergane/tool.rb

Instance Attribute Summary collapse

Attributes inherited from CommandDefinition

#chain, #description, #label, #requirements_block, #run_block, #switch_definitions, #switch_parser

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandDefinition

#arguments, #commands, #default_switches, #define, #parse_args, #pretty_print, #run

Constructor Details

#initialize(label, *paths) ⇒ Tool

Returns a new instance of Tool.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ergane/tool.rb', line 6

def initialize(label, *paths)
  super(label)

  @title = label.capitalize
  @version = VERSION

  # Dogfood-ing
  define do
    description "Basic CLI Tool"
    switches do
      switch :help, default: false, kind: TrueClass, description: "Display this help block" do
        raise Help
      end
      switch :version, default: false, kind: TrueClass, description: "Display the version" do
        # TODO: Push ARGV into a variable at the command level that can be manipulated by flags/switches
        # NOTE: This would allow a --version to morph into a version command and we could push all this logic to that.
        # Additional logic for --version would be -1 or --short etc.
        puts "#{@title} Version: #{Ergane::VERSION}"
        exit
      end
      # switch verbose: FalseClass, short: :v, "Turn on verbose logging"
    end

    run do
      begin
        command, args = self, []
        Process.setproctitle(label.to_s)
        command, args = self.parse_args(ARGV.dup)

        command.run(*args)
        puts "Finished running #{label}"
      rescue Interrupt
        puts "\nOkay. Aborting."
      rescue RuntimeError
        puts "RuntimeError"
        binding.pry
      rescue Help
        puts help(command, args)
      ensure
        system "printf '\033]0;\007'"
      end
    end
  end

  Pry.config.prompt_name = "#{title} ".light_blue

  load_commands(paths)
end

Instance Attribute Details

#titleObject (readonly)

capitalized :label



3
4
5
# File 'lib/ergane/tool.rb', line 3

def title
  @title
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/ergane/tool.rb', line 4

def version
  @version
end

Class Method Details

.define(label, chain: [], &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ergane/tool.rb', line 83

def self.define(label, chain: [], &block)
  c = CommandDefinition.define(label, chain: chain, &block)

  parent_command = if chain.any?
    Ergane.active_tool.dig(*chain)
  else
    Ergane.active_tool
  end

  parent_command[label] = c
end

Instance Method Details

#help(command, args = []) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ergane/tool.rb', line 55

def help(command, args=[])
  missing_args = command.arguments.product([false]).to_h.merge(args.product([true]).to_h).map do |arg, is_missing|
    a = arg.to_s.light_black.underline.tap do |b|
      b.blink if is_missing
    end
  end.join(' ')

  command.switch_parser.banner = [].tap do |text|
    text << "About: #{description}"
    text << "Version:  #{version.to_s.light_blue}"
    text << "Usage: #{([label] + chain).join(' ').light_red} #{'[options]'.light_cyan} "
    if commands.any?
      text.last << "[subcommand]".light_black.underline
      text << ("    ┌" + ("─" * (text.last.uncolorize.length - 12)) + "┘").light_black
      commands.each do |key, command|
        # text << "    ├─┐".light_black + " #{(klass.terms.join(', ')).ljust(24, ' ')} ".send(Athena::Util.next_color) + klass.description.light_black
        text << "    ├─┐".light_black + " #{key.to_s.ljust(24, ' ')} " + command.description.light_black
      end
      text << ("    └" + "─" * 64).light_black
    else
      # text.last << command.arguments(missing_args.keys)
    end
    # text << list_examples if examples.any?
    text << "Options:".light_cyan
  end.join("\n")
  switch_parser
end

#load_commands(paths) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ergane/tool.rb', line 95

def load_commands(paths)
  activate_tool do
    Ergane.logger.debug "Loading paths:"
    Array.wrap(paths).each do |path|
      Ergane.logger.debug "  - #{path}"
      Dir[path].each do |path|
        file = path.split('/').last
        Ergane.logger.debug "  - loading #{path.split('/').last(4).join('/')}"
        instance_eval(File.read(path), file)
      end
    end
  end
end