class Bashly::Commands::Generate

Attributes

watching[R]

Public Instance Methods

run() click to toggle source
# File lib/bashly/commands/generate.rb, line 29
def run
  Settings.env = args['--env'] if args['--env']
  @watching = args['--watch']

  generate
  watch if watching
end

Private Instance Methods

command() click to toggle source
# File lib/bashly/commands/generate.rb, line 170
def command
  @command ||= Script::Command.new config
end
create_all_command_files() click to toggle source
# File lib/bashly/commands/generate.rb, line 137
def create_all_command_files
  command.deep_commands.each do |subcommand|
    next if subcommand.commands.any?

    file = "#{Settings.source_dir}/#{subcommand.filename}"
    content = subcommand.render :default_script
    create_file file, content
  end
end
create_file(file, content) click to toggle source
# File lib/bashly/commands/generate.rb, line 147
def create_file(file, content)
  if File.exist?(file) && !args['--force']
    quiet_say "b`skipped` #{file} (exists)"
  else
    File.deep_write file, content
    quiet_say "g`created` #{file}"
  end
end
create_master_script() click to toggle source
# File lib/bashly/commands/generate.rb, line 156
def create_master_script
  File.write master_script_path, script.code(tab_indent: Settings.tab_indent)
  FileUtils.chmod '+x', master_script_path
  quiet_say "g`created` #{master_script_path}"
end
create_root_command_file() click to toggle source
# File lib/bashly/commands/generate.rb, line 133
def create_root_command_file
  create_file "#{Settings.source_dir}/#{command.filename}", command.render(:default_root_script)
end
create_user_files() click to toggle source
# File lib/bashly/commands/generate.rb, line 123
def create_user_files
  quiet_say "creating user files in g`#{Settings.source_dir}`"

  if command.commands.empty?
    create_root_command_file
  else
    create_all_command_files
  end
end
generate() click to toggle source
# File lib/bashly/commands/generate.rb, line 52
def generate
  with_valid_config do
    quiet_say 'creating g`production` version' if Settings.production?
    generate_all_files
    quiet_say "run m`#{master_script_path} --help` to test your bash script" unless watching
  end
end
generate_all_files() click to toggle source
# File lib/bashly/commands/generate.rb, line 71
def generate_all_files
  create_user_files
  upgrade_libs if args['--upgrade']
  create_master_script
end
generated_files() click to toggle source
# File lib/bashly/commands/generate.rb, line 89
def generated_files
  Dir["#{Settings.source_dir}/**/*.*"]
end
master_script_path() click to toggle source
# File lib/bashly/commands/generate.rb, line 166
def master_script_path
  "#{Settings.target_dir}/#{command.name}"
end
quiet_say(message) click to toggle source
# File lib/bashly/commands/generate.rb, line 67
def quiet_say(message)
  say message unless args['--quiet']
end
reset() click to toggle source
# File lib/bashly/commands/generate.rb, line 60
def reset
  @config = nil
  @config_validator = nil
  @command = nil
  @script = nil
end
script() click to toggle source
# File lib/bashly/commands/generate.rb, line 162
def script
  @script ||= Script::Wrapper.new command, args['--wrap']
end
upgrade(existing_file, library_name, *args) click to toggle source
# File lib/bashly/commands/generate.rb, line 93
def upgrade(existing_file, library_name, *args)
  if library_name.include? ';'
    source_name, library_name = library_name.split(';')
    source = Bashly::LibrarySource.new source_name
  else
    source = Bashly::LibrarySource.new
  end

  library = source.libraries[library_name.to_sym]

  if library
    library.args = args
    upgrade! existing_file, library
  else
    quiet_say "r`warning` not upgrading c`#{existing_file}`, " \
      "unknown library '#{library_name}'"
  end
end
upgrade!(existing_file, library) click to toggle source
# File lib/bashly/commands/generate.rb, line 112
def upgrade!(existing_file, library)
  file = library.find_file existing_file

  if file
    File.deep_write file[:path], file[:content]
    quiet_say "c`updated` #{file[:path]}"
  else
    quiet_say "r`warning` not upgrading c`#{existing_file}`, path mismatch"
  end
end
upgrade_libs() click to toggle source
# File lib/bashly/commands/generate.rb, line 77
def upgrade_libs
  generated_files.each do |file|
    content = File.read file
    next unless content =~ /\[@bashly-upgrade (.+)\]/

    args = $1.split

    library_name = args.shift
    upgrade file, library_name, *args
  end
end
watch() click to toggle source
# File lib/bashly/commands/generate.rb, line 39
def watch
  quiet_say "g`watching` #{Settings.source_dir}\n"

  Filewatcher.new([Settings.source_dir]).watch do
    reset
    generate
  rescue Bashly::ConfigurationError => e
    say! "rib` #{e.class} `\n#{e.message}"
  ensure
    quiet_say "g`waiting`\n"
  end
end