closure-stylesheets
closure-stylesheets copied to clipboard
Java API compiler
Would you consider adding a compiler that can be used directly in Java? Something like Compiler and CompilerOptions from Closure Compiler.
That already exists, to some extent. However, you need to wrangle the inputs, then run the passes you want yourself.
I'm certainly willing to accept patches that improve the usability of the compiler. If you'd like to contribute a more turnkey solution, propose a design and we'll discuss it.
Add the latest closure-stylesheets.jar to your Build Path
public static void main(String[] args) {
JobDescription job = createJobDescription(new File("your_css_file.css"));
ExitCodeHandler exitCodeHandler = new DefaultExitCodeHandler();
CompilerErrorManager errorManager = new CompilerErrorManager();
ClosureStylesheetCompiler compiler = new ClosureStylesheetCompiler(job, exitCodeHandler, errorManager);
String compilerOutput = compiler.execute();
}
/* create a basic job with basic settings */
private JobDescription createJobDescription(File file) throws IOException {
JobDescriptionBuilder builder = new JobDescriptionBuilder();
builder.setInputOrientation(InputOrientation.LTR);
builder.setOutputOrientation(OutputOrientation.LTR);
builder.setOutputFormat(JobDescription.OutputFormat.COMPRESSED);
builder.setCopyrightNotice(null);
builder.setTrueConditionNames(Lists.newArrayList());
builder.setAllowDefPropagation(true);
builder.setAllowUnrecognizedFunctions(true);
builder.setAllowedNonStandardFunctions(Lists.newArrayList());
builder.setAllowedUnrecognizedProperties(Lists.newArrayList());
builder.setAllowUnrecognizedProperties(true);
builder.setVendor(null);
builder.setAllowKeyframes(true);
builder.setAllowWebkitKeyframes(true);
builder.setProcessDependencies(true);
builder.setExcludedClassesFromRenaming(Lists.newArrayList());
builder.setSimplifyCss(true);
/* sadly the following line is necessary until they introduce support for --allow-duplicate-declarations */
builder.setEliminateDeadStyles(false);
builder.setCssSubstitutionMapProvider(new SubstitutionMapProvider() {
@Override
public SubstitutionMap get() { return new IdentitySubstitutionMap(); }
});
builder.setCssRenamingPrefix("");
builder.setPreserveComments(false);
builder.setOutputRenamingMapFormat(OutputRenamingMapFormat.JSON);
builder.setCompileConstants(new HashMap<String, Integer>());
GssFunctionMapProvider gssFunMapProv = new DefaultGssFunctionMapProvider();
builder.setGssFunctionMapProvider(gssFunMapProv);
builder.setSourceMapLevel(SourceMapDetailLevel.DEFAULT);
builder.setCreateSourceMap(false);
String fileContents = new String(Files.readAllBytes(file.toPath))
builder.addInput(new SourceCode(file.getName(), fileContents));
return builder.getJobDescription();
}
/* you'll need this */
final class CompilerErrorManager extends BasicErrorManager {
private boolean warningsAsErrors = false;
@Override
public void print(String msg) { System.err.println(msg); }
@Override
public void reportWarning(GssError warning) {
if (warningsAsErrors) {
report(warning);
} else {
super.reportWarning(warning);
}
}
public void setWarningsAsErrors(boolean state) {
warningsAsErrors = state;
}
}
/* and you'll need this which is the core entry point */
public class ClosureStylesheetCompiler extends DefaultCommandLineCompiler {
public ClosureStylesheetCompiler(JobDescription job, ExitCodeHandler exitCodeHandler, ErrorManager errorManager) {
super(job, exitCodeHandler, errorManager);
}
public String execute() { return super.execute(null, null); }
}
I hope it helps