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

Added main_files option to DSL #137

Merged
merged 1 commit into from
May 4, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
17 changes: 16 additions & 1 deletion lib/bower-rails/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 : {}

Expand All @@ -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] ||= {}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/bower-rails/performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^\.\//, '')) }
Expand Down Expand Up @@ -195,4 +192,4 @@ def find_command(cmd, paths = [])
end

end
end
end
13 changes: 13 additions & 0 deletions spec/bower-rails/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/bower-rails/performer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__) }
Expand All @@ -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/*"
Expand All @@ -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
Expand All @@ -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