Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
153 lines (131 sloc) 6.24 KB
require 'rake'
################################################################################################
# constants
ROOT_DIR = File.expand_path('.')
STORE_TEMPLATE_WORKING_DIR = File.join(ROOT_DIR, '_storetemplate')
STORE_TEMPLATE_WORKING_DIR_WINDOW = File.join(STORE_TEMPLATE_WORKING_DIR, 'window.xhtml')
STORE_TEMPLATE_WORKING_DIR_SHARED = File.join(STORE_TEMPLATE_WORKING_DIR, 'shared')
STORE_TEMPLATE_WORKING_DIR_SHARED_CSS = File.join(STORE_TEMPLATE_WORKING_DIR_SHARED, 'css')
STORE_TEMPLATE_WORKING_DIR_SHARED_IMG = File.join(STORE_TEMPLATE_WORKING_DIR_SHARED, 'img')
STORE_TEMPLATE_WORKING_DIR_SHARED_CSS_SITE = File.join(STORE_TEMPLATE_WORKING_DIR_SHARED_CSS, 'site.css')
SITE_DIR = File.join(ROOT_DIR, '_site')
SITE_SHARED_DIR = File.join(SITE_DIR, 'shared')
SITE_STORE_TEMPLATE = File.join(SITE_DIR, 'store-template.html')
SITE_STORE_FAVICON = File.join(SITE_DIR, 'favicon.ico')
SITE_STORE_ROBOTS = File.join(SITE_DIR, 'robots.txt')
ZIP_PATH = File.join(ROOT_DIR, 'store-template.zip')
################################################################################################
# dependencies
begin
require 'colored'
rescue LoadError
raise 'You must "gem install colored" to use terminal colors'
end
################################################################################################
# helpers
def die(msg, status=1)
puts "Error[#{status||$?}]: #{msg}".red
exit status||$?
end
def sys(cmd)
puts "> #{cmd}".yellow
system(cmd)
end
def patch(path, replacers)
puts "Patching " + path.blue
lines = []
File.open(path, "r") do |f|
f.each do |line|
replacers.each do |r|
line.gsub!(r[0], r[1])
end
lines << line
end
end
File.open(path, "w") do |f|
f << lines.join
end
end
################################################################################################
# tasks
desc "generate store template zip" # see https://springboard.fastspring.com/site/configuration/template/doc/templateOverview.xml
task :store do
# build the site using jekyll
sys("rm -rf \"#{SITE_DIR}\"")
sys('jekyll')
# copy interesting parts
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR}\"")
sys("mkdir -p \"#{STORE_TEMPLATE_WORKING_DIR}\"")
sys("cp -r \"#{SITE_SHARED_DIR}\" \"#{STORE_TEMPLATE_WORKING_DIR}\"")
sys("cp \"#{SITE_STORE_TEMPLATE}\" \"#{STORE_TEMPLATE_WORKING_DIR_WINDOW}\"")
sys("cp \"#{SITE_STORE_FAVICON}\" \"#{STORE_TEMPLATE_WORKING_DIR}\"")
sys("cp \"#{SITE_STORE_ROBOTS}\" \"#{STORE_TEMPLATE_WORKING_DIR}\"")
# make our HTML valid XHTML
patch(STORE_TEMPLATE_WORKING_DIR_WINDOW, [
["<!DOCTYPE html>", ""],
["<html ", "<html xmlns=\"http://www.w3.org/1999/xhtml\"\n "],
[/<body(.*?)>/, "<body\\1><div id=\"page-store-template\">"],
["</body>", "</div></body>"],
[/<script(.*?)>/, "<script\\1>//<![CDATA[\n"],
[/<\/script>/, "\n//]]></script>"],
[/<link(.*?)([\/]{0,1})>/, "<link\\1/>"],
[/<img(.*?)([\/]{0,1})>/, "<img\\1/>"],
[/<br>/, "<br/>"],
[/href="\/([^\/])/, "href=\"\\1"],
[/src="\/([^\/])/, "src=\"\\1"],
["&nbsp;", "&#160;"],
["&copy;", "&#169;"],
["##INSERT STORE CONTENT HERE##", "\n<!-- TemplateBeginEditable name=\"Content\" -->\n\n<!-- TemplateEndEditable -->"],
["</title>", "</title>\n<link title=\"main\" rel=\"stylesheet\" href=\"http://resource.fastspring.com/app/s/style/base.css\" media=\"screen,projection\" type=\"text/css\" />\n<link title=\"main\" rel=\"stylesheet\" href=\"http://resource.fastspring.com/app/store/style/base.css\" media=\"screen,projection\" type=\"text/css\" />"]
])
content = File.read(STORE_TEMPLATE_WORKING_DIR_WINDOW)
content.gsub!(/<!-- SCRIPTS START -->(.*?)<!-- SCRIPTS END -->(.*?)<body(.*?)>/m, "\\2<body\\3>\\1")
content.gsub!(/src="\/\/cdn\.optimizely\.com/m, "src=\"https://cdn.optimizely.com") # FastSpring tries to be too smart during upload and removes non-resolvable urls with unknown.gif
content.gsub!(/src="http:/m, "src=\"https:")
File.open(STORE_TEMPLATE_WORKING_DIR_WINDOW, "w") do |f|
f << content
end
# fix paths in CSS
patch(STORE_TEMPLATE_WORKING_DIR_SHARED_CSS_SITE, [
[/\/shared\//, "../"]
])
# clean up shared folder to reduce size
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/overlay\"")
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/scrollable\"")
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/tabs\"")
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/flags\"")
sys("rm -rf \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/icons\"")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/totalfinder-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/visor-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/firequery-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/drydrop-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/xrefresh-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/firelogger-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/firepython-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/firerainbow-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/firelogger4php-\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/osx\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/ffintro\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/about-photo\"*")
sys("rm \"#{STORE_TEMPLATE_WORKING_DIR_SHARED_IMG}/howto\"*")
# zip it!
sys("rm \"#{ZIP_PATH}\"") # clean!
Dir.chdir(STORE_TEMPLATE_WORKING_DIR) do
sys("zip -r \"#{ZIP_PATH}\" .")
end
puts "Store template is in " + ZIP_PATH.green + ". Don't forget to upload it to FastSpring"
end
desc "generate 404.html and copy it to all subdomains"
task :error404 do
# build the site using jekyll
sys("rm -rf \"#{SITE_DIR}\"")
sys('jekyll')
page = File.join(SITE_DIR, "404.html")
die("something went wrong, page #{page} does not exist") unless File.exists?(page)
Dir.glob(File.join(ROOT_DIR, "..", "*")) do |path|
next unless File.directory?(path)
dir = File.expand_path(path)
sys("cp \"#{page}\" \"#{dir}\"")
end
end
task :default => :store
Something went wrong with that request. Please try again.