-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
130 lines (119 loc) · 3.72 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* a (rhino) build script - compiles the .js file into a minified version
* using (google's) closure compiler REST API.
*
* `rhino -f build.js`
*/
if (typeof java === "undefined") {
throw "this script requires rhino/ringo (java) to run !";
}
importPackage(java.lang);
function println(msg) {
System.out.println(msg);
}
/**
* Read a file.
* @param path
* @returns content
*/
function readFile(path) {
importPackage(java.io);
var file = new File(path);
var reader = new BufferedReader(new FileReader(file));
var line, content = [];
while ((line = reader.readLine()) != null) {
content.push(line);
}
reader.close();
return content.join('\n');
}
/**
* POST data to an url.
* @param url
* @param data
* @returns response
*/
var responseHeaders = null;
function postData(url, data) {
importPackage(java.io, java.net);
var connection = new URL(url).openConnection();
connection.setRequestMethod('POST');
connection.addRequestProperty('Content-type', 'application/x-www-form-urlencoded');
connection.setDoOutput(true);
var outStream = connection.getOutputStream();
var dataBuffer = new java.lang.String(data).getBytes('UTF-8');
outStream.write(dataBuffer, 0, dataBuffer.length);
outStream.close();
var c = connection;
responseHeaders = {};
for ( var i=1; i<c.getHeaderFields().size(); i++ ) {
responseHeaders[c.getHeaderFieldKey(i)] = c.getHeaderField(i);
}
var reader = new BufferedReader(
new InputStreamReader( connection.getInputStream() ) );
var line, response = [];
while ((line = reader.readLine()) != null) {
response.push(line);
}
response = response.join('\n');
println('HTTP response: ' + connection.getResponseCode() +
' ' + connection.getResponseMessage() +
' (size = ' + response.length + ')');
return response;
}
function printResponseHeaders() {
for (var prop in responseHeaders) {
println(prop + ' = ' + responseHeaders[prop]);
}
}
/**
* Encode the given content.
* @returns encoded content
*/
function encodeContent(content) {
if (typeof content === 'string') {
return encodeURI(content);
}
if (typeof content !== 'object') {
throw 'could not encode content of type: ' + (typeof content);
}
var prop, contentMap = [];
for ( prop in content ) {
contentMap.push( encodeURIComponent(prop) + '=' + encodeURIComponent(content[prop]) );
}
return contentMap.join('&');
}
/**
* Validate the received response from the compiler.
*/
function validateResponse(response) {
var jResponse = new java.lang.String(response).trim();
if ( jResponse.length() == 0 ) {
printResponseHeaders();
throw 'the HTTP response was empty !';
}
if ( jResponse.startsWith('Error(') ) {
println(response);
throw 'compiler returned an error ...';
}
}
var SCRIPT_NAME = 'script';
var SCRIPT_FILE = SCRIPT_NAME + '.js';
var SCRIPT_MIN_FILE = SCRIPT_NAME + '.min.js';
var COMPILER_URL = 'http://closure-compiler.appspot.com/compile';
var jsContent = readFile(SCRIPT_FILE);
var compilerOptions = {
js_code: jsContent,
output_info: 'compiled_code',
output_format: 'text',
compilation_level: 'SIMPLE_OPTIMIZATIONS' // 'ADVANCED_OPTIMIZATIONS'
};
var jsMinified = postData( COMPILER_URL, encodeContent(compilerOptions) );
validateResponse( jsMinified );
var minFile = new File(SCRIPT_MIN_FILE);
var minFileWriter = new FileWriter(minFile, false);
minFileWriter.write(jsMinified, 0, jsMinified.length);
minFileWriter.close();
println("js output written to: '" + minFile + "'" +
' (size reduced from ' + jsContent.length +
' bytes to ' + jsMinified.length + ' bytes)');