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

Commit 045460a

Browse files
committed
#3 add more compileX methods to LessCompiler
1 parent 63312ee commit 045460a

15 files changed

+710
-146
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<dependency>
9696
<groupId>org.mockito</groupId>
9797
<artifactId>mockito-core</artifactId>
98-
<version>2.7.2</version>
98+
<version>2.7.12</version>
9999
<scope>test</scope>
100100
</dependency>
101101
</dependencies>
@@ -187,7 +187,7 @@
187187
<plugin>
188188
<groupId>org.jacoco</groupId>
189189
<artifactId>jacoco-maven-plugin</artifactId>
190-
<version>0.7.8</version>
190+
<version>0.7.9</version>
191191
<configuration>
192192
<rules>
193193
<rule>

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

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

Lines changed: 317 additions & 36 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public LessCompilerOptions(final LessCompilerOptions options) {
6161
compressionEnabled = options.compressionEnabled;
6262
ieCompatabilityEnabled = options.ieCompatabilityEnabled;
6363
javaScriptAllowed = options.javaScriptAllowed;
64-
includePaths = options.includePaths == null ? Collections.<String>emptyList() : new ArrayList<String>(options.includePaths);
65-
lineNumbers = options.lineNumbers == null ? LineNumbersValue.DISABLED : options.lineNumbers;
64+
includePaths = new ArrayList<String>(options.includePaths);
65+
lineNumbers = options.lineNumbers;
6666
rootPath = options.rootPath;
6767
relativeUrlsRewriteEnabled = options.relativeUrlsRewriteEnabled;
6868
strictMathEnabled = options.strictMathEnabled;

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.mozilla.javascript.ScriptableObject;
3333
import org.mozilla.javascript.tools.shell.Global;
3434

35-
import biz.gabrys.lesscss.compiler2.util.IOUtils;
35+
import biz.gabrys.lesscss.compiler2.util.io.IOUtils;
3636

3737
/**
3838
* <p>
@@ -111,8 +111,8 @@ public NativeLessCompiler() {
111111
* @return the compiler output (depends on options it can be e.g. CSS code, logs).
112112
* @throws InitializationException if an error occurred during compiler initialization.
113113
* @throws ConfigurationException if the compiler is configured incorrectly.
114-
* @throws ReadFileException if the compiler cannot read file content.
115-
* @throws ImportNotFoundException if any &#64;import operation point to non-existent file.
114+
* @throws ReadFileException if the compiler cannot read source file or any &#64;import operation point to
115+
* non-existent file.
116116
* @throws SyntaxException if a syntax error occurred during source file compilation.
117117
* @throws CompilerException if an other error occurred during execution.
118118
* @since 2.0.0
@@ -187,8 +187,8 @@ private static CompilerException parseException(final JavaScriptException except
187187
if (value != null && ScriptableObject.hasProperty(value, "message")) {
188188
final String message = ScriptableObject.getProperty(value, "message").toString();
189189

190-
final Iterable<ExceptionConverter> converters = Arrays.asList(new ImportExceptionConverter(),
191-
new ConfigurationExceptionConverter(), new ReadFileExceptionConverter());
190+
final Iterable<ExceptionConverter> converters = Arrays.asList(new ImportFileExceptionConverter(),
191+
new ConfigurationExceptionConverter(), new SourceFileExceptionConverter());
192192

193193
for (final ExceptionConverter converter : converters) {
194194
final Matcher matcher = converter.getPattern().matcher(message);
@@ -208,16 +208,17 @@ private interface ExceptionConverter {
208208
CompilerException createException(Exception cause, Matcher matcher, String message);
209209
}
210210

211-
private static class ImportExceptionConverter implements ExceptionConverter {
211+
private static class ImportFileExceptionConverter implements ExceptionConverter {
212212

213213
@Override
214214
public Pattern getPattern() {
215-
return Pattern.compile("^FileError:\\s+'(.+)'\\s+wasn't\\s+found\\s+(?s).*");
215+
return Pattern.compile("^FileError:\\s+'(.+)'\\s+does\\snot\\sexist\\sin\\s((?s).*)$");
216216
}
217217

218218
@Override
219219
public CompilerException createException(final Exception cause, final Matcher matcher, final String message) {
220-
return new ImportNotFoundException(message, cause, matcher.group(1));
220+
return new ReadFileException(String.format("File \"%s\" does not exist. Error in %s", matcher.group(1), matcher.group(2)),
221+
cause, matcher.group(1));
221222
}
222223
}
223224

@@ -234,11 +235,11 @@ public CompilerException createException(final Exception cause, final Matcher ma
234235
}
235236
}
236237

237-
private static class ReadFileExceptionConverter implements ExceptionConverter {
238+
private static class SourceFileExceptionConverter implements ExceptionConverter {
238239

239240
@Override
240241
public Pattern getPattern() {
241-
return Pattern.compile("^Couldn't open source file: (.*)");
242+
return Pattern.compile("^Couldn't open file (.*)");
242243
}
243244

244245
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public NativeLessCompilerOptionsBuilder() {
5656
* <li>{@link #withIeCompatability(boolean)}</li>
5757
* <li>{@link #withJavaScript(boolean)}</li>
5858
* <li>{@link #withIncludePaths(List)}</li>
59-
* <li>{@link #withLineNumbers(LineNumbersValue)}</li>
59+
* <li>{@link #withLineNumbers(LessCompilerOptions.LineNumbersValue)}</li>
6060
* <li>{@link #withRootPath(String)}</li>
6161
* <li>{@link #withRelativeUrls(boolean)}</li>
6262
* <li>{@link #withStrictMath(boolean)}</li>

src/main/java/biz/gabrys/lesscss/compiler2/util/IOUtils.java renamed to src/main/java/biz/gabrys/lesscss/compiler2/util/io/IOUtils.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
* - a copy of the License at project page
1111
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
1212
*/
13-
package biz.gabrys.lesscss.compiler2.util;
13+
package biz.gabrys.lesscss.compiler2.util.io;
1414

1515
import java.io.Closeable;
16+
import java.io.File;
1617
import java.io.IOException;
18+
import java.io.PrintWriter;
19+
import java.io.Writer;
1720

1821
/**
1922
* Provides tools for working with I/O.
@@ -40,4 +43,29 @@ public static void closeQuietly(final Closeable closeable) {
4043
// do nothing
4144
}
4245
}
46+
47+
/**
48+
* Writes a content to a file.
49+
* @param file the file (cannot be {@code null}).
50+
* @param content the content (cannot be {@code null}).
51+
* @throws IllegalArgumentException if the file or the content is {@code null}.
52+
* @throws IOException if an I/O error occurs.
53+
* @since 2.0.0
54+
*/
55+
public static void write(final File file, final CharSequence content) throws IOException {
56+
if (file == null) {
57+
throw new IllegalArgumentException("File cannot be null");
58+
}
59+
if (content == null) {
60+
throw new IllegalArgumentException("Content cannot be null");
61+
}
62+
63+
Writer writer = null;
64+
try {
65+
writer = new PrintWriter(file, "UTF-8");
66+
writer.write(content.toString());
67+
} finally {
68+
IOUtils.closeQuietly(writer);
69+
}
70+
}
4371
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.util.io;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
18+
/**
19+
* Responsible for creating temporary files in a system default temporary directory.
20+
* @since 2.0.0
21+
*/
22+
public class SystemDefaultTemporaryFileFactory implements TemporaryFileFactory {
23+
24+
/**
25+
* The prefix string used in generating the temporary files names.
26+
* @since 2.0.0
27+
*/
28+
public static final String FILENAME_PREFIX = "gabrys-lesscss-compiler-";
29+
/**
30+
* The suffix string used in generating the temporary files names.
31+
* @since 2.0.0
32+
*/
33+
public static final String FILENAME_SUFFIX = ".less";
34+
35+
@Override
36+
public File create() throws IOException {
37+
return File.createTempFile(FILENAME_PREFIX, FILENAME_SUFFIX);
38+
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.util.io;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
18+
/**
19+
* Responsible for creating temporary files.
20+
* @since 2.0.0
21+
*/
22+
public interface TemporaryFileFactory {
23+
24+
/**
25+
* Creates a temporary file.
26+
* @return the temporary file.
27+
* @throws IOException if the temporary file could not be created.
28+
* @since 2.0.0
29+
*/
30+
File create() throws IOException;
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
/**
14+
* Utilities classes responsible for operating on files.
15+
* @since 2.0.0
16+
*/
17+
package biz.gabrys.lesscss.compiler2.util.io;

src/main/java/biz/gabrys/lesscss/compiler2/util/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
1212
*/
1313
/**
14-
* Utilities classes responsible for operating on strings, files, collections etc.
14+
* Utilities classes responsible for operating on strings and collections.
1515
* @since 2.0.0
1616
*/
1717
package biz.gabrys.lesscss.compiler2.util;

src/main/javascript/biz/gabrys/lesscss/compiler2/lessc-rhino-1.7.5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ less.Parser.fileLoader = function(file, currentFileInfo, callback, env) {
9898
} catch (e) {
9999
callback({
100100
type: 'File',
101-
message: "'" + less.modules.path.basename(href) + "' wasn't found"
101+
message: "'" + less.modules.path.basename(href) + "' does not exist"
102102
});
103103
return;
104104
}
@@ -280,7 +280,7 @@ function writeFile(filename, content) {
280280
try {
281281
input = readFile(source, 'utf-8');
282282
} catch (e) {
283-
throw new Error('Couldn\'t open source file: ' + source);
283+
throw new Error('Couldn\'t open file ' + source);
284284
}
285285

286286
options.filename = source;

0 commit comments

Comments
 (0)