Class: Swat::RailsLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_loader.rb

Overview

Loads Rails command runner

Loads RAILS_RUNNER_COMMAND from the environment to define

Sample script:

SCRIPTS_LOCAL_PATH=path_to/scripts bundle exec rails runner path_to/scripts/lib/swat_run.rb execute test success success 2&>/dev/null

Can read the runner command and the working dir from environment variables such as:

RAILS_RUNNER_COMMAND
RAILS_WORKING_DIR

Instance Method Summary collapse

Constructor Details

#initialize(command = ENV["RAILS_RUNNER_COMMAND"] || "", working_dir = ENV["RAILS_WORKING_DIR"] || Dir.getwd) ⇒ RailsLoader

Returns a new instance of RailsLoader.



20
21
22
23
24
25
26
# File 'lib/rails_loader.rb', line 20

def initialize(command = ENV["RAILS_RUNNER_COMMAND"] || "",
               working_dir = ENV["RAILS_WORKING_DIR"] || Dir.getwd)
  fail "Invalid RAILS_RUNNER_COMMAND, please provide a rails command" if command.nil? || command.empty?
  fail "Invalid RAILS_WORKING_DIR, '#{working_dir}' does not exists" unless Dir.exist?(working_dir)
  @rails_command = command
  @rails_working_dir = working_dir
end

Instance Method Details

#run(args) ⇒ Object



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

def run(args)
  command = "#{@rails_command} #{args}"
  env = {
    "PATH" => ENV["PATH"],
    "SCRIPTS_REMOTE_URL" => ENV["SCRIPTS_REMOTE_URL"],
    "SCRIPTS_LOCAL_PATH" => ENV["SCRIPTS_LOCAL_PATH"]
  }
  Open3.popen3(env, command, unsetenv_others: true, chdir: @rails_working_dir) do |_, stdout, stderr, wait_thr|
    if wait_thr.value != 0
      fail "Command #{args} failed with err: '#{stderr.read.strip}' " \
           "out: '#{stdout.read.strip}'"
    end
    JSON.parse(stdout.read.strip)
  end
end