Skip to content

Commit

Permalink
Merge pull request #162 from gabealmer/use_gem_deps
Browse files Browse the repository at this point in the history
Load Bowerfile from all gem dependencies before load
  • Loading branch information
SergeyKishenin committed Aug 13, 2015
2 parents 9a19dfc + 148adcc commit acd11e1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ BowerRails.configure do |bower_rails|
# Invokes rake bower:install:deployment instead rake bower:install. Defaults to false
bower_rails.use_bower_install_deployment = true

# rake bower:install will search for gem dependencies and in each gem it will search for Bowerfile
# and then concatenate all Bowerfile for evaluation
bower_rails.use_gem_deps_for_bowerfile = true

# Passes the -F option to rake bower:install or rake bower:install:deployment. Defaults to false.
bower_rails.force_install = true
end
Expand Down
6 changes: 6 additions & 0 deletions lib/bower-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class << self
# instead of rake bower:install before assets precompilation
attr_accessor :use_bower_install_deployment

# If set to true then rake bower:install will search for gem dependencies
# and in each gem it will search for Bowerfile and then concatenate all Bowerfile
# for evaluation
attr_accessor :use_gem_deps_for_bowerfile

# If set to true then rake bower:install[-f] will be invoked
# instead of rake bower:install before assets precompilation
attr_accessor :force_install
Expand Down Expand Up @@ -58,5 +63,6 @@ def collect_tasks
@resolve_before_precompile = false
@clean_before_precompile = false
@use_bower_install_deployment = false
@use_gem_deps_for_bowerfile = false
@force_install = false
end
18 changes: 16 additions & 2 deletions lib/bower-rails/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ class Dsl
DEFAULT_DEPENDENCY_GROUP = :dependencies

def self.evalute(root_path, filename)
new(root_path).tap { |dsl| dsl.eval_file(File.join(root_path, filename)) }
new(root_path).tap do |dsl|
dsl.send(eval_file_method, File.join(root_path, filename))
end
end

def self.eval_file_method
BowerRails.use_gem_deps_for_bowerfile ? :eval_file_with_deps : :eval_file
end

attr_reader :dependencies, :root_path
Expand Down Expand Up @@ -67,7 +73,15 @@ def directories
end

def eval_file(file)
instance_eval(File.open(file, "rb") { |f| f.read }, file.to_s)
instance_eval(File.open(file, 'rb') { |f| f.read }, file.to_s)
end

def eval_file_with_deps(file)
Gem::Specification.map do |dep|
bowerfile_in_dep = File.join(dep.gem_dir, 'Bowerfile')
eval_file(bowerfile_in_dep) if bowerfile_in_dep != file && File.exist?(bowerfile_in_dep)
end
eval_file(file)
end

def final_assets_path
Expand Down
38 changes: 38 additions & 0 deletions spec/bower-rails/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,42 @@
subject.send(:default_group).should eq [:vendor, { :assets_path => "assets/somepath" }]
end
end

describe '.evalute' do
let(:tmp_bowerfile) { 'asset "moment"' }
before do
allow(File).to receive(:open).with('tmp/Bowerfile', 'rb').and_return(tmp_bowerfile)
end

context 'when use_gem_deps_for_bowerfile is true' do
let(:gem1) { double(Gem::Specification, gem_dir: 'gem1') }
let(:gem1_bowerfile) { 'asset "jquery"' }
let(:gem2) { double(Gem::Specification, gem_dir: 'gem2') }

before do
BowerRails.use_gem_deps_for_bowerfile = true
allow(Gem::Specification).to receive(:map).and_yield(gem1).and_yield(gem2)
allow(File).to receive(:exist?).with('gem1/Bowerfile').and_return(true)
allow(File).to receive(:exist?).with('gem2/Bowerfile').and_return(false)
allow(File).to receive(:open).with('gem1/Bowerfile', 'rb').and_return(gem1_bowerfile)
end

it 'should also evaluate Bowerfile in dependency' do
dsl = BowerRails::Dsl.evalute('tmp', 'Bowerfile')
dependencies = dsl.dependencies['tmp/vendor/assets'][:dependencies]
expect(dependencies.count).to eq(2)
expect(dependencies).to have_key('jquery')
expect(dependencies).to have_key('moment')
end
end

context 'when use_gem_deps_for_bowerfile is false' do
it 'should evaluate Bowerfile in project' do
dsl = BowerRails::Dsl.evalute('tmp', 'Bowerfile')
dependencies = dsl.dependencies['tmp/vendor/assets'][:dependencies]
expect(dependencies.count).to eq(1)
expect(dependencies).to have_key('moment')
end
end
end
end

0 comments on commit acd11e1

Please sign in to comment.