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.



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

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

Instance Method Details

#build_parserObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/rigor/module_graph/cli.rb', line 282

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("-h", "--help") do
      @stdout.puts opts
      exit 0
    end
  end
end

#ensure_output_dirsObject



308
309
310
311
312
313
# File 'lib/rigor/module_graph/cli.rb', line 308

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

#run(argv) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rigor/module_graph/cli.rb', line 262

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

  ensure_output_dirs
  runner = RigorRunner.new(rigor_cmd: @options[:rigor_cmd], cache: @options[:cache])
  edges, nodes = runner.analyse(paths)
  write_edges(edges)
  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



315
316
317
318
319
# File 'lib/rigor/module_graph/cli.rb', line 315

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

#write_nodes(nodes) ⇒ Object



321
322
323
324
325
# File 'lib/rigor/module_graph/cli.rb', line 321

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