lib/middleman-deploy/commands.rb in middleman-deploy-0.0.12 vs lib/middleman-deploy/commands.rb in middleman-deploy-0.1.0
- old
+ new
@@ -20,25 +20,36 @@
# 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",
+ desc "deploy [options]", Middleman::Deploy::TAGLINE
+ method_option "build_before",
:type => :boolean,
- :aliases => "-c",
- :desc => "Remove orphaned files or directories on the remote host"
+ :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
+ builder = ::Middleman::Cli::Build.new(args=[], options={:instrument=>false})
+ builder.build
+ end
send("deploy_#{self.deploy_options.method}")
end
protected
def print_usage_and_die(message)
raise Error, "ERROR: " + message + "\n" + <<EOF
-You should follow one of the three examples below to setup the deploy
+You should follow one of the four examples below to setup the deploy
extension in config.rb.
# To deploy the build directory to a remote host via rsync:
activate :deploy do |deploy|
deploy.method = :rsync
@@ -68,10 +79,20 @@
deploy.host = "ftp.example.com"
deploy.user = "tvaughan"
deploy.password = "secret"
deploy.path = "/srv/www/site"
end
+
+# To deploy the build directory to a remote host via sftp:
+activate :deploy do |deploy|
+ deploy.method = :sftp
+ # host, user, passwword and path *must* be set
+ deploy.host = "sftp.example.com"
+ deploy.user = "tvaughan"
+ deploy.password = "secret"
+ deploy.path = "/srv/www/site"
+end
EOF
end
def inst
::Middleman::Application.server.inst
@@ -88,19 +109,18 @@
if (!options.method)
print_usage_and_die "The deploy extension requires you to set a method."
end
- if (options.method == :rsync)
+ case options.method
+ when :rsync
if (!options.host || !options.user || !options.path)
print_usage_and_die "The rsync deploy method requires host, user, and path to be set."
end
- end
-
- if (options.method == :ftp)
+ when :ftp, :sftp
if (!options.host || !options.user || !options.password || !options.path)
- print_usage_and_die "The ftp deploy method requires host, user, password, and path to be set."
+ print_usage_and_die "The #{options.method} method requires host, user, password, and path to be set."
end
end
options
end
@@ -113,17 +133,11 @@
puts "## Deploying via rsync to #{user}@#{host}:#{path} port=#{port}"
command = "rsync -avze '" + "ssh -p #{port}" + "' #{self.inst.build_dir}/ #{user}@#{host}:#{path}"
- if options.has_key? "clean"
- clean = options.clean
- else
- clean = self.deploy_options.clean
- end
-
- if clean
+ if self.deploy_options.clean
command += " --delete"
end
run command
end
@@ -156,11 +170,11 @@
`git remote add origin #{remote}`
end
end
#if there is a branch with that name, switch to it, otherwise create a new one and switch to it
- if `git branch`.split("\n").delete_if{ |r| r =~ Regexp.new(branch,true) }.count == 0
+ if `git branch`.split("\n").any? { |b| b =~ /#{branch}/i }
`git checkout #{branch}`
else
`git checkout -b #{branch}`
end
@@ -216,9 +230,48 @@
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"