class RailsPackager::CommandParser

Attributes

args[R]
env[R]
name[R]

Public Class Methods

new(command) click to toggle source
# File lib/rails_packager/command_parser.rb, line 9
def initialize(command)
  @parsed = false
  @unparsed = command
end
parse(command) click to toggle source
# File lib/rails_packager/command_parser.rb, line 5
def self.parse(command)
  new(command).tap(&:parse)
end

Public Instance Methods

parse() click to toggle source
# File lib/rails_packager/command_parser.rb, line 14
def parse
  return if @parsed
  @parsed = true

  result =
    if @unparsed.is_a?(Array)
      @env = parse_env(@unparsed.last)
      parse_command(@unparsed.first)
    else
      @env = {}
      parse_command(@unparsed)
    end

  raise ArgumentError, "Empty command is not allowed" if result.empty?
  @name = result.shift
  @args = result
end

Private Instance Methods

parse_command(command, result = []) click to toggle source
# File lib/rails_packager/command_parser.rb, line 48
def parse_command(command, result = [])
  command = command.strip
  return result if command.empty?

  if command[0] == "'"
    value, remaining = command[1..-1].split("'", 2)
    raise ArgumentError, "Mismatched single quote" unless remaining
  elsif command[0] == '"'
    value, remaining = command[1..-1].split('"', 2)
    raise ArgumentError, "Mismatched single quote" unless remaining
  else
    value, remaining = command.split(" ", 2)
  end

  result << value
  parse_command(remaining || "", result)
end
parse_env(options) click to toggle source
# File lib/rails_packager/command_parser.rb, line 34
def parse_env(options)
  {}.tap do |result|
    if options.include?("unsetenv")
      options["unsetenv"].each do |var|
        result[var] = nil
      end
    end

    if options.include?("env")
      result.merge!(options["env"])
    end
  end
end