Created
November 26, 2010 16:59
-
-
Save twerth/716955 to your computer and use it in GitHub Desktop.
Converts a dynamic rails (could be any site really) site into static files and rsyncs them up to the server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
require 'net/http' | |
URL = 'http://localhost:3000' | |
OUT_DIR = 'static_output' | |
SSH_PORT = 2222 | |
SSH_URL = '[email protected]_server.com:/your_folder/on_server/' | |
resp = Net::HTTP.get_response URI.parse(URL) rescue puts 'Error connecting' | |
unless resp && resp.code == '200' | |
puts "App not running (make sure to run rails under deploy: rails server -e deploy)" | |
puts "Turn off asset tags in deploy env: ENV['RAILS_ASSET_ID'] = ''" | |
exit 0 | |
end | |
Dir.mkdir(OUT_DIR) unless File.exist?(OUT_DIR) | |
Dir.chdir(OUT_DIR) do | |
puts `rm -R *` | |
# Copy public, most of this will come over in the wget, but not all | |
puts `cp -Rpv ../public/ .` | |
`wget -m -x -p --no-host-directories --no-cache -erobots=off #{URL}` | |
# Hack! I need files without extentions to still work on the server (they won't without | |
# help). There are 2 ways to do this that I thought of, they are below. | |
# | |
# It would be great if I could get wget to append all html files without an html extention | |
# to something like .foo so I can identify them. | |
# I guess I could parse the file for html or xml, I'll do that later | |
Dir.glob("**/*").reject{|o| File.directory?(o)}.reject{|o| o =~ /.+\..+/}.each do |f| | |
# There are 2 ways to do this, the best way is to mod nginx to turn any | |
# url without an extention into one with .html (silently). I'm going to use that | |
# approach. Here is an example config for nginx (note that try_files is the trick): | |
# | |
# location / { | |
# root /your_server_files/public; | |
# index index.html; | |
# try_files $uri $uri.html $uri/ @notfound; | |
# } | |
# location @notfound { | |
# return 404; | |
# } | |
# | |
puts "Changing #{f} to #{f}.html" | |
FileUtils.move(f,"#{f}.html") | |
# This is a nother approach, creating a folder with the same name an sticking | |
# the file inside it, renaming it to index.html. This is less good | |
# | |
#puts "Changing #{f} to index.html in new #{f} folder" | |
#FileUtils.move(f,"#{f}.temp") | |
#Dir.mkdir(f) | |
#FileUtils.move("#{f}.temp", "#{f}/index.html") | |
end | |
puts 'Rsyncing to server' | |
puts `rsync -rv --stats --delete --exclude=.* -e "ssh -p #{SSH_PORT}" . #{SSH_URL}` | |
end | |
puts "\n\nAll finished" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment