# Scans your project for CSS and JS files and # runs them through the Yahoo Compression utility # and then uploads the entire site to your web server via SCP. # Install prerequisites: # sudo gem install net-scp # Download http://www.julienlecomte.net/yuicompressor/ # Create a bin/ folder and place the following files from the download into the folder: # # * yuicompressor-2.4.2.jar # * rhino-1.6R7.jar # * jargs-1.0.jar # Configure your settings below and be sure # to supply the proper path to the Yahoo compressor. # Set the COMPRESS flag to false to skip compression COMPRESS = true WORKING_DIR = "working" REMOTE_USER = "foo" REMOTE_HOST = "foo.org" REMOTE_PORT = 22 REMOTE_DIR = "/home/#{REMOTE_USER}/#{REMOTE_HOST}/" LOCAL_DIR = "site" FILES = ["index.html", "faq.html", "about.html", "contact.html", "products", "services", "stylesheets", "images", "javascripts" ] COMPRESSOR_CMD = 'java -jar ../bin/yuicompressor-2.4.2.jar' # DONE CONFIGURING require 'rubygems' require 'net/scp' require 'fileutils' @errors = [] puts "Building page" `staticmatic build .` puts "Done building" Dir.chdir(LOCAL_DIR) FileUtils.rm_rf WORKING_DIR puts "Copying working files" FileUtils.mkdir WORKING_DIR FILES.each do |f| if File.directory?(f) FileUtils.cp_r f, WORKING_DIR else FileUtils.cp f, WORKING_DIR end end # Upload files in our working directory to the server def upload(files) puts "uploading..." Net::SCP.start(REMOTE_HOST, REMOTE_USER, :port => REMOTE_PORT) do |scp| files.each do |file| puts "uploading #{file}..." if File.directory?(file) scp.upload! "working/#{file}", REMOTE_DIR, :recursive => true else scp.upload! "working/#{file}", REMOTE_DIR end end end end # Minify all CSS and JS files found within the working # directory def minify(working_dir) files = Dir.glob("#{working_dir}/**/*.{css, js}") files.each do |file| type = File.extname(file) == ".css" ? "css" : "js" newfile = file.gsub(".#{type}", ".new.#{type}") puts "minifying #{file}" `#{COMPRESSOR_CMD} --type #{type} #{file} > #{newfile}` if File.size(newfile) > 0 FileUtils.cp newfile, file else @errors << "Unable to process #{file}." end end end if COMPRESS puts "Minifying CSS and JS files" minify(WORKING_DIR) end if @errors.length == 0 puts "Deploying" upload(FILES) else puts "Unable to deploy." @errors.each{|e| puts e} end