Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 95 lines (77 sloc) 2.447 kb
f9736a4 Karol Bucek use Temfile instead of POpen piping (for JRuby compatibility)
kares authored
1 require 'stringio'
2 require 'tempfile'
70b2ab0 Jeremy Ashkenas redo of tidy changes.
jashkenas authored
3
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
4 module Closure
5
467bfbc Jeremy Ashkenas Closure Compiler 0.1.7, with exceptions for failed compilation
jashkenas authored
6 # We raise a Closure::Error when compilation fails for any reason.
7 class Error < StandardError; end
8
3c2e526 Jeremy Ashkenas initial commit -- working closure compiler gem
jashkenas authored
9 # The Closure::Compiler is a basic wrapper around the actual JAR. There's not
10 # much to see here.
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
11 class Compiler
e5555e3 Andrew Lunny expose #options attribute
alunny authored
12
13 attr_accessor :options
757696c alubbe fixed '\e[m' creeping into compressed result on windows OS'
alubbe authored
14
f4436bb Michael Rykov Use ECMAScript 5 by default
rykov authored
15 DEFAULT_OPTIONS = {
16 :warning_level => 'QUIET',
17 :language_in => 'ECMASCRIPT5'
18 }
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
19
3c2e526 Jeremy Ashkenas initial commit -- working closure compiler gem
jashkenas authored
20 # When you create a Compiler, pass in the flags and options.
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
21 def initialize(options={})
3114936 Jeremy Ashkenas version 0.1.1, which can override :java and :jar_file
jashkenas authored
22 @java = options.delete(:java) || JAVA_COMMAND
23 @jar = options.delete(:jar_file) || COMPILER_JAR
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
24 @options = DEFAULT_OPTIONS.merge(options)
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
25 end
26
3c2e526 Jeremy Ashkenas initial commit -- working closure compiler gem
jashkenas authored
27 # Can compile a JavaScript string or open IO object. Returns the compiled
28 # JavaScript as a string or yields an IO object containing the response to a
29 # block, for streaming.
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
30 def compile(io)
f9736a4 Karol Bucek use Temfile instead of POpen piping (for JRuby compatibility)
kares authored
31 tempfile = Tempfile.new('closure_compiler')
32 if io.respond_to? :read
33 while buffer = io.read(4096) do
34 tempfile.write(buffer)
5acf230 Jeremy Ashkenas Closure-Compiler 0.3.1, with homebrew support for Windows.
jashkenas authored
35 end
f9736a4 Karol Bucek use Temfile instead of POpen piping (for JRuby compatibility)
kares authored
36 else
37 tempfile.write(io.to_s)
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
38 end
1f4f79b Karol Bucek ensure tempfile is deleted
kares authored
39 tempfile.flush
f9736a4 Karol Bucek use Temfile instead of POpen piping (for JRuby compatibility)
kares authored
40
41 begin
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
42 result = compile_files(tempfile.path)
43 rescue Exception => e
44 raise e
1f4f79b Karol Bucek ensure tempfile is deleted
kares authored
45 ensure
46 tempfile.close!
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
47 end
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
48
49 yield(StringIO.new(result)) if block_given?
50 result
51 end
52 alias_method :compress, :compile
53
54 # Takes an array of javascript file paths or a single path. Returns the
55 # resulting JavaScript as a string or yields an IO object containing the
56 # response to a block, for streaming.
57 def compile_files(files)
7cabf29 Alex Tambellini Use 1.8 hash syntax
atambo authored
58 @options.merge!(:js => files)
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
59
60 begin
757696c alubbe fixed '\e[m' creeping into compressed result on windows OS'
alubbe authored
61 redirect_stderr = "2>&1" if !Gem.win_platform?
62 result = `#{command} #{redirect_stderr}`
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
63 rescue Exception
64 raise Error, "compression failed: #{result}"
65 end
66
f9736a4 Karol Bucek use Temfile instead of POpen piping (for JRuby compatibility)
kares authored
67 unless $?.exitstatus.zero?
68 raise Error, result
69 end
70
71 yield(StringIO.new(result)) if block_given?
467bfbc Jeremy Ashkenas Closure Compiler 0.1.7, with exceptions for failed compilation
jashkenas authored
72 result
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
73 end
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
74 alias_method :compile_file, :compile_files
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
75
76 private
77
3c2e526 Jeremy Ashkenas initial commit -- working closure compiler gem
jashkenas authored
78 # Serialize hash options to the command-line format.
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
79 def serialize_options(options)
7fb8f71 Maciej K. Allowing multiple externs to be passed in options
xiazek authored
80 options.map do |k, v|
81 if (v.is_a?(Array))
82 v.map {|v2| ["--#{k}", v2.to_s]}
83 else
84 ["--#{k}", v.to_s]
85 end
86 end.flatten
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
87 end
88
89 def command
ec898c9 Jayme Deffenbaugh Allowing multiple js files to be specified for compilation
jdeff authored
90 [@java, '-jar', "\"#{@jar}\"", serialize_options(@options)].flatten.join(' ')
b1d8bc0 Jeremy Ashkenas initial commit ... got the closure compiler working with jammit
jashkenas authored
91 end
92
93 end
099a3d0 Joshua Peek Ensure popen is required from closure/compiler
josh authored
94 end
Something went wrong with that request. Please try again.