Class: StitchPlus

Inherits:
Object
  • Object
show all
Defined in:
lib/stitch-plus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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={})

  @options = {
    :dependencies   => nil,
    :paths          => nil,
    :output         => 'all.js',
    :fingerprint    => false,
    :cleanup        => true,
    :uglify         => false,
    :uglify_options => {},
    :config         => nil
  }
  @deleted = []

  set_options(options)

  begin
    require 'coffee-script'
    @has_coffee = true
  rescue LoadError
  end

end

Instance Attribute Details

#deletedObject

Returns the value of attribute deleted.



7
8
9
# File 'lib/stitch-plus.rb', line 7

def deleted
  @deleted
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/stitch-plus.rb', line 7

def options
  @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(options=nil)
  temp_options(options) if options
  files = []
  files << dependencies if @options[:dependencies]
  files << Dir.glob(File.join(@options[:paths], '**/*')) if @options[:paths]
  reset_options if options
  files.flatten.uniq
end

#compileObject

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(options=nil)
  temp_options(options) if options
  fingerprint = Digest::MD5.hexdigest(all_files.map! { |path| "#{File.mtime(path).to_i}" }.join + @options.to_s)
  reset_options if options
  fingerprint
end

#last_writeObject

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(options=nil)
  temp_options(options) if options
  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

  reset_options if options
  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 set_options(options={}, temporary=false)
  @old_options = @options if temporary

  # If options is a file path, read options from yaml
  options = { :config => options } if options.class == String
  options = load_options(options)

  @options = @options.merge symbolize_keys(options)

  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 temp_options(options)
  set_options(options, 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(options=nil)
  temp_options(options) if options

  @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}/", '')
    reset_options if options
    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
      reset_options if options
      false
    end
  end

end