lib/middleman-deploy/commands.rb in middleman-deploy-0.0.6 vs lib/middleman-deploy/commands.rb in middleman-deploy-0.0.7

- old
+ new

@@ -36,11 +36,11 @@ protected def print_usage_and_die(message) raise Error, "ERROR: " + message + "\n" + <<EOF -You should follow one of the two examples below to setup the deploy +You should follow one of the three 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 @@ -60,10 +60,20 @@ deploy.remote = "some-other-remote-name" # branch is optional (default is "gh-pages") # run `git branch -a` to see a list of possible branches deploy.branch = "some-other-branch-name" end + +# To deploy the build directory to a remote host via ftp: +activate :deploy do |deploy| + deploy.method = :ftp + # host, user, passwword and path *must* be set + deploy.host = "ftp.example.com" + deploy.user = "tvaughan" + deploy.password = "secret" + deploy.path = "/srv/www/site" +end EOF end def deploy_options options = nil @@ -136,9 +146,57 @@ repo.push("origin", branch) end orig.push(remote, branch) orig.remote(remote).fetch + 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 + Dir['**/*'].each do |f| + if File.directory?(f) + begin + ftp.mkdir(f) + rescue + puts "Folder '#{f}' exists. skipping..." + 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 + end + end + end + ftp.close end end # Alias "d" to "deploy"