1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.theme.preprocessor;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.krad.theme.util.ThemeBuilderConstants;
21 import org.kuali.rice.krad.theme.util.ThemeBuilderUtils;
22 import org.lesscss.LessCompiler;
23 import org.lesscss.LessException;
24 import org.lesscss.LessSource;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Properties;
30
31
32
33
34
35
36
37
38
39
40
41 public class LessThemePreProcessor implements ThemePreProcessor {
42 private static final Logger LOG = Logger.getLogger(LessThemePreProcessor.class);
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public void processTheme(String themeName, File themeDirectory, Properties themeProperties) {
59 if (LOG.isDebugEnabled()) {
60 LOG.debug("Performing Less compilation for theme " + themeName);
61 }
62
63 File stylesheetsDirectory = new File(themeDirectory, ThemeBuilderConstants.ThemeDirectories.STYLESHEETS);
64 if (!stylesheetsDirectory.exists()) {
65 throw new RuntimeException("Stylesheets directory does not exist for theme: " + themeName);
66 }
67
68 List<String> lessFileNames = getLessFileNamesForTheme(themeName, themeDirectory, themeProperties,
69 stylesheetsDirectory);
70
71 LessCompiler lessCompiler = new LessCompiler();
72
73
74 lessCompiler.setCompress(false);
75
76 for (String lessFileName : lessFileNames) {
77 LOG.info("compiling less file: " + lessFileName);
78
79 File sourceLessFile = new File(stylesheetsDirectory, lessFileName);
80 File compiledLessFile = new File(stylesheetsDirectory, lessFileName.replace(
81 ThemeBuilderConstants.FileExtensions.LESS, ThemeBuilderConstants.FileExtensions.CSS));
82
83 try {
84 LessSource lessSource = new LessSource(sourceLessFile);
85
86 lessCompiler.compile(lessSource, compiledLessFile, true);
87 } catch (IOException e) {
88 throw new RuntimeException("Error while compiling LESS source: " + lessFileName, e);
89 } catch (LessException e) {
90 throw new RuntimeException("Error while compiling LESS source: " + lessFileName, e);
91 }
92 }
93
94
95
96 themeProperties.put(ThemeBuilderConstants.DerivedConfiguration.THEME_LESS_FILES,
97 StringUtils.join(lessFileNames, ","));
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 protected List<String> getLessFileNamesForTheme(String themeName, File themeDirectory, Properties themeProperties,
120 File stylesheetsDirectory) {
121 String[] lessIncludes = ThemeBuilderUtils.getPropertyValueAsArray(
122 ThemeBuilderConstants.ThemeConfiguration.LESS_INCLUDES, themeProperties);
123
124 if ((lessIncludes == null) || (lessIncludes.length == 0)) {
125 lessIncludes = new String[1];
126
127 lessIncludes[0] = ThemeBuilderConstants.Patterns.ANT_MATCH_ALL + ThemeBuilderConstants.FileExtensions.LESS;
128 }
129
130 String[] lessExcludes = ThemeBuilderUtils.getPropertyValueAsArray(
131 ThemeBuilderConstants.ThemeConfiguration.LESS_EXCLUDES, themeProperties);
132
133 lessExcludes = ThemeBuilderUtils.addToArray(lessExcludes,
134 ThemeBuilderConstants.ThemeDirectories.INCLUDES + ThemeBuilderConstants.Patterns.ANT_MATCH_DIR);
135
136 return ThemeBuilderUtils.getDirectoryContents(stylesheetsDirectory, lessIncludes, lessExcludes);
137 }
138 }