class Rigit::Commands::Build::BuildHandler

Internal class to handle scaffolding for the {CommandLine} class.

Attributes

args[R]
force[R]
overwrite_all[RW]
rig_name[R]
skip_all[RW]
target_dir[R]

Public Class Methods

new(args) click to toggle source
# File lib/rigit/commands/build.rb, line 18
def initialize(args)
  @args = args
  @rig_name = args['RIG']
  @force = args['--force'] || (config.has_key?(:force) && config.force)
  @target_dir = '.'
end

Public Instance Methods

execute() click to toggle source
# File lib/rigit/commands/build.rb, line 25
def execute
  say "Building !txtgrn!#{rig_name}"
  say config.intro if config.has_key? :intro
  verify_dirs
  arguments = prompt.get_input params

  scaffold arguments

  say config.has_key?(:outro) ? config.outro : "Done"
end

Private Instance Methods

config() click to toggle source
# File lib/rigit/commands/build.rb, line 102
def config
  @config ||= rig.config
end
create_subdir() click to toggle source
# File lib/rigit/commands/build.rb, line 154
def create_subdir
  folder = tty_prompt.ask "Sub directory to create:", default: 'app'
  @target_dir = "#{target_dir}/#{folder}"
  Dir.mkdir target_dir unless Dir.exist? target_dir
  say "Creating in #{target_dir}"
end
execute_actions(actions, arguments) click to toggle source

Execute user-defined system commands. Actions are expected to be provided as a hash (label=command) and both labels and commands accept string interpolation +%{tokens}+

# File lib/rigit/commands/build.rb, line 53
def execute_actions(actions, arguments)
  actions.each do |label, command|
    say "!txtgrn!#{label}" % arguments
    system command % arguments
  end
end
overwrite_file?(file) click to toggle source

Check various scenarios to decide if the file should be overwritten or not. These are the scenarios covered by this method:

  1. The user provided --focce in the command line

  2. The user answered “overwrite all” or “skip all” when he asked about the first conflicting file.

  3. In cases where an additive dir contains a file that was originally new, approved or rejected - we use this existing knowledge (which is stored in response_log.

# File lib/rigit/commands/build.rb, line 68
def overwrite_file?(file)
  return response_log[file] if response_log.has_key? file

  response_log[file] = true

  unless overwrite_all or force
    if skip_all
      response_log[file] = false
    elsif File.exist? file
      response_log[file] = prompt_user_to_overwrite file
    end
  end

  response_log[file]
end
params() click to toggle source
# File lib/rigit/commands/build.rb, line 106
def params
  @params ||= params!
end
params!() click to toggle source
# File lib/rigit/commands/build.rb, line 110
def params!
  output = {}
  input = args['PARAMS']
  input.each do |param|
    key, value = param.split '='
    output[key.to_sym] = value
  end
  output
end
prompt() click to toggle source
# File lib/rigit/commands/build.rb, line 120
def prompt
  @prompt ||= Rigit::Prompt.new config.params
end
prompt_user_to_overwrite(file) click to toggle source
# File lib/rigit/commands/build.rb, line 84
def prompt_user_to_overwrite(file)
  say "File !txtgrn!#{file}!txtrst! already exists."
  tty_prompt.expand "  Overwrite?" do |menu|
    menu.choice key: 'y', name: 'overwrite',     value: true
    menu.choice key: 'n', name: 'skip',          value: false
    menu.choice key: 'a', name: 'overwrite all'  do @overwrite_all = true; true end
    menu.choice key: 's', name: 'skip all'       do @skip_all = true; false end
  end
end
response_log() click to toggle source
# File lib/rigit/commands/build.rb, line 94
def response_log
  @response_log ||= {}
end
rig() click to toggle source
# File lib/rigit/commands/build.rb, line 98
def rig
  @rig ||= Rigit::Rig.new rig_name
end
scaffold(arguments) click to toggle source

Call Rig#scaffold while checking each file to see if it should be overwritten or not.

# File lib/rigit/commands/build.rb, line 40
def scaffold(arguments)
  execute_actions config.before, arguments if config.has_key? :before
  
  rig.scaffold arguments: arguments, target_dir: target_dir do |file|
    overwrite_file? file
  end

  execute_actions config.after, arguments if config.has_key? :after
end
tty_prompt() click to toggle source
# File lib/rigit/commands/build.rb, line 124
def tty_prompt
  @tty_prompt ||= TTY::Prompt.new
end
verify_dirs() click to toggle source
# File lib/rigit/commands/build.rb, line 128
def verify_dirs
  verify_source_dir
  verify_target_dir
end
verify_source_dir() click to toggle source
# File lib/rigit/commands/build.rb, line 133
def verify_source_dir
  raise Rigit::Exit, "No such rig: #{rig_name}" unless rig.exist?
end
verify_target_dir() click to toggle source
# File lib/rigit/commands/build.rb, line 137
def verify_target_dir
  return if Dir.empty?(target_dir) or force

  dirstring = target_dir == '.' ? 'Current directory' : "Directory '#{target_dir}'"
  options = { "Abort" => :abort, "Continue here" => :continue, "Continue in a sub directory" => :create }
  response = tty_prompt.select "#{dirstring} is not empty.", options, symbols: { marker: '>' }
  
  case response
  when :abort
    raise Rigit::Exit, "Goodbye"
  
  when :create
    create_subdir
    verify_target_dir
  end
end