jsc 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.bnsignore +18 -0
- data/.gitignore +7 -0
- data/History.txt +3 -0
- data/README.rdoc +81 -0
- data/Rakefile +37 -0
- data/bin/jsc +89 -0
- data/js/compiled_code.js +4 -0
- data/js/errors.js +4 -0
- data/js/warnings.js +5 -0
- data/lib/jsc/closure_compiler.rb +168 -0
- data/lib/jsc.rb +47 -0
- data/plugins/bin/google_closure_compiler +42 -0
- data/plugins/jsc.el +44 -0
- data/spec/jsc.rb +132 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/wip_spec.rb +6 -0
- data/tasks/jsc.rake +4 -0
- data/test/test_jsc.rb +0 -0
- metadata +84 -0
data/.bnsignore
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# The list of files that should be ignored by Mr Bones.
|
2
|
+
# Lines that start with '#' are comments.
|
3
|
+
#
|
4
|
+
# A .gitignore file can be used instead by setting it as the ignore
|
5
|
+
# file in your Rakefile:
|
6
|
+
#
|
7
|
+
# Bones {
|
8
|
+
# ignore_file '.gitignore'
|
9
|
+
# }
|
10
|
+
#
|
11
|
+
# For a project with a C extension, the following would be a good set of
|
12
|
+
# exclude patterns (uncomment them if you want to use them):
|
13
|
+
# *.[oa]
|
14
|
+
# *~
|
15
|
+
announcement.txt
|
16
|
+
coverage
|
17
|
+
doc
|
18
|
+
pkg
|
data/.gitignore
ADDED
data/History.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
jsc, a Ruby REST API to Google Closure Compiler web service
|
2
|
+
by sub
|
3
|
+
http://github.com/sub/jsc
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A simple and clear Ruby REST API to {Google Closure Compiler service}[http://code.google.com/closure/compiler/].
|
8
|
+
|
9
|
+
== FEATURES:
|
10
|
+
|
11
|
+
The package comes with a jsc commands which accepts several options, run
|
12
|
+
|
13
|
+
jsc --help
|
14
|
+
|
15
|
+
for help.
|
16
|
+
|
17
|
+
More in details:
|
18
|
+
* Ruby API actually is a single function call (ex: JSCompiler.compile(file_name, "statistics", "SIMPLE_OPTIMIZATIONS") )
|
19
|
+
* Handling of JSON responses, parse and print them (same output of the Google web interface!)
|
20
|
+
* Handling of Server Errors responses
|
21
|
+
* Compile a piece of code, a file or a whole directory
|
22
|
+
* <b>Emacs snippet</b> to compile code your code for errors and warnings
|
23
|
+
|
24
|
+
Check {Google API Reference}[http://code.google.com/intl/it-IT/closure/compiler/docs/api-ref.html] for more info about accepted parameters.
|
25
|
+
|
26
|
+
== SYNOPSIS:
|
27
|
+
|
28
|
+
Compile a file:
|
29
|
+
|
30
|
+
ruby bin/jsc -f js/compiled_code.js
|
31
|
+
|
32
|
+
Compile a file, check for errors:
|
33
|
+
|
34
|
+
ruby bin/jsc -f js/errors.js -e
|
35
|
+
|
36
|
+
Compile a file, check for warnings:
|
37
|
+
|
38
|
+
ruby bin/jsc -f js/warnings.js -w
|
39
|
+
|
40
|
+
Compile a file and get compression stats:
|
41
|
+
|
42
|
+
ruby bin/jsc -f js/compiled_code.js -s
|
43
|
+
|
44
|
+
Compile a piece of code, check for errors:
|
45
|
+
|
46
|
+
ruby bin/jsc -e "function("
|
47
|
+
|
48
|
+
<b>NOTE</b>: the library is a single file, so you can include it and just call the JSCompiler.compile(file_name, operations, compilation_level) function everywhere in your code. Check the plugins dir for samples or snippets.
|
49
|
+
<b>UPDATE 20091212</b>: bin file plugins/bin/google_closure_compiler not working at the moment due to changes in project name and structure
|
50
|
+
|
51
|
+
== EMACS SNIPPETS
|
52
|
+
|
53
|
+
Wants to compile your code right from Emacs?
|
54
|
+
|
55
|
+
Copy
|
56
|
+
|
57
|
+
plugins/jsc.el
|
58
|
+
|
59
|
+
to your emacs <em>load-path</em> and restart Emacs.
|
60
|
+
|
61
|
+
Now, select the code to compile and run:
|
62
|
+
* <em>CcJe</em> to check for errors
|
63
|
+
* <em>CcJw</em> to check for warnings.
|
64
|
+
|
65
|
+
== REQUIREMENTS:
|
66
|
+
|
67
|
+
No requirements
|
68
|
+
|
69
|
+
== INSTALL:
|
70
|
+
|
71
|
+
Create your gem with:
|
72
|
+
|
73
|
+
rake gem:package
|
74
|
+
|
75
|
+
In order to install it, run:
|
76
|
+
|
77
|
+
rake gem:install
|
78
|
+
|
79
|
+
== LICENSE:
|
80
|
+
|
81
|
+
GNU General Public License (GPLv3)
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
require 'lib/jsc.rb'
|
6
|
+
#require 'lib/jsc/tasks'
|
7
|
+
|
8
|
+
require 'term/ansicolor'
|
9
|
+
|
10
|
+
class Color
|
11
|
+
class << self
|
12
|
+
include Term::ANSIColor
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
## Mr.Bones config
|
17
|
+
begin
|
18
|
+
require 'bones'
|
19
|
+
rescue LoadError
|
20
|
+
abort '### Please install the "bones" gem ###'
|
21
|
+
end
|
22
|
+
|
23
|
+
ensure_in_path 'lib'
|
24
|
+
require 'jsc'
|
25
|
+
|
26
|
+
task :default => 'test:run'
|
27
|
+
task 'gem:release' => 'test:run'
|
28
|
+
|
29
|
+
Bones {
|
30
|
+
name 'jsc'
|
31
|
+
authors 'sub'
|
32
|
+
email 'fitzkarraldo@gmail.com'
|
33
|
+
url 'http://github.com/sub/jsc'
|
34
|
+
version '0.1.0'
|
35
|
+
summary 'Google Closure Compiler Ruby REST API'
|
36
|
+
}
|
37
|
+
|
data/bin/jsc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# == Synopsis
|
4
|
+
#
|
5
|
+
# jsc, JavaScript Compiler.
|
6
|
+
#
|
7
|
+
# This command compiles your JavaScript code throught Google
|
8
|
+
# Closure Compiler Service.
|
9
|
+
# Look at http://github.com/sub/google_closure_compiler for more info
|
10
|
+
#
|
11
|
+
# == Usage
|
12
|
+
#
|
13
|
+
# jsc [OPTION] ... CODE
|
14
|
+
#
|
15
|
+
# -h, --help:
|
16
|
+
# show help
|
17
|
+
#
|
18
|
+
# --file x, -f x:
|
19
|
+
# compile file x
|
20
|
+
#
|
21
|
+
# --level value, -l value:
|
22
|
+
# compile with level value
|
23
|
+
# If this option is not supplied, SIMPLE_OPTIMIZATIONS will be used
|
24
|
+
# Look at Google API for accepted values
|
25
|
+
#
|
26
|
+
# --errors, -e:
|
27
|
+
# check for errors
|
28
|
+
#
|
29
|
+
# --warns, -w:
|
30
|
+
# check for warnings
|
31
|
+
#
|
32
|
+
# --stats, -s:
|
33
|
+
# get statistics for compiled code
|
34
|
+
#
|
35
|
+
# CODE: The code that will be compiled.
|
36
|
+
|
37
|
+
require 'getoptlong'
|
38
|
+
require 'rdoc/usage'
|
39
|
+
|
40
|
+
require File.expand_path(
|
41
|
+
File.join(File.dirname(__FILE__), %w[.. lib jsc]))
|
42
|
+
|
43
|
+
opts = GetoptLong.new(
|
44
|
+
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
45
|
+
[ '--file', '-f', GetoptLong::OPTIONAL_ARGUMENT ],
|
46
|
+
[ '--level','-l', GetoptLong::REQUIRED_ARGUMENT ],
|
47
|
+
[ '--errors', '-e', GetoptLong::NO_ARGUMENT ],
|
48
|
+
[ '--warns', '-w', GetoptLong::NO_ARGUMENT ],
|
49
|
+
[ '--stats', '-s', GetoptLong::NO_ARGUMENT ]
|
50
|
+
)
|
51
|
+
|
52
|
+
file = false
|
53
|
+
output_info, level, file_path = String.new
|
54
|
+
|
55
|
+
opts.each do |opt, arg|
|
56
|
+
case opt
|
57
|
+
when '--help'
|
58
|
+
RDoc::usage
|
59
|
+
when '--file'
|
60
|
+
file = true
|
61
|
+
file_path = arg
|
62
|
+
when '--level'
|
63
|
+
if arg == ''
|
64
|
+
level = "SIMPLE_OPTIMIZATIONS"
|
65
|
+
else
|
66
|
+
level = arg
|
67
|
+
end
|
68
|
+
when '--errors'
|
69
|
+
output_info = "errors"
|
70
|
+
when '--warns'
|
71
|
+
output_info = "warnings"
|
72
|
+
when '--stats'
|
73
|
+
output_info = "statistics"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# -f option or CODE arg requested
|
78
|
+
unless ARGV.length > 0 or file
|
79
|
+
puts "Missing any argument (try --help)"
|
80
|
+
exit 0
|
81
|
+
end
|
82
|
+
|
83
|
+
code = ARGV.shift
|
84
|
+
|
85
|
+
if file
|
86
|
+
puts JSCompiler.compile_file(file_path, output_info, level)
|
87
|
+
else
|
88
|
+
puts JSCompiler.compile(code, output_info, level)
|
89
|
+
end
|
data/js/compiled_code.js
ADDED
data/js/errors.js
ADDED
data/js/warnings.js
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'rubygems' # include RubyGems
|
2
|
+
gem 'activesupport' # load ActiveSupport
|
3
|
+
require 'activesupport' # include ActiveSupport
|
4
|
+
require 'active_support/core_ext/integer/inflections'
|
5
|
+
|
6
|
+
require 'json'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
module JSCompiler
|
10
|
+
|
11
|
+
# CONFIGURE this with the relative path to your javascript
|
12
|
+
# folder (typically public/javascripts in a RAILS APP)
|
13
|
+
# NO MORE NEEDED
|
14
|
+
#JAVASCRIPTS_DIR = "js/"
|
15
|
+
|
16
|
+
# Link to Google Closure Compiler service
|
17
|
+
GOOGLE_SERVICE_ADDRESS = "http://closure-compiler.appspot.com/compile"
|
18
|
+
# Default output_info parameter
|
19
|
+
DEFAULT_SERVICE = "compiled_code"
|
20
|
+
# Default compilation_level parameter
|
21
|
+
DEFAULT_LEVEL = "SIMPLE_OPTIMIZATIONS"
|
22
|
+
|
23
|
+
class << self
|
24
|
+
|
25
|
+
# Creates the <em>JSON</em> hash for the request and returns the hash to send along with the request
|
26
|
+
#
|
27
|
+
# Accepted parameters:
|
28
|
+
# * <b>code</b>: json_code parameter
|
29
|
+
# * <b>level</b>: compilation_level parameter
|
30
|
+
def create_json_request(code)
|
31
|
+
parameters = {
|
32
|
+
"code" => code,
|
33
|
+
"level" => @level,
|
34
|
+
"format" => "json",
|
35
|
+
"info" => @op
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
# Sends the JSON request <em>data</em> hash to Google service and returns its response
|
40
|
+
#
|
41
|
+
def post_to_cc(data)
|
42
|
+
post_args = {
|
43
|
+
'js_code' => data["code"],
|
44
|
+
'compilation_level' => data["level"],
|
45
|
+
'output_format' => data["format"],
|
46
|
+
'output_info' => data["info"]
|
47
|
+
}
|
48
|
+
# send the request
|
49
|
+
resp, data = Net::HTTP.post_form(URI.parse(GOOGLE_SERVICE_ADDRESS), post_args)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Reads the <em>file_name</em> file and calls compile method on it
|
53
|
+
#
|
54
|
+
# Accepted parameters:
|
55
|
+
# * <b>file_name</b>: absolute path to file
|
56
|
+
# * <b>op</b>: output_info parameter
|
57
|
+
# * <b>level</b>: compilation_level parameter
|
58
|
+
def compile_file(file_name, op, level)
|
59
|
+
# javascript_code = read_file(JAVASCRIPTS_DIR + file_name)
|
60
|
+
# resp, data = post_to_cc(create_json_request(javascript_code, op, level))
|
61
|
+
# parse_json_output(data, op)
|
62
|
+
|
63
|
+
javascript_code = read_file(file_name)
|
64
|
+
compile(javascript_code, op, level)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Compiles <em>javascript_code</em> code and returns parsed output
|
68
|
+
#
|
69
|
+
# Accepted parameters:
|
70
|
+
# * <b>javascript_code</b>: the code to compile
|
71
|
+
# * <b>op</b>: output_info parameter
|
72
|
+
# * <b>level</b>: compilation_level parameter
|
73
|
+
def compile(javascript_code, op, level)
|
74
|
+
@op = op.blank? ? DEFAULT_SERVICE : op
|
75
|
+
@level = level.blank? ? DEFAULT_LEVEL : level
|
76
|
+
|
77
|
+
resp, data = post_to_cc(create_json_request(javascript_code))
|
78
|
+
parse_json_output(data)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Calls compile method for every file in <em>dir</em> directory
|
82
|
+
#
|
83
|
+
# Accepted parameters:
|
84
|
+
# * <b>dir</b>: the directory
|
85
|
+
# * <b>op</b>: output_info parameter
|
86
|
+
# * <b>level</b>: compilation_level parameter
|
87
|
+
def compile_dir(dir, op, level)
|
88
|
+
out = String.new
|
89
|
+
Dir.entries(dir).each do |file|
|
90
|
+
if File.extname(file) == ".js"
|
91
|
+
out << "Statistics for file #{file}...\n"
|
92
|
+
out << compile_file(file, op, level) + "\n***************\n"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
return out
|
96
|
+
end
|
97
|
+
|
98
|
+
# Parses and returns JSON server <em>response</em>
|
99
|
+
#
|
100
|
+
# Accepted parameters:
|
101
|
+
# * <b>response</b>: the server response
|
102
|
+
def parse_json_output(response)
|
103
|
+
out = String.new
|
104
|
+
parsed_response = JSON.parse(response, :max_nesting => false)
|
105
|
+
#p response
|
106
|
+
|
107
|
+
if parsed_response.has_key?("serverErrors")
|
108
|
+
result = parsed_response['serverErrors']
|
109
|
+
return "Server Error: #{result[0]['error']} - Error Code: #{result[0]['code']}"
|
110
|
+
end
|
111
|
+
|
112
|
+
case @op
|
113
|
+
when "compiled_code"
|
114
|
+
out = parsed_response['compiledCode']
|
115
|
+
when "statistics"
|
116
|
+
result = parsed_response['statistics']
|
117
|
+
out = create_statistics_output(result)
|
118
|
+
else "errors"
|
119
|
+
#case for errors or warnings
|
120
|
+
begin
|
121
|
+
result = parsed_response[@op]
|
122
|
+
unless result.nil?
|
123
|
+
result.each do |message|
|
124
|
+
out = "#{message['type']}: " + message[@op.singularize] + " at line #{message['lineno']} character #{message['charno']}\n"
|
125
|
+
out << message['line'] unless message['line'].nil?
|
126
|
+
return out
|
127
|
+
end
|
128
|
+
else
|
129
|
+
return "No #{@op}"
|
130
|
+
end
|
131
|
+
rescue
|
132
|
+
out = "Error parsing JSON output...Check your output"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Reads file and returns its content
|
138
|
+
#
|
139
|
+
# Accepted parameters:
|
140
|
+
# * <b>file_name</b>: the absolute path to the file
|
141
|
+
def read_file(file_name)
|
142
|
+
begin
|
143
|
+
# content = File.open(JAVASCRIPTS_DIR + file_name).read
|
144
|
+
content = File.open(file_name).read
|
145
|
+
return content, true
|
146
|
+
rescue
|
147
|
+
out = "ERROR reading #{file_name} file"
|
148
|
+
return out, false
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Parses and returns the <em>result</em> JSON server response
|
153
|
+
#
|
154
|
+
# Accepted parameters:
|
155
|
+
# * <b>result</b>: the already parsed JSON server response
|
156
|
+
def create_statistics_output(result)
|
157
|
+
size_improvement = result['originalSize'] - result['compressedSize']
|
158
|
+
size_gzip_improvement = result['originalGzipSize'] - result['compressedGzipSize']
|
159
|
+
rate_improvement = (size_improvement * 100)/result['originalSize']
|
160
|
+
rate_gzip_improvement = (size_gzip_improvement * 100)/result['originalGzipSize']
|
161
|
+
out = "Original Size: #{result['originalSize']} bytes (#{result['originalGzipSize']} bytes gzipped) \n"
|
162
|
+
out << "Compiled Size: #{result['compressedSize']} bytes (#{result['compressedGzipSize']} bytes gzipped) \n"
|
163
|
+
out << "\t Saved #{rate_improvement}% off the original size (#{rate_gzip_improvement}% off the gzipped size)"
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
data/lib/jsc.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module JSCompiler
|
2
|
+
|
3
|
+
# :stopdoc:
|
4
|
+
# VERSION = '1.0.0'
|
5
|
+
# LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
6
|
+
# PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
7
|
+
# :startdoc:
|
8
|
+
|
9
|
+
# Returns the version string for the library.
|
10
|
+
#
|
11
|
+
def self.version
|
12
|
+
VERSION
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns the library path for the module. If any arguments are given,
|
16
|
+
# they will be joined to the end of the libray path using
|
17
|
+
# <tt>File.join</tt>.
|
18
|
+
#
|
19
|
+
def self.libpath( *args )
|
20
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the lpath for the module. If any arguments are given,
|
24
|
+
# they will be joined to the end of the path using
|
25
|
+
# <tt>File.join</tt>.
|
26
|
+
#
|
27
|
+
def self.path( *args )
|
28
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Utility method used to require all files ending in .rb that lie in the
|
32
|
+
# directory below this file that has the same name as the filename passed
|
33
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
34
|
+
# the _filename_ does not have to be equivalent to the directory.
|
35
|
+
#
|
36
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
37
|
+
dir ||= ::File.basename(fname, '.*')
|
38
|
+
search_me = ::File.expand_path(
|
39
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
40
|
+
|
41
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
42
|
+
end
|
43
|
+
|
44
|
+
end # module JSCompiler
|
45
|
+
|
46
|
+
JSCompiler.require_all_libs_relative_to(__FILE__)
|
47
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(
|
4
|
+
File.join(File.dirname(__FILE__), %w[.. .. lib google_closure_compiler]))
|
5
|
+
|
6
|
+
# Give me errors!
|
7
|
+
puts ClosureCompiler.compile_file("test_errors.js", "errors")
|
8
|
+
puts "**************\n"
|
9
|
+
|
10
|
+
# Read file and compile it!
|
11
|
+
|
12
|
+
# function passing it the relative path of the file
|
13
|
+
code_to_compile, result = ClosureCompiler.read_file('test.js')
|
14
|
+
if result
|
15
|
+
compiled_code = ClosureCompiler.compile(code_to_compile, "compiled_code")
|
16
|
+
unless compiled_code.nil?
|
17
|
+
puts compiled_code
|
18
|
+
end
|
19
|
+
else
|
20
|
+
puts "ERROR reading file\n"
|
21
|
+
end
|
22
|
+
puts "**************\n"
|
23
|
+
|
24
|
+
# Give me statistics!
|
25
|
+
code_to_compile = "function hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
26
|
+
puts ClosureCompiler.compile(code_to_compile, "statistics")
|
27
|
+
puts "**************\n"
|
28
|
+
|
29
|
+
# Compile a whole directory
|
30
|
+
js_dir = "js" # relative path to js directory
|
31
|
+
puts "Compiling a WHOLE directory...\n"
|
32
|
+
puts ClosureCompiler.compile_dir(js_dir, "statistics")
|
33
|
+
puts "**************\n"
|
34
|
+
|
35
|
+
# Execute every check on the file
|
36
|
+
puts ClosureCompiler.compile_file("js/compiled_code.js", "compiled_code")
|
37
|
+
puts "*******************"
|
38
|
+
puts ClosureCompiler.compile_file("js/compiled_code.js", "warnings")
|
39
|
+
puts "*******************"
|
40
|
+
puts ClosureCompiler.compile_file("js/compiled_code.js", "errors")
|
41
|
+
puts "*******************"
|
42
|
+
puts ClosureCompiler.compile_file("js/compiled_code.js", "statistics")
|
data/plugins/jsc.el
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
;; snippet Google Closure Compiler
|
2
|
+
(define-key global-map "\C-cJe" 'fitz-jsc-errors-current-selection)
|
3
|
+
|
4
|
+
(defun fitz-jsc-errors-current-selection ()
|
5
|
+
"run jsc for errors on current selection"
|
6
|
+
(interactive)
|
7
|
+
(let ((tmp-buffer (get-buffer-create "**fitz-cc**"))
|
8
|
+
(region-str (buffer-substring (region-beginning) (region-end)))
|
9
|
+
(cc-output (shell-command-to-string
|
10
|
+
(concat "jsc -e \""
|
11
|
+
(buffer-substring (region-beginning) (region-end)) "\""))))
|
12
|
+
(set-buffer tmp-buffer)
|
13
|
+
(goto-char (point-max))
|
14
|
+
(insert (concat "===== BEGIN =====\n"
|
15
|
+
"Selected Code:\n\t"
|
16
|
+
region-str
|
17
|
+
"\n\n"
|
18
|
+
"JSCompiler Errors:\n"
|
19
|
+
cc-output
|
20
|
+
"===== END =====\n\n")
|
21
|
+
(switch-to-buffer-other-window tmp-buffer)
|
22
|
+
(recenter (point-max)))))
|
23
|
+
|
24
|
+
(define-key global-map "\C-cJw" 'fitz-jsc-warnings-current-selection)
|
25
|
+
|
26
|
+
(defun fitz-jsc-warnings-current-selection ()
|
27
|
+
"run jsc for warnings on current selection"
|
28
|
+
(interactive)
|
29
|
+
(let ((tmp-buffer (get-buffer-create "**fitz-cc**"))
|
30
|
+
(region-str (buffer-substring (region-beginning) (region-end)))
|
31
|
+
(cc-output (shell-command-to-string
|
32
|
+
(concat "jsc -w \""
|
33
|
+
(buffer-substring (region-beginning) (region-end)) "\""))))
|
34
|
+
(set-buffer tmp-buffer)
|
35
|
+
(goto-char (point-max))
|
36
|
+
(insert (concat "===== BEGIN =====\n"
|
37
|
+
"Selected Code:\n\t"
|
38
|
+
region-str
|
39
|
+
"\n\n"
|
40
|
+
"JSCompiler Warnings:\n"
|
41
|
+
cc-output
|
42
|
+
"===== END =====\n\n")
|
43
|
+
(switch-to-buffer-other-window tmp-buffer)
|
44
|
+
(recenter (point-max)))))
|
data/spec/jsc.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
3
|
+
|
4
|
+
describe JSCompiler do
|
5
|
+
|
6
|
+
describe 'Use the web service through API' do
|
7
|
+
|
8
|
+
describe 'Put all keys in the request' do
|
9
|
+
|
10
|
+
before do
|
11
|
+
code = "function hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
12
|
+
@request = JSCompiler.create_json_request(code)
|
13
|
+
@request['level'] = "SIMPLE_OPTIMIZATIONS"
|
14
|
+
@request['info'] = "compiled_code"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should create a request with code key' do
|
18
|
+
@request['code'].should_not be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should create a request with level key' do
|
22
|
+
@request['level'].should_not be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should create a request with format key' do
|
26
|
+
@request['format'].should_not be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should create a request with info key' do
|
30
|
+
@request['info'].should_not be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should create a JSON request with all requested keys' do
|
34
|
+
["code","level","format","info"].each { |key| @request[key.intern].should }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'create a JSON compile-code request (default request)' do
|
39
|
+
before do
|
40
|
+
code = "function hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
41
|
+
@request = JSCompiler.create_json_request(code)
|
42
|
+
@request['level'] = "SIMPLE_OPTIMIZATIONS"
|
43
|
+
@request['info'] = "compiled_code"
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'with the right value for level key' do
|
47
|
+
@request['level'].should == 'SIMPLE_OPTIMIZATIONS'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'with the right value for format key' do
|
51
|
+
@request['format'].should == 'json'
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'with the right value for info key' do
|
55
|
+
@request['info'].should == 'compiled_code'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'with the right param for code key' do
|
59
|
+
@request['code'].should_not be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'compile code and get compiled code' do
|
64
|
+
before do
|
65
|
+
code = "function hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
66
|
+
@resp = JSCompiler.compile(code, "compiled_code", "SIMPLE_OPTIMIZATIONS")
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should receive the compiled code' do
|
70
|
+
@resp.should_not be_nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'compile code and find errors' do
|
75
|
+
before do
|
76
|
+
code = "functiont hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
77
|
+
@resp = JSCompiler.compile(code, "errors", "SIMPLE_OPTIMIZATIONS")
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should return the result string' do
|
81
|
+
@resp.should match(/at line/)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'compile code and find warnings' do
|
86
|
+
before do
|
87
|
+
code = "function hello(a){return; alert(\"Hello, \"+a)}hello(\"New user\");"
|
88
|
+
@resp = JSCompiler.compile(code, "warnings", "SIMPLE_OPTIMIZATIONS")
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should return the result string' do
|
92
|
+
@resp.should match(/at line/)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe 'compile code and obtain statistics' do
|
97
|
+
before do
|
98
|
+
code = "function hello(a){alert(\"Hello, \"+a)}hello(\"New user\");"
|
99
|
+
@resp = JSCompiler.compile(code, "statistics", "SIMPLE_OPTIMIZATIONS")
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should return the result string' do
|
103
|
+
@resp.should match(/Original Size:/)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe 'read .js file' do
|
109
|
+
before do
|
110
|
+
#test file
|
111
|
+
@file_name = 'js/compiled_code.js'
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should return the code without errors' do
|
115
|
+
result, value = JSCompiler.read_file(@file_name)
|
116
|
+
value.should be_true
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'compile a whole directory' do
|
121
|
+
|
122
|
+
it 'should get statistics for every file' do
|
123
|
+
js_dir = "js"
|
124
|
+
result = JSCompiler.compile_dir(js_dir, "statistics", String.new)
|
125
|
+
# suppose I have 3 files in the dir
|
126
|
+
3.times do
|
127
|
+
result.should =~ /Original Size:/
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(
|
3
|
+
File.join(File.dirname(__FILE__), %w[.. lib jsc]))
|
4
|
+
|
5
|
+
Spec::Runner.configure do |config|
|
6
|
+
# == Mock Framework
|
7
|
+
#
|
8
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
9
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
10
|
+
#
|
11
|
+
# config.mock_with :mocha
|
12
|
+
# config.mock_with :flexmock
|
13
|
+
# config.mock_with :rr
|
14
|
+
end
|
15
|
+
|
data/spec/wip_spec.rb
ADDED
data/tasks/jsc.rake
ADDED
data/test/test_jsc.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sub
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-12 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bones
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.1.0
|
24
|
+
version:
|
25
|
+
description: A simple and clear Ruby REST API to {Google Closure Compiler service}[http://code.google.com/closure/compiler/].
|
26
|
+
email: fitzkarraldo@gmail.com
|
27
|
+
executables:
|
28
|
+
- jsc
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- History.txt
|
33
|
+
- README.rdoc
|
34
|
+
- bin/jsc
|
35
|
+
files:
|
36
|
+
- .bnsignore
|
37
|
+
- .gitignore
|
38
|
+
- History.txt
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- bin/jsc
|
42
|
+
- js/compiled_code.js
|
43
|
+
- js/errors.js
|
44
|
+
- js/warnings.js
|
45
|
+
- lib/jsc.rb
|
46
|
+
- lib/jsc/closure_compiler.rb
|
47
|
+
- plugins/bin/google_closure_compiler
|
48
|
+
- plugins/jsc.el
|
49
|
+
- spec/jsc.rb
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
- spec/wip_spec.rb
|
52
|
+
- tasks/jsc.rake
|
53
|
+
- test/test_jsc.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/sub/jsc
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: jsc
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Google Closure Compiler Ruby REST API
|
83
|
+
test_files:
|
84
|
+
- test/test_jsc.rb
|