Skip to content

Commit 0f5efa7

Browse files
committed
support extra source dir configuration
1 parent aae5442 commit 0f5efa7

6 files changed

Lines changed: 140 additions & 45 deletions

File tree

src/main/java/act/app/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public boolean isProd() {
165165
return mode().isProd();
166166
}
167167

168-
public AppConfig config() {
168+
public AppConfig<?> config() {
169169
return config;
170170
}
171171

src/main/java/act/app/DevModeClassLoader.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class DevModeClassLoader extends AppClassLoader {
3535
private FsChangeDetector libChangeDetector;
3636
private FsChangeDetector resourceChangeDetector;
3737
private FsChangeDetector sourceChangeDetector;
38+
private List<FsChangeDetector> extraSourceChangeDetector;
3839

3940
public DevModeClassLoader(App app) {
4041
super(app);
@@ -97,25 +98,23 @@ public Source source(String className) {
9798
}
9899

99100
private void preloadSources() {
100-
final File sourceRoot = app().layout().source(app().base());
101-
Files.filter(sourceRoot, App.F.JAVA_SOURCE, new $.Visitor<File>() {
102-
@Override
103-
public void visit(File file) throws $.Break {
104-
Source source = Source.ofFile(sourceRoot, file);
105-
if (null != source) {
106-
if (null == sources) {
107-
sources = C.newMap();
108-
}
109-
sources.put(source.className(), source);
110-
}
111-
}
112-
});
101+
List<File> baseDirs = C.newList();
102+
App app = app();
103+
baseDirs.add(app.layout().source(app.base()));
104+
for (File file : app().config().extraSourceDirs()) {
105+
baseDirs.add(file);
106+
}
113107
if ("test".equals(app().profile())) {
114-
final File testSourceRoot = app().layout().testSource(app().base());
115-
Files.filter(testSourceRoot, App.F.JAVA_SOURCE, new $.Visitor<File>() {
108+
baseDirs.add(app.layout().testSource(app.base()));
109+
for (File file : app().config().extraTestSourceDirs()) {
110+
baseDirs.add(file);
111+
}
112+
}
113+
for (final File base : baseDirs) {
114+
Files.filter(base, App.F.JAVA_SOURCE, new $.Visitor<File>() {
116115
@Override
117116
public void visit(File file) throws $.Break {
118-
Source source = Source.ofFile(sourceRoot, file);
117+
Source source = Source.ofFile(base, file);
119118
if (null != source) {
120119
if (null == sources) {
121120
sources = C.newMap();
@@ -251,6 +250,9 @@ public void detectChanges() {
251250
detectChanges(libChangeDetector);
252251
detectChanges(resourceChangeDetector);
253252
detectChanges(sourceChangeDetector);
253+
for (FsChangeDetector detector : extraSourceChangeDetector) {
254+
detectChanges(detector);
255+
}
254256
super.detectChanges();
255257
}
256258

@@ -279,6 +281,20 @@ private void setupFsChangeDetectors() {
279281
confChangeDetector = new FsChangeDetector(rsrc, App.F.CONF_FILE.or(App.F.ROUTES_FILE), confChangeListener);
280282
resourceChangeDetector = new FsChangeDetector(rsrc, null, resourceChangeListener);
281283
}
284+
285+
extraSourceChangeDetector = C.newList();
286+
for (File file : app().config().extraSourceDirs()) {
287+
extraSourceChangeDetector.add(new FsChangeDetector(file, App.F.JAVA_SOURCE, sourceChangeListener));
288+
}
289+
290+
if ("test".equals(Act.profile())) {
291+
extraSourceChangeDetector.add(new FsChangeDetector(layout.testSource(appBase), App.F.JAVA_SOURCE, sourceChangeListener));
292+
for (File file : app().config().extraTestSourceDirs()) {
293+
extraSourceChangeDetector.add(new FsChangeDetector(file, App.F.JAVA_SOURCE, sourceChangeListener));
294+
}
295+
296+
extraSourceChangeDetector.add(new FsChangeDetector(layout.testResource(appBase), null, resourceChangeListener));
297+
}
282298
}
283299

284300
private final FsEventListener sourceChangeListener = new FsEventListener() {

src/main/java/act/conf/AppConfig.java

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import act.app.ActionContext;
66
import act.app.App;
77
import act.app.AppHolder;
8+
import act.app.ProjectLayout;
89
import act.app.conf.AppConfigurator;
910
import act.app.event.AppEventId;
1011
import act.app.util.NamedPort;
@@ -30,6 +31,7 @@
3031

3132
import javax.inject.Provider;
3233
import javax.validation.MessageInterpolator;
34+
import java.io.File;
3335
import java.text.DateFormat;
3436
import java.text.SimpleDateFormat;
3537
import java.util.*;
@@ -704,6 +706,33 @@ private void _mergeUrlContext(AppConfig conf) {
704706
}
705707
}
706708

709+
private String defViewName = null;
710+
private View defView = null;
711+
712+
protected T defaultView(View view) {
713+
E.NPE(view);
714+
defView = view;
715+
return me();
716+
}
717+
718+
public View defaultView() {
719+
if (null == defViewName) {
720+
defViewName = get(AppConfigKey.VIEW_DEFAULT);
721+
if (null == defViewName) {
722+
defViewName = "rythm";
723+
}
724+
defView = Act.viewManager().view(defViewName);
725+
}
726+
return defView;
727+
}
728+
729+
private void _mergeDefaultView(AppConfig conf) {
730+
if (null == get(AppConfigKey.VIEW_DEFAULT)) {
731+
defViewName = conf.defViewName;
732+
defView = conf.defView;
733+
}
734+
}
735+
707736
private String xForwardedProtocol = null;
708737

709738
protected T forceHttps() {
@@ -1673,33 +1702,6 @@ private void _mergeTemplateHome(AppConfig conf) {
16731702
}
16741703
}
16751704

1676-
private String defViewName = null;
1677-
private View defView = null;
1678-
1679-
protected T defaultView(View view) {
1680-
E.NPE(view);
1681-
defView = view;
1682-
return me();
1683-
}
1684-
1685-
public View defaultView() {
1686-
if (null == defViewName) {
1687-
defViewName = get(AppConfigKey.VIEW_DEFAULT);
1688-
if (null == defViewName) {
1689-
defViewName = "rythm";
1690-
}
1691-
defView = Act.viewManager().view(defViewName);
1692-
}
1693-
return defView;
1694-
}
1695-
1696-
private void _mergeDefaultView(AppConfig conf) {
1697-
if (null == get(AppConfigKey.VIEW_DEFAULT)) {
1698-
defViewName = conf.defViewName;
1699-
defView = conf.defView;
1700-
}
1701-
}
1702-
17031705
private boolean pingPathResolved = false;
17041706
private String pingPath = null;
17051707

@@ -1933,6 +1935,48 @@ private void _mergeSecret(AppConfig config) {
19331935
}
19341936
}
19351937

1938+
private List<File> extraSourceDirs;
1939+
public List<File> extraSourceDirs() {
1940+
if (null == extraSourceDirs) {
1941+
String v = get(AppConfigKey.SOURCE_DIR_EXTRA);
1942+
extraSourceDirs = processExtraSourceDirs(v);
1943+
}
1944+
return extraSourceDirs;
1945+
}
1946+
1947+
private List<File> extraTestSourceDirs;
1948+
public List<File> extraTestSourceDirs() {
1949+
if (null == extraTestSourceDirs) {
1950+
String v = get(AppConfigKey.TEST_SOURCE_DIR_EXTRA);
1951+
extraTestSourceDirs = processExtraSourceDirs(v);
1952+
}
1953+
return extraTestSourceDirs;
1954+
}
1955+
1956+
private List<File> processExtraSourceDirs(String v) {
1957+
if (S.blank(v)) {
1958+
return C.list();
1959+
} else {
1960+
List<File> files = C.newList();
1961+
File base = app.base();
1962+
for (String s: v.trim().split("[;:]+")) {
1963+
s = s.trim();
1964+
File file;
1965+
if (s.startsWith("/") || s.startsWith("\\")) {
1966+
file = new File(s);
1967+
} else {
1968+
file = ProjectLayout.Utils.file(base, s);
1969+
}
1970+
if (!file.isDirectory()) {
1971+
logger.warn("Cannot locate extra source dir: %s", s);
1972+
} else {
1973+
files.add(file);
1974+
}
1975+
}
1976+
return C.list(files);
1977+
}
1978+
}
1979+
19361980
private MessageInterpolator _messageInterpolator = null;
19371981
protected T messageInterpolator(MessageInterpolator messageInterpolator) {
19381982
this._messageInterpolator = $.notNull(messageInterpolator);

src/main/java/act/conf/AppConfigKey.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,20 @@ public <T> T val(Map<String, ?> configuration) {
625625
*/
626626
SESSION_SECURE("session.secure.enabled"),
627627

628+
/**
629+
* Specify extra source folder. Not used in PROD mode.
630+
*
631+
* Default value: null
632+
*/
633+
SOURCE_DIR_EXTRA("source.dir.extra"),
634+
635+
/**
636+
* Specify extra test source folder. Not used in PROD mode.
637+
*
638+
* Default value: null
639+
*/
640+
TEST_SOURCE_DIR_EXTRA("test_source.dir.extra"),
641+
628642
/**
629643
* {@code source_version} specifies the java version
630644
* of the src code. This configuration is used only

src/main/java/act/util/Files.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public static void filter(File baseDir, $.F1<String, Boolean> filter, $.F1<File,
4040
}
4141
}
4242

43+
public static List<File> filter(List<File> baseDirs, $.F1<String, Boolean> filter) {
44+
ListBuilder<File> list = ListBuilder.create(500);
45+
filter(baseDirs, filter, C.F.addTo(list));
46+
return list.toList();
47+
}
48+
49+
public static void filter(List<File> baseDirs, $.F1<String, Boolean> filter, $.F1<File, ?> visitor) {
50+
C.List<File> files = C.newList();
51+
for (File baseDir : baseDirs) {
52+
files.addAll(C.listOf(baseDir.listFiles()));
53+
}
54+
for (File file: files) {
55+
if (isValidDir(file)) {
56+
filter(file, filter, visitor);
57+
} else {
58+
visitor.apply(file);
59+
}
60+
}
61+
}
62+
4363
private static boolean isValidDir(File file) {
4464
return file.isDirectory() && !isHiddenDir(file);
4565
}

src/main/java/act/view/rythm/Tags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package act.view.rythm;
22

3+
import act.Act;
34
import act.app.ActionContext;
45
import act.route.Router;
56
import org.osgl.inject.annotation.TypeOf;
@@ -56,7 +57,7 @@ protected void call(__ParameterList parameterList, __Body body) {
5657
}
5758

5859
ActionContext context = ActionContext.current();
59-
Router router = context.router();
60+
Router router = null == context ? Act.app().router() : context.router();
6061

6162
if (value.contains("/") && fullUrl) {
6263
int n = parameterList.size();

0 commit comments

Comments
 (0)