Skip to content

Commit

Permalink
Add gzip_level option to set the compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored and mojavelinux committed Sep 16, 2013
1 parent 387f486 commit 6cfa003
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
7 changes: 4 additions & 3 deletions lib/awestruct/deploy/base_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(site_config, deploy_config)
# Add a single front slash at the end of output dir
@site_path = File.join( site_config.output_dir, '/' ).gsub(/^\w:\//, '/')
@gzip = deploy_config['gzip']
@gzip_level = deploy_config['gzip_level'] || Zlib::BEST_COMPRESSION
@source_dir = deploy_config['source_dir'] || site_config.dir
@ignore_uncommitted = deploy_config['uncommitted']
init_scm(deploy_config['scm'] || 'git')
Expand Down Expand Up @@ -61,16 +62,16 @@ def gzip_site(site_path)
when :css, :js, :html
require 'zlib'
if !is_gzipped item
gzip_file item
gzip_file(item, @gzip_level)
end
end
end
end
end

def gzip_file(filename)
def gzip_file(filename, level)
$LOG.debug "Gzipping File #{filename}"
Zlib::GzipWriter.open("#{filename}.gz") do |gz|
Zlib::GzipWriter.open("#{filename}.gz", level) do |gz|
gz.mtime = File.mtime(filename)
gz.orig_name = filename
gz.write File.binread(filename)
Expand Down
47 changes: 38 additions & 9 deletions spec/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

deploy_config = mock
deploy_config.stub(:[]).with('gzip').and_return true
deploy_config.stub(:[]).with('gzip_level')
deploy_config.stub(:[]).with('source_dir').and_return '.'
deploy_config.stub(:[]).with('scm').and_return nil
deploy_config.stub(:[]).with('uncommitted').and_return true
Expand All @@ -64,16 +65,17 @@

deploy_config = mock
deploy_config.stub(:[]).with('gzip').and_return true
deploy_config.stub(:[]).with('gzip_level')
deploy_config.stub(:[]).with('source_dir').and_return '.'
deploy_config.stub(:[]).with('scm').and_return nil
deploy_config.stub(:[]).with('uncommitted').and_return nil

deployer = Awestruct::Deploy::Base.new(site_config, deploy_config)
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.html")
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.js")
deployer.should_receive(:gzip_file).with("#{site_dir}/subdir/yes.css")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.txt")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.html.gz")
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.html", Zlib::BEST_COMPRESSION)
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.js", Zlib::BEST_COMPRESSION)
deployer.should_receive(:gzip_file).with("#{site_dir}/subdir/yes.css", Zlib::BEST_COMPRESSION)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.txt", Zlib::BEST_COMPRESSION)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.html.gz", Zlib::BEST_COMPRESSION)
deployer.gzip_site(site_dir)
end

Expand All @@ -88,6 +90,7 @@

deploy_config = mock
deploy_config.stub(:[]).with('gzip').and_return true
deploy_config.stub(:[]).with('gzip_level')
deploy_config.stub(:[]).with('source_dir').and_return '.'
deploy_config.stub(:[]).with('scm').and_return nil
deploy_config.stub(:[]).with('uncommitted').and_return nil
Expand All @@ -96,11 +99,37 @@
deployer.gzip_site(site_dir)

# Gzip only once
deployer.should_not_receive(:gzip_file).with("#{site_dir}/yes.html")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/yes.js")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/subdir/yes.css")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.txt")
deployer.should_not_receive(:gzip_file).with("#{site_dir}/yes.html", Zlib::BEST_COMPRESSION)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/yes.js", Zlib::BEST_COMPRESSION)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/subdir/yes.css", Zlib::BEST_COMPRESSION)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.txt", Zlib::BEST_COMPRESSION)
deployer.gzip_site(site_dir)
end

it "should gzip with the compression level" do
site_tmp_dir = Dir.mktmpdir("site_dir")
site_src_dir = File.join(File.dirname(__FILE__), 'test-data/gzip')
FileUtils.cp_r(site_src_dir, site_tmp_dir)
site_dir = "#{site_tmp_dir}/gzip"

site_config = mock
site_config.stub(:output_dir).and_return "#{site_dir}"

deploy_config = mock
deploy_config.stub(:[]).with('gzip').and_return true
deploy_config.stub(:[]).with('gzip_level').and_return 6
deploy_config.stub(:[]).with('source_dir').and_return '.'
deploy_config.stub(:[]).with('scm').and_return nil
deploy_config.stub(:[]).with('uncommitted').and_return nil

deployer = Awestruct::Deploy::Base.new(site_config, deploy_config)
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.html", 6)
deployer.should_receive(:gzip_file).with("#{site_dir}/yes.js", 6)
deployer.should_receive(:gzip_file).with("#{site_dir}/subdir/yes.css", 6)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.txt", 6)
deployer.should_not_receive(:gzip_file).with("#{site_dir}/no.html.gz", 6)
deployer.gzip_site(site_dir)
end


end
1 change: 1 addition & 0 deletions spec/github_pages_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@deploy_config.stub(:[]).with('branch').and_return('the-branch')
@deploy_config.stub(:[]).with('repository').and_return('the-repo')
@deploy_config.stub(:[]).with('gzip').and_return('false')
@deploy_config.stub(:[]).with('gzip_level')
@deploy_config.stub(:[]).with('scm').and_return('git')
@deploy_config.stub(:[]).with('source_dir').and_return('.')
@deploy_config.stub(:[]).with('uncommitted').and_return('false')
Expand Down

0 comments on commit 6cfa003

Please sign in to comment.