lib/vendorer.rb in vendorer-0.1.16 vs lib/vendorer.rb in vendorer-0.2.0

- old
+ new

@@ -1,7 +1,8 @@ require 'tempfile' require 'tmpdir' +require 'shellwords' class Vendorer def initialize(options={}) @options = options @sub_path = [] @@ -12,27 +13,27 @@ end def file(path, url=nil) target_path = complete_path(path) update_or_not target_path do - run "mkdir -p #{File.dirname(target_path)}" + run "mkdir", "-p", File.dirname(target_path) if @copy_from_url copy_from_path(target_path, url || path) else - run "curl '#{url}' -L --compressed -o #{target_path}" + run "curl", url, "--fail", "-L", "--compressed", "-o", target_path raise "Downloaded empty file" unless File.exist?(target_path) end yield target_path if block_given? end end def folder(path, url=nil, options={}) if @copy_from_path or url target_path = complete_path(path) update_or_not target_path do - run "rm -rf #{target_path}" - run "mkdir -p #{File.dirname(target_path)}" + run "rm", "-rf", target_path + run "mkdir", "-p", File.dirname(target_path) if @copy_from_path copy_from_path(target_path, url || path) else download_repository(url, target_path, options) end @@ -82,35 +83,32 @@ else puts "keeping #{path}" end end - def run(cmd) - output = '' - IO.popen(cmd + ' 2>&1') do |pipe| - while line = pipe.gets - output << line - end - end - raise output unless $?.success? + def run(*cmd, dir: nil) + cmd = "#{cmd.shelljoin} 2>&1" + cmd = ["cd", dir].shelljoin + " && #{cmd}" if dir + output = `#{cmd}` + raise "Failed: #{cmd}\n#{output}" unless $?.success? end def complete_path(path) File.join(@sub_path + [path]) end def download_repository(url, to, options) - run "git clone '#{url}' #{to}" + run "git", "clone", url, to if commit = (options[:ref] || options[:tag] || options[:branch]) - run "cd #{to} && git checkout '#{commit}'" + run "git", "checkout", commit, dir: to end - run "cd #{to} && git submodule update --init --recursive" - run "rm -rf #{to}/.git" + run "git", "submodule", "update", "--init", "--recursive", dir: to + run "rm", "-rf", ".git", dir: to end def copy_from_path(dest_path, src_path) src_path ||= dest_path copy_from = File.join(@copy_from_path, src_path) raise "'#{src_path}' not found in #{@copy_from_url}" unless File.exist?(copy_from) - run "cp -Rp #{copy_from} #{dest_path}" + run "cp", "-Rp", copy_from, dest_path end end