Class: Closure

Inherits:
Object
  • Object
show all
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.

Examples:

config.ru

#\ -p 8080 -E none
require 'closure'
Closure.add_source :goog, '/goog'
Closure.add_source './src/myapp', '/myapp'
use Rack::ShowExceptions
use Closure::Middleware
run Rack::File.new './public'

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

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.

Examples:

Closure.add_source :soy, '/soy_js'
Closure.add_source './myapp', '/myapp'

Parameters:

  • path (String)

    http server mount point.

  • directory (String)

    Where the scripts are in the filesystem.

  • built_in (Symbol)

Raises:

  • (Errno::ENOENT)


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.expand_path(directory, Dir.pwd) unless File.directory? directory
  sources.add directory, path
end

.base_pathString

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.

Returns:

  • (String)


36
37
38
# File 'lib/closure.rb', line 36

def self.base_path
  @@base_path ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
end

.configOpenStruct

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.

Returns:

  • (OpenStruct)


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

Parameters:

  • jar (String)

    Path to .jar file

  • mainClass (String)

    Class with main(String[] args)

  • args (Array)

    Arguments

Returns:

  • (Array)
    stdout, stderr


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.expand_path(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

.sourcesArray

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().

Returns:

  • (Array)


82
83
84
# File 'lib/closure.rb', line 82

def self.sources
  @@sources ||= Sources.new
end

.welcomeObject

Run the welcome server. Handy for gem users.

Examples:

ruby -e "require 'rubygems'; gem 'closure'; require 'closure'; Closure.welcome"


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.options[:Port] || server.default_options[:Port]
  print "Closure Script Welcome Server: http://localhost:#{port}/\n"
  server.start
end