Skip to content

Commit 0073aad

Browse files
pirhoobamthomas
authored andcommitted
feat: make env's appFolder configurable
1 parent 3535977 commit 0073aad

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

src/main/java/net/codestory/http/misc/Env.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.codestory.http.misc;
1717

18+
import com.google.common.annotations.VisibleForTesting;
1819
import net.codestory.http.reload.MasterFolderWatch;
1920

2021
import java.io.File;
@@ -37,6 +38,7 @@ public class Env implements Serializable {
3738
private final boolean injectLiveReloadScript;
3839
private final boolean diskCache;
3940
private final Supplier<MasterFolderWatch> folderWatch;
41+
private final String appFolder;
4042

4143
public Env() {
4244
this(
@@ -47,11 +49,12 @@ public Env() {
4749
!getBoolean("http.disable.gzip", false),
4850
getBoolean("http.livereload.server", true),
4951
getBoolean("http.livereload.script", true),
50-
getBoolean("http.cache.disk", true)
52+
getBoolean("http.cache.disk", true),
53+
get("APP_FOLDER", "app")
5154
);
5255
}
5356

54-
private Env(File workingDir, boolean prodMode, boolean classPath, boolean filesystem, boolean gzip, boolean liveReloadServer, boolean injectLiveReloadScript, boolean diskCache) {
57+
private Env(File workingDir, boolean prodMode, boolean classPath, boolean filesystem, boolean gzip, boolean liveReloadServer, boolean injectLiveReloadScript, boolean diskCache, String appFolder) {
5558
this.workingDir = workingDir;
5659
this.prodMode = prodMode;
5760
this.classPath = classPath;
@@ -61,50 +64,55 @@ private Env(File workingDir, boolean prodMode, boolean classPath, boolean filesy
6164
this.injectLiveReloadScript = injectLiveReloadScript;
6265
this.diskCache = diskCache;
6366
this.folderWatch = memoize(() -> new MasterFolderWatch(this));
67+
this.appFolder = appFolder;
6468
}
6569

6670
// helper factories
6771

6872
public static Env prod() {
69-
return new Env(new File("."), true, true, true, true, false, false, true);
73+
return new Env(new File("."), true, true, true, true, false, false, true, "app");
7074
}
7175

7276
public static Env dev() {
73-
return new Env(new File("."), false, true, true, false, true, true, true);
77+
return new Env(new File("."), false, true, true, false, true, true, true, "app");
7478
}
7579

76-
public static Env dev(File workingDir) { return new Env(workingDir, false, false, true, false, true, true, true);}
80+
public static Env dev(File workingDir) { return new Env(workingDir, false, false, true, false, true, true, true, "app");}
7781

7882
public Env withWorkingDir(File newWorkingDir) {
79-
return new Env(newWorkingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache);
83+
return new Env(newWorkingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache, appFolder);
8084
}
8185

8286
public Env withProdMode(boolean newProdMode) {
83-
return new Env(workingDir, newProdMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache);
87+
return new Env(workingDir, newProdMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache, appFolder);
8488
}
8589

8690
public Env withClassPath(boolean shouldScanCassPath) {
87-
return new Env(workingDir, prodMode, shouldScanCassPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache);
91+
return new Env(workingDir, prodMode, shouldScanCassPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache, appFolder);
8892
}
8993

9094
public Env withFilesystem(boolean shouldScanFilesystem) {
91-
return new Env(workingDir, prodMode, classPath, shouldScanFilesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache);
95+
return new Env(workingDir, prodMode, classPath, shouldScanFilesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache, appFolder);
9296
}
9397

9498
public Env withGzip(boolean shouldGzipResponse) {
95-
return new Env(workingDir, prodMode, classPath, filesystem, shouldGzipResponse, liveReloadServer, injectLiveReloadScript, diskCache);
99+
return new Env(workingDir, prodMode, classPath, filesystem, shouldGzipResponse, liveReloadServer, injectLiveReloadScript, diskCache, appFolder);
96100
}
97101

98102
public Env withLiveReloadServer(boolean shouldStartLiveReloadServer) {
99-
return new Env(workingDir, prodMode, classPath, filesystem, gzip, shouldStartLiveReloadServer, injectLiveReloadScript, diskCache);
103+
return new Env(workingDir, prodMode, classPath, filesystem, gzip, shouldStartLiveReloadServer, injectLiveReloadScript, diskCache, appFolder);
100104
}
101105

102106
public Env withInjectLiveReloadScript(boolean shouldInjectLiveReloadScript) {
103-
return new Env(workingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, shouldInjectLiveReloadScript, diskCache);
107+
return new Env(workingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, shouldInjectLiveReloadScript, diskCache, appFolder);
104108
}
105109

106110
public Env withDiskCache(boolean shouldUseDiskCache) {
107-
return new Env(workingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, shouldUseDiskCache);
111+
return new Env(workingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, shouldUseDiskCache, appFolder);
112+
}
113+
114+
public Env withAppFolder(String shouldAppFolder) {
115+
return new Env(workingDir, prodMode, classPath, filesystem, gzip, liveReloadServer, injectLiveReloadScript, diskCache, shouldAppFolder);
108116
}
109117

110118
//
@@ -118,7 +126,7 @@ public File workingDir() {
118126
}
119127

120128
public String appFolder() {
121-
return "app";
129+
return appFolder;
122130
}
123131

124132
public List<Path> foldersToWatch() {
@@ -165,11 +173,21 @@ public boolean diskCache() {
165173
return diskCache;
166174
}
167175

176+
@VisibleForTesting
177+
private static String getenv(String envVar) {
178+
return System.getenv(envVar);
179+
}
180+
168181
private static String get(String propertyName) {
169-
String env = System.getenv(propertyName);
182+
String env = getenv(propertyName);
170183
return (env != null) ? env : System.getProperty(propertyName);
171184
}
172185

186+
private static String get(String propertyName, String defaultValue) {
187+
String env = getenv(propertyName);
188+
return (env != null) ? env : System.getProperty(propertyName, defaultValue);
189+
}
190+
173191
private static boolean getBoolean(String propertyName, boolean defaultValue) {
174192
String value = get(propertyName);
175193
return (value == null) ? defaultValue : Boolean.parseBoolean(value);

src/test/java/net/codestory/http/misc/EnvTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ public void without_gzip() {
127127
assertThat(env.diskCache()).isTrue();
128128
}
129129

130+
@Test
131+
public void with_app_folder() {
132+
Env env = Env.prod().withAppFolder("app/public");
133+
134+
assertThat(env.prodMode()).isTrue();
135+
assertThat(env.classPath()).isTrue();
136+
assertThat(env.filesystem()).isTrue();
137+
assertThat(env.gzip()).isTrue();
138+
assertThat(env.workingDir()).isEqualTo(new File("."));
139+
assertThat(env.appFolder()).isEqualTo("app/public");
140+
assertThat(env.injectLiveReloadScript()).isFalse();
141+
assertThat(env.liveReloadServer()).isFalse();
142+
assertThat(env.diskCache()).isTrue();
143+
}
144+
145+
@Test
146+
public void with_app_folder_system_property() {
147+
System.setProperty("APP_FOLDER", "app/public");
148+
Env env = new Env();
149+
assertThat(env.appFolder()).isEqualTo("app/public");
150+
// tear down system property
151+
System.clearProperty("APP_FOLDER");
152+
}
153+
130154
@Test
131155
public void injectLiveReloadScript() {
132156
Env env = Env.prod().withInjectLiveReloadScript(true);
@@ -170,4 +194,4 @@ public void diskCache() {
170194
assertThat(env.liveReloadServer()).isFalse();
171195
assertThat(env.diskCache()).isFalse();
172196
}
173-
}
197+
}

0 commit comments

Comments
 (0)