#!/usr/bin/env ruby require 'parlour' require 'commander/import' require 'bundler' require 'rainbow' program :name, 'parlour' program :version, Parlour::VERSION program :description, 'An RBI generator and plugin system' default_command :run command :run do |c| c.syntax = 'parlour run [options]' c.description = 'Generates an RBI file from a list of plugins' c.option '--requires STRING', String, 'A comma-separated string of gems to require' c.option '--relative-requires STRING', String, 'A comma-separated string of files to require, relative to the working dir' c.option '--tab-size INTEGER', Integer, 'The size of tabs to use' c.option '--break-params INTEGER', Integer, 'Break params onto their own lines if there are this many' c.action do |args, options| options.default( tab_size: 2, break_params: 4, requires: '', relative_requires: '' ) options.requires.split(',').each { |source| require(source) } options.relative_requires.split(',').each do |source| require(File.join(Dir.pwd, source)) end *plugin_names, output_file = args raise 'no output file specified' if output_file.nil? plugin_instances = [] # Collect the instances of each plugin into an array plugin_names.each do |name| plugin = Parlour::Plugin.registered_plugins[name] raise "missing plugin #{name}" unless plugin plugin_instances << plugin end # Create a generator instance and run all plugins on it gen = Parlour::RbiGenerator.new( break_params: options.break_params, tab_size: options.tab_size ) Parlour::Plugin.run_plugins(plugin_instances, gen) # Run a pass of the conflict resolver Parlour::ConflictResolver.new.resolve_conflicts(gen.root) do |msg, candidates| puts Rainbow('Conflict! ').red.bright.bold + Rainbow(msg).blue.bright puts 'Multiple different definitions have been produced for the same object.' puts 'They could not be merged automatically.' puts Rainbow('What would you like to do?').bold + ' Type a choice and press Enter.' puts puts Rainbow(' [0] ').yellow + 'Remove ALL definitions' puts puts "Or select one definition to keep:" puts candidates.each.with_index do |candidate, i| puts Rainbow(" [#{i + 1}] ").yellow + candidate.describe end puts choice = ask("? ", Integer) { |q| q.in = 0..candidates.length } choice == 0 ? nil : candidates[choice - 1] end # Write the final RBI File.write(output_file, gen.rbi) end end