Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Rakefile to handle external site deployment

  • Loading branch information...
commit b7e8374adcec13ec7f597e54a6e3f1f6885facb4 1 parent 7e3715a
@mhayes mhayes authored
Showing with 191 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +58 −0 Rakefile
  3. +4 −0 deploy.yml.sample
  4. +128 −0 lib/better_ftp.rb
View
1  .gitignore
@@ -1,3 +1,4 @@
.DS_Store
stylesheets/*
.sass-cache/*
+deploy.yml
View
58 Rakefile
@@ -0,0 +1,58 @@
+require "yaml"
+require "./lib/better_ftp"
+root = File.dirname(__FILE__)
+deploy = YAML::load_file(File.join(root, "deploy.yml"))
+deploy_to = deploy["deploy_to"]
+
+task :default => [:export_to_site]
+
+task :test => [:replace_tabs_with_2_spaces, :check_for_non_sass_urls, :run_s3_validations]
+
+task :replace_tabs_with_2_spaces do
+ true
+end
+
+task :run_w3c_validations do
+ true
+end
+
+task :check_for_non_sass_urls do
+ # scss_files = Dir.glob("#{root}/sass/**/*.scss")
+ #
+ # scss_files.each do |scss|
+ # open(scss) { |f| f.grep(/url/) }
+ # end
+ true
+end
+
+task :export_to_site do
+ ftp = BetterFTP.open(deploy["host"], deploy["username"], deploy["password"])
+ files = `git ls-files`.split("\n").map {|f| "#{root}/#{f}"}
+ `compass compile`
+ css_files = Dir.glob("#{root}/stylesheets/**/*.css")
+
+ files = files + css_files
+ files.each do |abs_path|
+ f = File.open(abs_path)
+ relative_path = abs_path.gsub(root,"")
+ puts "Uploading: #{relative_path}"
+ begin
+ ftp.put(f, "#{deploy_to}/#{relative_path}")
+ rescue => e
+ if ftp.directory?("#{deploy_to}/#{File.dirname(relative_path)}")
+ puts "Error: #{e.to_s}"
+ else
+ puts "Making directory: #{deploy_to}/#{File.dirname(relative_path)}"
+ ftp.mkdir_p("#{deploy_to}/#{File.dirname(relative_path)}")
+ begin
+ # Try to upload again now that directory exists
+ ftp.put(f, "#{deploy_to}/#{relative_path}")
+ rescue => e2
+ puts e2.to_s
+ end
+ end
+ end
+ f.close
+ end
+ ftp.quit
+end
View
4 deploy.yml.sample
@@ -0,0 +1,4 @@
+deploy_to: /wired
+username: secret
+password: secret
+host: secret
View
128 lib/better_ftp.rb
@@ -0,0 +1,128 @@
+require 'net/ftp'
+
+class BetterFTP < Net::FTP
+
+ attr_accessor :port
+ attr_accessor :public_ip
+ alias_method :cd, :chdir
+ attr_reader :home
+
+ def initialize(host = nil, user = nil, passwd = nil, acct = nil)
+ super
+ @host = host
+ @user = user
+ @passwd = passwd
+ @acct = acct
+ @home = self.pwd
+ initialize_caches
+ end
+
+ def initialize_caches
+ @created_paths_cache = []
+ @deleted_paths_cache = []
+ end
+
+ def connect(host, port = nil)
+ port ||= @port || FTP_PORT
+ if @debug_mode
+ print "connect: ", host, ", ", port, "\n"
+ end
+ synchronize do
+ initialize_caches
+ @sock = open_socket(host, port)
+ voidresp
+ end
+ end
+
+ def reconnect!
+ if @host
+ connect(@host)
+ if @user
+ login(@user, @passwd, @acct)
+ end
+ end
+ end
+
+ def directory?(path)
+ chdir(path)
+
+ return true
+ rescue Net::FTPPermError
+ return false
+ end
+
+ def file?(path)
+ chdir(File.dirname(path))
+
+ begin
+ size(path)
+ return true
+ rescue Net::FTPPermError
+ return false
+ end
+ end
+
+ def mkdir_p(dir)
+ parts = dir.split("/")
+ if parts.first == "~"
+ growing_path = ""
+ else
+ growing_path = "/"
+ end
+ for part in parts
+ next if part == ""
+ if growing_path == ""
+ growing_path = part
+ else
+ growing_path = File.join(growing_path, part)
+ end
+ unless @created_paths_cache.include?(growing_path)
+ # puts "Creating #{growing_path.inspect}" if @debug_mode
+ begin
+ mkdir(growing_path)
+ chdir(growing_path)
+ rescue Net::FTPPermError, Net::FTPTempError => e
+ # puts "Received #{e.class}: #{e.message}" if @debug_mode
+ end
+ @created_paths_cache << growing_path
+ else
+ # puts "Cache says we already created #{growing_path.inspect}" if @debug_mode
+ end
+ end
+ end
+
+ def rm_r(path)
+ return if @deleted_paths_cache.include?(path)
+ @deleted_paths_cache << path
+ if directory?(path)
+ chdir path
+
+ begin
+ files = nlst
+ files.each {|file| rm_r "#{path}/#{file}"}
+ rescue Net::FTPTempError
+ # maybe all files were deleted already
+ end
+
+ rmdir path
+ else
+ rm(path)
+ end
+ end
+
+ def rm(path)
+ chdir File.dirname(path)
+ delete File.basename(path)
+ end
+
+private
+
+ def makeport
+ sock = TCPServer.open(@sock.addr[3], 0)
+ port = sock.addr[1]
+ host = @public_ip || sock.addr[3]
+ resp = sendport(host, port)
+ return sock
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.