Class: Planter::Script
- Inherits:
-
Object
- Object
- Planter::Script
- Defined in:
- lib/planter/script.rb
Overview
Script handler
Instance Attribute Summary collapse
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Instance Method Summary collapse
-
#find_script(template_dir, script) ⇒ String
Locate a script in either the base directory or template directory.
-
#initialize(template_dir, output_dir, script) ⇒ Script
constructor
Initialize a Script object.
-
#make_executable ⇒ Object
Make a script executable if it's not already.
-
#run ⇒ Boolean
Execute script, passing template directory and output directory as arguments $1 and $2.
Constructor Details
#initialize(template_dir, output_dir, script) ⇒ Script
Initialize a Script object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/planter/script.rb', line 15 def initialize(template_dir, output_dir, script) found = find_script(template_dir, script) die("Script #{script} not found", :script) unless found @script = found make_executable die("Output directory #{output_dir} not found", :script) unless File.directory?(output_dir) @template_directory = template_dir @directory = output_dir end |
Instance Attribute Details
#script ⇒ Object (readonly)
Returns the value of attribute script.
6 7 8 |
# File 'lib/planter/script.rb', line 6 def script @script end |
Instance Method Details
#find_script(template_dir, script) ⇒ String
Locate a script in either the base directory or template directory
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/planter/script.rb', line 42 def find_script(template_dir, script) parts = Shellwords.split(script) script_name = parts[0] args = parts[1..-1].join(' ') return script if File.exist?(script_name) if File.exist?(File.join(template_dir, '_scripts', script_name)) return "#{File.join(template_dir, '_scripts', script_name)} #{args}".strip elsif File.exist?(File.join(Planter.base_dir, 'scripts', script_name)) return "#{File.join(Planter.base_dir, 'scripts', script_name)} #{args}".strip end nil end |
#make_executable ⇒ Object
Make a script executable if it's not already
29 30 31 32 |
# File 'lib/planter/script.rb', line 29 def make_executable File.chmod(0o755, @script) unless File.executable?(@script) File.executable?(@script) end |
#run ⇒ Boolean
Execute script, passing template directory and output directory as arguments $1 and $2
62 63 64 65 66 67 68 69 |
# File 'lib/planter/script.rb', line 62 def run stdout, stderr, status = Open3.capture3(@script, @template_directory, @directory) Planter.notify("STDOUT:\n#{stdout}", :debug) unless stdout.empty? Planter.notify("STDERR:\n#{stderr}", :debug) unless stderr.empty? die("Error running #{@script}", :script) unless status.success? true end |