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

Commit 955de46

Browse files
committed
#3 fix sourceMap mechanism in less.js files, start mapping Source Map parameters, remove SystemOperatingChecker (use own separator)
1 parent 15d5715 commit 955de46

File tree

10 files changed

+336
-168
lines changed

10 files changed

+336
-168
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
*/
6868
public class NativeLessCompiler {
6969

70+
/**
71+
* Separator used to extract paths from {@code --include-path"} option.
72+
* @since 2.0.0
73+
*/
74+
public static final String INCLUDE_PATHS_SEPARATOR = "gabrys-lesscss-compiler-path-separator";
75+
7076
private static final Pattern CONFIGURATION_EXCEPTION_PATTERN = Pattern.compile("^Configuration problem: (.*)");
7177
private static final int CONFIGURATION_EXCEPTION_MESSAGE_GROUP_INDEX = 1;
7278
private static final Pattern IMPORT_EXCEPTION_PATTERN = Pattern.compile("^FileError:\\s+'(.+)'\\s+wasn't\\s+found\\s+(?s).*");
@@ -91,9 +97,24 @@ public NativeLessCompiler() {
9197
}
9298

9399
/**
94-
* Executes the compiler with options.
100+
* Executes the compiler. You can use standard
101+
* <a href="http://lesscss.org/usage/index.html#command-line-usage-options">Less options</a> with some exceptions:
102+
* <ul>
103+
* <li>use {@link #INCLUDE_PATHS_SEPARATOR} to separate paths in {@code --include-path"} option instead of ';'
104+
* character on Windows and ':' character on Unix/Linux machines</li>
105+
* <li>does not support options:
106+
* <ul>
107+
* <li>allow imports from insecure HTTPS hosts ({@code --insecure})</li>
108+
* <li>lint ({@code -l} or {@code --lint})</li>
109+
* <li>makefile ({@code -M} or {@code --depends})</li>
110+
* <li>no color ({@code --no-color})</li>
111+
* <li>plugins ({@code --plugin})</li>
112+
* <li>version ({@code -v} or {@code --version})</li>
113+
* </ul>
114+
* </li>
115+
* </ul>
95116
* @param options the compiler options.
96-
* @return the compiler output (depends on options it can be e.g. CSS code, Source Map etc).
117+
* @return the compiler output (depends on options it can be e.g. CSS code, logs).
97118
* @throws InitializationException if an error occurred during compiler initialization.
98119
* @throws ConfigurationException if the compiler is configured incorrectly.
99120
* @throws ReadFileException if the compiler cannot read file content.

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

Lines changed: 118 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@
2222
import biz.gabrys.lesscss.compiler2.util.StringUtils;
2323

2424
/**
25-
* Options that control the <a href="http://lesscss.org/">Less</a> compilation process.
25+
* Responsible for creating options that control the {@link NativeLessCompiler} compilation process.
2626
* @since 2.0.0
2727
* @see <a href="http://lesscss.org/usage/index.html#command-line-usage-options">Less options</a>
2828
*/
2929
public class NativeLessCompilerOptionsBuilder {
3030

31-
/**
32-
* The operating system software checker responsible for verifing system distriubution.
33-
* @since 2.0.0
34-
*/
35-
protected OperatingSystemChecker systemChecker;
36-
3731
private File input;
3832
private File output;
3933
private boolean silent;
@@ -43,6 +37,9 @@ public class NativeLessCompilerOptionsBuilder {
4337
private boolean javaScriptEnabled = true;
4438
private List<String> includePaths = Collections.emptyList();
4539
private LineNumbersValue lineNumbers = LineNumbersValue.DISABLED;
40+
private boolean sourceMapDefault;
41+
private File sourceMapFile;
42+
private boolean sourceMapInline;
4643
private String rootPath;
4744
private boolean relativeUrls;
4845
private boolean strictMath;
@@ -53,22 +50,7 @@ public class NativeLessCompilerOptionsBuilder {
5350
* @since 2.0.0
5451
*/
5552
public NativeLessCompilerOptionsBuilder() {
56-
systemChecker = new OperatingSystemChecker() {
57-
58-
@Override
59-
public boolean isWindows() {
60-
return System.getProperty("os.name").startsWith("Windows");
61-
}
62-
};
63-
}
64-
65-
/**
66-
* Constructs a new instance.
67-
* @param systemChecker the operating system software checker.
68-
* @since 2.0.0
69-
*/
70-
public NativeLessCompilerOptionsBuilder(final OperatingSystemChecker systemChecker) {
71-
this.systemChecker = systemChecker;
53+
// do nothing
7254
}
7355

7456
/**
@@ -252,10 +234,9 @@ protected String getJavaScriptEnabledCompilerOption() {
252234
}
253235

254236
/**
255-
* Sets available include paths (default: empty collection). Separated by ':' or ';' on Windows. If the file in an
256-
* &#64;import rule does not exist at that exact location, less will look for it at the location(s) passed to this
257-
* option. You might use this for instance to specify a path to a library which you want to be referenced simply and
258-
* relatively in the less files.
237+
* Sets available include paths (default: empty collection). If the file in an &#64;import rule does not exist at
238+
* that exact location, less will look for it at the location(s) passed to this option. You might use this for
239+
* instance to specify a path to a library which you want to be referenced simply and relatively in the less files.
259240
* @param includePaths the available include paths.
260241
* @return {@code this} builder.
261242
* @since 2.0.0
@@ -284,7 +265,7 @@ protected String getIncludePathsCompilerOption() {
284265
if (StringUtils.isNotBlank(path)) {
285266
final boolean separatorRequired = paths.length() != 0;
286267
if (separatorRequired) {
287-
paths.append(systemChecker.isWindows() ? ';' : ':');
268+
paths.append(NativeLessCompiler.INCLUDE_PATHS_SEPARATOR);
288269
}
289270
paths.append(path.trim());
290271
}
@@ -318,6 +299,110 @@ protected String getLineNumbersCompilerOption() {
318299
}
319300
}
320301

302+
/**
303+
* Sets whether a compiler should generate a Source Map file with a default name equal to
304+
* {@link #withOutputFile(File) output file name} plus {@code .map} suffix. It does not matter whether you call this
305+
* method before or after the {@link #withOutputFile(File)} method.
306+
* <p>
307+
* This method clears bulider's data set by the {@link #withSourceMapFile(File)} and the
308+
* {@link #withSourceMapInline(boolean)} methods.
309+
* </p>
310+
* @param enabled {@code true} whether the compiler should generate the Source Map file, otherwise {@code false}.
311+
* @return {@code this} builder.
312+
* @since 2.0.0
313+
* @see #withOutputFile(File)
314+
* @see #withSourceMapFile(File)
315+
* @see #withSourceMapInline(boolean)
316+
*/
317+
public NativeLessCompilerOptionsBuilder withSourceMapDefault(final boolean enabled) {
318+
sourceMapDefault = enabled;
319+
sourceMapFile = null;
320+
sourceMapInline = false;
321+
return this;
322+
}
323+
324+
/**
325+
* Returns a command line option which enables/disables generating a Source Map file with default name.
326+
* @return the command line option (never {@code null}).
327+
* @since 2.0.0
328+
* @see #withSourceMapDefault(boolean)
329+
*/
330+
protected String getSourceMapDefaultCompilerOption() {
331+
if (sourceMapDefault) {
332+
return "--source-map";
333+
} else {
334+
return "";
335+
}
336+
}
337+
338+
/**
339+
* Sets whether a compiler should generate a Source Map file.
340+
* <p>
341+
* This method clears bulider's data set by the {@link #withSourceMapDefault(boolean)} and the
342+
* {@link #withSourceMapInline(boolean)} methods.
343+
* </p>
344+
* @param sourceMap the Source Map file.
345+
* @return {@code this} builder.
346+
* @since 2.0.0
347+
* @see #withSourceMapDefault(boolean)
348+
* @see #withSourceMapInline(boolean)
349+
*/
350+
public NativeLessCompilerOptionsBuilder withSourceMapFile(final File sourceMap) {
351+
sourceMapDefault = false;
352+
sourceMapFile = sourceMap;
353+
sourceMapInline = false;
354+
return this;
355+
}
356+
357+
/**
358+
* Returns a command line option which enables/disables generating a Source Map file.
359+
* @return the command line option (never {@code null}).
360+
* @since 2.0.0
361+
* @see #withSourceMapFile(File)
362+
*/
363+
protected String getSourceMapFileCompilerOption() {
364+
if (sourceMapFile != null) {
365+
return "--source-map=" + sourceMapFile.getAbsolutePath();
366+
} else {
367+
return "";
368+
}
369+
}
370+
371+
/**
372+
* Sets whether a compiler should generate a Source Map and put it to CSS code. This is not recommended for
373+
* production, but for development it allows the compiler to produce a single output file which in browsers that
374+
* support it, use the compiled CSS but show you the non-compiled Less source.
375+
* <p>
376+
* This method clears bulider's data set by the {@link #withSourceMapDefault(boolean)} and the
377+
* {@link #withSourceMapFile(File)} methods.
378+
* </p>
379+
* @param inline {@code true} whether the compiler should generate a Source Map, otherwise {@code false}.
380+
* @return {@code this} builder.
381+
* @since 2.0.0
382+
* @see #withSourceMapDefault(boolean)
383+
* @see #withSourceMapFile(File)
384+
*/
385+
public NativeLessCompilerOptionsBuilder withSourceMapInline(final boolean inline) {
386+
sourceMapDefault = false;
387+
sourceMapFile = null;
388+
sourceMapInline = inline;
389+
return this;
390+
}
391+
392+
/**
393+
* Returns a command line options which enables/disables generating a Source Map which will be put to CSS code.
394+
* @return the command line options (never {@code null}).
395+
* @since 2.0.0
396+
* @see #withSourceMapInline(boolean)
397+
*/
398+
protected String[] getSourceMapInlineCompilerOptions() {
399+
if (sourceMapInline) {
400+
return new String[] { "--source-map", "--source-map-map-inline" };
401+
} else {
402+
return new String[0];
403+
}
404+
}
405+
321406
///////
322407
// TODO source maps
323408
///////
@@ -522,6 +607,11 @@ public Collection<Object> create() {
522607
options.add(getJavaScriptEnabledCompilerOption());
523608
options.add(getIncludePathsCompilerOption());
524609
options.add(getLineNumbersCompilerOption());
610+
611+
options.add(getSourceMapDefaultCompilerOption());
612+
options.add(getSourceMapFileCompilerOption());
613+
options.add(getSourceMapInlineCompilerOptions());
614+
525615
options.add(getRootPathCompilerOption());
526616
options.add(getRelativeUrlsCompilerOption());
527617
options.add(getStrictMathCompilerOption());
@@ -539,20 +629,6 @@ public Collection<Object> create() {
539629
return options.create();
540630
}
541631

542-
/**
543-
* Responsible for checking operating system family.
544-
* @since 2.0.0
545-
*/
546-
public interface OperatingSystemChecker {
547-
548-
/**
549-
* Tests whether operating system is <a href="http://www.microsoft.com/">Windows</a>.
550-
* @return {@code true} whether operating system is Windows, otherwise {@code false}.
551-
* @since 2.0.0
552-
*/
553-
boolean isWindows();
554-
}
555-
556632
/**
557633
* Available values for {@link NativeLessCompilerOptionsBuilder#withLineNumbers(LineNumbersValue) line numbers}
558634
* option.

src/main/java/biz/gabrys/lesscss/compiler2/util/ListWithoutEmptyValuesBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ public ListWithoutEmptyValuesBuilder() {
3232
// do nothing
3333
}
3434

35+
/**
36+
* Appends all values which are not blank.
37+
* @param values the appended values.
38+
* @return {@code this} builder.
39+
* @since 2.0.0
40+
*/
41+
public ListWithoutEmptyValuesBuilder add(final String[] values) {
42+
if (values != null) {
43+
for (final String value : values) {
44+
add(value);
45+
}
46+
}
47+
return this;
48+
}
49+
3550
/**
3651
* Appends a value only if it is not blank.
3752
* @param value the appended value.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,7 +3049,7 @@ less.Parser.serializeVars = function(vars) {
30493049

30503050
if (useBase64) {
30513051
try {
3052-
returner = require('./encoder').encodeBase64(returner); // TODO browser implementation
3052+
returner = less.encoder.encodeBase64(returner);
30533053
} catch (e) {
30543054
useBase64 = false;
30553055
}
@@ -7948,7 +7948,7 @@ less.Parser.serializeVars = function(vars) {
79487948
if (this._writeSourceMap) {
79497949
this._writeSourceMap(sourceMapContent);
79507950
} else {
7951-
sourceMapURL = "data:application/json;base64," + require('./encoder.js').encodeBase64(sourceMapContent);
7951+
sourceMapURL = "data:application/json;base64," + less.encoder.encodeBase64(sourceMapContent);
79527952
}
79537953

79547954
if (sourceMapURL) {

0 commit comments

Comments
 (0)