bower-rails 0.7.1 → 0.7.2

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: d18d9ca48d1438cf86e7d8effff84410300bb0f8
4
- data.tar.gz: 77bdfb89794820ff714e879006daa84ec7cb8002
3
+ metadata.gz: 73ea0764f11696a1dde32c69b4bfed873bf3c93b
4
+ data.tar.gz: 314587a37f67499acd1638c3698997db6fe1d9d0
5
5
  SHA512:
6
- metadata.gz: 7664df0469ff3e0d3e5b97327f0a6e99b3c7c790dbbe244262b625394c4e8ebc52660db962dde90888e227ef2edea3b52e3c70b42e71efdd2e13430aa6fb356c
7
- data.tar.gz: 7a87fe21e8d47e61d5a08fd5ebb53e73155ffbc2095d32494281678907939b16b75ab9ea4db69db645efa4cdbf7e040c6a8cb08a5ab52ea3bc2028298cb8acc6
6
+ metadata.gz: 23ab1f087a70761a17840ca4f98ecf351d5de2d78aba5d57d58dde0f89da53a8853041dc97a757c4a54c6017499c4d2858da48774becb32993092ad1444f321f
7
+ data.tar.gz: 690d0aeebc8295c770bed50965679f7a6d055760b5f4ac43d2584575cca81b455cf2d092862d2f9f9a42cfc214a58caac1c90e28a1ae5e54d18750bebf38ff32
data/README.md CHANGED
@@ -22,7 +22,7 @@ Check out [changelog][] for the latest changes and releases.
22
22
  in Gemfile
23
23
 
24
24
  ``` Ruby
25
- gem "bower-rails", "~> 0.7.1"
25
+ gem "bower-rails", "~> 0.7.2"
26
26
  ```
27
27
 
28
28
  ##JSON configuration
@@ -75,7 +75,14 @@ By default assets are put to `./vendor/assets/bower_components` directory:
75
75
 
76
76
  # Puts to ./vendor/assets/bower_components
77
77
  asset "backbone"
