require "middleman-core/cli" require "middleman-deploy/extension" require "middleman-deploy/pkg-info" module Middleman module Cli # This class provides a "deploy" command for the middleman CLI. class Deploy < Thor include Thor::Actions check_unknown_options! namespace :deploy # Tell Thor to exit with a nonzero exit code on failure def self.exit_on_failure? true end desc "deploy [options]", Middleman::Deploy::TAGLINE method_option "build_before", :type => :boolean, :aliases => "-b", :desc => "Run `middleman build` before the deploy step" def deploy if options.has_key? "build_before" build_before = options.build_before else build_before = self.deploy_options.build_before end if build_before # http://forum.middlemanapp.com/t/problem-with-the-build-task-in-an-extension run("middleman build") || exit(1) end send("deploy_#{self.deploy_options.method}") end protected def print_usage_and_die(message) raise Error, "ERROR: " + message + "\n" + < e reply = e.message err_code = reply[0,3].to_i if err_code == 550 if File.binary?(f) ftp.putbinaryfile(f, f) else ftp.puttextfile(f, f) end end end puts "Copied #{f}" end end end ftp.close end def deploy_sftp require 'net/sftp' require 'ptools' host = self.deploy_options.host user = self.deploy_options.user pass = self.deploy_options.password path = self.deploy_options.path puts "## Deploying via sftp to #{user}@#{host}:#{path}" Net::SFTP.start(host, user, :password => pass) do |sftp| sftp.mkdir(path) Dir.chdir(self.inst.build_dir) do files = Dir.glob('**/*', File::FNM_DOTMATCH) files.reject { |a| a =~ Regexp.new('\.$') }.each do |f| if File.directory?(f) begin sftp.mkdir("#{path}/#{f}") puts "Created directory #{f}" rescue end else begin sftp.upload(f, "#{path}/#{f}") rescue Exception => e reply = e.message err_code = reply[0,3].to_i if err_code == 550 sftp.upload(f, "#{path}/#{f}") end end puts "Copied #{f}" end end end end end end # Alias "d" to "deploy" Base.map({ "d" => "deploy" }) end end