Skip to content
This repository was archived by the owner on Mar 15, 2021. It is now read-only.

Commit af59db7

Browse files
committed
#3 cleanup lessc-rhino, add first version of LessCompiler, rename Less175 to NativeLess
1 parent 5b38426 commit af59db7

18 files changed

+785
-389
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,26 @@ You can download the library from [this page](http://lesscss-compiler.projects.g
2424
or using various [dependency management tools](http://lesscss-compiler.projects.gabrys.biz/LATEST/dependency-info.html).
2525

2626
# Concept
27-
The idea for the `Less175Compiler` class was based on the [lesscss-java](https://github.com/marceloverdijk/lesscss-java)
27+
The library contains two compilers:
28+
* `NativeLessCompiler` a compiler with a shell-type API
29+
* `LessCompiler` a facade for the `NativeLessCompiler` with a developer-friendly API
30+
31+
The idea for the `NativeLessCompiler` class was based on the [lesscss-java](https://github.com/marceloverdijk/lesscss-java)
2832
library by [Marcel Overdijk](https://github.com/marceloverdijk).
2933

3034
# Usage
3135
How to compile a source file:
3236
```
33-
// create compiler & source file
34-
LessCompiler compiler = new Less175Compiler();
35-
File source = new File("/less/file.less");
37+
// create compiler and source file
38+
LessCompiler compiler = new LessCompiler();
39+
File source = new File("/source.less");
3640
37-
// compile file with default options
41+
// compile source file
3842
String cssCode = compiler.compile(source);
3943
40-
// set custom option: compress CSS code
41-
Collection<Object> options = new Less175CompilerOptionsBuilder().withCompress(true).create();
42-
String cssCompressedCode = compiler.compile(source, options);
44+
// compile source file and save to output file
45+
compiler.compile(source, new File("/output.css"));
46+
47+
// compile source file and compress CSS code
48+
String compressedCssCode = compiler.compileAndCompress(source);
4349
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* LessCSS Compiler
3+
* http://lesscss-compiler.projects.gabrys.biz/
4+
*
5+
* Copyright (c) 2015 Adam Gabryś
6+
*
7+
* This file is licensed under the BSD 3-Clause (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain:
10+
* - a copy of the License at project page
11+
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
12+
*/
13+
package biz.gabrys.lesscss.compiler2;
14+
15+
/**
16+
* Thrown to indicate that an error occurred during build compiler options.
17+
* @since 2.0.0
18+
*/
19+
public class BuildCompilerOptionsException extends RuntimeException {
20+
21+
private static final long serialVersionUID = -2386288381210804468L;
22+
23+
/**
24+
* Constructs a new instance with the specified detail message.
25+
* @param message the detail message.
26+
* @since 2.0.0
27+
*/
28+
public BuildCompilerOptionsException(final String message) {
29+
super(message);
30+
}
31+
32+
/**
33+
* Constructs a new instance with the specified detail message and cause.
34+
* @param message the detail message.
35+
* @param cause the cause.
36+
* @since 2.0.0
37+
*/
38+
public BuildCompilerOptionsException(final String message, final Throwable cause) {
39+
super(message, cause);
40+
}
41+
42+
/**
43+
* Constructs a new instance with the specified cause.
44+
* @param cause the cause.
45+
* @since 2.0.0
46+
*/
47+
public BuildCompilerOptionsException(final Throwable cause) {
48+
super(cause);
49+
}
50+
}

src/main/java/biz/gabrys/lesscss/compiler2/ResolveImportException.java renamed to src/main/java/biz/gabrys/lesscss/compiler2/ImportNotFoundException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
package biz.gabrys.lesscss.compiler2;
1414

1515
/**
16-
* Thrown to indicate that an error occurred while resolving imports.
16+
* Thrown to indicate that an @import operation points to non-existent file.
1717
* @since 2.0.0
1818
*/
19-
public class ResolveImportException extends CompilerException {
19+
public class ImportNotFoundException extends CompilerException {
2020

2121
private static final long serialVersionUID = 3800611680823858853L;
2222

@@ -28,7 +28,7 @@ public class ResolveImportException extends CompilerException {
2828
* @param fileName the imported file name.
2929
* @since 2.0.0
3030
*/
31-
public ResolveImportException(final String message, final String fileName) {
31+
public ImportNotFoundException(final String message, final String fileName) {
3232
super(message);
3333
this.fileName = fileName;
3434
}
@@ -40,18 +40,18 @@ public ResolveImportException(final String message, final String fileName) {
4040
* @param fileName the imported file name.
4141
* @since 2.0.0
4242
*/
43-
public ResolveImportException(final String message, final Throwable cause, final String fileName) {
43+
public ImportNotFoundException(final String message, final Throwable cause, final String fileName) {
4444
super(message, cause);
4545
this.fileName = fileName;
4646
}
4747

4848
/**
49-
* Constructs a new instance with the imported file name and cause.
49+
* Constructs a new instance with the cause and imported file name.
5050
* @param cause the cause.
5151
* @param fileName the imported file name.
5252
* @since 2.0.0
5353
*/
54-
public ResolveImportException(final Throwable cause, final String fileName) {
54+
public ImportNotFoundException(final Throwable cause, final String fileName) {
5555
super(cause);
5656
this.fileName = fileName;
5757
}

src/main/java/biz/gabrys/lesscss/compiler2/LessCompiler.java

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,129 @@
1313
package biz.gabrys.lesscss.compiler2;
1414

1515
import java.io.File;
16-
import java.util.Collection;
1716

1817
/**
19-
* Responsible for executing <a href="http://lesscss.org/">Less</a> compiler for sources files.
18+
* <p>
19+
* <a href="http://lesscss.org/">Less</a> compiler compatible with
20+
* <a href="https://github.com/less/less.js/releases/tag/v1.7.5">Less 1.7.5</a>.
21+
* </p>
22+
* <p>
23+
* How to use:
24+
* </p>
25+
*
26+
* <pre>
27+
* // create compiler and source file
28+
* LessCompiler compiler = new LessCompiler();
29+
* File source = new File("/source.less");
30+
*
31+
* // compile source file
32+
* String cssCode = compiler.compile(source);
33+
*
34+
* // compile source file and save to output file
35+
* compiler.compile(source, new File("/output.css"));
36+
*
37+
* // compile source file and compress CSS code
38+
* String compressedCssCode = compiler.compileAndCompress(source);
39+
* </pre>
40+
*
2041
* @since 2.0.0
2142
*/
22-
public interface LessCompiler {
43+
public class LessCompiler {
44+
45+
private final NativeLessCompiler compiler;
46+
47+
/**
48+
* Constructs a new instance.
49+
* @since 2.0.0
50+
*/
51+
public LessCompiler() {
52+
this(new NativeLessCompiler());
53+
}
54+
55+
/**
56+
* Constructs a new instance.
57+
* @param compiler the native compiler used to compile source files.
58+
* @since 2.0.0
59+
*/
60+
protected LessCompiler(final NativeLessCompiler compiler) {
61+
this.compiler = compiler;
62+
}
63+
64+
/**
65+
* Creates a new instance of the {@link NativeLessCompilerOptionsBuilder} used to create configuration options for
66+
* the native compiler.
67+
* @return the new instance of the {@link NativeLessCompilerOptionsBuilder}.
68+
* @since 2.0.0
69+
*/
70+
protected NativeLessCompilerOptionsBuilder createOptionsBuilder() {
71+
return new NativeLessCompilerOptionsBuilder();
72+
}
73+
74+
/**
75+
* Compiles a Less source file.
76+
* @param source the source file.
77+
* @return CSS code.
78+
* @throws InitializationException if an error occurred during compiler initialization.
79+
* @throws ImportNotFoundException if any &#64;import operation point to non-existent file.
80+
* @throws SourceFileNotFoundException if a source (input) file does not exist.
81+
* @throws SyntaxException if a syntax error occurred during source file compilation.
82+
* @throws CompilerException if an other error occurred during execution.
83+
* @since 2.0.0
84+
*/
85+
public String compile(final File source) {
86+
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
87+
builder.withInputFile(source);
88+
return compiler.execute(builder.create());
89+
}
90+
91+
/**
92+
* Compiles and compress a Less source file.
93+
* @param source the source file.
94+
* @return compressed CSS code.
95+
* @throws InitializationException if an error occurred during compiler initialization.
96+
* @throws ImportNotFoundException if any &#64;import operation point to non-existent file.
97+
* @throws SourceFileNotFoundException if a source (input) file does not exist.
98+
* @throws SyntaxException if a syntax error occurred during source file compilation.
99+
* @throws CompilerException if an other error occurred during execution.
100+
* @since 2.0.0
101+
*/
102+
public String compileAndCompress(final File source) {
103+
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
104+
builder.withInputFile(source).withCompress(true);
105+
return compiler.execute(builder.create());
106+
}
23107

24108
/**
25-
* Executes a Less compiler for source file with default options.
109+
* Compiles a Less source file and saves result in an output file.
26110
* @param source the source file.
27-
* @return the compiler output (depends on default options it can be e.g. CSS code, Source Map etc).
28-
* @throws CompilerException if an error occurred during execution.
111+
* @param output the output file.
112+
* @throws InitializationException if an error occurred during compiler initialization.
113+
* @throws ImportNotFoundException if any &#64;import operation point to non-existent file.
114+
* @throws SourceFileNotFoundException if a source (input) file does not exist.
115+
* @throws SyntaxException if a syntax error occurred during source file compilation.
116+
* @throws CompilerException if an other error occurred during execution.
29117
* @since 2.0.0
30118
*/
31-
String execute(File source);
119+
public void compile(final File source, final File output) {
120+
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
121+
builder.withInputFile(source).withOutputFile(output);
122+
compiler.execute(builder.create());
123+
}
32124

33125
/**
34-
* Executes a Less compiler for source file with custom options.
126+
* Compiles and compress a Less source file and saves result in an output file.
35127
* @param source the source file.
36-
* @param options the compiler options.
37-
* @return the compiler output (depends on options it can be e.g. CSS code, Source Map etc).
38-
* @throws CompilerException if an error occurred during execution.
128+
* @param output the output file.
129+
* @throws InitializationException if an error occurred during compiler initialization.
130+
* @throws ImportNotFoundException if any &#64;import operation point to non-existent file.
131+
* @throws SourceFileNotFoundException if a source (input) file does not exist.
132+
* @throws SyntaxException if a syntax error occurred during source file compilation.
133+
* @throws CompilerException if an other error occurred during execution.
39134
* @since 2.0.0
40135
*/
41-
String execute(File source, Collection<Object> options);
136+
public void compileAndCompress(final File source, final File output) {
137+
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
138+
builder.withInputFile(source).withOutputFile(output).withCompress(true);
139+
compiler.execute(builder.create());
140+
}
42141
}

0 commit comments

Comments
 (0)