|
13 | 13 | package biz.gabrys.lesscss.compiler2;
|
14 | 14 |
|
15 | 15 | import java.io.File;
|
16 |
| -import java.util.Collection; |
17 | 16 |
|
18 | 17 | /**
|
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 | + * |
20 | 41 | * @since 2.0.0
|
21 | 42 | */
|
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 @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 @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 | + } |
23 | 107 |
|
24 | 108 | /**
|
25 |
| - * Executes a Less compiler for source file with default options. |
| 109 | + * Compiles a Less source file and saves result in an output file. |
26 | 110 | * @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 @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. |
29 | 117 | * @since 2.0.0
|
30 | 118 | */
|
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 | + } |
32 | 124 |
|
33 | 125 | /**
|
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. |
35 | 127 | * @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 @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. |
39 | 134 | * @since 2.0.0
|
40 | 135 | */
|
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 | + } |
42 | 141 | }
|
0 commit comments