lib/planter/script.rb in planter-cli-0.0.3 vs lib/planter/script.rb in planter-cli-0.0.4

- old
+ new

@@ -11,35 +11,46 @@ ## @param template_dir [String] Path to the current template dir ## @param output_dir [String] The planted template directory ## @param script [String] The script name ## def initialize(template_dir, output_dir, script) - found = find_script(template_dir, output_dir, script) - Planter.notify("Script #{script} not found", :error, exit_code: 10) unless found + found = find_script(template_dir, script) + raise ScriptError.new("Script #{script} not found") unless found + @script = found + make_executable - Planter.notify("Directory #{output_dir} not found", :error, exit_code: 10) unless File.directory?(output_dir) + raise ScriptError.new("Output directory #{output_dir} not found") unless File.directory?(output_dir) + @template_directory = template_dir @directory = output_dir end + ## Make a script executable if it's not already + def make_executable + File.chmod(0o755, @script) unless File.executable?(@script) + File.executable?(@script) + end + ## ## Locate a script in either the base directory or template directory ## ## @param template_dir [String] The template dir ## @param script [String] The script name ## ## @return [String] Path to script ## def find_script(template_dir, script) - parts = Shellwords.split(script).first - return script if File.exist?(parts[0]) + 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', parts[0])) - return "#{File.join(template_dir, '_scripts', parts[0])} #{parts[1..-1]}" - elsif File.exist?(File.join(BASE_DIR, 'scripts', parts[0])) - return "#{File.join(BASE_DIR, 'scripts', parts[0])} #{parts[1..-1]}" + 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 @@ -47,12 +58,13 @@ ## Execute script, passing template directory and output directory as arguments $1 and $2 ## ## @return [Boolean] true if success? ## def run - `#{@script} "#{@template_directory}" "#{@directory}"` - - Planter.notify("Error running #{File.basename(@script)}", :error, exit_code: 128) unless $?.success? + 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? + raise ScriptError.new("Error running #{@script}") unless status.success? true end end end