Class: Nutella::RunCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/commands/meta/run_command.rb

Overview

This class describes a run command which can be either start or stop. It is mostly a commodity class for code reuse.

Direct Known Subclasses

Compile, Dependencies, Start, Stop

Instance Method Summary collapse

Instance Method Details

#compile_and_dependencies(script, in_progress_message, complete_message) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/commands/meta/run_command.rb', line 97

def compile_and_dependencies( script , in_progress_message, complete_message)
  # If the current directory is not a nutella application, return
  unless Nutella.current_app.exist?
    console.warn 'The current directory is not a nutella application'
    return
  end

  # Run script
  return unless run_script_for_all_bots_in( Dir.pwd, script, in_progress_message )

  # Output success message
  console.success "All #{complete_message} for #{Nutella.current_app.config['name']}"
end

#components_in_dir(dir) ⇒ Object

Returns all the components in a certain directory



54
55
56
# File 'lib/commands/meta/run_command.rb', line 54

def components_in_dir( dir )
  Dir.entries(dir).select {|entry| File.directory?(File.join(dir, entry)) && !(entry =='.' || entry == '..') }
end

#for_each_component_in_dir(dir) {|component| ... } ⇒ Object

Executes a code block for each component in a certain directory

Yields:

  • (component)

    Passes the component name to the block



62
63
64
65
66
# File 'lib/commands/meta/run_command.rb', line 62

def for_each_component_in_dir( dir, &block )
  components_in_dir(dir).each do |component|
    block.call component
  end
end

#parse_cli_parameters(args) ⇒ Hash

Parse the command line parameters



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/commands/meta/run_command.rb', line 39

def parse_cli_parameters( args )
  begin
    opts = Slop::Options.new
    opts.array '-wo', '--without', 'A list of components NOT to start'
    opts.array '-w', '--with', 'A list of components that needs to be started'
    parser = Slop::Parser.new(opts)
    result = parser.parse(args)
    result.to_hash
  rescue
    raise StandardError.new 'The only supported parameters are --with (-w) and --without (-wo)'
  end
end

#parse_run_id_from(args) ⇒ String

Extracts the run_id from the parameters passed to the command line



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/commands/meta/run_command.rb', line 17

def parse_run_id_from( args )
  # Simple `nutella start/stop` (no args)
  if args.nil? || args.empty?
    return 'default'
  end
  # If the first argument is a parameter, set the run_id to default
  if args[0].start_with? '-'
    run_id =  'default'
  else
    # If the first argument is a run_id, check that it's not 'default' and return it
    # and shift the arguments so we are left with only the parameters in args
    run_id = args[0]
    raise StandardError.new 'Unfortunately you can\'t use `default` as a run_id because it is reserved :(' if run_id=='default'
    args.shift
  end
  run_id
end

Prints a success message if the command completes correctly



88
89
90
91
92
93
94
# File 'lib/commands/meta/run_command.rb', line 88

def print_success_message(app_id, run_id, action)
  if run_id == 'default'
    console.success "Application #{app_id} #{action}!"
  else
    console.success "Application #{app_id}, run #{run_id} #{action}!"
  end
end

#run(args = nil) ⇒ Object



9
10
11
# File 'lib/commands/meta/run_command.rb', line 9

def run (args=nil)
  console.error 'Running the generic RunCommand!!! WAT? https://www.destroyallsoftware.com/talks/wat'
end

#run_script_for_all_bots_in(dir, script, message) ⇒ Object

Runs a script for each bot in a certain directory. Message is displayed in case something goes wrong



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/commands/meta/run_command.rb', line 71

def run_script_for_all_bots_in( dir, script, message )
  for_each_component_in_dir dir do |bot|
    # Skip bot if there is no script
    next unless File.exist? "#{dir}/bots/#{bot}/#{script}"
    # Output message
    console.info "#{message} bot #{bot}."
    # Execute 'script' script
    cur_dir = Dir.pwd
    Dir.chdir "#{dir}/bots/#{bot}"
    system "./#{script}"
    Dir.chdir cur_dir
  end
  true
end