Class: Sunshine::RunCommand

Inherits:
DefaultCommand show all
Defined in:
lib/commands/run.rb

Overview

Run one or more sunshine scripts.

Usage: sunshine run [options] [run_file] …

Arguments:

run_file     Load a script or app path. Defaults to ./Sunshine

Options:

-l, --level LEVEL         Set trace level. Defaults to info.
-e, --env DEPLOY_ENV      Sets the deploy env. Defaults to development.
-a, --auto                Non-interactive - automate or fail.
    --no-trace            Don't trace any output.

Class Method Summary collapse

Methods inherited from DefaultCommand

build_response, copy_middleware, copy_rakefile, opt_parser, parse_remote_args

Class Method Details

.exec(run_files, config) ⇒ Object

Takes an array and a hash, runs the command and returns:

true: success
false: failed
exitcode:
  code == 0: success
  code != 0: failed

and optionally an accompanying message.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/commands/run.rb', line 28

def self.exec run_files, config

  run_files.each do |run_file|

    run_file = run_file_from run_file

    with_load_path File.dirname(run_file) do

      puts "Running #{run_file}"

      get_file_data run_file

      require run_file
    end
  end

  return true
end

.get_file_data(run_file) ⇒ Object

Returns file data in a run file as a File IO object.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/commands/run.rb', line 93

def self.get_file_data run_file
  # TODO: Find a better way to make file data accessible to App objects.
  Sunshine.send :remove_const, "DATA" if defined?(Sunshine::DATA)
  data_marker = "__END__\n"
  line = nil

  Sunshine.const_set("DATA", File.open(run_file, 'r'))

  until line == data_marker || Sunshine::DATA.eof?
    line = Sunshine::DATA.gets
  end
end

.parse_args(argv) ⇒ Object

Parses the argv passed to the command



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/commands/run.rb', line 110

def self.parse_args argv
  options = {'trace' => true}

  opts = opt_parser(options) do |opt|
    opt.banner = <<-EOF

Usage: #{opt.program_name} run [options] [run_file] ...

Arguments:
run_file     Load a script or app path. Defaults to ./Sunshine
    EOF

    opt.separator nil
    opt.separator "Options:"

    opt.on('-l', '--level LEVEL',
           'Set trace level. Defaults to info.') do |value|
      options['level'] = value
    end

    opt.on('-e', '--env DEPLOY_ENV',
           'Sets the deploy env. Defaults to development.') do |value|
      options['deploy_env'] = value
    end

    opt.on('-a', '--auto',
           'Non-interactive - automate or fail.') do
      options['auto'] = true
    end

    opt.on('--no-trace',
           "Don't trace any output.") do
      options['trace'] = false
    end
  end

  opts.parse! argv

  options
end

.run_file_from(run_file) ⇒ Object

Tries to infer what run file to used based on a given path:

run_file_from "path/to/some/dir"
  #=> "path/to/some/dir/Sunshine"
run_file_from nil
  #=> "Sunshine"
run_file_from "path/to/run_script.rb"
  #=> "path/to/run_script.rb"


57
58
59
60
61
62
63
64
# File 'lib/commands/run.rb', line 57

def self.run_file_from run_file
  run_file = File.join(run_file, "Sunshine") if
    run_file && File.directory?(run_file)

  run_file ||= "Sunshine"

  File.expand_path run_file
end

.with_load_path(path) ⇒ Object

Adds a directory to the ruby load path and runs the passed block. Useful for scripts to be able to reference their own dirs.



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

def self.with_load_path path
  path = File.expand_path path

  # TODO: Find a better way to make file path accessible to App objects.
  Sunshine.send :remove_const, "PATH" if defined?(Sunshine::PATH)
  Sunshine.const_set "PATH", path

  added = unless $:.include? path
            $: << path && true
          end

  yield

  Sunshine.send :remove_const, "PATH"

  $:.delete path if added
end