Module: Emblaze
- Includes:
- Rake::DSL
- Defined in:
- lib/emblaze/tasks.rb,
lib/emblaze/emblaze.rb,
lib/emblaze/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .access_key ⇒ Object
- .bucket_name ⇒ Object
- .file_types ⇒ Object
- .gzip(data) ⇒ Object
- .install_tasks ⇒ Object
- .local_dir ⇒ Object
- .region ⇒ Object
- .s3 ⇒ Object
- .secret_key ⇒ Object
- .traverse_directory(path) ⇒ Object
- .upload_compressed_object(key, f, ext) ⇒ Object
- .upload_object(key, f, ext) ⇒ Object
Class Method Details
.access_key ⇒ Object
7 |
# File 'lib/emblaze/emblaze.rb', line 7 def access_key; ENV['AWS_ACCESS_KEY']; end |
.bucket_name ⇒ Object
10 |
# File 'lib/emblaze/emblaze.rb', line 10 def bucket_name; ENV['AWS_BUCKET_NAME']; end |
.file_types ⇒ Object
12 13 14 15 16 17 |
# File 'lib/emblaze/emblaze.rb', line 12 def file_types { ".html" => { type: "text/html" }, ".css" => { type: "text/css" }, ".js" => { type: "application/javascript" } } end |
.gzip(data) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/emblaze/emblaze.rb', line 31 def gzip(data) sio = StringIO.new gz = Zlib::GzipWriter.new(sio) gz.write(data) gz.close sio.string end |
.install_tasks ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/emblaze/tasks.rb', line 7 def install_tasks desc "Deploy via S3" task :s3 do Dotenv.load page = s3.list_objects(bucket: bucket_name) p "deleting content" loop do page.contents.each do |ob| s3.delete_object bucket: bucket_name, key: ob.key end break unless page.next_page? page = page.next_page end p "uploading _site" traverse_directory(local_dir).flatten.compact.each do |f| key = f.gsub(local_dir+"/", "") ext = File.extname(f) if file_types.keys.include? ext upload_compressed_object(key, f, ext) else upload_object(key, f, ext) end end p "s3 deploy complete" end # Shamelessly copied from # https://gist.github.com/rrevanth/9377cb1f1664bf610e38#file-rakefile-L43 # https://github.com/stereobooster/jekyll-press/issues/26 desc "Minify site" task :minify do puts "\n## Compressing static assets" original = 0.0 compressed = 0 Dir.glob("_site/**/*.*") do |file| case File.extname(file) when ".css", ".gif", ".html", ".jpg", ".jpeg", ".js", ".png", ".xml" puts "Processing: #{file}" original += File.size(file).to_f min = Reduce.reduce(file) File.open(file, "w") do |f| f.write(min) end compressed += File.size(file) else puts "Skipping: #{file}" end end puts "Total compression %0.2f\%" % (((original-compressed)/original)*100) end end |
.local_dir ⇒ Object
5 |
# File 'lib/emblaze/emblaze.rb', line 5 def local_dir; './_site'; end |
.region ⇒ Object
9 |
# File 'lib/emblaze/emblaze.rb', line 9 def region; ENV['AWS_REGION']; end |
.s3 ⇒ Object
57 58 59 60 61 62 |
# File 'lib/emblaze/emblaze.rb', line 57 def s3 @@s3 ||= Aws::S3::Client.new( region: region, credentials: Aws::Credentials.new(access_key, secret_key) ) end |
.secret_key ⇒ Object
8 |
# File 'lib/emblaze/emblaze.rb', line 8 def secret_key; ENV['AWS_SECRET_KEY']; end |
.traverse_directory(path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/emblaze/emblaze.rb', line 19 def traverse_directory(path) Dir.entries(path).map do |f| next if [".", ".."].include? f f_path = File.join(path, f) if File.directory? f_path traverse_directory f_path else f_path end end end |
.upload_compressed_object(key, f, ext) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/emblaze/emblaze.rb', line 39 def upload_compressed_object(key, f, ext) s3.put_object bucket: bucket_name, key: key, body: gzip(File.read(f)), acl: "public-read", content_type: file_types[ext][:type], content_encoding: "gzip", cache_control: "max-age=604800" end |
.upload_object(key, f, ext) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/emblaze/emblaze.rb', line 49 def upload_object(key, f, ext) s3.put_object bucket: bucket_name, key: key, body: File.open(f), acl: "public-read", cache_control: "max-age=604800" end |