bower-rails 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16fcf55d5a780de4c4ae812c3f56174cdc3db86b
4
- data.tar.gz: 874ce01615f934ea561b4c6eb5c8209b2c59205b
3
+ metadata.gz: 98d9ed52d0507bd2dcc3e78fefa66982aae28f3e
4
+ data.tar.gz: c483f89a0b7509543f31ba6d4922c86dcc8d560a
5
5
  SHA512:
6
- metadata.gz: 852c5924db1d15a70a3bf45cd90cab5a700180efa52c6257acc8e36ae924e58e29d8b9bc7a7ebb256f872b129ffc3f325934b85b1f1cd002ae96188335cde921
7
- data.tar.gz: db064ec965fbe7fa76069997576a763ed3d91a16754efe02c4533201a1b6f61bce21533dc24eb59568f9073de0b69341bcbad3b1b922c642252fd29ccea21415
6
+ metadata.gz: c2cf8e667c67755b5ff2f3694896f454c0286a56b4f84e7ba84dc9ae454f73d1bb482ea8e59b688c5ed455ad6b754736b4edf87a2df55f0a77faba097b688d32
7
+ data.tar.gz: 120b78bc07539c8616b09843473b23807f8b97abaa7193227fd26f173c273f398110c0f7d6fc68504f695b737a13cce0fe26002f5d3c1b714a8283f140ac2224
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  bower-rails
2
2
  ===========
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/bower-rails.png)](http://badge.fury.io/rb/bower-rails)
5
+ [![Code Climate](https://codeclimate.com/github/42dev/bower-rails.png)](https://codeclimate.com/github/42dev/bower-rails)
6
+ [![Dependency Status](https://gemnasium.com/SergeyKishenin/bower-rails.png)](https://gemnasium.com/SergeyKishenin/bower-rails)
7
+ [![Build Status](https://travis-ci.org/42dev/bower-rails.png?branch=master)](https://travis-ci.org/42dev/bower-rails)
8
+ [![Coverage Status](https://coveralls.io/repos/42dev/bower-rails/badge.png)](https://coveralls.io/r/42dev/bower-rails)
9
+
4
10
  Bower support for Rails projects. Dependency file is bower.json in Rails root dir or Bowerfile if you use DSL.
5
11
  Check out Changelog.md for the latest changes and releases.
6
12
 
@@ -14,20 +20,20 @@ Check out Changelog.md for the latest changes and releases.
14
20
  in Gemfile
15
21
 
16
22
  ``` Ruby
17
- gem "bower-rails", "~> 0.4.4"
23
+ gem "bower-rails", "~> 0.5.0"
18
24
  ```
19
25
 
20
- **Initialize**
26
+ ##JSON configuration
27
+
28
+ Bower-rails now supports the standard [bower package](https://github.com/bower/bower#defining-a-package) format out-of-the-box. Simply place your bower.json file the Rails root directory to start. Using the standard format will default all bower components to be installed under the `vendor` directory.
21
29
 
22
- To add an empty bower.json file to the project root.
30
+ To install dependencies into both `lib` and `vendor` directories, run the initializer to generate a custom bower.json:
23
31
 
24
32
  ``` Bash
25
- rails g bower_rails:initialize
33
+ rails g bower_rails:initialize
26
34
  ```
27
35
 
28
- ##JSON configuration
29
-
30
- The bower.json file is two seperate bower [component.js](https://github.com/twitter/bower#defining-a-package) files. Defining a package in lib and vendor will install those packages to the corresponding directories.
36
+ This will generate a special bower.json that combines two standard bower packages into one. Simply specify your dependencies under each folder name to install them into the corresponding directories.
31
37
 
32
38
  **example bower.json file**
33
39
 
@@ -91,6 +97,7 @@ group :lib do
91
97
  asset "backbone", "1.2"
92
98
  end
93
99
  ```
100
+ NOTE: Available groups are `:lib` and `:vendor`. Others are not allowed according to the Rails convention.
94
101
  NOTE: All the assets should be stored in `/assets` subdirectory so putting it under `./vendor/js` directory is unavailable
95
102
 
96
103
  ##Rake tasks
@@ -102,3 +109,16 @@ Once you are done with `bower.json` or `Bowerfile` you can run
102
109
  * `rake bower:update` to update js components
103
110
  * `rake bower:update:prune` to update components and uninstall extraneous packages
104
111
  * `rake bower:list` to list all packages
112
+ * `rake bower:resolve` to resolve [relative asset paths](#relative-asset-paths) in components
113
+
114
+ ##Bower Configuration
115
+
116
+ If you provide a `.bowerrc` in the rails project root, bower-rails will use it for bower configuration.
117
+ Some .bowerrc options are not supported: `directory`, `cwd`, and `interactive`. Bower-rails
118
+ will ignore the `directory` property and instead will use the automatically generated asset path.
119
+
120
+ ##Relative asset paths
121
+
122
+ Some bower components (eg. [Bootstrap](https://github.com/twbs/bootstrap/blob/0016c17f9307bc71fc96d8d4680a9c861f137cae/dist/css/bootstrap.css#L2263)) have relative urls in the CSS files for imports, images, etc. Rails prefers using [helper methods](http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets) for linking to assets within CSS. Relative paths can cause issues when assets are precompiled for production.
123
+
124
+ Before the `rake assets:precompile` task is run, the bower assets will be reinstalled with the relative paths replaced with calls to `asset_path` so that all asset links work in production.
@@ -3,19 +3,17 @@ require 'fileutils'
3
3
 
4
4
  module BowerRails
5
5
  class Dsl
6
+
6
7
  def self.evalute(filename)
7
- instance = new
8
- instance.eval_file(File.join(instance.root_path, filename))
9
- instance
8
+ new.tap { |dsl| dsl.eval_file(File.join(dsl.root_path, filename)) }
10
9
  end
11
10
 
12
11
  attr_reader :dependencies, :root_path
13
12
 
14
13
  def initialize
15
14
  @dependencies = {}
16
- @root_path ||= defined?(Rails) ? Rails.root : Dir.pwd
15
+ @root_path ||= Dir.pwd
17
16
  @assets_path ||= "assets"
18
- @groups ||= [[:vendor, { assets_path: @assets_path }]]
19
17
  end
20
18
 
21
19
  def eval_file(file)
@@ -26,64 +24,72 @@ module BowerRails
26
24
  @dependencies.keys
27
25
  end
28
26
 
29
- def group(*args, &block)
30
- if args[1]
31
- custom_assets_path = args[1][:assets_path]
32
- raise ArgumentError, "Assets should be stored in /assets directory, try :assets_path => 'assets/#{custom_assets_path}' instead" unless custom_assets_path.start_with?('assets', '/assets')
33
- new_group = [args[0], args[1]]
34
- else
35
- new_group = [args[0]]
36
- end
37
-
38
- @groups << new_group
39
-
27
+ def group(name, options = {}, &block)
28
+ options[:assets_path] ||= @assets_path
29
+
30
+ assert_asset_path options[:assets_path]
31
+ assert_group_name name
32
+
33
+ @current_group = add_group name, options
40
34
  yield if block_given?
41
35
  end
42
36
 
43
- def asset(name, *args)
44
- version = args.first || "latest"
45
-
46
- @groups.each do |g|
47
- g_norm = normalize_location_path(g.first, group_assets_path(g))
48
- @dependencies[g_norm] ||= {}
49
- @dependencies[g_norm][name] = version
50
- end
51
- end
37
+ def asset(name, version = "latest")
38
+ group = @current_group ? @current_group : default_group
52
39
 
53
- def to_json(location)
54
- dependencies_to_json @dependencies[normalize_location_path(location)]
40
+ normalized_group_path = normalize_location_path(group.first, group_assets_path(group))
41
+ @dependencies[normalized_group_path] ||= {}
42
+ @dependencies[normalized_group_path][name] = version
55
43
  end
56
44
 
57
45
  def write_bower_json
58
- @dependencies.each do |dir,data|
46
+ @dependencies.each do |dir, data|
59
47
  FileUtils.mkdir_p dir unless File.directory? dir
60
- File.open(File.join(dir,"bower.json"), "w") do |f|
48
+ File.open(File.join(dir, "bower.json"), "w") do |f|
61
49
  f.write(dependencies_to_json(data))
62
50
  end
63
51
  end
64
52
  end
65
53
 
54
+ def generate_dotbowerrc
55
+ contents = JSON.parse(File.read(File.join(@root_path, '.bowerrc'))) rescue {}
56
+ contents["directory"] = "bower_components"
57
+ JSON.pretty_generate(contents)
58
+ end
59
+
66
60
  def write_dotbowerrc
67
- @groups.map do |g|
68
- File.open(File.join(g.first.to_s, group_assets_path(g), ".bowerrc"), "w") do |f|
69
- f.write(JSON.pretty_generate({:directory => "bower_components"}))
61
+ groups.map do |group|
62
+ normalized_group_path = normalize_location_path(group.first, group_assets_path(group))
63
+ File.open(File.join(normalized_group_path, ".bowerrc"), "w") do |f|
64
+ f.write(generate_dotbowerrc)
70
65
  end
71
66
  end
72
67
  end
73
68
 
74
69
  def final_assets_path
75
- @groups.map do |g|
76
- [g.first.to_s, group_assets_path(g)]
70
+ groups.map do |group|
71
+ [group.first.to_s, group_assets_path(group)]
77
72
  end
78
73
  end
79
74
 
80
75
  def group_assets_path group
81
- group_options = Hash === group.last ? group.last : {:assets_path => @assets_path}
82
- group_options[:assets_path]
83
- end
76
+ group.last[:assets_path]
77
+ end
84
78
 
85
79
  private
86
80
 
81
+ def add_group(*group)
82
+ @groups = (groups << group) and return group
83
+ end
84
+
85
+ def groups
86
+ @groups ||= [default_group]
87
+ end
88
+
89
+ def default_group
90
+ [:vendor, { :assets_path => @assets_path }]
91
+ end
92
+
87
93
  def dependencies_to_json(data)
88
94
  JSON.pretty_generate({
89
95
  :name => "dsl-generated dependencies",
@@ -92,10 +98,20 @@ module BowerRails
92
98
  end
93
99
 
94
100
  def assets_path(assets_path)
95
- raise ArgumentError, "Assets should be stored in /assets directory, try assets_path 'assets/#{assets_path}' instead" unless assets_path.start_with?('assets', '/assets')
101
+ assert_asset_path assets_path
96
102
  @assets_path = assets_path
97
103
  end
98
104
 
105
+ def assert_asset_path(path)
106
+ unless path.start_with?('assets', '/assets')
107
+ raise ArgumentError, "Assets should be stored in /assets directory, try assets_path 'assets/#{path}' instead"
108
+ end
109
+ end
110
+
111
+ def assert_group_name name
112
+ raise ArgumentError, "Group name should be :lib or :vendor only" unless [:lib, :vendor].include?(name)
113
+ end
114
+
99
115
  def normalize_location_path(loc, assets_path)
100
116
  File.join(@root_path, loc.to_s, assets_path)
101
117
  end
@@ -30,7 +30,7 @@ namespace :bower do
30
30
  perform false do
31
31
  sh 'bower list'
32
32
  end
33
- end
33
+ end
34
34
 
35
35
  namespace :update do
36
36
  desc "Update existing components and uninstalls extraneous components"
@@ -40,8 +40,18 @@ namespace :bower do
40
40
  end
41
41
  end
42
42
  end
43
+
44
+ desc "Resolve assets paths in bower components"
45
+ task :resolve do
46
+ perform false do
47
+ resolve_asset_paths
48
+ end
49
+ end
43
50
  end
44
51
 
52
+ # Install bower assets before precompile
53
+ Rake::Task['assets:precompile'].enhance ['bower:install', 'bower:resolve']
54
+
45
55
  def perform remove_components = true, &block
46
56
  entries = Dir.entries(get_bower_root_path)
47
57
 
@@ -55,11 +65,7 @@ def perform remove_components = true, &block
55
65
  end
56
66
 
57
67
  def get_bower_root_path
58
- if defined?(Rails)
59
- return Rails.root
60
- else
61
- return Dir.pwd
62
- end
68
+ Dir.pwd
63
69
  end
64
70
 
65
71
  def dsl_perform_command remove_components = true, &block
@@ -87,10 +93,26 @@ def perform_command remove_components = true, &block
87
93
  txt = File.read(File.join(bower_root, "bower.json"))
88
94
  json = JSON.parse(txt)
89
95
 
90
- ["lib", "vendor"].each do |dir|
96
+
97
+ #load and merge root .bowerrc
98
+ dot_bowerrc = JSON.parse(File.read(File.join(bower_root, '.bowerrc'))) rescue {}
99
+ dot_bowerrc["directory"] = "bower_components"
100
+
101
+ if json.except('lib', 'vendor').empty?
102
+ folders = json.keys
103
+ else
104
+ raise "Assuming a standard bower package but cannot find the required 'name' key" unless !!json['name']
105
+ folders = ['vendor']
106
+ end
107
+
108
+ folders.each do |dir|
109
+ puts "\nInstalling dependencies into #{dir}"
91
110
 
92
111
  data = json[dir]
93
112
 
113
+ # assume using standard bower.json if folder name is not found
114
+ data = json if data.nil?
115
+
94
116
  #check folder existence and create?
95
117
  dir = File.join(bower_root, dir, "assets")
96
118
  FileUtils.mkdir_p dir unless File.directory? dir
@@ -107,7 +129,7 @@ def perform_command remove_components = true, &block
107
129
 
108
130
  #create .bowerrc
109
131
  File.open(".bowerrc", "w") do |f|
110
- f.write(JSON.pretty_generate({:directory => "bower_components"}))
132
+ f.write(JSON.pretty_generate(dot_bowerrc))
111
133
  end
112
134
 
113
135
  #run command
@@ -121,3 +143,31 @@ def perform_command remove_components = true, &block
121
143
  end if data && !data["dependencies"].empty?
122
144
  end
123
145
  end
146
+
147
+ def resolve_asset_paths
148
+ # Resolve relative paths in CSS
149
+ Dir['bower_components/**/*.css'].each do |filename|
150
+ contents = File.read(filename)
151
+ # http://www.w3.org/TR/CSS2/syndata.html#uri
152
+ url_regex = /url\(\s*['"]?(?![a-z]+:)([^'"\)]*)['"]?\s*\)/
153
+
154
+ # Resolve paths in CSS file if it contains a url
155
+ if contents =~ url_regex
156
+ directory_path = Pathname.new(File.dirname(filename))
157
+ .relative_path_from(Pathname.new('bower_components'))
158
+
159
+ # Replace relative paths in URLs with Rails asset_path helper
160
+ new_contents = contents.gsub(url_regex) do |match|
161
+ relative_path = $1
162
+ image_path = directory_path.join(relative_path).cleanpath
163
+ puts "#{match} => #{image_path}"
164
+
165
+ "url(<%= asset_path '#{image_path}' %>)"
166
+ end
167
+
168
+ # Replace CSS with ERB CSS file with resolved asset paths
169
+ FileUtils.rm(filename)
170
+ File.write(filename + '.erb', new_contents)
171
+ end
172
+ end
173
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bower-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Harrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-23 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -44,12 +44,12 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
+ - lib/tasks/bower.rake
47
48
  - lib/bower-rails/railtie.rb
48
49
  - lib/bower-rails/dsl.rb
49
50
  - lib/bower-rails.rb
50
51
  - lib/generators/bower_rails/initialize/templates/bower.json
51
52
  - lib/generators/bower_rails/initialize/initialize_generator.rb
52
- - lib/tasks/bower.rake
53
53
  - MIT-LICENSE
54
54
  - README.md
55
55
  homepage: https://github.com/rharriso/bower-rails
@@ -72,9 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.0.3
75
+ rubygems_version: 2.1.10
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: Bower for Rails
79
79
  test_files: []
80
- has_rdoc: