Class: StitchPlus
- Inherits:
-
Object
- Object
- StitchPlus
- Defined in:
- lib/stitch-plus.rb
Instance Attribute Summary collapse
-
#deleted ⇒ Object
Returns the value of attribute deleted.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#all_files(options = nil) ⇒ Object
Get a list of all files to be stitched.
-
#compile ⇒ Object
Compile javascripts, uglifying if necessary.
-
#file_fingerprint(options = nil) ⇒ Object
Get and MD5 hash of files including order of dependencies.
-
#initialize(options = {}) ⇒ StitchPlus
constructor
A new instance of StitchPlus.
-
#last_write ⇒ Object
Return the path for the last file written This exists because it’s more performant than calling output_file unnecessarily.
-
#output_file(options = nil) ⇒ Object
return the compiled js path including fingerprint if enabled.
-
#set_options(options = {}, temporary = false) ⇒ Object
Set or temporarily set options.
- #temp_options(options) ⇒ Object
-
#write(options = nil) ⇒ Object
Write compiled javascripts to disk.
Constructor Details
#initialize(options = {}) ⇒ StitchPlus
Returns a new instance of StitchPlus.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/stitch-plus.rb', line 9 def initialize(={}) @options = { :dependencies => nil, :paths => nil, :output => 'all.js', :fingerprint => false, :cleanup => true, :uglify => false, :uglify_options => {}, :config => nil } @deleted = [] () begin require 'coffee-script' @has_coffee = true rescue LoadError end end |
Instance Attribute Details
#deleted ⇒ Object
Returns the value of attribute deleted.
7 8 9 |
# File 'lib/stitch-plus.rb', line 7 def deleted @deleted end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/stitch-plus.rb', line 7 def @options end |
Instance Method Details
#all_files(options = nil) ⇒ Object
Get a list of all files to be stitched
138 139 140 141 142 143 144 145 |
# File 'lib/stitch-plus.rb', line 138 def all_files(=nil) () if files = [] files << dependencies if @options[:dependencies] files << Dir.glob(File.join(@options[:paths], '**/*')) if @options[:paths] if files.flatten.uniq end |
#compile ⇒ Object
Compile javascripts, uglifying if necessary
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/stitch-plus.rb', line 58 def compile if all_files.join().match(/\.coffee/) and !@has_coffee error "Cannot compile coffeescript".red error "Add ".white + "gem 'coffee-script'".yellow + " to your Gemfile." end if @options[:uglify] and !@uglifier error "Cannot uglify javascript".red error "Add ".white + "gem 'uglifier'".yellow + " to your Gemfile." end begin js = Stitch::Package.new(:dependencies => dependencies, :paths => @options[:paths]).compile js = @uglifier.compile(js) if @uglifier js rescue StandardError => e error "Stitch failed to compile".red error e false end end |
#file_fingerprint(options = nil) ⇒ Object
Get and MD5 hash of files including order of dependencies
148 149 150 151 152 153 |
# File 'lib/stitch-plus.rb', line 148 def file_fingerprint(=nil) () if fingerprint = Digest::MD5.hexdigest(all_files.map! { |path| "#{File.mtime(path).to_i}" }.join + @options.to_s) if fingerprint end |
#last_write ⇒ Object
Return the path for the last file written This exists because it’s more performant than calling output_file unnecessarily
133 134 135 |
# File 'lib/stitch-plus.rb', line 133 def last_write @file end |
#output_file(options = nil) ⇒ Object
return the compiled js path including fingerprint if enabled
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/stitch-plus.rb', line 115 def output_file(=nil) () if file = @options[:output] if @options[:fingerprint] @fingerprint ||= file_fingerprint basename = File.basename(file).split(/(\..+)$/).join("-#{@fingerprint}") dir = File.dirname(file) file = File.join(dir, basename) end if file end |
#set_options(options = {}, temporary = false) ⇒ Object
Set or temporarily set options
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/stitch-plus.rb', line 34 def (={}, temporary=false) @old_options = @options if temporary # If options is a file path, read options from yaml = { :config => } if .class == String = () @options = @options.merge symbolize_keys() if @options[:uglify] begin require 'uglifier' @uglifier = Uglifier.new(@options[:uglify_options]) rescue LoadError end end end |
#temp_options(options) ⇒ Object
53 54 55 |
# File 'lib/stitch-plus.rb', line 53 def () (, true) end |
#write(options = nil) ⇒ Object
Write compiled javascripts to disk
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/stitch-plus.rb', line 82 def write(=nil) () if @fingerprint = file_fingerprint @file = output_file js = "/* Build fingerprint: #{@fingerprint} */\n" + compile if has_fingerprint(@file, @fingerprint) info "Stitch " + "identical ".green + @file.sub("#{Dir.pwd}/", '') if true else begin write_msg = (File.exists?(@file) ? "overwrite " : "created ").yellow + @file.sub("#{Dir.pwd}/", '') cleanup(@file) if @options[:cleanup] FileUtils.mkdir_p(File.dirname(@file)) File.open(@file, 'w') { |f| f.write js } info "Stitch " + write_msg true rescue StandardError => e error "Stitch failed to write #{@file.sub("#{Dir.pwd}/", '')}".red error e if false end end end |