Class: Hiiro::RunnerTool

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/runner_tool.rb

Constant Summary collapse

CONFIG_FILE =
File.join(Dir.home, '.config', 'hiiro', 'tools.yml')
KNOWN_CHANGE_SETS =
%w[dirty branch all].freeze
KNOWN_TOOL_TYPES =
%w[lint test format].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file: CONFIG_FILE) ⇒ RunnerTool

Returns a new instance of RunnerTool.



13
14
15
# File 'lib/hiiro/runner_tool.rb', line 13

def initialize(config_file: CONFIG_FILE)
  @config_file = config_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



11
12
13
# File 'lib/hiiro/runner_tool.rb', line 11

def config_file
  @config_file
end

Class Method Details

.build_hiiro(parent_hiiro, rt, git: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hiiro/runner_tool.rb', line 125

def self.build_hiiro(parent_hiiro, rt, git: nil)
  parent_hiiro.make_child(:run) do |h|
    h.add_default do |*run_args|
      change_set = nil
      tool_type = nil
      file_type_group = nil
      variation = nil

      positional = []
      args_iter = run_args.each
      loop do
        arg = args_iter.next
        case arg
        when '--variation', '-v'
          variation = args_iter.next
        else
          positional << arg
        end
      end

      positional.each do |arg|
        if KNOWN_CHANGE_SETS.include?(arg)
          change_set = arg
        elsif KNOWN_TOOL_TYPES.include?(arg)
          tool_type = arg
        else
          # Treat as file_type_group
          file_type_group ||= arg
        end
      end

      change_set ||= 'dirty'

      rt.run(
        change_set: change_set,
        tool_type: tool_type,
        file_type_group: file_type_group,
        variation: variation,
        git: git,
      )
    end

    h.add_subcmd(:ls) do
      configs = rt.tools
      if configs.empty?
        puts "No tools configured."
        puts "Use 'run add' to add one, or edit #{rt.config_file}"
        next
      end

      puts "Configured tools:"
      puts
      configs.each do |name, cfg|
        variations = cfg['variations'] ? " (#{cfg['variations'].keys.join(', ')})" : ""
        puts format("  %-15s  [%s]  %-10s  exts: %s%s",
          name,
          cfg['tool_type'] || '?',
          cfg['file_type_group'] || '?',
          cfg['file_extensions'] || '*',
          variations)
      end
    end

    h.add_subcmd(:add) do |*add_args|
      template = {
        'tool_type' => 'lint',
        'command' => 'echo [FILENAMES]',
        'variations' => {},
        'file_type_group' => '',
        'file_extensions' => '',
      }

      input = InputFile.yaml_file(hiiro: h, content: YAML.dump({ 'new_tool' => template }), prefix: 'tool')
      input.edit

      begin
        data = input.parsed_file(permitted_classes: [Symbol]) || {}
        data.each do |name, cfg|
          rt.add_tool({ 'name' => name }.merge(cfg || {}))
        end
      rescue => e
        puts "Error parsing config: #{e.message}"
      ensure
        input.cleanup
      end
    end

    h.add_subcmd(:rm) do |tool_name=nil|
      unless tool_name
        puts "Usage: run rm <name>"
        next
      end

      rt.remove_tool(tool_name)
    end

    h.add_subcmd(:config) do
      h.edit_files(rt.config_file)
    end
  end
end

Instance Method Details

#add_tool(config_hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hiiro/runner_tool.rb', line 93

def add_tool(config_hash)
  name = config_hash.delete('name') || config_hash.delete(:name)
  unless name
    puts "Tool name required"
    return false
  end

  configs = load_config
  if configs.key?(name)
    puts "Tool '#{name}' already exists"
    return false
  end

  configs[name] = config_hash.transform_keys(&:to_s)
  save_config(configs)
  puts "Added tool '#{name}'"
  true
end

#changed_files(change_set, git: nil) ⇒ Object



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

def changed_files(change_set, git: nil)
  case change_set
  when 'dirty'
    output = `git status --porcelain 2>/dev/null`
    output.lines.map { |l| l.strip.sub(/^.{3}/, '') }.reject(&:empty?)
  when 'branch'
    base = `git merge-base HEAD main 2>/dev/null`.chomp
    base = 'main' if base.empty?
    output = `git diff --name-only #{base}...HEAD 2>/dev/null`
    output.lines.map(&:chomp).reject(&:empty?)
  when 'all'
    nil
  else
    nil
  end
end

#find_tool(name) ⇒ Object



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

def find_tool(name)
  configs = tools
  names = configs.keys.map { |k| OpenStruct.new(name: k) }
  result = Hiiro::Matcher.new(names, :name).by_prefix(name)
  match = result.resolved || result.first
  return nil unless match

  tool_name = match.item.name
  { name: tool_name, **symbolize_keys(configs[tool_name]) }
end

#find_tools(tool_type: nil, file_type_group: nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/hiiro/runner_tool.rb', line 32

def find_tools(tool_type: nil, file_type_group: nil)
  configs = tools
  configs.select { |_, cfg|
    (tool_type.nil? || cfg['tool_type'] == tool_type) &&
      (file_type_group.nil? || cfg['file_type_group'] == file_type_group)
  }
end

#remove_tool(name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hiiro/runner_tool.rb', line 112

def remove_tool(name)
  configs = load_config
  unless configs.key?(name)
    puts "Tool '#{name}' not found"
    return false
  end

  configs.delete(name)
  save_config(configs)
  puts "Removed tool '#{name}'"
  true
end

#run(change_set: 'dirty', tool_type: nil, file_type_group: nil, variation: nil, git: nil) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
# File 'lib/hiiro/runner_tool.rb', line 57

def run(change_set: 'dirty', tool_type: nil, file_type_group: nil, variation: nil, git: nil)
  matching = find_tools(tool_type: tool_type, file_type_group: file_type_group)

  if matching.empty?
    puts "No tools found matching criteria"
    puts "  tool_type: #{tool_type || '(any)'}"
    puts "  file_type_group: #{file_type_group || '(any)'}"
    return false
  end

  files = changed_files(change_set, git: git)

  matching.each do |name, cfg|
    tool_files = filter_files_for_tool(files, cfg)

    if files && tool_files.empty?
      puts "No matching files for '#{name}' (#{cfg['file_extensions']})"
      next
    end

    cmd = if variation && cfg['variations'] && cfg['variations'][variation]
      cfg['variations'][variation]
    else
      cfg['command']
    end

    filenames = tool_files ? tool_files.join(' ') : ''
    cmd = cmd.gsub('[FILENAMES]', filenames)

    puts "Running #{name}: #{cmd}"
    system(cmd)
  end

  true
end

#toolsObject



17
18
19
# File 'lib/hiiro/runner_tool.rb', line 17

def tools
  load_config
end