Class: Rigor::ModuleGraph::CLI::Collect

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/module_graph/cli.rb

Overview

‘collect` shells out to `rigor check –format json` and writes a JSONL edge file by filtering the diagnostics for our `source_family` + `rule`.

Constant Summary collapse

DEFAULT_PATHS =
[].freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:) ⇒ Collect

Returns a new instance of Collect.



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rigor/module_graph/cli.rb', line 253

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
  @options = {
    output: DEFAULT_EDGES_PATH,
    nodes_output: DEFAULT_NODES_PATH,
    cache: false,
    quiet: false,
    rigor_cmd: ENV.fetch("RIGOR_CMD", "rigor")
  }
end

Instance Method Details

#build_parserObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rigor/module_graph/cli.rb', line 296

def build_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: rigor-module-graph collect [options] [PATHS...]"
    opts.on("-o", "--output PATH",
            "Write edges to PATH (default: #{DEFAULT_EDGES_PATH})") do |path|
      @options[:output] = path
    end
    opts.on("--nodes-output PATH",
            "Write nodes to PATH (default: #{DEFAULT_NODES_PATH})") do |path|
      @options[:nodes_output] = path
    end
    opts.on("--[no-]cache",
            "Pass `--cache` / `--no-cache` to rigor (default: --no-cache)") do |cache|
      @options[:cache] = cache
    end
    opts.on("--rigor-cmd CMD",
            "Override the rigor binary (default: rigor or $RIGOR_CMD)") do |cmd|
      @options[:rigor_cmd] = cmd
    end
    opts.on("-q", "--quiet", "Suppress step-level progress on stderr") do
      @options[:quiet] = true
    end
    opts.on("-h", "--help") do
      @stdout.puts opts
      exit 0
    end
  end
end

#ensure_output_dirsObject



325
326
327
328
329
330
# File 'lib/rigor/module_graph/cli.rb', line 325

def ensure_output_dirs
  [@options[:output], @options[:nodes_output]].each do |path|
    dir = File.dirname(path)
    FileUtils.mkdir_p(dir) unless dir.empty?
  end
end

#rigor_step_label(paths) ⇒ Object

Path-aware label so the user can see which paths Rigor is being pointed at when the step is slow.



291
292
293
294
# File 'lib/rigor/module_graph/cli.rb', line 291

def rigor_step_label(paths)
  target = paths.empty? ? "configured paths" : paths.join(", ")
  "Running rigor check on #{target}"
end

#run(argv) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rigor/module_graph/cli.rb', line 265

def run(argv)
  parser = build_parser
  paths = parser.parse(argv)

  status = Rigor::ModuleGraph::StatusReporter.new(stderr: @stderr, quiet: @options[:quiet])

  ensure_output_dirs
  runner = RigorRunner.new(rigor_cmd: @options[:rigor_cmd], cache: @options[:cache])
  edges, nodes = status.step(rigor_step_label(paths)) { runner.analyse(paths) }
  status.info "#{edges.size} edge(s), #{nodes.size} node(s)"
  status.step("Writing #{@options[:output]}") { write_edges(edges) }
  status.step("Writing #{@options[:nodes_output]}") { write_nodes(nodes) }

  @stderr.puts "rigor-module-graph: wrote #{edges.size} edge(s) to #{@options[:output]}, " \
               "#{nodes.size} node(s) to #{@options[:nodes_output]}"
  0
rescue OptionParser::ParseError => e
  @stderr.puts "rigor-module-graph collect: #{e.message}"
  2
rescue CollectError => e
  @stderr.puts "rigor-module-graph collect: #{e.message}"
  1
end

#write_edges(edges) ⇒ Object



332
333
334
335
336
# File 'lib/rigor/module_graph/cli.rb', line 332

def write_edges(edges)
  File.open(@options[:output], "w") do |io|
    EdgeIO.write(edges, io)
  end
end

#write_nodes(nodes) ⇒ Object



338
339
340
341
342
# File 'lib/rigor/module_graph/cli.rb', line 338

def write_nodes(nodes)
  File.open(@options[:nodes_output], "w") do |io|
    NodeIO.write(nodes, io)
  end
end