78
- asset "moment"
78
+ asset "moment", "2.0.0" # get exactly version 2.0.0
79
+ asset "secret_styles", "git@github.com:initech/secret_styles" # get from a git repo
80
+
81
+ # get from a git repo using the tag 1.0.0
82
+ asset "secret_logic", "1.0.0", git: "git@github.com:initech/secret_logic"
83
+
84
+ # get from a github repo
85
+ asset "secret_logic", "1.0.0", github: "initech/secret_logic"
79
86
  ```
80
87
 
81
88
  But the default value can be overridden by `assets_path` method:
@@ -95,7 +102,7 @@ assets_path "assets/javascript"
95
102
 
96
103
  # Puts files under ./vendor/assets/js/bower_components
97
104
  group :vendor, :assets_path => "assets/js" do
98
- asset "jquery" # Assummes it's latests
105
+ asset "jquery" # Defaults to 'latest'
99
106
  asset "backbone", "1.1.1"
100
107
  end
101
108
 
@@ -108,17 +115,48 @@ end
108
115
  NOTE: Available groups are `:lib` and `:vendor`. Others are not allowed according to the Rails convention.
109
116
  NOTE: All the assets should be stored in `/assets` subdirectory so putting it under `./vendor/js` directory is unavailable
110
117
 
118
+ ##Configuration
119
+
120
+ Change options in your `config/initializers/bower_rails.rb`:
121
+
122
+ ``` ruby
123
+ BowerRails.configure do |bower_rails|
124
+ # By default options are false
125
+ bower_rails.resolve_before_precompile = true # invokes rake bower:resolve before precompilation
126
+ bower_rails.clean_before_precompile = true # invokes rake bower:clean before precompilation
127
+ end
128
+ ```
129
+
130
+ If you are using Rails version < 4.0.0 then you are to require `bower_rails.rb` initializer manually in `application.rb`:
131
+
132
+ ```ruby
133
+ module YourAppName
134
+ class Application < Rails::Application
135
+ require "#{Rails.root}/config/initializers/bower_rails.rb"
136
+ ...
137
+ end
138
+ end
139
+ ```
140
+
141
+ By default this line is added while running the generator.
142
+
111
143
  ##Rake tasks
112
144
 
113
145
  Once you are done with `bower.json` or `Bowerfile` you can run
114
146
 
115
147
  * `rake bower:install` to install js components
116
- * `rake bower:install:force` to install with force option
117
148
  * `rake bower:update` to update js components
118
149
  * `rake bower:update:prune` to update components and uninstall extraneous packages
119
150
  * `rake bower:list` to list all packages
151
+ * `rake bower:clean` to remove all files not listed as [main files](#bower-main-files) (if specified)
120
152
  * `rake bower:resolve` to resolve [relative asset paths](#relative-asset-paths) in components
121
153
 
154
+ If you'd like to pass any bower CLI options to a rake task, like `-f`, `-j`, you can simply do:
155
+
156
+ ```bash
157
+ rake bower:install['-f']
158
+ ```
159
+
122
160
  ##Bower Configuration
123
161
 
124
162
  If you provide a `.bowerrc` in the rails project root, bower-rails will use it for bower configuration.
@@ -133,12 +171,8 @@ will ignore the `directory` property and instead will use the automatically gene
133
171
 
134
172
  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.
135
173
 
136
- If you would like the bower assets to be reinstalled with the relative paths on every deploy, provide an option for `bower-rails` so it do it automatically before the `rake assets:precompile` task is run:
137
-
138
- ``` ruby
139
- BowerRails.configure do |bower_rails|
140
- # By default this option is false
141
- bower_rails.resolve_before_precompile = true
142
- end
143
- ```
144
174
  Remember that you should have [bower installed](#bower-installation) either locally in your project or on a remote server.
175
+
176
+ ##Bower Main Files
177
+
178
+ 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.
data/lib/bower-rails.rb CHANGED
@@ -2,16 +2,39 @@ module BowerRails
2
2
  require 'bower-rails/railtie' if defined?(Rails)
3
3
  require 'bower-rails/dsl'
4
4
 
5
+ extend self
6
+
5
7
  class << self
8
+ # An array of tasks to enhance `rake assets:precompile`
9
+ attr_reader :tasks
10
+
6
11
  # If set to true then rake bower:install && rake bower:resolve tasks
7
12
  # are invoked before assets precompilation
8
13
  attr_accessor :resolve_before_precompile
9
14
 
15
+ # If set to true then rake bower:install && rake bower:clean && rake bower:resolve tasks
16
+ # are invoked before assets precompilation
17
+ attr_accessor :clean_before_precompile
18
+
10
19
  def configure &block
11
20
  yield self if block_given?
21
+ collect_tasks
12
22
  end
23
+
24
+ private
25
+
26
+ def collect_tasks
27
+ @tasks << ['bower:install', 'bower:resolve'] if @resolve_before_precompile
28
+ @tasks << ['bower:install', 'bower:clean'] if @clean_before_precompile
29
+ @tasks.flatten!
30
+ @tasks.uniq!
31
+ end
13
32
  end
14
33
 
34
+ # By default tasks are empty
35
+ @tasks = []
36
+
15
37
  # Set default values for options
16
38
  @resolve_before_precompile = false
39
+ @clean_before_precompile = false
17
40
  end
@@ -26,16 +26,29 @@ module BowerRails
26
26
 
27
27
  def group(name, options = {}, &block)
28
28
  options[:assets_path] ||= @assets_path
29
-
29
+
30
30
  assert_asset_path options[:assets_path]
31
31
  assert_group_name name
32
-
32
+
33
33
  @current_group = add_group name, options
34
34
  yield if block_given?
35
35
  end
36
36
 
37
- def asset(name, version = "latest")
38
- group = @current_group ? @current_group : default_group
37
+ def asset(name, *args)
38
+ group = @current_group || default_group
39
+
40
+ options = Hash === args.last ? args.pop.dup : {}
41
+ version = args.last || "latest"
42
+
43
+ options[:git] = "git://github.com/#{options[:github]}" if options[:github]
44
+
45
+ if options[:git]
46
+ version = if version == 'latest'
47
+ options[:git]
48
+ else
49
+ options[:git] + "#" + version
50
+ end
51
+ end
39
52
 
40
53
  normalized_group_path = normalize_location_path(group.first, group_assets_path(group))
41
54
  @dependencies[normalized_group_path] ||= {}
@@ -70,7 +83,7 @@ module BowerRails
70
83
  groups.map do |group|
71
84
  [group.first.to_s, group_assets_path(group)]
72
85
  end
73
- end
86
+ end
74
87
 
75
88
  def group_assets_path group
76
89
  group.last[:assets_path]
@@ -24,6 +24,7 @@ module BowerRails
24
24
  end
25
25
 
26
26
  rake_tasks do
27
+ require "tasks/helpers/before_hook.rb"
27
28
  load "tasks/bower.rake"
28
29
  end
29
30
  end
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module BowerRails
3
+ VERSION = "0.7.2"
4
+ end
@@ -18,6 +18,12 @@ module BowerRails
18
18
  def copy_initializer_file
19
19
  copy_file 'bower_rails.rb', 'config/initializers/bower_rails.rb'
20
20
  end
21
+
22
+ def require_initializer_in_application_rb
23
+ if Rails.version < "4.0.0"
24
+ environment { "require \"#{Rails.root}/config/initializers/bower_rails.rb\"" }
25
+ end
26
+ end
21
27
  end
22
28
  end
23
29
  end
@@ -1,6 +1,7 @@
1
1
  BowerRails.configure do |bower_rails|
2
- # Uncomment this line if you want `rake bower:resolve` task to be run
3
- # automatically before the `rake assets:precompile` task.
4
-
2
+ # Invoke `rake bower:resolve` automatically before the `rake assets:precompile` task.
5
3
  # bower_rails.resolve_before_precompile = true
4
+
5
+ # Invoke `rake bower:clean` automatically before the `rake assets:precompile` task.
6
+ # bower_rails.resolve_before_precompile = true
6
7
  end
data/lib/tasks/bower.rake CHANGED
@@ -1,27 +1,23 @@
1
1
  require 'json'
2
2
  require 'pp'
3
+ require 'find'
4
+
5
+ include BeforeHook
3
6
 
4
7
  namespace :bower do
5
8
  desc "Install components from bower"
6
- task :install do
9
+ task :install, :options do |_, args|
10
+ args.with_defaults(:options => '')
7
11
  perform do |bower|
8
- sh "#{bower} install"
9
- end
10
- end
11
-
12
- namespace :install do
13
- desc "Install components with -F option"
14
- task :force do
15
- perform do |bower|
16
- sh "#{bower} install -F"
17
- end
12
+ sh "#{bower} install #{args[:options]}"
18
13
  end
19
14
  end
20
15
 
21
16
  desc "Update bower components"
22
- task :update do
17
+ task :update, :options do |_, args|
18
+ args.with_defaults(:options => '')
23
19
  perform do |bower|
24
- sh "#{bower} update"
20
+ sh "#{bower} update #{args[:options]}"
25
21
  end
26
22
  end
27
23
 
@@ -34,9 +30,10 @@ namespace :bower do
34
30
 
35
31
  namespace :update do
36
32
  desc "Update existing components and uninstalls extraneous components"
37
- task :prune do
33
+ task :prune, :options do |_, args|
34
+ args.with_defaults(:options => '')
38
35
  perform do |bower|
39
- sh "#{bower} update && #{bower} prune"
36
+ sh "#{bower} update #{args[:options]} && #{bower} prune #{args[:options]}"
40
37
  end
41
38
  end
42
39
  end
@@ -47,10 +44,20 @@ namespace :bower do
47
44
  resolve_asset_paths
48
45
  end
49
46
  end
47
+
48
+ desc "Attempt to keep only files listed in 'main' of each component's bower.json"
49
+ task :clean do
50
+ perform false do
51
+ remove_extra_files
52
+ end
53
+ end
50
54
  end
51
55
 
52
- # Install bower assets before precompile if an corresponding option provided
53
- Rake::Task['assets:precompile'].enhance ['bower:install', 'bower:resolve'] if BowerRails.resolve_before_precompile
56
+ before 'assets:precompile' do
57
+ BowerRails.tasks.map do |task|
58
+ Rake::Task[task].invoke
59
+ end
60
+ end
54
61
 
55
62
  def perform remove_components = true, &block
56
63
  entries = Dir.entries(get_bower_root_path)
@@ -62,7 +69,7 @@ def perform remove_components = true, &block
62
69
  $stderr.puts <<EOS
63
70
  Bower not found! You can install Bower using Node and npm:
64
71
  $ npm install bower -g
65
- For more info see http://twitter.github.com/bower/
72
+ For more info see http://bower.io/
66
73
  EOS
67
74
  return
68
75
  end
@@ -122,8 +129,6 @@ def perform_command remove_components = true, &block
122
129
  end
123
130
 
124
131
  folders.each do |dir|
125
- puts "\nInstalling dependencies into #{dir}"
126
-
127
132
  data = json[dir]
128
133
 
129
134
  # assume using standard bower.json if folder name is not found
@@ -188,6 +193,41 @@ def resolve_asset_paths
188
193
  end
189
194
  end
190
195
 
196
+ def remove_extra_files
197
+ puts "\nAttempting to remove all but main files as specified by bower\n"
198
+
199
+ Dir['bower_components/*'].each do |component_dir|
200
+ if File.exists?(File.join(component_dir, 'bower.json'))
201
+ bower_file = File.read(File.join(component_dir, 'bower.json'))
202
+ elsif File.exists?(File.join(component_dir, '.bower.json'))
203
+ bower_file = File.read(File.join(component_dir, '.bower.json'))
204
+ else
205
+ next
206
+ end
207
+
208
+ # parse bower.json
209
+ bower_json = JSON.parse(bower_file)
210
+ main_files = bower_json['main']
211
+ next unless main_files
212
+
213
+ # handle singular or multiple files
214
+ main_files = [main_files] unless main_files.is_a?(Array)
215
+
216
+ # Remove "./" relative path from main file strings
217
+ main_files.map! { |file| File.join(component_dir, file.gsub(/^\.\//, '')) }
218
+
219
+ # delete all files that are not in main
220
+ Find.find(component_dir).reverse_each do |file_or_dir|
221
+ next if main_files.include?(file_or_dir)
222
+ if File.directory?(file_or_dir)
223
+ Dir.rmdir(file_or_dir) if (Dir.entries(file_or_dir) - %w[ . .. ]).empty?
224
+ else
225
+ FileUtils.rm(file_or_dir)
226
+ end
227
+ end
228
+ end
229
+ end
230
+
191
231
  # http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
192
232
  def find_command(cmd, paths = [])
193
233
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
@@ -195,7 +235,7 @@ def find_command(cmd, paths = [])
195
235
  paths.each do |path|
196
236
  exts.each do |ext|
197
237
  exe = File.join(path, "#{cmd}#{ext}")
198
- return exe if File.executable? exe
238
+ return exe if (File.executable?(exe) && File.file?(exe))
199
239
  end
200
240
  end
201
241
  nil
@@ -0,0 +1,14 @@
1
+ module BeforeHook
2
+ # The `before` hook for rake tasks.
3
+ # The code was taken from https://github.com/guillermo/rake-hooks/blob/master/lib/rake/hooks.rb#L2
4
+ def before(*task_names, &new_task)
5
+ task_names.each do |task_name|
6
+ old_task = Rake.application.instance_variable_get('@tasks').delete(task_name.to_s)
7
+ desc old_task.full_comment
8
+ task task_name => old_task.prerequisites do
9
+ new_task.call
10
+ old_task.invoke
11
+ end
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bower-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Harrison
@@ -63,11 +63,13 @@ files:
63
63
  - lib/bower-rails.rb
64
64
  - lib/bower-rails/dsl.rb
65
65
  - lib/bower-rails/railtie.rb
66
+ - lib/bower-rails/version.rb
66
67
  - lib/generators/bower_rails/initialize/initialize_generator.rb
67
68
  - lib/generators/bower_rails/initialize/templates/Bowerfile
68
69
  - lib/generators/bower_rails/initialize/templates/bower.json
69
70
  - lib/generators/bower_rails/initialize/templates/bower_rails.rb
70
71
  - lib/tasks/bower.rake
72
+ - lib/tasks/helpers/before_hook.rb
71
73
  homepage: https://github.com/rharriso/bower-rails
72
74
  licenses:
73
75
  - MIT
@@ -88,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
90
  version: '0'
89
91
  requirements: []
90
92
  rubyforge_project:
91
- rubygems_version: 2.2.1
93
+ rubygems_version: 2.2.2
92
94
  signing_key:
93
95
  specification_version: 4
94
96
  summary: Bower for Rails