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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package com.vaadin.buildhelpers;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import com.vaadin.sass.ScssStylesheet;
/**
* Helper to combine css divided into separate per component dirs into one to
* optimize http requests.
*/
public class CompileTheme {
/**
* @param args
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws IOException, ParseException {
Options options = new Options();
options.addOption("t", "theme", true, "the theme to compile");
options.addOption("v", "theme-version", true,
"the version to add to the compiled theme");
options.addOption("f", "theme-folder", true,
"the folder containing the theme");
CommandLineParser parser = new PosixParser();
CommandLine params = parser.parse(options, args);
if (!params.hasOption("theme") || !params.hasOption("theme-folder")) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(CompileTheme.class.getName(), options);
return;
}
String themeName = params.getOptionValue("theme");
String themeFolder = params.getOptionValue("theme-folder");
String themeVersion = params.getOptionValue("theme-version");
// Regular theme
try {
processSassTheme(themeFolder, themeName, "styles", themeVersion);
System.out.println("Compiling theme " + themeName
+ " styles successful");
} catch (Exception e) {
System.err.println("Compiling theme " + themeName
+ " styles failed");
e.printStackTrace();
}
// Legacy theme w/o .themename{} wrapping
try {
processSassTheme(themeFolder, themeName, "legacy-styles",
themeVersion);
System.out.println("Compiling theme " + themeName
+ " legacy-styles successful");
} catch (Exception e) {
System.err.println("Compiling theme " + themeName
+ " legacy-styles failed");
e.printStackTrace();
}
}
private static void processSassTheme(String themeFolder, String themeName,
String variant, String version) throws Exception {
StringBuffer cssHeader = new StringBuffer();
version = version.replaceAll("\\.", "_");
cssHeader.append(".v-theme-version:after {content:\"" + version
+ "\";}\n");
cssHeader.append(".v-theme-version-" + version + " {display: none;}\n");
String stylesCssDir = themeFolder + File.separator + themeName
+ File.separator;
String stylesCssName = stylesCssDir + variant + ".css";
// Process as SASS file
String sassFile = stylesCssDir + variant + ".scss";
ScssStylesheet scss = ScssStylesheet.get(sassFile);
if (scss == null) {
throw new IllegalArgumentException("SASS file: " + sassFile
+ " not found");
}
scss.compile();
BufferedWriter out = new BufferedWriter(new FileWriter(stylesCssName));
out.write(cssHeader.toString());
out.write(scss.toString());
out.close();
System.out.println("Compiled CSS to " + stylesCssName + " ("
+ scss.toString().length() + " bytes)");
createSprites(themeFolder, themeName);
File oldCss = new File(stylesCssName);
File newCss = new File(stylesCssDir + variant + "-sprite.css");
if (newCss.exists()) {
// Theme contained sprites. Renamed "styles-sprite.css" ->
// "styles.css"
oldCss.delete();
boolean ok = newCss.renameTo(oldCss);
if (!ok) {
throw new RuntimeException("Rename " + newCss + " -> " + oldCss
+ " failed");
}
}
}
private static void createSprites(String themeFolder, String themeName)
throws FileNotFoundException, IOException {
try {
// Try loading the class separately from using it to avoid
// hiding other classpath issues
Class<?> smartSpritesClass = org.carrot2.labs.smartsprites.SmartSprites.class;
} catch (NoClassDefFoundError e) {
System.err
.println("Could not find smartsprites. No sprites were generated. The theme should still work.");
return;
}
String[] parameters = new String[] { "--sprite-png-depth", "AUTO",
"--css-file-suffix", "-sprite", "--css-file-encoding", "UTF-8",
"--root-dir-path", themeFolder + File.separator + themeName,
"--log-level", "WARN" };
org.carrot2.labs.smartsprites.SmartSprites.main(parameters);
System.out.println("Generated sprites");
}
}
|