Class: Closure::Goog
- Inherits:
-
Object
- Object
- Closure::Goog
- Includes:
- Enumerable
- Defined in:
- lib/closure/goog.rb
Overview
Scripts render with an instance named goog in the context.
Instance Method Summary (collapse)
-
- add_dependency(dependency)
You can add additional files to have their mtimes scanned.
-
- (String) base_js
The Google Closure base.js script.
-
- (Compilation) compile(args)
Compile javascript.
-
- (String) deps_js
This is where base.js looks to find deps.js by default.
-
- (Rack::Response) deps_response
You can serve a deps.js from anywhere you want to drop a script.
-
- each
Advanced Scripts may need to know where all the sources are.
-
- (Array) files_for(namespace, filenames = nil)
Calculate files needed to satisfy a namespace.
-
- (Goog) initialize(env, sources, render_stack)
constructor
A new instance of Goog.
-
- refresh
If you change any javascript sources then you need to tell Script.
-
- soy_to_js(args)
Convert soy templates to javascript.
-
- (String) src_for(filename)
Calculate the deps src for a filename.
Constructor Details
- (Goog) initialize(env, sources, render_stack)
A new instance of Goog
22 23 24 25 26 27 |
# File 'lib/closure/goog.rb', line 22 def initialize(env, sources, render_stack) @sources = sources @env = env @render_stack = render_stack @dependencies = [] end |
Instance Method Details
- add_dependency(dependency)
You can add additional files to have their mtimes scanned. Perhaps you want to use a .yml file to define build options. Closure::Script calls this for every render so you don’t need to define compiler arguments in the same script that calls compile.
33 34 35 36 |
# File 'lib/closure/goog.rb', line 33 def add_dependency(dependency) dependency = File. dependency, File.dirname(@render_stack.last) @dependencies << dependency unless @dependencies.include? dependency end |
- (String) base_js
The Google Closure base.js script. If you use this instead of a static link, you are free to relocate relative to the Google Closure library without updating every html fixture page.
159 160 161 |
# File 'lib/closure/goog.rb', line 159 def base_js @sources.base_js(@env) end |
- (Compilation) compile(args)
Compile javascript. Accepts every argument that compiler.jar supports. This method supports all compiler augmentations added by Closure Script. Path options are expanded relative to the script calling #compile.
`--ns namespace` expands in place to `--js filename` arguments which satisfy the namespace.
`--module name:*:dep` File count will be filled in automatically. The * is replaced with the count of files up to next --module or the end.
`--js_output_file file` is compared against sources modification times to determine if compilation is to be performed.
`--compilation_level` when not supplied, the scripts are loaded raw.
70 71 72 73 74 75 76 77 78 79 80 81 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/closure/goog.rb', line 70 def compile(args) args = Array.new args # work on a copy pre_js_tempfile = nil begin Compiler::Util. args, File.dirname(@render_stack.last) mods = Compiler::Util.augment args, @sources, @env if Compiler::Util.arg_values(args, '--compilation_level').empty? # Raw mode comp = Compiler::Compilation.new @env if mods comp << Compiler::Util.module_info(mods) comp << Compiler::Util.module_uris_raw(mods, @sources) end js_counter = 0 args_index = 0 while args_index < args.length option, value = args[args_index, 2] if option == '--js' value = File. value, File.dirname(@render_stack.last) script_tag = "<script src=#{src_for(value).dump}></script>" comp << "document.write(#{script_tag.dump});\n" js_counter += 1 # For modules, just the files for the first module break if mods and js_counter >= mods[0][:files].length end args_index = args_index + 2 end else # Compiled mode module_output_path_prefix = Compiler::Util.arg_values(args, '--module_output_path_prefix').last if mods and !module_output_path_prefix # raise this before compilation so we don't write to a weird place raise "--module_output_path_prefix is required when using --module" end comp = Compiler.compile args, @dependencies, @env if mods refresh # compilation may add new files, module_uris_compiled uses src_for prefix = File. module_output_path_prefix, File.dirname(@render_stack.last) if comp.js_output_file File.open comp.js_output_file, 'w' do |f| f.write Compiler::Util.module_info mods f.write Compiler::Util.module_uris_compiled mods, @sources, prefix end else comp << Compiler::Util.module_info(mods) comp << Compiler::Util.module_uris_compiled(mods, @sources, prefix) end # Load the first module first_module_file = module_output_path_prefix + mods[0][:name] + '.js' first_module_file = File. first_module_file, File.dirname(@render_stack.last) script_tag = "<script src=#{src_for(first_module_file).dump}></script>" comp << "document.write(#{script_tag.dump});\n" end end comp ensure if pre_js_tempfile pre_js_tempfile.close pre_js_tempfile.unlink end end end |
- (String) deps_js
This is where base.js looks to find deps.js by default. You will always be served a Closure Script generated deps.js from this location. Very old Library versions may get confused by the forward caching query string; either update your base.js, install a deps_response Script where it’s looking, or manually set CLOSURE_BASE_PATH.
169 170 171 |
# File 'lib/closure/goog.rb', line 169 def deps_js @sources.deps_js(@env) end |
- (Rack::Response) deps_response
You can serve a deps.js from anywhere you want to drop a script.
177 178 179 |
# File 'lib/closure/goog.rb', line 177 def deps_response @sources.deps_response(File.dirname(Rack::Utils.unescape(@env["PATH_INFO"])), @env) end |
- each
Advanced Scripts may need to know where all the sources are. This has potential for a source browser, editor, and more.
185 186 187 188 189 |
# File 'lib/closure/goog.rb', line 185 def each @sources.each do |directory, path| yield directory, path end end |
- (Array) files_for(namespace, filenames = nil)
Calculate files needed to satisfy a namespace. This will be especially useful for module generation. If you pass the filenames returned from last run, additional files (if any) will be appended to satisfy the new namespace.
149 150 151 |
# File 'lib/closure/goog.rb', line 149 def files_for(namespace, filenames=nil) @sources.files_for(namespace, filenames, @env) end |
- refresh
If you change any javascript sources then you need to tell Script. This is a lazy refresh, you may call it repeatedly.
40 41 42 |
# File 'lib/closure/goog.rb', line 40 def refresh @sources.invalidate @env end |
- soy_to_js(args)
Convert soy templates to javascript. Accepts all arguments that SoyToJsSrcCompiler.jar support plus it expands filename globs. All source filenames are relative to the script calling #soy_to_js.
48 49 50 51 |
# File 'lib/closure/goog.rb', line 48 def soy_to_js(args) Templates::compile(args, File.dirname(@render_stack.last)) refresh end |
- (String) src_for(filename)
Calculate the deps src for a filename.
136 137 138 139 |
# File 'lib/closure/goog.rb', line 136 def src_for(filename) filename = File. filename @sources.src_for(filename, @env) end |