jsc 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/README.rdoc +20 -10
- data/Rakefile +2 -1
- data/bin/jsc +47 -16
- data/lib/jsc.rb +1 -1
- data/lib/jsc/closure_compiler.rb +33 -20
- data/plugins/jsc.el +2 -2
- data/spec/jsc_spec.rb +52 -21
- metadata +3 -7
- data/js/temp.js +0 -1
- data/lib/jsc/clo2.rb +0 -169
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.2.0 / 2009-12-14
|
2
|
+
|
3
|
+
added -a, -v, -c options
|
4
|
+
now compile file by default (-f option removed)
|
5
|
+
If no output_info options are passed to bin first check for errors and, if not any, return compiled code
|
6
|
+
|
1
7
|
== 0.1.3 / 2009-12-14
|
2
8
|
|
3
9
|
BUG fix: now show every error in a file, not just the first
|
data/README.rdoc
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
jsc, JavaScript Compiler
|
2
|
-
by
|
2
|
+
by Davide Saurino
|
3
3
|
http://github.com/sub/jsc
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
@@ -27,27 +27,29 @@ Check {Google API Reference}[http://code.google.com/intl/it-IT/closure/compiler/
|
|
27
27
|
|
28
28
|
== SYNOPSIS:
|
29
29
|
|
30
|
-
|
30
|
+
Get compiled code, if no errors are found:
|
31
31
|
|
32
|
-
ruby bin/jsc
|
32
|
+
ruby bin/jsc js/compiled_code.js
|
33
33
|
|
34
34
|
Compile a file, check for errors:
|
35
35
|
|
36
|
-
ruby bin/jsc
|
36
|
+
ruby bin/jsc js/errors.js -e
|
37
37
|
|
38
38
|
Compile a file, check for warnings:
|
39
39
|
|
40
|
-
ruby bin/jsc
|
40
|
+
ruby bin/jsc js/warnings.js -w
|
41
41
|
|
42
42
|
Compile a file and get compression stats:
|
43
43
|
|
44
|
-
ruby bin/jsc
|
44
|
+
ruby bin/jsc js/compiled_code.js -s
|
45
45
|
|
46
46
|
Compile a piece of code, check for errors:
|
47
47
|
|
48
|
-
ruby bin/jsc -e "function("
|
48
|
+
ruby bin/jsc -e -c "function("
|
49
49
|
|
50
|
-
|
50
|
+
Compile a file and get compiled code if no errors or warnings are found:
|
51
|
+
|
52
|
+
ruby bin/jsc js/compiled_code.js -a
|
51
53
|
|
52
54
|
== EMACS SNIPPETS
|
53
55
|
|
@@ -71,8 +73,16 @@ jsc requires the {bones}[http://gemcutter.org/gems/bones] gem.
|
|
71
73
|
|
72
74
|
If you have gemcutter in your gem sources, run:
|
73
75
|
|
74
|
-
gem install jsc
|
76
|
+
[sudo] gem install jsc
|
75
77
|
|
76
78
|
== LICENSE:
|
77
79
|
|
78
|
-
|
80
|
+
(The MIT License)
|
81
|
+
|
82
|
+
Copyright © 2009 Davide Saurino
|
83
|
+
|
84
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
85
|
+
|
86
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
87
|
+
|
88
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
data/bin/jsc
CHANGED
@@ -15,10 +15,10 @@ Look at http://gemcutter.org/gems/jsc for more info.
|
|
15
15
|
|
16
16
|
== Usage
|
17
17
|
|
18
|
-
jsc [options] ...
|
18
|
+
jsc [options] ... ARG
|
19
19
|
|
20
|
-
--
|
21
|
-
compile
|
20
|
+
--code x, -c x:
|
21
|
+
compile code x
|
22
22
|
|
23
23
|
--errors, -e:
|
24
24
|
check for errors
|
@@ -29,38 +29,55 @@ jsc [options] ... CODE
|
|
29
29
|
--stats, -s:
|
30
30
|
get statistics for compiled code
|
31
31
|
|
32
|
+
--all, -a:
|
33
|
+
execute every check for this code
|
34
|
+
|
32
35
|
--level value, -l value:
|
33
36
|
compile with level value
|
34
37
|
If this option is not supplied, SIMPLE_OPTIMIZATIONS will be used
|
35
38
|
(look at Google API for accepted values).
|
36
39
|
|
40
|
+
--version, -v:
|
41
|
+
show version
|
42
|
+
|
37
43
|
--help, -h:
|
38
44
|
this help
|
39
45
|
|
40
|
-
|
41
|
-
|
46
|
+
ARG: A path to a javascript file or the code that will be compiled.
|
47
|
+
|
48
|
+
If one of the following options is not specified
|
49
|
+
|
50
|
+
-e, -w, -s, -a
|
51
|
+
|
52
|
+
the code will be first compiled for errors and only if no errors are found
|
53
|
+
it will return the compiled code
|
42
54
|
EOU
|
43
55
|
|
44
56
|
opts = GetoptLong.new(
|
45
57
|
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
46
|
-
[ '--
|
58
|
+
[ '--version', '-v', GetoptLong::NO_ARGUMENT ],
|
59
|
+
[ '--code', '-c', GetoptLong::OPTIONAL_ARGUMENT ],
|
47
60
|
[ '--errors', '-e', GetoptLong::NO_ARGUMENT ],
|
48
61
|
[ '--warns', '-w', GetoptLong::NO_ARGUMENT ],
|
49
62
|
[ '--stats', '-s', GetoptLong::NO_ARGUMENT ],
|
63
|
+
[ '--all', '-a', GetoptLong::NO_ARGUMENT ],
|
50
64
|
[ '--level','-l', GetoptLong::REQUIRED_ARGUMENT ]
|
51
65
|
)
|
52
66
|
|
53
|
-
file =
|
54
|
-
output_info, level,
|
67
|
+
file = true
|
68
|
+
output_info, level, code = ""
|
55
69
|
|
56
70
|
opts.each do |opt, arg|
|
57
71
|
case opt
|
58
72
|
when '--help'
|
59
73
|
puts USAGE_PREAMBLE
|
60
74
|
exit 0
|
61
|
-
when '--
|
62
|
-
|
63
|
-
|
75
|
+
when '--version'
|
76
|
+
puts JSCompiler::VERSION
|
77
|
+
exit 0
|
78
|
+
when '--code'
|
79
|
+
file = false
|
80
|
+
code = arg
|
64
81
|
when '--level'
|
65
82
|
if arg == ''
|
66
83
|
level = "SIMPLE_OPTIMIZATIONS"
|
@@ -73,14 +90,28 @@ opts.each do |opt, arg|
|
|
73
90
|
output_info = "warnings"
|
74
91
|
when '--stats'
|
75
92
|
output_info = "statistics"
|
93
|
+
when '--all'
|
94
|
+
output_info = "all"
|
76
95
|
end
|
77
96
|
end
|
78
97
|
|
79
98
|
# -f option or CODE arg requested
|
80
|
-
unless ARGV.length > 0 or
|
81
|
-
|
82
|
-
|
99
|
+
unless ARGV.length > 0 or code
|
100
|
+
puts "Missing any argument (try --help)"
|
101
|
+
exit 0
|
83
102
|
end
|
84
103
|
|
85
|
-
arg =
|
86
|
-
|
104
|
+
arg = code.blank? ? ARGV.shift : code
|
105
|
+
|
106
|
+
if output_info.eql?("all")
|
107
|
+
puts JSCompiler.full_compile(arg, file, level)
|
108
|
+
elsif output_info.blank?
|
109
|
+
errors_output = JSCompiler.compile(arg, file, "errors", level)
|
110
|
+
unless errors_output.eql?("No errors")
|
111
|
+
puts errors_output
|
112
|
+
else
|
113
|
+
puts JSCompiler.compile(arg, file, "", level)
|
114
|
+
end
|
115
|
+
else
|
116
|
+
puts JSCompiler.compile(arg, file, output_info, level)
|
117
|
+
end
|
data/lib/jsc.rb
CHANGED
data/lib/jsc/closure_compiler.rb
CHANGED
@@ -57,26 +57,35 @@ module JSCompiler
|
|
57
57
|
@level = level.blank? ? DEFAULT_LEVEL : level
|
58
58
|
value = true
|
59
59
|
|
60
|
-
if is_file
|
61
|
-
js_code, value = read_file(arg)
|
62
|
-
else
|
63
|
-
js_code = arg
|
64
|
-
end
|
65
|
-
# js_code = is_file ? read_file(arg) : arg
|
66
|
-
|
67
|
-
unless value
|
68
|
-
return "Error reading file #{arg}"
|
69
|
-
end
|
70
|
-
|
71
60
|
begin
|
61
|
+
if is_file
|
62
|
+
js_code = read_file(arg)
|
63
|
+
else
|
64
|
+
js_code = arg
|
65
|
+
end
|
72
66
|
resp, data = post_to_cc(create_json_request(js_code))
|
73
|
-
rescue
|
74
|
-
return
|
67
|
+
rescue StandardError => e
|
68
|
+
return e
|
75
69
|
end
|
76
70
|
|
77
71
|
parse_json_output(data)
|
78
72
|
end
|
79
73
|
|
74
|
+
# Compiles a file or a piece of code and returns parsed output
|
75
|
+
# if no errors or warnings are found
|
76
|
+
#
|
77
|
+
# Accepted parameters:
|
78
|
+
# * <b>arg</b>: the code or the file path to compile
|
79
|
+
# * <b>file</b>: 0 => arg is code
|
80
|
+
# 1 => arg is a file path
|
81
|
+
# * <b>level</b>: compilation_level parameter
|
82
|
+
def full_compile(arg, file, level)
|
83
|
+
['errors', 'warnings','compiled_code'].each do |x|
|
84
|
+
str = JSCompiler.compile(arg, file, x, level)
|
85
|
+
return str unless str.eql?("No " + x)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
80
89
|
# Calls compile method for every file in <em>dir</em> directory
|
81
90
|
#
|
82
91
|
# Accepted parameters:
|
@@ -84,7 +93,7 @@ module JSCompiler
|
|
84
93
|
# * <b>op</b>: output_info parameter
|
85
94
|
# * <b>level</b>: compilation_level parameter
|
86
95
|
def compile_dir(dir, op, level)
|
87
|
-
out =
|
96
|
+
out = ""
|
88
97
|
Dir.entries(dir).each do |file|
|
89
98
|
if File.extname(file) == ".js"
|
90
99
|
out << "Statistics for file #{file}...\n"
|
@@ -99,7 +108,7 @@ module JSCompiler
|
|
99
108
|
# Accepted parameters:
|
100
109
|
# * <b>response</b>: the server response
|
101
110
|
def parse_json_output(response)
|
102
|
-
out =
|
111
|
+
out = ""
|
103
112
|
parsed_response = JSON.parse(response, :max_nesting => false)
|
104
113
|
|
105
114
|
if parsed_response.has_key?("serverErrors")
|
@@ -118,9 +127,14 @@ module JSCompiler
|
|
118
127
|
begin
|
119
128
|
result = parsed_response[@op]
|
120
129
|
unless result.nil?
|
130
|
+
num = result.size
|
131
|
+
out << "You've got #{result.size} #{@op}\n"
|
132
|
+
i = 0
|
121
133
|
result.each do |message|
|
122
|
-
|
123
|
-
out <<
|
134
|
+
i += 1
|
135
|
+
out << "\n#{@op.singularize.capitalize} n.#{i}\n"
|
136
|
+
out << "\t#{message['type']}: " + message[@op.singularize] + " at line #{message['lineno']} character #{message['charno']}\n"
|
137
|
+
out << "\t" + message['line'] + "\n" unless message['line'].nil?
|
124
138
|
end
|
125
139
|
return out
|
126
140
|
else
|
@@ -139,10 +153,9 @@ module JSCompiler
|
|
139
153
|
def read_file(file_name)
|
140
154
|
begin
|
141
155
|
content = File.open(file_name).read
|
142
|
-
return content
|
156
|
+
return content
|
143
157
|
rescue
|
144
|
-
|
145
|
-
return out, false
|
158
|
+
raise
|
146
159
|
end
|
147
160
|
end
|
148
161
|
|
data/plugins/jsc.el
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
(let ((tmp-buffer (get-buffer-create "**fitz-cc**"))
|
8
8
|
(region-str (buffer-substring (region-beginning) (region-end)))
|
9
9
|
(cc-output (shell-command-to-string
|
10
|
-
(concat "jsc -e \""
|
10
|
+
(concat "jsc -e -c \""
|
11
11
|
(buffer-substring (region-beginning) (region-end)) "\""))))
|
12
12
|
(set-buffer tmp-buffer)
|
13
13
|
(goto-char (point-max))
|
@@ -29,7 +29,7 @@
|
|
29
29
|
(let ((tmp-buffer (get-buffer-create "**fitz-cc**"))
|
30
30
|
(region-str (buffer-substring (region-beginning) (region-end)))
|
31
31
|
(cc-output (shell-command-to-string
|
32
|
-
(concat "jsc -w \""
|
32
|
+
(concat "jsc -w -c \""
|
33
33
|
(buffer-substring (region-beginning) (region-end)) "\""))))
|
34
34
|
(set-buffer tmp-buffer)
|
35
35
|
(goto-char (point-max))
|
data/spec/jsc_spec.rb
CHANGED
@@ -96,18 +96,17 @@ describe JSCompiler do
|
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'should return the result string' do
|
99
|
-
@resp.should match(/
|
99
|
+
@resp.should match(/Error n./)
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
103
|
describe 'compile code and find warnings' do
|
104
104
|
before do
|
105
105
|
@resp = JSCompiler.compile(WARNING_CODE, false, "warnings", "SIMPLE_OPTIMIZATIONS")
|
106
|
-
puts @resp
|
107
106
|
end
|
108
107
|
|
109
108
|
it 'should return the result string' do
|
110
|
-
@resp.should match(/
|
109
|
+
@resp.should match(/Warning n./)
|
111
110
|
end
|
112
111
|
end
|
113
112
|
|
@@ -121,7 +120,40 @@ describe JSCompiler do
|
|
121
120
|
end
|
122
121
|
end
|
123
122
|
end
|
124
|
-
|
123
|
+
|
124
|
+
describe 'FULL code compile' do
|
125
|
+
describe 'without errors or warnings' do
|
126
|
+
before do
|
127
|
+
@resp = JSCompiler.full_compile(COMPILE_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should receive the compiled code' do
|
131
|
+
@resp.should_not be_nil
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'and get errors' do
|
136
|
+
before do
|
137
|
+
@resp = JSCompiler.full_compile(ERROR_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should return the result string' do
|
141
|
+
@resp.should match(/Error n./)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe 'and get warnings' do
|
146
|
+
before do
|
147
|
+
@resp = JSCompiler.full_compile(WARNING_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should return the result string' do
|
151
|
+
@resp.should match(/Warning n./)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
125
157
|
describe 'read .js file' do
|
126
158
|
before do
|
127
159
|
#test file
|
@@ -130,26 +162,25 @@ describe JSCompiler do
|
|
130
162
|
end
|
131
163
|
|
132
164
|
it 'should return the code without errors' do
|
133
|
-
|
134
|
-
value.should be_true
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'should return error if file not found' do
|
138
|
-
result, value = JSCompiler.read_file(@file_not_found)
|
139
|
-
result.should match(/ERROR reading /)
|
165
|
+
lambda { JSCompiler.read_file(@file_name) }.should_not raise_error
|
140
166
|
end
|
141
|
-
end
|
142
|
-
|
143
|
-
describe 'compile a whole directory' do
|
144
167
|
|
145
|
-
it 'should
|
146
|
-
|
147
|
-
result = JSCompiler.compile_dir(js_dir, "statistics", String.new)
|
148
|
-
# suppose I have 3 files in the dir
|
149
|
-
3.times do
|
150
|
-
result.should =~ /Original Size:/
|
151
|
-
end
|
168
|
+
it 'should raise exception if file not found' do
|
169
|
+
lambda { JSCompiler.read_file(@file_not_found) }.should raise_error
|
152
170
|
end
|
153
171
|
end
|
172
|
+
|
173
|
+
# TODO
|
174
|
+
# describe 'compile a whole directory' do
|
175
|
+
|
176
|
+
# it 'should get statistics for every file' do
|
177
|
+
# js_dir = "js"
|
178
|
+
# result = JSCompiler.compile_dir(js_dir, "statistics", "")
|
179
|
+
# # suppose I have 3 files in the dir
|
180
|
+
# 3.times do
|
181
|
+
# result.should =~ /Original Size:/
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
# end
|
154
185
|
|
155
186
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sub
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-25 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -25,30 +25,26 @@ dependencies:
|
|
25
25
|
description: Simple Ruby API to Google Closure Compiler Web service.
|
26
26
|
email: fitzkarraldo@gmail.com
|
27
27
|
executables:
|
28
|
-
- "#jsc#"
|
29
28
|
- jsc
|
30
29
|
extensions: []
|
31
30
|
|
32
31
|
extra_rdoc_files:
|
33
32
|
- History.txt
|
34
33
|
- README.rdoc
|
35
|
-
- bin/#jsc#
|
36
34
|
- bin/jsc
|
37
35
|
files:
|
38
36
|
- .gitignore
|
39
37
|
- History.txt
|
40
38
|
- README.rdoc
|
41
39
|
- Rakefile
|
42
|
-
- bin/#jsc#
|
43
40
|
- bin/jsc
|
44
41
|
- js/compiled_code.js
|
45
42
|
- js/errors.js
|
46
|
-
- js/temp.js
|
47
43
|
- js/warnings.js
|
48
44
|
- lib/jsc.rb
|
49
|
-
- lib/jsc/clo2.rb
|
50
45
|
- lib/jsc/closure_compiler.rb
|
51
46
|
- plugins/jsc.el
|
47
|
+
- spec/#wip_spec.rb#
|
52
48
|
- spec/jsc_spec.rb
|
53
49
|
- spec/spec_helper.rb
|
54
50
|
- spec/wip_spec.rb
|
data/js/temp.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
function hello(a){return; alert(\"Hello, \"+a)}hello(\"New user\")
|
data/lib/jsc/clo2.rb
DELETED
@@ -1,169 +0,0 @@
|
|
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>req</b>: the code to compile
|
71
|
-
# * <b>op</b>: output_info parameter
|
72
|
-
# * <b>level</b>: compilation_level parameter
|
73
|
-
def compile(req, is_file, op, level)
|
74
|
-
@op = op.blank? ? DEFAULT_SERVICE : op
|
75
|
-
@level = level.blank? ? DEFAULT_LEVEL : level
|
76
|
-
|
77
|
-
js_code = is_file ? read_file(qualcosa) : qualcosa
|
78
|
-
resp, data = post_to_cc(create_json_request(js_code))
|
79
|
-
parse_json_output(data)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Calls compile method for every file in <em>dir</em> directory
|
83
|
-
#
|
84
|
-
# Accepted parameters:
|
85
|
-
# * <b>dir</b>: the directory
|
86
|
-
# * <b>op</b>: output_info parameter
|
87
|
-
# * <b>level</b>: compilation_level parameter
|
88
|
-
def compile_dir(dir, op, level)
|
89
|
-
out = String.new
|
90
|
-
Dir.entries(dir).each do |file|
|
91
|
-
if File.extname(file) == ".js"
|
92
|
-
out << "Statistics for file #{file}...\n"
|
93
|
-
out << compile_file(file, op, level) + "\n***************\n"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
return out
|
97
|
-
end
|
98
|
-
|
99
|
-
# Parses and returns JSON server <em>response</em>
|
100
|
-
#
|
101
|
-
# Accepted parameters:
|
102
|
-
# * <b>response</b>: the server response
|
103
|
-
def parse_json_output(response)
|
104
|
-
out = String.new
|
105
|
-
parsed_response = JSON.parse(response, :max_nesting => false)
|
106
|
-
#p response
|
107
|
-
|
108
|
-
if parsed_response.has_key?("serverErrors")
|
109
|
-
result = parsed_response['serverErrors']
|
110
|
-
return "Server Error: #{result[0]['error']} - Error Code: #{result[0]['code']}"
|
111
|
-
end
|
112
|
-
|
113
|
-
case @op
|
114
|
-
when "compiled_code"
|
115
|
-
out = parsed_response['compiledCode']
|
116
|
-
when "statistics"
|
117
|
-
result = parsed_response['statistics']
|
118
|
-
out = create_statistics_output(result)
|
119
|
-
else "errors"
|
120
|
-
#case for errors or warnings
|
121
|
-
begin
|
122
|
-
result = parsed_response[@op]
|
123
|
-
unless result.nil?
|
124
|
-
result.each do |message|
|
125
|
-
out = "#{message['type']}: " + message[@op.singularize] + " at line #{message['lineno']} character #{message['charno']}\n"
|
126
|
-
out << message['line'] unless message['line'].nil?
|
127
|
-
return out
|
128
|
-
end
|
129
|
-
else
|
130
|
-
return "No #{@op}"
|
131
|
-
end
|
132
|
-
rescue
|
133
|
-
out = "Error parsing JSON output...Check your output"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
# Reads file and returns its content
|
139
|
-
#
|
140
|
-
# Accepted parameters:
|
141
|
-
# * <b>file_name</b>: the absolute path to the file
|
142
|
-
def read_file(file_name)
|
143
|
-
begin
|
144
|
-
# content = File.open(JAVASCRIPTS_DIR + file_name).read
|
145
|
-
content = File.open(file_name).read
|
146
|
-
return content, true
|
147
|
-
rescue
|
148
|
-
out = "ERROR reading #{file_name} file"
|
149
|
-
return out, false
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Parses and returns the <em>result</em> JSON server response
|
154
|
-
#
|
155
|
-
# Accepted parameters:
|
156
|
-
# * <b>result</b>: the already parsed JSON server response
|
157
|
-
def create_statistics_output(result)
|
158
|
-
size_improvement = result['originalSize'] - result['compressedSize']
|
159
|
-
size_gzip_improvement = result['originalGzipSize'] - result['compressedGzipSize']
|
160
|
-
rate_improvement = (size_improvement * 100)/result['originalSize']
|
161
|
-
rate_gzip_improvement = (size_gzip_improvement * 100)/result['originalGzipSize']
|
162
|
-
out = "Original Size: #{result['originalSize']} bytes (#{result['originalGzipSize']} bytes gzipped) \n"
|
163
|
-
out << "Compiled Size: #{result['compressedSize']} bytes (#{result['compressedGzipSize']} bytes gzipped) \n"
|
164
|
-
out << "\t Saved #{rate_improvement}% off the original size (#{rate_gzip_improvement}% off the gzipped size)"
|
165
|
-
end
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|