Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify in html generator #32

Merged
merged 3 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/minimart/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Minimart
VERSION = "1.1.3"
VERSION = "1.1.4"
end
38 changes: 31 additions & 7 deletions lib/minimart/web/html_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'sprockets'
require 'sass'
require 'uglifier'

require 'minimart/web/dashboard_generator'
require 'minimart/web/cookbook_show_page_generator'

Expand All @@ -8,11 +12,7 @@ module Web
class HtmlGenerator
include Minimart::Web::TemplateHelper

# @return [String] the directory to put any generated HTML in
attr_reader :web_directory

# @return [Minimart::Web::Cookbooks] the set of cookbooks to generate HTML for
attr_reader :cookbooks
attr_reader :web_directory, :cookbooks

# @param [Hash] opts
# @option opts [String] :web_directory The directory to put any generated HTML in
Expand All @@ -24,15 +24,39 @@ def initialize(opts = {})

# Generate any HTML!
def generate
copy_assets
generate_assets
generate_index
generate_cookbook_show_pages
end

private

def generate_assets
generate_js
generate_css
copy_assets
end

def copy_assets
FileUtils.cp_r(File.join(minimart_web_directory, 'assets'), web_directory)
FileUtils.cp_r(compiled_asset_directory, web_directory)
end

def generate_js
js_dir = Pathname.new(compiled_asset_directory).join('javascripts')
FileUtils.mkdir_p(js_dir)
sprockets = Sprockets::Environment.new(minimart_root_directory)
sprockets.js_compressor = :uglify
sprockets.append_path(Pathname.new(raw_asset_directory).join('javascripts'))
sprockets['manifest.js'].write_to(js_dir.join('application.min.js'))
end

def generate_css
css_dir = Pathname.new(compiled_asset_directory).join('stylesheets')
FileUtils.mkdir_p(css_dir)
sprockets = Sprockets::Environment.new(minimart_root_directory)
sprockets.css_compressor = :sass
sprockets.append_path(Pathname.new(raw_asset_directory).join('stylesheets'))
sprockets['manifest.css'].write_to(css_dir.join('application.min.css'))
end

def generate_index
Expand Down
14 changes: 13 additions & 1 deletion lib/minimart/web/template_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ def template_file(template_name)

# @return [String] The path to the Minimart web directory
def minimart_web_directory
File.join(Minimart.root_path, '..', 'web')
File.join(minimart_root_directory, '..', 'web')
end

def minimart_root_directory
Minimart.root_path
end

def raw_asset_directory
File.join(minimart_web_directory, '_assets')
end

def compiled_asset_directory
File.join(minimart_web_directory, 'assets')
end

# Get the path for a web asset (CSS, JS, etc...)
Expand Down
5 changes: 4 additions & 1 deletion minimart.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ Gem::Specification.new do |spec|
spec.add_dependency 'git', '~> 1.2', '>= 1.2.6'
spec.add_dependency 'minitar', '~> 0.5'
spec.add_dependency 'octokit', '~> 3.7'
spec.add_dependency 'redcarpet', '~> 3.2'
spec.add_dependency 'redcarpet', '= 3.3.2'
spec.add_dependency 'rest-client', '~> 1.7'
spec.add_dependency 'ridley', '~> 4.1'
spec.add_dependency 'solve', '~> 1.2', '>= 1.2.1'
spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'tilt', '~> 2.0'
spec.add_dependency 'sprockets', '~> 3.5'
spec.add_dependency 'uglifier', '~> 2.7'
spec.add_dependency 'sass', '~> 3.4'

spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.3'
Expand Down
16 changes: 14 additions & 2 deletions spec/lib/minimart/web/html_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
end

let(:web_directory) { test_directory }
let(:assets_directory) { File.join(web_directory, 'assets') }
let(:stylesheets_directory) { File.join(assets_directory, 'stylesheets') }
let(:javascripts_directory) { File.join(assets_directory, 'javascripts') }

subject do
Minimart::Web::HtmlGenerator.new(
Expand All @@ -17,7 +20,17 @@
describe '#generate' do
it 'should copy any available assets' do
subject.generate
expect(Dir.exists?(File.join(web_directory, 'assets'))).to eq true
expect(Dir.exist?(assets_directory)).to eq true
end

it 'should minify raw CSS files' do
subject.generate
expect(Dir.entries(stylesheets_directory)).to include('application.min.css')
end

it 'should unglify raw JS files' do
subject.generate
expect(Dir.entries(javascripts_directory)).to include('application.min.js')
end

it 'should generate the dashboard page' do
Expand All @@ -30,5 +43,4 @@
subject.generate
end
end

end
19 changes: 14 additions & 5 deletions web/assets/javascripts/application.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/assets/stylesheets/application.min.css

Large diffs are not rendered by default.