require "middleman-core/cli" require "middleman-deploy/extension" require "middleman-deploy/pkg-info" PACKAGE = "#{Middleman::Deploy::PACKAGE}" VERSION = "#{Middleman::Deploy::VERSION}" 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", "Deploy build directory to a remote host via rsync or git" method_option "clean", :type => :boolean, :aliases => "-c", :desc => "Remove orphaned files or directories on the remote host" def deploy send("deploy_#{self.deploy_options.method}") end protected def print_usage_and_die(message) raise Error, "ERROR: " + message + "\n" + < 0 `git checkout #{branch}` else `git checkout -b #{branch}` end `git add -A` `git commit --allow-empty -am 'Automated commit at #{Time.now.utc} by #{PACKAGE} #{VERSION}'` `git push -f origin #{branch}` end end def deploy_ftp require 'net/ftp' 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 ftp to #{user}@#{host}:#{path}" ftp = Net::FTP.new(host) ftp.login(user, pass) ftp.chdir(path) ftp.passive = true Dir.chdir('build/') do files = Dir.glob('**/*', File::FNM_DOTMATCH) files.reject { |a| a =~ Regexp.new('\.$') }.each do |f| if File.directory?(f) begin ftp.mkdir(f) puts "Created directory #{f}" rescue end else begin if File.binary?(f) ftp.putbinaryfile(f, f) else ftp.puttextfile(f, f) end rescue Exception => 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 end # Alias "d" to "deploy" Base.map({ "d" => "deploy" }) end end