Skip to content

Commit

Permalink
Closure-Compiler => Dart-Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
guilleiguaran committed Oct 13, 2011
1 parent 17eb474 commit 935ba21
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 356 deletions.
41 changes: 11 additions & 30 deletions README.textile
@@ -1,49 +1,30 @@
h1. The Closure Compiler (as a Ruby Gem)
h1. The Dart Compiler (as a Ruby Gem)

The *closure-compiler* gem is a svelte wrapper around the "Google Closure Compiler":http://code.google.com/closure/compiler/ for JavaScript compression.
The *dart-compiler* gem is a svelte wrapper around the "Dart Compiler":http://www.dartlang.org/ (Dart to JavaScript compiler)

Latest Version: *"1.1.4":http://gemcutter.org/gems/closure-compiler*
The Dart Compiler's *2011-10-12* JAR-files are included with the gem.

The Closure Compiler's *2011-10-03* JAR-file is included with the gem.
This wrapper is largelly based in "Closure Compiler":https://github.com/documentcloud/closure-compiler by Jeremy Ashkenas

h2. Installation

<pre>
sudo gem install closure-compiler
sudo gem install dart-compiler
</pre>

h2. Usage

The @Closure::Compiler@ has a single method, @compile@, which can be passed a string or an open @IO@ object, and returns the compiled JavaScript. The result is returned as a string, or, if a block is passed, yields as an @IO@ object for streaming writes.
The @Dart::Compiler@ has a single method, @compile@, which can be passed a string or an open @IO@ object, and returns the compiled JavaScript. The result is returned as a string.

<pre>
require 'rubygems'
require 'closure-compiler'
Closure::Compiler.new.compile(File.open('underscore.js', 'r'))
require 'dart-compiler'
Dart::Compiler.new.compile(File.open('hello.dart', 'r'))

=> "(function(){var j=this,m=j._;function i(a){......
=> "function native_ArrayFactory__new(typeToken, length) {......
</pre>

When creating a @Closure::Compiler@, you can pass "any options that the command-line compiler accepts":http://code.google.com/closure/compiler/docs/gettingstarted_app.html to the initializer and they'll be forwarded. For example, to raise the compilation level up a notch:
When creating a @Dart::Compiler@, you can pass any options that the command-line compiler accepts to the initializer and they'll be forwarded.

<pre>
closure = Closure::Compiler.new(:compilation_level => 'ADVANCED_OPTIMIZATIONS')
closure.compile(File.open('underscore.js', 'r'))

=> "(function(){var j=this,m=j.h;function i(a){......
</pre>

The default values of all the compiler flags are identical to the command-line version. The default *compilation_level* is "SIMPLE_OPTIMIZATIONS".

A @Closure::Error@ exception will be raised, explaining the JavaScript syntax error, if compilation fails for any reason.

h2. YUI Compressor Compatibility

Effort has been made to make the "closure-compiler" gem a drop-in alternative to the "ruby-yui-compressor". To that end, @Closure::Compiler#compile@ has been aliased as @compress@, and can take the same string or IO argument that a @YUI::JavaScriptCompressor#compress@ can. In addition, the @Closure::Compiler@ initializer can take @java@ and @jar_file@ options, overriding the location of the Java command and the Closure Compiler JAR file, respectively.

<pre>
compiler = Closure::Compiler.new(
:java => '/usr/local/bin/java16',
:jar_file => '/usr/src/closure/build/latest.jar'
)
</pre>
A @Dart::Error@ exception will be raised, explaining the Dart syntax error, if compilation fails for any reason.
18 changes: 4 additions & 14 deletions Rakefile
@@ -1,24 +1,14 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs += ["lib", "test"]
t.test_files = FileList["test/**/*_test.rb"]
t.verbose = true
end

namespace :gem do

desc 'Build and install the closure-compiler gem'
desc 'Build and install the dart-compiler gem'
task :install do
sh "gem build closure-compiler.gemspec"
sh "gem build dart-compiler.gemspec"
sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc"
end

desc 'Uninstall the closure-compiler gem'
desc 'Uninstall the dart-compiler gem'
task :uninstall do
sh "sudo gem uninstall -x closure-compiler"
sh "sudo gem uninstall -x dart-compiler"
end

end

task :default => :test
24 changes: 0 additions & 24 deletions closure-compiler.gemspec

This file was deleted.

22 changes: 22 additions & 0 deletions dart-compiler.gemspec
@@ -0,0 +1,22 @@
Gem::Specification.new do |s|
s.name = 'dart-compiler'
s.version = '0.0.1' # Keep version in sync with dart-compiler.rb
s.date = '2011-10-13'

