require 'fileutils' module CapistranoExtensions module Files module Remote def tail_f(file, n=10) cmd = "tail -n #{n} -f #{_q file}" _via == :system ? system(cmd) : stream(cmd, :via => _via) rescue Interrupt logger.trace "interrupted (Ctrl-C)" if logger end def upload(*args) _via == :system ? cp(*args) : @config.upload(*args) end def download(*args) _via == :system ? cp(*args) : @config.download(*args) end def cd(dir, options={}) if block_given? dir, dir2 = pwd, dir _r 'cd', dir2 yield end _r 'cd', dir end def pwd capture("pwd", :via => _via) end def mkdir(*args) options = args.pop if Hash === args.last _r 'mkdir', args end def mkdir_p(*args) options = args.pop if Hash === args.last _r 'mkdir -p', args end def rmdir(*args) options = args.pop if Hash === args.last _r 'rmdir', args end def ln(*args) options = args.pop if Hash === args.last _r 'ln', args, 2 end def ln_s(*args) options = args.pop if Hash === args.last _r 'ln -s', args, 2 end def ln_sf(*args) options = args.pop if Hash === args.last _r 'ln -sf', args, 2 end def cp(*args) options = args.pop if Hash === args.last _r 'cp', args end def cp_r(*args) options = args.pop if Hash === args.last _r 'cp -r', args end def mv(*args) options = args.pop if Hash === args.last _r 'mv', args, 2 end def rm(*args) options = args.pop if Hash === args.last _r 'rm', args end def rm_r(*args) options = args.pop if Hash === args.last _r 'rm -r', args end def rm_rf(*args) options = args.pop if Hash === args.last _r 'rm -rf', args end def install(*args) options = args.pop if Hash === args.last _r 'install', args, 2 end def chmod(*args) options = args.pop if Hash === args.last _r 'chmod', args end def chmod_R(*args) options = args.pop if Hash === args.last _r 'chmod -R', args end def chown(*args) options = args.pop if Hash === args.last _r 'chown', args end def chown_R(*args) options = args.pop if Hash === args.last _r 'chown -R', args end def touch(*args) options = args.pop if Hash === args.last _r 'touch', args end def exists?(a) _t "test -f", a end def directory?(a, options={:verbose=>false}) _t "test -d", a end def executable?(a, options={:verbose=>false}) _t "test -x", a end private def _t(cmd, args=nil, min=nil) cmd = _a cmd, args, min if _via == :system then system(cmd) else capture("#{cmd}; echo $?", :via => _via).strip == '0' end end def _r(cmd, args=nil, min=nil) cmd = _a cmd, args, min if _via != :system then invoke_command(cmd, :via => _via) else $stdout.puts cmd system cmd end end def _a(cmd, args=nil, min=nil) case args when NilClass raise ArgumentError unless min.nil? or min.zero? when Array args = args.flatten raise ArgumentError if (min || 1) > args.length cmd = "#{cmd} #{_q(*args)}" if args.any? else raise ArgumentError if min and min < 1 cmd = "#{cmd} #{_q args}" end end def _q(*list) list.map { |l| "'#{l.gsub("'", "\\'")}'" }.join ' ' end def _via case (v = @config.fetch(:files_via, nil)) when :local :system when :remote, NilClass @config.fetch(:run_method, nil) else v end end end end end Capistrano.plugin :remote_files, CapistranoExtensions::Files::Remote