Class: Closure::Compiler::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/closure/compiler.rb

Overview

Closure Script extends compiler.jar by transforming the arguments in novel ways. The most obvious augmentation is to support –ns for compiling namespaces. We can also expand paths to a new base, work with modules, and much more. These all will directly modify args.

Class Method Summary collapse

Class Method Details

.arg_values(args, options) ⇒ Array<String>

Extracts the values for a options in the arguments. Use Array#last to emulate compiler.jar for single options. Will collect from an array of options.

Parameters:

  • args (Array<String>)
  • options (String|Array<String>)

    One or more options.

Returns:

  • (Array<String>)


245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/closure/compiler.rb', line 245

def self.arg_values(args, options)
  options = [options].flatten unless options.kind_of? Array
  values = []
  args_index = 0
  while args_index < args.length
    if options.include? args[args_index]
      values << args[args_index+1] 
    end
    args_index = args_index + 2
  end
  values
end

.augment(args, sources, env = {}) ⇒ Array<Hash>

Main function to convert –ns arguments into –js arguments. Returns module info when modules are processed.

Parameters:

  • args (Array<String>)

Returns:

  • (Array<Hash>)

    mods



228
229
230
231
232
233
234
235
236
# File 'lib/closure/compiler.rb', line 228

def self.augment(args, sources, env={})
  mods = extract_modules args
  if mods
    module_augment args, sources, mods, env
  else
    namespace_augment args, sources, [], env
  end
  mods
end

.expand_paths(args, base) ⇒ Array<String>

Expands all filesystem argument values to a specified folder.

Parameters:

  • args (Array<String>)

Returns:

  • (Array<String>)

    args



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/closure/compiler.rb', line 173

def self.expand_paths(args, base)
  file_options = INPUT_OPTIONS + OUTPUT_OPTIONS
  args_index = 0
  while args_index < args.length
    option, value = args[args_index, 2]
    value = File.expand_path value, base
    if file_options.include? option
      args[args_index+1] = value
    end
    args_index = args_index + 2
  end
  args
end

.module_info(mods) ⇒ Object

The javascript snippet for module info

Parameters:

  • mods (Array<Hash>)


190
191
192
193
194
195
196
197
# File 'lib/closure/compiler.rb', line 190

def self.module_info(mods)
  js = "var MODULE_INFO = {"
  js += mods.map do |mod|
    reqs = mod[:requires].map{ |r| r.dump }
    s = "#{mod[:name].dump}: [#{reqs.join ', '}]"
  end.join ', '
  js += "};\n"
end

.module_uris_compiled(mods, sources, prefix) ⇒ Object

The javascript snippet for compiled module file locations

Parameters:

  • mods (Array<Hash>)


214
215
216
217
218
219
220
221
# File 'lib/closure/compiler.rb', line 214

def self.module_uris_compiled(mods, sources, prefix)
  js = "var MODULE_URIS = {\n"
  js += mods.map do |mod|
    file = sources.src_for prefix + mod[:name] + '.js'
    s = "#{mod[:name].dump}: [#{file.dump}]"
  end.join ",\n"
  js += "\n};\n"
end

.module_uris_raw(mods, sources) ⇒ Object

The javascript snippet for raw module file locations

Parameters:

  • mods (Array<Hash>)


202
203
204
205
206
207
208
209
# File 'lib/closure/compiler.rb', line 202

def self.module_uris_raw(mods, sources)
  js = "var MODULE_URIS = {\n"
  js += mods.map do |mod|
    files = mod[:files].map{ |r| (sources.src_for r).dump }
    s = "#{mod[:name].dump}: [\n#{files.join ",\n"}]"
  end.join ",\n"
  js += "\n};\n"
end