Skip to content
This repository was archived by the owner on Jan 9, 2019. It is now read-only.

Commit 92a6272

Browse files
committed
1. Remove unnecessary files
2. Update Compile to allow regular lessc arguments 3. Update LessCompiler to use default rhino less/lessc from less project 4. Update tests
1 parent 547ebbc commit 92a6272

11 files changed

Lines changed: 3699 additions & 22075 deletions

File tree

src/main/java/org/lesscss/Compile.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@
55

66
import java.io.File;
77
import java.util.Arrays;
8+
import java.util.List;
89

910
public class Compile {
1011

1112
private static final LessLogger logger = LessLoggerFactory.getLogger( Compile.class );
1213

1314
public static void main(String[] args) throws Exception {
1415
if( args.length < 1 ) {
15-
logger.info("usage: org.lesscss.Compile <less_filename>");
16+
logger.info("usage: org.lesscss.Compile <args> <less_filename>");
1617
System.exit(-1);
1718
}
19+
20+
List<String> argList = Arrays.asList(args);
21+
String fileName = argList.get(argList.size() - 1);
22+
argList = argList.subList(0, argList.size() - 1);
1823

19-
File output = new File( args[0] + ".css" );
24+
File output = new File( fileName + ".css" );
2025
logger.info("Compiler output = %s", output.getCanonicalPath() );
2126

2227
long start = System.currentTimeMillis();
23-
LessCompiler lessCompiler = new LessCompiler(Arrays.asList("-ru"));
28+
LessCompiler lessCompiler = new LessCompiler(argList);
2429

25-
lessCompiler.compile( new File( args[0] ), output );
30+
lessCompiler.compile( new File( fileName ), output );
2631

2732
long duration = System.currentTimeMillis() - start;
2833
logger.info("Done. %,d ms", duration);

src/main/java/org/lesscss/LessCompiler.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import java.io.ByteArrayOutputStream;
1818
import java.io.File;
1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.io.InputStreamReader;
2122
import java.io.OutputStreamWriter;
2223
import java.io.PrintStream;
2324
import java.io.PrintWriter;
25+
import java.io.SequenceInputStream;
2426
import java.net.URL;
2527
import java.util.ArrayList;
2628
import java.util.Arrays;
@@ -68,7 +70,8 @@ public class LessCompiler {
6870

6971
private static final LessLogger logger = LessLoggerFactory.getLogger(LessCompiler.class);
7072

71-
private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.5.1.js");
73+
private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.6.1.js");
74+
private URL lesscJs = LessCompiler.class.getClassLoader().getResource("META-INF/lessc-rhino-1.6.1.js");
7275
private List<URL> customJs = Collections.emptyList();
7376
private List<String> options = Collections.emptyList();
7477
private Boolean compress = null;
@@ -144,6 +147,28 @@ public synchronized void setLessJs(URL lessJs) {
144147
this.lessJs = lessJs;
145148
}
146149

150+
/**
151+
* Returns the LESSC JavaScript file used by the compiler.
152+
* COMPILE_STRING
153+
* @return The LESSC JavaScript file used by the compiler.
154+
*/
155+
public URL getLesscJs() {
156+
return lesscJs;
157+
}
158+
159+
/**
160+
* Sets the LESSC JavaScript file used by the compiler.
161+
* Must be set before {@link #init()} is called.
162+
*
163+
* @param lesscJs LESSC JavaScript file used by the compiler.
164+
*/
165+
public synchronized void setLesscJs(URL lesscJs) {
166+
if (scope != null) {
167+
throw new IllegalStateException("This method can only be called before init()");
168+
}
169+
this.lesscJs = lesscJs;
170+
}
171+
147172
/**
148173
* Returns the custom JavaScript files used by the compiler.
149174
*
@@ -249,21 +274,24 @@ public synchronized void init() {
249274
out = new ByteArrayOutputStream();
250275
global.setOut(new PrintStream(out));
251276

252-
// Load the compiler into a function we can run
253-
compiler = (Function) cx.compileReader(new InputStreamReader(lessJs.openConnection().getInputStream()), lessJs.toString(), 1, null);
277+
// Combine all of the streams (less, custom, lessc) into one big stream
278+
List<InputStream> streams = new ArrayList<InputStream>();
254279

255-
List<URL> jsUrls = new ArrayList<URL>();
256-
jsUrls.addAll( customJs );
257-
258-
// load any custom JS
259-
for(URL url : jsUrls) {
260-
InputStreamReader inputStreamReader = new InputStreamReader(url.openConnection().getInputStream());
261-
try{
262-
cx.evaluateReader(scope, inputStreamReader, url.toString(), 1, null);
263-
}finally{
264-
inputStreamReader.close();
265-
}
280+
// less should be first
281+
streams.add(lessJs.openConnection().getInputStream());
282+
283+
// then the custom js so it has a chance to add any hooks
284+
for(URL url : customJs) {
285+
streams.add(url.openConnection().getInputStream());
266286
}
287+
288+
// then the lessc so we can do the compile
289+
streams.add(lesscJs.openConnection().getInputStream());
290+
291+
InputStreamReader reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(streams)));
292+
293+
// Load the streams into a function we can run
294+
compiler = (Function) cx.compileReader(reader, lessJs.toString(), 1, null);
267295

268296
}
269297
catch (Exception e) {

0 commit comments

Comments
 (0)