lib/jim/bundler.rb in jim-0.2.1 vs lib/jim/bundler.rb in jim-0.2.2

- old
+ new

@@ -1,7 +1,7 @@ module Jim - # Bundler takes parses a Jimfile that specifies requirements as names and + # Bundler takes parses a Jimfile that specifies requirements as names and # versions and then can bundle, compress, or copy those files into specific dirs # or files. # # A Jimfile has a really simple format: # @@ -13,16 +13,16 @@ # // requirements are resolved and bundled in order of specification # jquery 1.4.2 # jquery.color # sammy 0.5.0 # - # + # class Bundler class MissingFile < Jim::Error; end - attr_accessor :jimfile, :index, :requirements, :paths, :options - + attr_accessor :jimfile, :index, :requirements, :paths, :options + # create a new bundler instance passing in the Jimfile as a `Pathname` or a # string. `index` is a Jim::Index def initialize(jimfile, index = nil, extra_options = {}) self.jimfile = jimfile.is_a?(Pathname) ? jimfile.read : jimfile self.index = index || Jim::Index.new @@ -31,21 +31,21 @@ parse_jimfile self.options = options.merge(extra_options) self.paths = [] if options[:vendor_dir] logger.debug "adding vendor dir to index #{options[:vendor_dir]}" - self.index.add(options[:vendor_dir]) + self.index.add(options[:vendor_dir]) end end # resove the requirements specified into Jimfile or raise a MissingFile error def resolve! self.requirements.each do |search| name, version = search.strip.split(/\s+/) path = self.index.find(name, version) if !path - raise(MissingFile, + raise(MissingFile, "Could not find #{name} #{version} in any of these paths #{index.directories.join(':')}") end self.paths << [path, name, version] end paths @@ -53,28 +53,28 @@ # concatenate all the requirements into a single file and write to `to` or to the # path specified in the :bundled_path option def bundle!(to = nil) resolve! if paths.empty? - to = options[:bundled_path] if to.nil? && options[:bundled_path] - io = io_for_path(to) - logger.info "Bundling to #{to}" if to - paths.each do |path, name, version| - io << path.read << "\n" + to = options[:bundled_path] if to.nil? && to != false && options[:bundled_path] + io_for_path(to) do |io| + logger.info "Bundling to #{to}" if to + paths.each do |path, name, version| + io << path.read << "\n" + end end - io end - # concatenate all the requirements into a single file then run through a JS + # concatenate all the requirements into a single file then run through a JS # then write to `to` or to the path specified in the :bundled_path option. # You can also use the YUI compressor by setting the option :compressor to 'yui' def compress!(to = nil) to = options[:compressed_path] if to.nil? && options[:compressed_path] - io = io_for_path(to) - logger.info "Compressing to #{to}" - io << compress_js(bundle!(false)) - io + io_for_path(to) do |io| + logger.info "Compressing to #{to}" + io << compress_js(bundle!(false)) + end end # copy each of the requirements into the dir specified with `dir` or the path # specified with the :vendor_dir option def vendor!(dir = nil, force = false) @@ -90,18 +90,22 @@ end # Run the uncompressed test through a JS compressor (closure-compiler) by # default. Setting options[:compressor] == 'yui' will force the YUI JS Compressor def compress_js(uncompressed) + # if uncompressed.is_a?(File) && uncompressed.closed? + # puts "uncompressed is a file" + # uncompressed = File.read(uncompressed.path) + # end if options[:compressor] == 'yui' begin require "yui/compressor" rescue LoadError raise "You must install the yui compressor gem to use the compressor\ngem install yui-compressor" end compressor = ::YUI::JavaScriptCompressor.new - else + else begin require 'closure-compiler' rescue LoadError raise "You must install the closure compiler gem to use the compressor\ngem install closure-compiler" end @@ -109,22 +113,26 @@ end compressor.compress(uncompressed) end private - def io_for_path(to) + def io_for_path(to, &block) case to when IO + yield to + to.close to when Pathname to.dirname.mkpath - to.open('w') + io = to.open('w') {|f| yield f } when String to = Pathname.new(to) - io_for_path(to) + io_for_path(to, &block) else - "" + io = "" + yield io + io end end def parse_jimfile jimfile.each_line do |line| @@ -133,12 +141,12 @@ elsif line !~ /^\// && line.strip != '' self.requirements << line end end end - + def logger Jim.logger end end -end \ No newline at end of file +end