Skip to content

Commit

Permalink
Option to overwrite the hard coded bower_components directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Posen committed Dec 28, 2015
1 parent c7fe07b commit f1d6a31
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Edge version

* Load Bowerfile from all gem dependencies before load by @gabealmer [#162][]
* Make bower components directory configurable [#183][]

[#162]: https://github.com/rharriso/bower-rails/pull/162
[#183]: https://github.com/rharriso/bower-rails/pull/183

## v0.10.0

Expand Down
4 changes: 4 additions & 0 deletions lib/bower-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class << self
# instead of rake bower:install before assets precompilation
attr_accessor :force_install

# Where to store the bower components
attr_accessor :bower_components_directory

def configure &block
yield self if block_given?
collect_tasks
Expand Down Expand Up @@ -69,4 +72,5 @@ def collect_tasks
@use_bower_install_deployment = false
@use_gem_deps_for_bowerfile = false
@force_install = false
@bower_components_directory = "bower_components"
end
16 changes: 10 additions & 6 deletions lib/bower-rails/performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def perform_command(remove_components = true, &block)

# Load and merge root .bowerrc
dot_bowerrc = JSON.parse(File.read(File.join(root_path, '.bowerrc'))) rescue {}
dot_bowerrc["directory"] = "bower_components"
dot_bowerrc["directory"] = components_directory

if json.except('lib', 'vendor').empty?
folders = json.keys
Expand All @@ -91,7 +91,7 @@ def perform_command(remove_components = true, &block)
Dir.chdir(dir) do

# Remove old components
FileUtils.rm_rf("bower_components/*") if remove_components
FileUtils.rm_rf("#{components_directory}/*") if remove_components

# Create bower.json
File.open("bower.json", "w") do |f|
Expand All @@ -115,17 +115,17 @@ def perform_command(remove_components = true, &block)
end
end

def resolve_asset_paths
def resolve_asset_paths(root_directory = components_directory)
# Resolve relative paths in CSS
Dir['bower_components/**/*.css'].each do |filename|
Dir["#{components_directory}/**/*.css"].each do |filename|

This comment has been minimized.

Copy link
@dmfrancisco

dmfrancisco Apr 11, 2016

@jimpo I think it should be root_directory instead of components_directory here, or am I missing something?

This comment has been minimized.

Copy link
@jimpo

jimpo Apr 11, 2016

Contributor

Yeah, I think that's more correct. I guess it hasn't been a problem for us since our root_directory is a subdirectory of components_directory.

contents = File.read(filename) if FileTest.file?(filename)
# http://www.w3.org/TR/CSS2/syndata.html#uri
url_regex = /url\((?!\#)\s*['"]?(?![a-z]+:)([^'"\)]*)['"]?\s*\)/

# Resolve paths in CSS file if it contains a url
if contents =~ url_regex
directory_path = Pathname.new(File.dirname(filename))
.relative_path_from(Pathname.new('bower_components'))
.relative_path_from(Pathname.new(root_directory))

# Replace relative paths in URLs with Rails asset_path helper
new_contents = contents.gsub(url_regex) do |match|
Expand All @@ -146,7 +146,7 @@ def resolve_asset_paths
def remove_extra_files
puts "\nAttempting to remove all but main files as specified by bower\n"

Dir['bower_components/*'].each do |component_dir|
Dir["#{components_directory}/*"].each do |component_dir|
component_name = component_dir.split('/').last
next if clean_should_skip_component? component_name

Expand Down Expand Up @@ -204,5 +204,9 @@ def clean_should_skip_component?(name)
BowerRails.exclude_from_clean.respond_to?(:include?) &&
BowerRails.exclude_from_clean.include?(name)
end

def components_directory
BowerRails.bower_components_directory
end
end
end
6 changes: 4 additions & 2 deletions lib/bower-rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class Railtie < Rails::Railtie
config.before_initialize do |app|
@dsl = BowerRails::Dsl.evalute(BowerRails.root_path, @@bowerfile)
@dsl.final_assets_path.map do |assets_root, assets_path|
app.config.assets.paths << Rails.root.join(assets_root, assets_path, "bower_components")
app.config.assets.paths <<
Rails.root.join(assets_root, assets_path, BowerRails.bower_components_directory)
end
end
else
config.before_initialize do |app|
["lib", "vendor"].each do |dir|
app.config.assets.paths << Rails.root.join(dir, 'assets', 'bower_components')
app.config.assets.paths <<
Rails.root.join(dir, 'assets', BowerRails.bower_components_directory)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
#
# Invokes rake bower:install and rake bower:install:deployment with -F (force) flag. Defaults to false
# bower_rails.force_install = true

# Change the default directory name
# bower_rails.bower_components_directory = 'bower_components'
end
5 changes: 3 additions & 2 deletions lib/tasks/bower.rake
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ namespace :bower do
end

desc "Resolve assets paths in bower components"
task :resolve do
task :resolve, :relative_directory do |_, args|
BowerRails::Performer.perform false do
resolve_asset_paths
resolve_asset_paths(
args[:relative_directory] || BowerRails.bower_components_directory)
end
end

Expand Down

0 comments on commit f1d6a31

Please sign in to comment.