Class: Closure
- Inherits:
-
Object
- Object
- Closure
- Defined in:
- lib/closure.rb,
lib/closure/goog.rb,
lib/closure/script.rb,
lib/closure/server.rb,
lib/closure/sources.rb,
lib/closure/version.rb,
lib/closure/compiler.rb,
lib/closure/beanshell.rb,
lib/closure/templates.rb,
lib/closure/middleware.rb,
lib/closure/file_response.rb,
lib/closure/show_exceptions.rb
Overview
Closure tools may be called directly, run as a stand-alone server, installed as middleware into a framework like Rails, or adapted to anything with a rack environment.
Defined Under Namespace
Classes: BeanShell, Compiler, FileResponse, Goog, Middleware, Script, Server, ShowExceptions, Sources, Templates
Constant Summary collapse
- BUILT_INS =
Scripts that are distributed with the gem. These will help get you started quickly.
{ :soy => File.join(base_path, 'closure-templates'), :docs => File.join(base_path, 'docs') }
- VERSION =
"1.5.5"
Class Method Summary collapse
-
.add_source(directory, path) ⇒ Object
Easy config.
-
.base_path ⇒ String
Filesystem location of the Closure Script install.
-
.config ⇒ OpenStruct
Set these before the rack server is called for the first time.
-
.exclude(directory) ⇒ Object
Easy config.
-
.run_java(jar, mainClass, args) ⇒ Array
Execute jar in a REPL or with JRuby.
-
.sources ⇒ Array
This is a global instance of sources, configured with Closure.add_source() and used for Script::Middleware by default.
-
.welcome ⇒ Object
Run the welcome server.
Class Method Details
.add_source(directory, path) ⇒ Object .add_source(built_in, path) ⇒ Object
Easy config. This adds to the global instance of sources and supports using the BUILT_INS.
58 59 60 61 62 63 64 65 66 |
# File 'lib/closure.rb', line 58 def self.add_source(directory, path) if directory.kind_of? Symbol dir = BUILT_INS[directory] raise "Unknown built-in: #{directory}" unless dir directory = dir end raise Errno::ENOENT, File.(directory, Dir.pwd) unless File.directory? directory sources.add directory, path end |
.base_path ⇒ String
Filesystem location of the Closure Script install. Typically, where the gem was installed. This is mainly used internally but may be useful for experimental configurations.
36 37 38 |
# File 'lib/closure.rb', line 36 def self.base_path @@base_path ||= File.(File.join(File.dirname(__FILE__), '..')) end |
.config ⇒ OpenStruct
Set these before the rack server is called for the first time.
Attributes:
-
(String) java – default: “java” – Your Java executable. Not used under JRuby.
-
(String) compiler_jar – A compiler.jar to use instead of the packaged one.
-
(String) soy_js_jar – A SoyToJsSrcCompiler.jar to use instead of the packaged one.
-
(Array) engines – Add new script engines here.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/closure.rb', line 123 def self.config return @@config if defined? @@config @@config = OpenStruct.new({ :compiler_jar => File.join(base_path, 'closure-compiler', 'compiler.jar'), :soy_js_jar => File.join(base_path, 'closure-templates', 'SoyToJsSrcCompiler.jar'), :engines => {} }) if !defined? JRUBY_VERSION @@config.java = 'java' end @@config end |
.exclude(directory) ⇒ Object
Easy config. Exclude a subdirectories of sources. This is typically used to exclude the build folder for whitespace builds (which contain “goog.provide”).
72 73 74 75 |
# File 'lib/closure.rb', line 72 def self.exclude(directory) # no check if exists, build may create later sources.add directory, nil end |
.run_java(jar, mainClass, args) ⇒ Array
Execute jar in a REPL or with JRuby
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/closure.rb', line 93 def self.run_java(jar, mainClass, args) jar = File.(jar) cmdout = Tempfile.new 'closure_java_out' cmderr = Tempfile.new 'closure_java_err' begin if defined? JRUBY_VERSION require 'java' require File.join(base_path, 'lib', 'shim.jar') Java::ClosureScript.run(jar, mainClass, cmdout.path, cmderr.path, args) else @@beanshell ||= BeanShell.new File.join(base_path, 'lib', 'shim.jar') java_opts = args.collect{|a|a.to_s.dump}.join(', ') cmd = "ClosureScript.run(#{jar.dump}, #{mainClass.dump}, #{cmdout.path.dump}, #{cmderr.path.dump}, new String[]{#{java_opts}});" @@beanshell.run(cmd) end ensure out = cmdout.read; cmdout.close; cmdout.unlink err = cmderr.read; cmderr.close; cmderr.unlink end [out, err] end |
.sources ⇒ Array
This is a global instance of sources, configured with Closure.add_source() and used for Script::Middleware by default. Path and directory pairs configured with Closure.add_source().
82 83 84 |
# File 'lib/closure.rb', line 82 def self.sources @@sources ||= Sources.new end |
.welcome ⇒ Object
Run the welcome server. Handy for gem users.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/closure.rb', line 139 def self.welcome raise 'Use rackup, config.ru already exists.' if File.exist? 'config.ru' gem 'rack', '>= 1.1.0' require 'rack' ENV["CLOSURE_SCRIPT_WELCOME"] = 'true' server = Rack::Server.new :config => File.join(base_path, 'scripts', 'config.ru') # Make a phony request so options[:Port] gets set from config.ru Rack::MockRequest.new(server.app).request port = server.[:Port] || server.[:Port] print "Closure Script Welcome Server: http://localhost:#{port}/\n" server.start end |