This repository was archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathNodeJsLessCompiler.java
More file actions
173 lines (156 loc) · 5.62 KB
/
NodeJsLessCompiler.java
File metadata and controls
173 lines (156 loc) · 5.62 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lesscss.mojo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;
import org.lesscss.LessException;
import org.lesscss.LessSource;
public class NodeJsLessCompiler {
private static final List<String> resources = Arrays.asList(
"lessc.js",
"less/browser.js",
"less/colors.js",
"less/functions.js",
"less/index.js",
"less/lessc_helper.js",
"less/parser.js",
"less/rhino.js",
"less/tree.js",
"less/tree/alpha.js",
"less/tree/anonymous.js",
"less/tree/assignment.js",
"less/tree/call.js",
"less/tree/color.js",
"less/tree/comment.js",
"less/tree/condition.js",
"less/tree/dimension.js",
"less/tree/directive.js",
"less/tree/element.js",
"less/tree/expression.js",
"less/tree/import.js",
"less/tree/javascript.js",
"less/tree/keyword.js",
"less/tree/media.js",
"less/tree/mixin.js",
"less/tree/operation.js",
"less/tree/paren.js",
"less/tree/quoted.js",
"less/tree/ratio.js",
"less/tree/rule.js",
"less/tree/ruleset.js",
"less/tree/selector.js",
"less/tree/unicode-descriptor.js",
"less/tree/url.js",
"less/tree/value.js",
"less/tree/variable.js");
private final Log log;
private final boolean compress;
private final String encoding;
private final File tempDir;
private final File nodeExecutable;
public NodeJsLessCompiler(File nodeExecutable, boolean compress, String encoding, Log log) throws IOException {
this.nodeExecutable = nodeExecutable;
this.compress = compress;
this.encoding = encoding;
this.log = log;
tempDir = createTempDir("lessc");
new File(tempDir, "less/tree").mkdirs();
for (String resource : resources) {
InputStream in = NodeJsLessCompiler.class.getClassLoader()
.getResourceAsStream("org/lesscss/mojo/js/" + resource);
FileOutputStream out = new FileOutputStream(new File(tempDir, resource));
IOUtils.copy(in, out);
in.close();
out.close();
}
}
public void close() {
for (String resource : resources) {
File tempFile = new File(tempDir, resource);
if (!tempFile.delete()) {
log.warn("Could not delete temp file: " + tempFile.getAbsolutePath());
}
}
File lessSubdir = new File(tempDir, "less");
File treeSubdir = new File(lessSubdir, "tree");
if (!treeSubdir.delete()) {
log.warn("Could not delete temp dir: " + treeSubdir.getAbsolutePath());
}
if (!lessSubdir.delete()) {
log.warn("Could not delete temp dir: " + lessSubdir.getAbsolutePath());
}
if (!tempDir.delete()) {
log.warn("Could not delete temp dir: " + tempDir.getAbsolutePath());
}
}
public void compile(LessSource input, File output, boolean force)
throws IOException, LessException, InterruptedException {
if (force || !output.exists() || output.lastModified() < input.getLastModifiedIncludingImports()) {
String data = compile(input.getNormalizedContent());
FileUtils.writeStringToFile(output, data, encoding);
}
}
private String compile(String input) throws LessException, IOException, InterruptedException {
long start = System.currentTimeMillis();
File inputFile = File.createTempFile("lessc-input-", ".less");
FileOutputStream out = new FileOutputStream(inputFile);
IOUtils.write(input, out);
out.close();
File outputFile = File.createTempFile("lessc-output-", ".css");
File lesscJsFile = new File(tempDir, "lessc.js");
ProcessBuilder pb = new ProcessBuilder(nodeExecutable.getAbsolutePath(), lesscJsFile.getAbsolutePath(),
inputFile.getAbsolutePath(), outputFile.getAbsolutePath(), String.valueOf(compress));
pb.redirectErrorStream(true);
Process process = pb.start();
IOUtils.copy(process.getInputStream(), System.out);
int exitStatus = process.waitFor();
FileInputStream in = new FileInputStream(outputFile);
String result = IOUtils.toString(in);
in.close();
if (!inputFile.delete()) {
log.warn("Could not delete temp file: " + inputFile.getAbsolutePath());
}
if (!outputFile.delete()) {
log.warn("Could not delete temp file: " + outputFile.getAbsolutePath());
}
if (exitStatus != 0) {
throw new LessException(result, null);
}
log.debug("Finished compilation of LESS source in " + (System.currentTimeMillis() - start) + " ms.");
return result;
}
// copied from guava's Files.createTempDir, with added prefix
private static File createTempDir(String prefix) {
final int tempDirAttempts = 10000;
File baseDir = new File(System.getProperty("java.io.tmpdir"));
String baseName = prefix + "-" + System.currentTimeMillis() + "-";
for (int counter = 0; counter < tempDirAttempts; counter++) {
File tempDir = new File(baseDir, baseName + counter);
if (tempDir.mkdir()) {
return tempDir;
}
}
throw new IllegalStateException("Failed to create directory within " + tempDirAttempts
+ " attempts (tried " + baseName + "0 to " + baseName + (tempDirAttempts - 1) + ')');
}
}