diff --git a/README.md b/README.md index 1189be7..d68625b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/bower-rails.rb b/lib/bower-rails.rb index 17e4bb2..b2d422c 100644 --- a/lib/bower-rails.rb +++ b/lib/bower-rails.rb @@ -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 @@ -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 diff --git a/lib/bower-rails/dsl.rb b/lib/bower-rails/dsl.rb index 544b4a5..788d6dc 100644 --- a/lib/bower-rails/dsl.rb +++ b/lib/bower-rails/dsl.rb @@ -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 @@ -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 diff --git a/spec/bower-rails/dsl_spec.rb b/spec/bower-rails/dsl_spec.rb index 5a0dd28..8f3dbb3 100644 --- a/spec/bower-rails/dsl_spec.rb +++ b/spec/bower-rails/dsl_spec.rb @@ -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