Class: Producer::Core::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/producer/core/cli.rb

Constant Summary collapse

ArgumentError =
Class.new(::ArgumentError)
USAGE =
"Usage: #{File.basename $0} [options] [recipes]".freeze
EX_USAGE =
64
EX_SOFTWARE =
70

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



33
34
35
36
37
38
39
# File 'lib/producer/core/cli.rb', line 33

def initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr)
  @arguments  = args
  @stdin      = stdin
  @stdout     = stdout
  @stderr     = stderr
  @env        = build_env
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



31
32
33
# File 'lib/producer/core/cli.rb', line 31

def arguments
  @arguments
end

#envObject (readonly)

Returns the value of attribute env.



31
32
33
# File 'lib/producer/core/cli.rb', line 31

def env
  @env
end

#stderrObject (readonly)

Returns the value of attribute stderr.



31
32
33
# File 'lib/producer/core/cli.rb', line 31

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



31
32
33
# File 'lib/producer/core/cli.rb', line 31

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



31
32
33
# File 'lib/producer/core/cli.rb', line 31

def stdout
  @stdout
end

Class Method Details

.run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/producer/core/cli.rb', line 12

def run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr)
  cli = new(arguments, stdin: stdin, stdout: stdout, stderr: stderr)
  begin
    cli.parse_arguments!
    cli.run
  rescue ArgumentError => e
    stderr.puts e.message
    exit EX_USAGE
  rescue StandardError => e
    ef = ErrorFormatter.new(
      debug:        cli.env.debug?,
      force_cause:  [RecipeEvaluationError]
    )
    stderr.puts ef.format e
    exit EX_SOFTWARE
  end
end

Instance Method Details

#evaluate_recipesObject



52
53
54
# File 'lib/producer/core/cli.rb', line 52

def evaluate_recipes
  @arguments.map { |e| Recipe::FileEvaluator.evaluate(e, @env) }
end

#parse_arguments!Object



41
42
43
44
# File 'lib/producer/core/cli.rb', line 41

def parse_arguments!
  option_parser.parse!(@arguments)
  fail ArgumentError, option_parser if @arguments.empty?
end

#run(worker: Worker.new(@env)) ⇒ Object



46
47
48
49
50
# File 'lib/producer/core/cli.rb', line 46

def run(worker: Worker.new(@env))
  evaluate_recipes.each { |recipe| worker.process recipe.tasks }
ensure
  @env.cleanup
end