/
LessEngineCli.java
executable file
·94 lines (87 loc) · 3.47 KB
/
LessEngineCli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.asual.lesscss;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
/**
* @author Rostislav Hristov
*/
public class LessEngineCli {
public static void main(String[] args) throws LessException, URISyntaxException {
Options cmdOptions = new Options();
cmdOptions.addOption(LessOptions.CHARSET_OPTION, true, "Input file charset encoding. Defaults to UTF-8.");
cmdOptions.addOption(LessOptions.COMPRESS_OPTION, false, "Flag that enables compressed CSS output.");
cmdOptions.addOption(LessOptions.CSS_OPTION, false, "Flag that enables compilation of .css files.");
cmdOptions.addOption(LessOptions.LESS_OPTION, true, "Path to a custom less.js for Rhino version.");
try {
CommandLineParser cmdParser = new GnuParser();
CommandLine cmdLine = cmdParser.parse(cmdOptions, args);
LessOptions options = new LessOptions();
if (cmdLine.hasOption(LessOptions.CHARSET_OPTION)) {
options.setCharset(cmdLine.getOptionValue(LessOptions.CHARSET_OPTION));
}
if (cmdLine.hasOption(LessOptions.COMPRESS_OPTION)) {
options.setCompress(true);
}
if (cmdLine.hasOption(LessOptions.CSS_OPTION)) {
options.setCss(true);
}
if (cmdLine.hasOption(LessOptions.LESS_OPTION)) {
options.setLess(new File(cmdLine.getOptionValue(LessOptions.LESS_OPTION)).toURI().toURL());
}
LessEngine engine = new LessEngine(options);
if (System.in.available() != 0) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringWriter sw = new StringWriter();
char[] buffer = new char[1024];
int n = 0;
while (-1 != (n = in.read(buffer))) {
sw.write(buffer, 0, n);
}
String src = sw.toString();
if (!src.isEmpty()) {
System.out.println(engine.compile(src, null, options.isCompress()));
System.exit(0);
}
}
String[] files = cmdLine.getArgs();
if (files.length == 1) {
System.out.println(engine.compile(new File(files[0]), options.isCompress()));
System.exit(0);
}
if (files.length == 2) {
engine.compile(new File(files[0]), new File(files[1]), options.isCompress());
System.exit(0);
}
} catch (IOException ioe) {
System.err.println("Error opening input file.");
} catch (ParseException pe) {
System.err.println("Error parsing arguments.");
}
String[] paths = LessEngine.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().split(File.separator);
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar " + paths[paths.length - 1] + " input [output] [options]", cmdOptions);
System.exit(1);
}
}