lib/bower-rails/performer.rb in bower-rails-0.10.0 vs lib/bower-rails/performer.rb in bower-rails-0.11.0

- old
+ new

@@ -13,20 +13,18 @@ def root_path BowerRails.root_path end def perform(remove_components = true, &block) - entries = Dir.entries(root_path) - npm_path = File.join(root_path, 'node_modules', '.bin') bower = find_command('bower', [npm_path]) if bower.nil? $stderr.puts ["Bower not found! You can install Bower using Node and npm:", "$ npm install bower -g", "For more info see http://bower.io/"].join("\n") - return + exit 127 end if entries.include?('Bowerfile') dsl_perform_command remove_components do instance_exec(bower, &block) if block_given? @@ -67,13 +65,13 @@ json = JSON.parse(txt) # 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? + if json.reject{ |key| ['lib', 'vendor'].include? key }.empty? folders = json.keys else raise "Assuming a standard bower package but cannot find the required 'name' key" unless !!json['name'] folders = ['vendor'] end @@ -89,11 +87,11 @@ FileUtils.mkdir_p dir unless File.directory? dir # Go in to dir to act 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| f.write(data.to_json) end @@ -113,29 +111,30 @@ FileUtils.rm(".bowerrc") end if data && !data["dependencies"].empty? 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| contents = File.read(filename) if FileTest.file?(filename) # http://www.w3.org/TR/CSS2/syndata.html#uri - url_regex = /url\((?!\#)\s*['"]?(?![a-z]+:)([^'"\)]*)['"]?\s*\)/ + 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| - relative_path = $1 + relative_path = $2 + params = $3 image_path = directory_path.join(relative_path).cleanpath - puts "#{match} => #{image_path}" + puts "#{match} => #{image_path} #{params}" - "url(<%= asset_path '#{image_path}' %>)" + "url(<%= asset_path '#{image_path}' %>#{params})" end # Replace CSS with ERB CSS file with resolved asset paths FileUtils.rm(filename) File.write(filename + '.erb', new_contents) @@ -144,22 +143,24 @@ end 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 + if File.exists?(File.join(component_dir, 'bower.json')) bower_file = File.read(File.join(component_dir, 'bower.json')) elsif File.exists?(File.join(component_dir, '.bower.json')) bower_file = File.read(File.join(component_dir, '.bower.json')) else next end # Parse bower.json bower_json = JSON.parse(bower_file) - component_name = component_dir.split('/').last main_files = Array(bower_json['main']) + main_files_for_component(component_name) next if main_files.empty? # Remove "./" relative path from main file strings main_files.map! { |file| File.join(component_dir, file.gsub(/^\.\//, '')) } @@ -184,18 +185,34 @@ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] paths += ENV['PATH'].split(File::PATH_SEPARATOR) paths.each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") - return exe if (File.executable?(exe) && File.file?(exe)) + if (File.executable?(exe) && File.file?(exe)) + return Shellwords.escape exe + end end end nil end private def main_files_for_component(name) + return [] unless entries.include?('Bowerfile') Array(dsl.main_files[name]) + end + + def entries + @entries ||= Dir.entries(root_path) + end + + 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