Module: JSCompiler

Defined in:
lib/jsc.rb,
lib/jsc/closure_compiler.rb

Constant Summary collapse

GOOGLE_SERVICE_ADDRESS =

Link to Google Closure Compiler service

"http://closure-compiler.appspot.com/compile"
DEFAULT_SERVICE =

Default output_info parameter

"compiled_code"
DEFAULT_LEVEL =

Default compilation_level parameter

"SIMPLE_OPTIMIZATIONS"

Class Method Summary collapse

Class Method Details

.compile(javascript_code, op, level) ⇒ Object

Compiles javascript_code code and returns parsed output

Accepted parameters:

  • javascript_code: the code to compile

  • op: output_info parameter

  • level: compilation_level parameter



73
74
75
76
77
78
79
# File 'lib/jsc/closure_compiler.rb', line 73

def compile(javascript_code, op, level)
  @op = op.blank? ? DEFAULT_SERVICE : op
  @level = level.blank? ? DEFAULT_LEVEL : level

  resp, data = post_to_cc(create_json_request(javascript_code))
  parse_json_output(data)
end

.compile_dir(dir, op, level) ⇒ Object

Calls compile method for every file in dir directory

Accepted parameters:

  • dir: the directory

  • op: output_info parameter

  • level: compilation_level parameter



87
88
89
90
91
92
93
94
95
96
# File 'lib/jsc/closure_compiler.rb', line 87

def compile_dir(dir, op, level)
  out = String.new
  Dir.entries(dir).each do |file|
    if File.extname(file) == ".js"
      out << "Statistics for file #{file}...\n"
      out << compile_file(file, op, level) + "\n***************\n"
    end
  end
  return out
end

.compile_file(file_name, op, level) ⇒ Object

Reads the file_name file and calls compile method on it

Accepted parameters:

  • file_name: absolute path to file

  • op: output_info parameter

  • level: compilation_level parameter



58
59
60
61
62
63
64
65
# File 'lib/jsc/closure_compiler.rb', line 58

def compile_file(file_name, op, level)
  #    javascript_code = read_file(JAVASCRIPTS_DIR + file_name)
  #    resp, data = post_to_cc(create_json_request(javascript_code, op, level))
  #    parse_json_output(data, op)
 
  javascript_code = read_file(file_name)
  compile(javascript_code, op, level)
end

.create_json_request(code) ⇒ Object

Creates the JSON hash for the request and returns the hash to send along with the request

Accepted parameters:

  • code: json_code parameter

  • level: compilation_level parameter



30
31
32
33
34
35
36
37
# File 'lib/jsc/closure_compiler.rb', line 30

def create_json_request(code)
  parameters = {
	"code" => code,
	"level" => @level,
	"format"   => "json",
	"info"  => @op
  }
end

.create_statistics_output(result) ⇒ Object

Parses and returns the result JSON server response

Accepted parameters:

  • result: the already parsed JSON server response



156
157
158
159
160
161
162
163
164
# File 'lib/jsc/closure_compiler.rb', line 156

def create_statistics_output(result)
  size_improvement = result['originalSize'] - result['compressedSize']
  size_gzip_improvement = result['originalGzipSize'] - result['compressedGzipSize']
  rate_improvement = (size_improvement * 100)/result['originalSize']
  rate_gzip_improvement = (size_gzip_improvement * 100)/result['originalGzipSize']
  out = "Original Size: #{result['originalSize']} bytes (#{result['originalGzipSize']} bytes gzipped) \n"
  out << "Compiled Size: #{result['compressedSize']} bytes (#{result['compressedGzipSize']} bytes gzipped) \n"
  out << "\t Saved #{rate_improvement}% off the original size (#{rate_gzip_improvement}% off the gzipped size)"
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



19
20
21
# File 'lib/jsc.rb', line 19

def self.libpath( *args )
  args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
end

.parse_json_output(response) ⇒ Object

Parses and returns JSON server response

Accepted parameters:

  • response: the server response



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jsc/closure_compiler.rb', line 102

def parse_json_output(response)
  out = String.new
  parsed_response = JSON.parse(response, :max_nesting => false)
  #p response

  if parsed_response.has_key?("serverErrors") 
    result = parsed_response['serverErrors']
    return "Server Error: #{result[0]['error']} - Error Code: #{result[0]['code']}"
  end

  case @op
  when "compiled_code"
    out = parsed_response['compiledCode']
  when "statistics"
    result = parsed_response['statistics']
    out = create_statistics_output(result)
  else "errors"
    #case for errors or warnings
    begin
      result = parsed_response[@op]
      unless result.nil?
        result.each do |message|
          out = "#{message['type']}: " + message[@op.singularize] + " at line #{message['lineno']} character #{message['charno']}\n"
          out << message['line'] unless message['line'].nil?
          return out
        end
      else
        return "No #{@op}"
      end
    rescue
      out = "Error parsing JSON output...Check your output"
    end
  end
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



27
28
29
# File 'lib/jsc.rb', line 27

def self.path( *args )
  args.empty? ? PATH : ::File.join(PATH, args.flatten)
end

.post_to_cc(data) ⇒ Object

Sends the JSON request data hash to Google service and returns its response



41
42
43
44
45
46
47
48
49
50
# File 'lib/jsc/closure_compiler.rb', line 41

def post_to_cc(data)
  post_args = { 
    'js_code' => data["code"],
    'compilation_level' => data["level"],
    'output_format' => data["format"],
    'output_info' => data["info"]
  }
  # send the request
  resp, data = Net::HTTP.post_form(URI.parse(GOOGLE_SERVICE_ADDRESS), post_args)
end

.read_file(file_name) ⇒ Object

Reads file and returns its content

Accepted parameters:

  • file_name: the absolute path to the file



141
142
143
144
145
146
147
148
149
150
# File 'lib/jsc/closure_compiler.rb', line 141

def read_file(file_name)
  begin
    #      content = File.open(JAVASCRIPTS_DIR + file_name).read
    content = File.open(file_name).read
    return content, true
  rescue
    out = "ERROR reading #{file_name} file"
    return out, false
  end
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



36
37
38
39
40
41
42
# File 'lib/jsc.rb', line 36

def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
      ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end

.versionObject

Returns the version string for the library.



11
12
13
# File 'lib/jsc.rb', line 11

def self.version
  VERSION
end