Created
November 14, 2014 23:18
-
-
Save ivarvong/25b16bb0b91bcd203906 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'sinatra' | |
require 'aws-sdk' | |
require 'digest/md5' | |
require 'dotenv' | |
Dotenv.load | |
$project_name = 'no-country' | |
$input_file = './index.html' | |
class S3Upload | |
def perform(options={}) | |
# kword args: access_key, access_secret, bucket, key, contents | |
# returns public url | |
url = AWS::S3.new( | |
access_key_id: options[:access_key], | |
secret_access_key: options[:access_secret] | |
) | |
.buckets[options[:bucket]] | |
.objects[options[:key]] | |
.write( | |
options[:contents], | |
acl: options[:acl] || :public_read | |
).public_url.to_s | |
puts "S3Uploaded: #{url}" | |
return url | |
end | |
end | |
class Inliner | |
def initialize(input_file) | |
@input_file = input_file | |
@scripts = [] | |
@styles = [] | |
end | |
def get_args(input) | |
input.split(' ').inject({}) {|obj, item| | |
k, v = item.split('=') | |
obj[k.to_sym] = (v || '').gsub("'", "").gsub('"', "") | |
obj | |
} | |
end | |
def resolve_file(relative_path) | |
File.expand_path(relative_path, File.dirname(@input_file)) | |
end | |
def read_file(relative_path) | |
File.open( resolve_file(relative_path) ).read | |
rescue | |
"<!-- problem opening #{relative_path} (#{resolve_file(relative_path)}) -->" | |
end | |
def script(body) | |
re = /\<script(.*)\>(.*)\<\/script\>/ | |
output = body.gsub(re) do |capture| | |
args = get_args($1) | |
if args[:src].include?('jquery') or args[:src].include?('underscore') | |
puts "skipping #{args[:src]}" | |
else | |
@scripts << read_file(args[:src]) | |
end | |
end | |
@scripts | |
end | |
def style(body) | |
re = /\<link(.*)\>/ | |
output = body.gsub(re) do |capture| | |
args = get_args($1) | |
@styles << read_file(args[:href]) | |
end | |
@styles | |
end | |
def get_body | |
@file_body ||= File.open(@input_file).read | |
end | |
def compressed_scripts | |
script( get_body() ) | |
.join("\n\n") | |
.gsub(/^\/\/\# sourceMappingURL=(.+).map$/, "") # remove source maps | |
end | |
def compressed_styles | |
style( get_body() ) | |
.join("\n\n") | |
end | |
end | |
def md5(input) | |
Digest::MD5.hexdigest(input) | |
end | |
get '/' do | |
publish() | |
erb :publish | |
end | |
def publish | |
inliner = Inliner.new($input_file) | |
js_text = inliner.compressed_scripts | |
js_key = "production/#{$project_name}/#{md5(js_text)}.js" | |
@js_url = upload(js_key, js_text) | |
css_text = inliner.compressed_styles | |
css_key = "production/#{$project_name}/#{md5(css_text)}.css" | |
@css_url = upload(css_key, css_text) | |
end | |
def upload(key, html) | |
url = S3Upload.new.perform( | |
access_key: ENV['S3_KEY'], | |
access_secret: ENV['S3_SECRET'], | |
bucket: ENV['S3_BUCKET'], | |
key: key, | |
contents: html | |
) | |
return url | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment