From daab0aa7ab8c0be3f7b5b901f059be0050f7c6f1 Mon Sep 17 00:00:00 2001 From: Gatis Tomsons Date: Sun, 3 May 2015 00:57:34 +0300 Subject: [PATCH] Added main_files option to DSL --- CHANGELOG.md | 1 + README.md | 26 +++++++++++++++++++++++++- lib/bower-rails/dsl.rb | 17 ++++++++++++++++- lib/bower-rails/performer.rb | 9 +++------ spec/bower-rails/dsl_spec.rb | 13 +++++++++++++ spec/bower-rails/performer_spec.rb | 17 +++++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5859c45..f505612 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Edge version * add ability to configure bower to pass -F to bower install by @hubert [#129][] +* add ability to append files into `main` directive by @gacha [#117][] [#129]: https://github.com/42dev/bower-rails/pull/129 diff --git a/README.md b/README.md index 31c4690..8a384c0 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,23 @@ end NOTE: Available groups are `:lib` and `:vendor`. Others are not allowed according to the Rails convention. NOTE: All the assets should be stored in `/assets` subdirectory so putting it under `./vendor/js` directory is unavailable +You can extend `main` directive to include some missing files using `main_files` +option as parameter or in a block: + +``` ruby +# via argument +asset "moment", "2.10.1", main_files: ["./locale/en-gb.js"] + +# or in block +asset "moment", "2.10.1" do + main_files: [ + "./locale/en-gb.js", + "./locale/fr.js", + "./locale/lv.js" + ] +end +``` + And finally, you can specify the assets to be in the devDependencies block: ``` ruby @@ -275,4 +292,11 @@ Remember that you should have [bower installed](#bower-installation) either loca ##Bower Main Files -Each bower component should follow the [bower.json spec](https://github.com/bower/bower.json-spec) which designates a recommended `main` directive that lists the primary files of that component. You may choose to reference these files if you are using the asset pipeline, in which case other extraneous includes of the bower component are not needed. The `rake bower:clean` task removes every file that isn't listed in the `main` directive, if the component specifies a `main` directive. Otherwise, the library will remain as bower installed it. It supports wildcards in files listed in `main` directive. +Each bower component should follow the [bower.json spec](https://github.com/bower/bower.json-spec) +which designates a recommended `main` directive that lists the primary files of +that component. You may choose to reference these files if you are using the asset +pipeline, in which case other extraneous includes of the bower component are not needed. +The `rake bower:clean` task removes every file that isn't listed in the `main` directive, +if the component specifies a `main` directive. Remember that you can extend the `main` directive +in [ruby DSL configuration](#ruby-dsl-configuration). Otherwise, the library will remain as bower installed it. It supports wildcards +in files listed in `main` directive. diff --git a/lib/bower-rails/dsl.rb b/lib/bower-rails/dsl.rb index 46e77ab..ae9d0cb 100644 --- a/lib/bower-rails/dsl.rb +++ b/lib/bower-rails/dsl.rb @@ -19,9 +19,10 @@ def initialize(root_path) @dependencies = {} @resolutions = {} @assets_path ||= "assets" + @main_files = [] end - def asset(name, *args) + def asset(name, *args, &block) group = @current_group || default_group options = Hash === args.last ? args.pop.dup : {} @@ -38,6 +39,12 @@ def asset(name, *args) end end + if options[:main_files] + main_files(options[:main_files]) + end + + instance_eval(&block) if block_given? + normalized_group_path = normalize_location_path(group.first, group_assets_path(group)) @dependencies[normalized_group_path] ||= {} @dependencies[normalized_group_path][current_dependency_group_normalized] ||= {} @@ -116,6 +123,14 @@ def write_dotbowerrc end end + # accepts string or array to alter main option for bower.json + def main_files(value = nil) + if value + @main_files = Array(value) + end + @main_files + end + private # Stores the dependency group name in the stack diff --git a/lib/bower-rails/performer.rb b/lib/bower-rails/performer.rb index fa41240..0ca08eb 100644 --- a/lib/bower-rails/performer.rb +++ b/lib/bower-rails/performer.rb @@ -157,11 +157,8 @@ def remove_extra_files # Parse bower.json bower_json = JSON.parse(bower_file) - main_files = bower_json['main'] - next unless main_files - - # Handle singular or multiple files - main_files = [main_files] unless main_files.is_a?(Array) + main_files = Array(bower_json['main']) + dsl.main_files + next if main_files.empty? # Remove "./" relative path from main file strings main_files.map! { |file| File.join(component_dir, file.gsub(/^\.\//, '')) } @@ -195,4 +192,4 @@ def find_command(cmd, paths = []) end end -end \ No newline at end of file +end diff --git a/spec/bower-rails/dsl_spec.rb b/spec/bower-rails/dsl_spec.rb index 59cc246..16fe184 100644 --- a/spec/bower-rails/dsl_spec.rb +++ b/spec/bower-rails/dsl_spec.rb @@ -112,6 +112,19 @@ subject.asset :new_hotness, :github => "initech/tps-kit", :ref => "b122a" subject.dependencies.values.should include :dependencies => {:new_hotness => "git://github.com/initech/tps-kit#b122a"} end + + it "should accept a main_files option and put it all together" do + subject.asset :new_hotness, :main_files => ['dist/foo.js'] + subject.main_files.should eq(['dist/foo.js']) + end + + it "should be able to set main_files via block and put it all together" do + subject.asset :new_hotness do + main_files ['dist/foo.js'] + end + subject.main_files.should eq(['dist/foo.js']) + end + end it "should have a private method to validate asset paths" do diff --git a/spec/bower-rails/performer_spec.rb b/spec/bower-rails/performer_spec.rb index ba48e03..cd070a5 100644 --- a/spec/bower-rails/performer_spec.rb +++ b/spec/bower-rails/performer_spec.rb @@ -5,6 +5,7 @@ describe BowerRails::Performer do let(:performer) { BowerRails::Performer.new } + let(:main_files) { [] } context "remove_extra_files" do let(:root) { File.expand_path('../../..', __FILE__) } @@ -25,6 +26,7 @@ FileUtils.touch("#{root}/tmp/vendor/assets/bower_components/moment/fonts/font.svg") FileUtils.touch("#{root}/tmp/vendor/assets/bower_components/moment/moment.js") FileUtils.touch("#{root}/tmp/vendor/assets/bower_components/moment/unknown.file") + FileUtils.touch("#{root}/tmp/vendor/assets/bower_components/moment/moment_plugin.js") FileUtils.mkdir("#{root}/tmp/vendor/assets/bower_components/moment/unknown_dir") # creates bower.json with `main` files: "./moment.js", "./fonts/*" @@ -38,6 +40,9 @@ # trick BowerRails that system has bower installed allow(performer).to receive(:find_command) { "bower" } + # sets main_files in DSL + allow_any_instance_of(BowerRails::Dsl).to receive(:main_files){ main_files } + Dir.chdir("#{root}/tmp") performer.perform false do @@ -64,6 +69,18 @@ it "keeps font/font.svg" do expect(File).to exist("#{root}/tmp/vendor/assets/bower_components/moment/fonts/font.svg") end + + it "removes moment_plugin.js" do + expect(File).to_not exist("#{root}/tmp/vendor/assets/bower_components/moment/moment_plugin.js") + end + + context "with additional main_files" do + let(:main_files) { ['./moment_plugin.js'] } + + it "keeps moment_plugin.js" do + expect(File).to exist("#{root}/tmp/vendor/assets/bower_components/moment/moment_plugin.js") + end + end end end