s.homepage = "http://github.com/guilleiguaran/dart-compiler/"
s.summary = "Ruby Wrapper for the Dart Compiler"
s.description = <<-EOS
A Ruby Wrapper for the Dart Compiler.
EOS

s.authors = ['Guillermo Iguaran']
s.email = 'guilleiguaran@gmail.com'

s.require_paths = ['lib']

s.rdoc_options << '--title' << 'Ruby Dart Compiler' <<
'--all'

s.files = Dir['lib/**/*', 'dart-compiler.gemspec', 'README.textile', 'LICENSE', 'COPYING']

end
15 changes: 0 additions & 15 deletions lib/closure-compiler.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/dart-compiler.rb
@@ -0,0 +1,15 @@
module Dart

VERSION = "0.0.1"

COMPILER_VERSION = "20111013"

JAVA_COMMAND = 'java'

COMPILER_ROOT = File.expand_path(File.dirname(__FILE__))

COMPILER_JAR_PATH = File.join(COMPILER_ROOT, "java")

end

require 'dart/compiler'
32 changes: 15 additions & 17 deletions lib/closure/compiler.rb → lib/dart/compiler.rb
@@ -1,29 +1,30 @@
require 'stringio'
require 'tempfile'

module Closure
module Dart

# We raise a Closure::Error when compilation fails for any reason.
# We raise a Dart::Error when compilation fails for any reason.
class Error < StandardError; end

# The Closure::Compiler is a basic wrapper around the actual JAR. There's not
# The Dart::Compiler is a basic wrapper around the Dart Compiler. There's not
# much to see here.
class Compiler

DEFAULT_OPTIONS = {:warning_level => 'QUIET'}

DEFAULT_OPTIONS = {:work => Dir.tmpdir, :out => "#{Dir.tmpdir}/output.js",
:optimize => nil}

# When you create a Compiler, pass in the flags and options.
def initialize(options={})
@java = options.delete(:java) || JAVA_COMMAND
@jar = options.delete(:jar_file) || COMPILER_JAR
@jar_path = options.delete(:jar_path) || COMPILER_JAR_PATH
@out = options[:out] || DEFAULT_OPTIONS[:out]
@options = serialize_options(DEFAULT_OPTIONS.merge(options))
end

# Can compile a JavaScript string or open IO object. Returns the compiled
# JavaScript as a string or yields an IO object containing the response to a
# block, for streaming.
# Can compile a Dart string or open IO object.
# Returns the compiled JavaScript as a string.
def compile(io)
tempfile = Tempfile.new('closure_compiler')
tempfile = Tempfile.new('dart_compiler')
if io.respond_to? :read
while buffer = io.read(4096) do
tempfile.write(buffer)
Expand All @@ -34,7 +35,7 @@ def compile(io)
tempfile.flush

begin
result = `#{command} --js #{tempfile.path} 2>&1`
result = `#{command} #{tempfile.path} 2>&1`
rescue Exception
raise Error, "compression failed: #{result}"
ensure
Expand All @@ -43,12 +44,8 @@ def compile(io)
unless $?.exitstatus.zero?
raise Error, result
end

yield(StringIO.new(result)) if block_given?
result
output = File.open(@out).read
end
alias_method :compress, :compile


private

Expand All @@ -64,7 +61,8 @@ def serialize_options(options)
end

def command
[@java, '-jar', "\"#{@jar}\"", @options].flatten.join(' ')
[@java, '-classpath', "\"#{@jar_path}/*\"",
'com.google.dart.compiler.DartCompiler', @options].flatten.join(' ')
end

end
Expand Down
Binary file added lib/java/args4j-2.0.12.jar
Binary file not shown.
Binary file renamed lib/closure-compiler-20111003.jar → lib/java/closure-compiler.jar 100755 → 100644
Binary file not shown.
Binary file added lib/java/corelib.jar
Binary file not shown.
Binary file added lib/java/dartc.jar
Binary file not shown.
Binary file added lib/java/domlib.jar
Binary file not shown.
Binary file added lib/java/guava-r09.jar
Binary file not shown.
Binary file added lib/java/htmllib.jar
Binary file not shown.
Binary file added lib/java/js.jar
Binary file not shown.
Binary file added lib/java/json.jar
Binary file not shown.
Binary file added lib/java/jsonlib.jar
Binary file not shown.

0 comments on commit 935ba21

Please sign in to comment.