class Pakyow::CLI

The Pakyow command line interface.

@api private

Constants

GLOBAL_OPTIONS

Public Class Methods

new(argv = ARGV.dup) click to toggle source
# File lib/pakyow/cli.rb, line 29
def initialize(argv = ARGV.dup)
  @argv = argv
  @options = {}
  @task = nil
  @command = nil

  parse_global_options

  if project_context?
    setup_environment
  end

  load_tasks

  if @command
    find_task_for_command
    set_app_for_command
    call_task
  else
    puts_help
  end
rescue StandardError => error
  if $stdout.isatty
    puts_error(error)

    if @task
      puts @task.help(describe: false)
    else
      puts_help(banner: false)
    end

    ::Process.exit(0)
  else
    raise error
  end
end

Private Instance Methods

call_task() click to toggle source
# File lib/pakyow/cli.rb, line 165
def call_task
  if @options[:help]
    puts @task.help
  else
    @task.call(@options.select { |key, _|
      (key == :app && @task.app?) || key != :app
    }, @argv.dup)
  end
rescue InvalidInput => error
  puts_error(error)
  puts @task.help(describe: false)
end
environment_to_setup() click to toggle source
# File lib/pakyow/cli.rb, line 127
def environment_to_setup
  case @command
  when "prototype"
    :prototype
  else
    @options[:env]
  end
end
find_task_for_command() click to toggle source
# File lib/pakyow/cli.rb, line 140
def find_task_for_command
  unless @task = tasks.find { |task| task.name == @command }
    raise UnknownCommand.new_with_message(
      command: @command
    )
  end
end
load_tasks() click to toggle source
# File lib/pakyow/cli.rb, line 136
def load_tasks
  Pakyow.load_tasks
end
parse_global_options() click to toggle source
# File lib/pakyow/cli.rb, line 78
def parse_global_options
  parse_with_unknown_args do
    OptionParser.new do |opts|
      opts.on("-eENV", "--env=ENV") do |e|
        @options[:env] = e
      end

      opts.on("-aAPP", "--app=APP") do |a|
        @options[:app] = a
      end

      opts.on("-h", "--help") do
        @options[:help] = true
      end
    end
  end

  case @command
  when "prototype"
    @options.delete(:env)
  else
    @options[:env] ||= ENV["APP_ENV"] || ENV["RACK_ENV"] || "development"
  end

  ENV["APP_ENV"] = ENV["RACK_ENV"] = @options[:env]
end
parse_with_unknown_args() { || ... } click to toggle source
# File lib/pakyow/cli.rb, line 105
def parse_with_unknown_args
  parser, original, unparsed = yield, @argv.dup, Array.new

  begin
    parser.order!(@argv) do |arg|
      if @command
        unparsed << arg
      else
        @command = arg
      end
    end
  rescue OptionParser::InvalidOption => error
    unparsed.concat(error.args); retry
  end

  @argv = (original & @argv) + unparsed
end
project_context?() click to toggle source
# File lib/pakyow/cli.rb, line 74
def project_context?
  File.exist?(Pakyow.config.environment_path + ".rb")
end
puts_banner() click to toggle source
# File lib/pakyow/cli.rb, line 195
def puts_banner
  puts Support::CLI.style.blue.bold("Pakyow Command Line Interface")
end
puts_commands() click to toggle source
# File lib/pakyow/cli.rb, line 205
def puts_commands
  puts
  puts Support::CLI.style.bold("COMMANDS")
  longest_name_length = tasks.map(&:name).max_by(&:length).length
  tasks.sort { |a, b| a.name <=> b.name }.each do |task|
    puts "  #{task.name}".ljust(longest_name_length + 4) + Support::CLI.style.yellow(task.description) + "\n"
  end
end
puts_error(error) click to toggle source
# File lib/pakyow/cli.rb, line 178
def puts_error(error)
  puts "  #{Support::CLI.style.red("›")} #{Error::CLIFormatter.format(error.to_s)}"
end
puts_help(banner: true) click to toggle source
# File lib/pakyow/cli.rb, line 186
def puts_help(banner: true)
  if banner
    puts_banner
  end

  puts_usage
  puts_commands
end
puts_usage() click to toggle source
# File lib/pakyow/cli.rb, line 199
def puts_usage
  puts
  puts Support::CLI.style.bold("USAGE")
  puts "  $ pakyow [COMMAND]"
end
puts_warning(warning) click to toggle source
# File lib/pakyow/cli.rb, line 182
def puts_warning(warning)
  puts "  #{Support::CLI.style.yellow("›")} #{warning}"
end
set_app_for_command() click to toggle source
# File lib/pakyow/cli.rb, line 148
def set_app_for_command
  if @task.app?
    Pakyow.boot
    @options[:app] = if @options.key?(:app)
      Pakyow.app(@options[:app]) || raise("`#{@options[:app]}' is not a known app")
    elsif Pakyow.apps.count == 1
      Pakyow.apps.first
    elsif Pakyow.apps.count > 0
      raise "multiple apps were found; please specify one with the --app option"
    else
      raise "couldn't find an app to run this command for"
    end
  elsif @options.key?(:app)
    puts_warning "app was ignored by command #{Support::CLI.style.blue(@command)}"
  end
end
setup_environment() click to toggle source
# File lib/pakyow/cli.rb, line 123
def setup_environment
  Pakyow.setup(env: environment_to_setup)
end
tasks() click to toggle source
# File lib/pakyow/cli.rb, line 68
def tasks
  Pakyow.tasks.select { |task|
    (task.global? && !project_context?) || (!task.global? && project_context?)
  }
end