class SetupScriptGenerator::Cli
Constants
- CUSTOMIZABLE_SECTION_PATH
- NON_CUSTOMIZABLE_SECTION_MARKER
- NON_CUSTOMIZABLE_SECTION_PATH
- PROVISIONS_DIR
- RenderFile
- SKELETON_TEMPLATE_PATH
- TEMPLATES_DIR
Attributes
args[R]
provision_names[R]
stderr[R]
stdout[R]
Public Class Methods
call(args, stdout, stderr)
click to toggle source
# File lib/setup_script_generator/cli.rb, line 18 def self.call(args, stdout, stderr) new(args, stdout, stderr).call end
new(args, stdout, stderr)
click to toggle source
# File lib/setup_script_generator/cli.rb, line 22 def initialize(args, stdout, stderr) @args = args @stdout = stdout @stderr = stderr @provision_names = [] @dry_run = false end
Public Instance Methods
call()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 31 def call parse_args!(args) validate_provision_names! if dry_run? stdout.puts generated_content else output_file.parent.mkpath output_file.write(generated_content) output_file.chmod(0744) stdout.puts "File written to: #{output_file}" end end
Private Instance Methods
customizable_section()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 168 def customizable_section @_customizable_section ||= File.read(CUSTOMIZABLE_SECTION_PATH) end
dry_run?()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 49 def dry_run? @dry_run end
error(message)
click to toggle source
# File lib/setup_script_generator/cli.rb, line 186 def error(message) stderr.puts "\e[31m[Error] #{message}\e[0m" end
generated_content()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 141 def generated_content if output_file.exist? content = "" output_file.each_line do |line| if line == "#{NON_CUSTOMIZABLE_SECTION_MARKER}\n" content << non_customizable_section break else content << line end end content else skeleton end end
non_customizable_section()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 172 def non_customizable_section @_non_customizable_section ||= RenderFile.call( NON_CUSTOMIZABLE_SECTION_PATH, provisions: provisions, version: SetupScriptGenerator::VERSION ) end
option_parser()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 64 def option_parser @_option_parser ||= OptionParser.new do |parser| parser.banner = "#{$0} OUTPUT_FILE [OPTIONS]" parser.on( "-p", "--provision [NAME]", String, "Inserts code into the setup script to provision a library or " + "package when the script runs." ) do |provision_name| provision_names << provision_name end parser.on( "-n", "--dry-run", "Outputs the generated script instead of writing to the file." ) do @dry_run = true end parser.on( "-l", "--list-provisions", "Lists the available provisions." ) do stdout.puts( "Here are the provisions you can specify with --provision NAME:" ) valid_provision_names.sort.each do |provision_name| stdout.puts "* #{provision_name}" end exit end parser.on( "-v", "--version", "Lists the version of setup_script_generator associated with this " + "executable." ) do puts "generate-setup version #{SetupScriptGenerator::VERSION}" exit end parser.on("-h", "--help", "You're looking at it!") do puts parser exit end end end
output_file()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 119 def output_file Pathname.new(args.first).expand_path(ENV["PWD"]) end
parse_args!(args)
click to toggle source
# File lib/setup_script_generator/cli.rb, line 53 def parse_args!(args) option_parser.parse!(args) if args.empty? error "Must provide an output file!" stderr.puts stderr.puts option_parser exit 1 end end
provisions()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 180 def provisions @_provisions ||= provision_names.map do |provision_name| Provision.new(provision_name, PROVISIONS_DIR) end end
skeleton()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 160 def skeleton @_skeleton ||= RenderFile.call( SKELETON_TEMPLATE_PATH, customizable_section: customizable_section, non_customizable_section: non_customizable_section ) end
valid_provision_names()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 135 def valid_provision_names @_valid_provision_names ||= PROVISIONS_DIR.glob("*").map do |path| path.basename(".sh").to_s end end
validate_provision_names!()
click to toggle source
# File lib/setup_script_generator/cli.rb, line 123 def validate_provision_names! invalid_provision_name = provision_names.find do |provision_name| !valid_provision_names.include?(provision_name) end if invalid_provision_name error "Invalid provision: #{invalid_provision_name}" stderr.puts "Valid provisions are: #{valid_provision_names.join(', ')}" exit 2 end end