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 +4 -4
- data/README.md +27 -7
- data/lib/bower-rails/dsl.rb +54 -38
- data/lib/tasks/bower.rake +58 -8
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98d9ed52d0507bd2dcc3e78fefa66982aae28f3e
|
4
|
+
data.tar.gz: c483f89a0b7509543f31ba6d4922c86dcc8d560a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
23
|
+
gem "bower-rails", "~> 0.5.0"
|
18
24
|
```
|
19
25
|
|
20
|
-
|
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
|
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
|
-
|
33
|
+
rails g bower_rails:initialize
|
26
34
|
```
|
27
35
|
|
28
|
-
|
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.
|
data/lib/bower-rails/dsl.rb
CHANGED
@@ -3,19 +3,17 @@ require 'fileutils'
|
|
3
3
|
|
4
4
|
module BowerRails
|
5
5
|
class Dsl
|
6
|
+
|
6
7
|
def self.evalute(filename)
|
7
|
-
|
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 ||=
|
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(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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,
|
44
|
-
|
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
|
-
|
54
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
76
|
-
[
|
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
|
-
|
82
|
-
|
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
|
-
|
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
|
data/lib/tasks/bower.rake
CHANGED
@@ -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
|
-
|
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
|
-
|
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(
|
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.
|
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-
|
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.
|
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:
|