Class: K4compiler::Closure

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from K4compiler::Base

Class Method Details

.optionsHash

Returns:

  • (Hash)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/k4compiler/compiler/closure.rb', line 5

def self.options
  return {
    load_paths: [],
    with_closure: false,
    # common options
    level: :advanced, # advanced(default) / simple / script
    java_command: 'java',
    compiler_jar: File.join(::K4compiler::Config::THIRD_PARTY_DIR, 'closure-compiler-20130603/compiler.jar'),
    # with closure options
    python_command: 'python',
    closure_builder: File.join(::K4compiler::Config::THIRD_PARTY_DIR, 'closure-library/closure/bin/build/closurebuilder.py'),
    closure_path: File.join(::K4compiler::Config::THIRD_PARTY_DIR, 'closure-library'),
    compile_output_wrapper: "(function(){%output%})();",
    compile_defined_options: ['goog.DEBUG=false;'],
  }
end

Instance Method Details

#build_compile_command_with_closure(namespace, roots = []) ⇒ Object

Parameters:

  • namespace (String)

    JavaScript file namespace.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/k4compiler/compiler/closure.rb', line 37

def build_compile_command_with_closure(namespace, roots=[])
  compile_mode = config.level == :script ? :script : :compiled

  puts config.load_paths

  com = []
  com << config.python_command
  com << config.closure_builder
  com << "-o #{compile_mode.to_s}"
  com << "-c #{config.compiler_jar}"
  com << "--namespace=\"#{namespace}\""
  com << "--root=#{config.closure_path}"
  if not roots.nil? and roots.is_a?(::Array)
    roots.each do |root|
      com << "--root=#{root}"
    end
  end
  config.load_paths.each do |path|
    com << "--root=#{path}"
  end
  # compiler options
  com << "-f --compilation_level=#{compilation_level}"
  com << "-f --output_wrapper=\"#{config.compile_output_wrapper}\""
  config.compile_defined_options.each do |define_opt|
    com << "-f --define=#{define_opt}"
  end
  return com.join(' ').chomp
end

#build_compile_command_without_closure(target_filepath, output_filepath) ⇒ Object

Parameters:

  • target_filepath (String)

    Compile file path.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/k4compiler/compiler/closure.rb', line 67

def build_compile_command_without_closure(target_filepath, output_filepath)
  if config.level == :script
    com = "cp -a #{target_filepath} #{output_filepath}"
    return com
  else
    com = [config.java_command]
    com << "-jar #{config.compiler_jar}"
    com << "--js #{target_filepath}"
    com << "--js_output_file #{output_filepath}"
    com << "--compilation_level #{compilation_level}"
    return com.join(' ').chomp
  end
end

#compilation_levelString

Returns:

  • (String)


82
83
84
85
86
87
88
89
# File 'lib/k4compiler/compiler/closure.rb', line 82

def compilation_level
  return case config.level
         when :script then 'SIMPLE_OPTIMIZATIONS'
         when :simple then 'SIMPLE_OPTIMIZATIONS'
         when :advanced then 'ADVANCED_OPTIMIZATIONS'
         else 'ADVANCED_OPTIMIZATIONS'
         end
end

#compile(*args) ⇒ Object

Parameters:

  • filepath (String)

    JavaScript source file path.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/k4compiler/compiler/closure.rb', line 23

def compile(*args)
  result = nil
  if config.with_closure
    command = build_compile_command_with_closure(*args)
    puts command
    result = IO.popen('-', 'r') {|pipe| pipe ? pipe.read : exec(*command) }
  else
    command = build_compile_command_without_closure(*args)
    result = system(command)
  end
  return result
end

#configOpenStruct

Returns:

  • (OpenStruct)


92
93
94
# File 'lib/k4compiler/compiler/closure.rb', line 92

def config
  return super.closure
end