Class: Dynosaur::Utils::RakeCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/dynosaur/utils/rake_command.rb

Constant Summary collapse

PARSE_REGEX =
/\brake\s+([\w:]+)(\[.*\])?/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, args: []) ⇒ RakeCommand

Returns a new instance of RakeCommand.



26
27
28
29
# File 'lib/dynosaur/utils/rake_command.rb', line 26

def initialize(task:, args: [])
  @task = task
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/dynosaur/utils/rake_command.rb', line 7

def args
  @args
end

#taskObject (readonly)

Returns the value of attribute task.



7
8
9
# File 'lib/dynosaur/utils/rake_command.rb', line 7

def task
  @task
end

Class Method Details

.parse(command) ⇒ RakeCommand

Returns an instance that represents the command being parsed.

Parameters:

  • command (String)

    the command to parse

Returns:

  • (RakeCommand)

    an instance that represents the command being parsed

Raises:

  • (ArgumentError)

    if the command is not a valid rake command



18
19
20
21
22
23
24
# File 'lib/dynosaur/utils/rake_command.rb', line 18

def self.parse(command)
  match = command.match(PARSE_REGEX)
  raise ArgumentError, %Q(Invalid rake command: "#{command}") unless match
  task = match[1]
  args = match[2] ? match[2].tr('[]', '').split(',') : []
  new(task: task, args: args)
end

.valid?(command) ⇒ true|false

Returns true iff the string is a valid rake command.

Parameters:

  • command (String)

    the command to test for validity

Returns:

  • (true|false)

    true iff the string is a valid rake command



11
12
13
# File 'lib/dynosaur/utils/rake_command.rb', line 11

def self.valid?(command)
  !(command =~ PARSE_REGEX).nil?
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/dynosaur/utils/rake_command.rb', line 40

def ==(other)
  to_s == other.to_s
end

#to_sString

Returns the full rake command, including arguments.

Returns:

  • (String)

    the full rake command, including arguments



32
33
34
35
36
37
38
# File 'lib/dynosaur/utils/rake_command.rb', line 32

def to_s
  formatted_args = args.map do |arg|
    arg.is_a?(String) ? arg.shellescape : arg
  end.join(',')
  task_with_args = args.empty? ? task : "#{task}[#{formatted_args}]"
  "rake #{task_with_args} --trace"
end