forked from dirkmc/press
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginConfig.java
153 lines (127 loc) · 6.93 KB
/
PluginConfig.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
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
144
145
146
147
148
149
150
151
152
153
package press;
import play.Play;
import play.Play.Mode;
public class PluginConfig {
/**
* Stores the default configuration for the plugin
*/
public static class DefaultConfig {
// Whether the plugin is enabled
public static final boolean enabled = (Play.mode == Mode.PROD);
// The caching strategy
public static final CachingStrategy cache = (Play.mode == Mode.DEV) ? CachingStrategy.Change
: CachingStrategy.Always;
// Whether the cache can be cleared through the web interface
// Default is to be available in dev only
public static final boolean cacheClearEnabled = (Play.mode == Mode.DEV);
// The amount of time that a compression key is stored for.
// This only needs to be as long as the time between when the action
// finishes and the browser requests the compressed javascript (usually
// less than a second)
public static final String compressionKeyStorageTime = "2mn";
// The maximum amount of time in milli-seconds allowed for compression
// to occur before a timeout exception is thrown.
public static final int maxCompressionTimeMillis = 60000;
public static class js {
// The directory where source javascript files are read from
public static final String srcDir = "/public/javascripts/";
// The directory where compressed javascript files are written to
public static final String compressedDir = "/public/javascripts/press/";
// Options for YUI JS compression
public static final int lineBreak = -1;
public static final boolean munge = true;
public static final boolean warn = false;
public static final boolean preserveAllSemiColons = false;
public static final boolean preserveStringLiterals = false;
}
public static class css {
// The directory where source css files are read from
public static final String srcDir = "/public/stylesheets/";
// The directory where compressed css files are written to
public static final String compressedDir = "/public/stylesheets/press/";
// Options for YUI CSS compression
public static final int lineBreak = -1;
}
}
public static boolean enabled;
public static CachingStrategy cache;
public static boolean cacheClearEnabled;
public static String compressionKeyStorageTime;
public static int maxCompressionTimeMillis;
public static class js {
public static String srcDir = DefaultConfig.js.srcDir;
public static String compressedDir = DefaultConfig.js.compressedDir;
// YUI JS compression options
public static int lineBreak = DefaultConfig.js.lineBreak;
public static boolean munge = DefaultConfig.js.munge;
public static boolean warn = DefaultConfig.js.warn;
public static boolean preserveAllSemiColons = DefaultConfig.js.preserveAllSemiColons;
public static boolean preserveStringLiterals = DefaultConfig.js.preserveStringLiterals;
}
public static class css {
public static String srcDir = DefaultConfig.css.srcDir;
public static String compressedDir = DefaultConfig.css.compressedDir;
public static int lineBreak = DefaultConfig.css.lineBreak;
}
/**
* Reads from the config file into memory. If the config file doesn't exist
* or is deleted, uses the default values.
*/
public static void readConfig() {
PressLogger.trace("Loading Press plugin configuration");
// press options
String cacheDefault = DefaultConfig.cache.toString();
cache = CachingStrategy.parse(ConfigHelper.getString("press.cache", cacheDefault));
cacheClearEnabled = ConfigHelper.getBoolean("press.cache.clearEnabled",
DefaultConfig.cacheClearEnabled);
enabled = ConfigHelper.getBoolean("press.enabled", DefaultConfig.enabled);
compressionKeyStorageTime = ConfigHelper.getString("press.key.lifetime",
DefaultConfig.compressionKeyStorageTime);
maxCompressionTimeMillis = ConfigHelper.getInt("press.compression.maxTimeMillis",
DefaultConfig.maxCompressionTimeMillis);
css.srcDir = ConfigHelper.getString("press.css.sourceDir", DefaultConfig.css.srcDir);
css.compressedDir = ConfigHelper.getString("press.css.outputDir",
DefaultConfig.css.compressedDir);
js.srcDir = ConfigHelper.getString("press.js.sourceDir", DefaultConfig.js.srcDir);
js.compressedDir = ConfigHelper.getString("press.js.outputDir",
DefaultConfig.js.compressedDir);
// YUI options
css.lineBreak = ConfigHelper.getInt("press.yui.css.lineBreak", DefaultConfig.css.lineBreak);
js.lineBreak = ConfigHelper.getInt("press.yui.js.lineBreak", DefaultConfig.js.lineBreak);
js.munge = ConfigHelper.getBoolean("press.yui.js.munge", DefaultConfig.js.munge);
js.warn = ConfigHelper.getBoolean("press.yui.js.warn", DefaultConfig.js.warn);
js.preserveAllSemiColons = ConfigHelper.getBoolean("press.yui.js.preserveAllSemiColons",
DefaultConfig.js.preserveAllSemiColons);
js.preserveStringLiterals = ConfigHelper.getBoolean("press.yui.js.preserveStringLiterals",
DefaultConfig.js.preserveStringLiterals);
// Add a trailing slash to directories, if necessary
css.srcDir = addTrailingSlash(css.srcDir);
css.compressedDir = addTrailingSlash(css.compressedDir);
js.srcDir = addTrailingSlash(js.srcDir);
js.compressedDir = addTrailingSlash(js.compressedDir);
// Log the newly loaded configuration
logConfig();
}
private static void logConfig() {
PressLogger.trace("enabled: %s", enabled);
PressLogger.trace("caching strategy: %s", cache);
PressLogger.trace("cache publicly clearable: %s", cacheClearEnabled);
PressLogger.trace("compression key storage time: %s", compressionKeyStorageTime);
PressLogger.trace("css source directory: %s", css.srcDir);
PressLogger.trace("css compressed output directory: %s", css.compressedDir);
PressLogger.trace("js source directory: %s", js.srcDir);
PressLogger.trace("js compressed output directory: %s", js.compressedDir);
PressLogger.trace("YUI css line break: %d", css.lineBreak);
PressLogger.trace("YUI js line break: %d", js.lineBreak);
PressLogger.trace("YUI js munge: %s", js.munge);
PressLogger.trace("YUI js warn: %s", js.warn);
PressLogger.trace("YUI js preserve all semi colons: %s", js.preserveAllSemiColons);
PressLogger.trace("YUI js preserve string literals: %s", js.preserveStringLiterals);
}
public static String addTrailingSlash(String dir) {
if (dir.charAt(dir.length() - 1) != '/') {
return dir + '/';
}
return dir;
}
}