Skip to content

Commit 75f4648

Browse files
authored
Merge pull request #15 from entityc/bobgarner/install-define-arg
Allows path of a repository to be set using a command line define variable.
2 parents c0749fc + e129f2a commit 75f4648

File tree

10 files changed

+137
-38
lines changed

10 files changed

+137
-38
lines changed

src/main/antlr4/org/entityc/compiler/EntityLanguage.g4

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,10 @@ formatStatement
11801180
: FORMAT id jsonObj
11811181
;
11821182

1183+
defineVariableString
1184+
: STRING | id
1185+
;
1186+
11831187
directory
11841188
: (OUTPUT | DIRECTORY) id '{' outputBody '}'
11851189
;
@@ -1192,7 +1196,7 @@ outputBody
11921196
;
11931197

11941198
outputPath
1195-
: PATH (STRING|id)
1199+
: PATH defineVariableString
11961200
;
11971201

11981202
templates
@@ -1263,7 +1267,7 @@ repositoryName
12631267
;
12641268

12651269
repositoryPath
1266-
: PATH STRING
1270+
: PATH defineVariableString
12671271
;
12681272

12691273
repositoryTag

src/main/java/org/entityc/compiler/ASTVisitor.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,21 @@ public MTMethodCall visitMethodCall(EntityLanguageParser.MethodCallContext ctx)
647647
return methodCall;
648648
}
649649

650+
@Override
651+
public String visitDefineVariableString(EntityLanguageParser.DefineVariableStringContext ctx) {
652+
String path;
653+
if (ctx.id() != null) {
654+
String idPath = idText(ctx.id());
655+
if (idPath == null) {
656+
ECLog.logFatal("Command line define variable \"" + ctx.id().getText() + "\" not set.");
657+
}
658+
path = idPath;
659+
} else {
660+
path = ECStringUtil.ProcessParserString(ctx.STRING().getText());
661+
}
662+
return path;
663+
}
664+
650665
@Override
651666
public Object visitRepository(EntityLanguageParser.RepositoryContext ctx) {
652667
MTRepository repository = new MTRepository(ctx, ctx.id().getText());
@@ -670,7 +685,10 @@ public Object visitRepository(EntityLanguageParser.RepositoryContext ctx) {
670685

671686
List<EntityLanguageParser.RepositoryPathContext> repositoryPathContexts = repositoryBodyContext.repositoryPath();
672687
if (repositoryPathContexts != null && repositoryPathContexts.size() > 0) {
673-
repository.setPath(ECStringUtil.ProcessParserString(repositoryPathContexts.get(0).STRING().getText()));
688+
689+
EntityLanguageParser.RepositoryPathContext pathContext = repositoryPathContexts.get(0);
690+
String path = visitDefineVariableString(pathContext.defineVariableString());
691+
repository.setPath(ECStringUtil.ProcessParserString(path));
674692
}
675693

676694
List<EntityLanguageParser.RepositoryTypeContext> repositoryTypeContexts = repositoryBodyContext.repositoryType();
@@ -2213,17 +2231,8 @@ public MTDirectory visitDirectory(EntityLanguageParser.DirectoryContext ctx) {
22132231
if (body.outputPath().size() == 0) {
22142232
ECLog.logFatal("An output must define a path.");
22152233
}
2216-
String path;
22172234
EntityLanguageParser.OutputPathContext outputPathContext = body.outputPath().get(body.outputPath().size() - 1);
2218-
if (outputPathContext.id() != null) {
2219-
String idPath = idText(outputPathContext.id());
2220-
if (idPath == null) {
2221-
ECLog.logFatal("Command line define variable \"" + outputPathContext.id().getText() + "\" not set.");
2222-
}
2223-
path = idPath;
2224-
} else {
2225-
path = ECStringUtil.ProcessParserString(outputPathContext.STRING().getText());
2226-
}
2235+
String path = visitDefineVariableString(outputPathContext.defineVariableString());
22272236
output.setPath(path);
22282237
currentConfiguration.addOutput(output);
22292238
return output;

src/main/java/org/entityc/compiler/EntityCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353
public class EntityCompiler {
5454

55-
public static final String COMPILER_VERSION = "0.13.3";
56-
public static final String LANGUAGE_VERSION = "0.12.4";
55+
public static final String COMPILER_VERSION = "0.14.0";
56+
public static final String LANGUAGE_VERSION = "0.12.5";
5757
private static final Map<String, String> defineValues = new HashMap<>();
5858
private static final Set<String> templateSearchPath = new HashSet<>();
5959
private static CommandLine commandLine;

src/main/java/org/entityc/compiler/transform/template/tree/FTInstall.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import org.entityc.compiler.model.config.MTRepository;
1717
import org.entityc.compiler.model.config.MTRepositoryImport;
1818
import org.entityc.compiler.model.config.MTTemplate;
19-
import org.entityc.compiler.model.domain.MTDomain;
20-
import org.entityc.compiler.model.language.MTLanguage;
2119
import org.entityc.compiler.repository.RepositoryCache;
2220
import org.entityc.compiler.repository.RepositoryFile;
2321
import org.entityc.compiler.repository.RepositoryImportManager;
@@ -51,7 +49,7 @@
5149
public class FTInstall extends FTNode {
5250

5351
private final FTExpression sourceExpression;
54-
private final FTExpression destNamespaceExpression;
52+
private final FTExpression destExpression;
5553
private String sourceRepositoryName;
5654
private boolean copyOnly;
5755

@@ -69,9 +67,9 @@ public FTInstall(ParserRuleContext ctx,
6967
+ "The path is relative to the directory in which the template was configured.")
7068
FTExpression destExpression) {
7169
super(ctx);
72-
this.copyOnly = copy;
73-
this.sourceExpression = sourceExpression;
74-
this.destNamespaceExpression = destExpression;
70+
this.copyOnly = copy;
71+
this.sourceExpression = sourceExpression;
72+
this.destExpression = destExpression;
7573
}
7674

7775
public boolean isCopyOnly() {
@@ -91,7 +89,7 @@ public void transform(FTTransformSession session) {
9189
if (!sourceExpression.isConstant()) {
9290
ECLog.logFatal("Only string constant supported for source directory/file.");
9391
}
94-
Object destFilepathObject = destNamespaceExpression.getValue(session);
92+
Object destFilepathObject = destExpression.getValue(session);
9593
if (!(destFilepathObject instanceof String)) {
9694
ECLog.logFatal(this, "Destination expression of install instruction must resolve to a string value.");
9795
}
@@ -112,18 +110,16 @@ public void transform(FTTransformSession session) {
112110

113111
//ECLog.logInfo("Full relative path for install source file: " + repository.getPath() + "/" + sourceFilePath);
114112

115-
String sourceFileExtension = null;
116-
String sourceFilename = null;
113+
String destFileExtension = null;
114+
String destFilename = null;
117115
int lastPathSeparatorIndex = sourceFilePath.lastIndexOf(File.separator);
118116
int lastExtensionIndex = sourceFilePath.lastIndexOf(".");
119117
if (lastExtensionIndex != -1) {
120118
if (lastPathSeparatorIndex == -1 || lastExtensionIndex > lastPathSeparatorIndex) {
121-
sourceFileExtension = sourceFilePath.substring(lastExtensionIndex + 1);
122-
sourceFilePath = sourceFilePath.substring(0, lastExtensionIndex);
119+
destFileExtension = sourceFilePath.substring(lastExtensionIndex + 1);
120+
destFilename = sourceFilePath.substring(0, lastExtensionIndex);
123121
if (lastPathSeparatorIndex != -1) {
124-
sourceFilename = sourceFilePath.substring(lastPathSeparatorIndex + 1);
125-
} else {
126-
sourceFilename = sourceFilePath;
122+
destFilename = destFilename.substring(lastPathSeparatorIndex + 1);
127123
}
128124
}
129125
}
@@ -134,13 +130,10 @@ public void transform(FTTransformSession session) {
134130
repositoryImport.setRepositoryName(sourceRepositoryName);
135131
repositoryImport.setFilename(sourceFilePath);
136132
RepositoryFile repositoryFile = importManager.importFromRepository(session.getConfiguration().getSpace(),
137-
repositoryImport, sourceFileExtension,
133+
repositoryImport, "eml",
138134
false);
139135
File fileToInstall = new File(repositoryFile.getFilepath());
140136

141-
MTDomain domain = (MTDomain) session.getValue("domain");
142-
MTLanguage language = (MTLanguage) session.getValue("language");
143-
144137
String sourceFilenameToInstall = null;
145138
// need to run this as if it is a template being run within the running template but its output automatically goes to the destination directory specified.
146139
try {
@@ -167,9 +160,9 @@ public void transform(FTTransformSession session) {
167160
}
168161
FTTemplate ftTemplate = fileTemplateTransform.getTemplate();
169162
// add a file instruction to write the entire "template" to the destination
170-
FTFile ftFile = new FTFile(null, ftTemplate, false, destNamespaceExpression,
171-
new FTConstant(null, sourceFilename),
172-
new FTConstant(null, sourceFileExtension));
163+
FTFile ftFile = new FTFile(null, ftTemplate, false, destExpression,
164+
new FTConstant(null, destFilename),
165+
new FTConstant(null, destFileExtension));
173166
if (copyOnly) {
174167
try {
175168
ftFile.open(session);
@@ -206,7 +199,7 @@ public boolean format(TemplateFormatController formatController, int indentLevel
206199
success = false;
207200
}
208201
formatController.addInstructionInside(RequiredSpace, " ", this.getStartLineNumber());
209-
if (!destNamespaceExpression.format(formatController, indentLevel)) {
202+
if (!destExpression.format(formatController, indentLevel)) {
210203
success = false;
211204
}
212205
formatController.addInstructionEnd(this);

src/main/java/org/entityc/compiler/util/ECStringUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,13 @@ public static String PathWithSeparator(String path) {
401401
}
402402
return path + File.separator;
403403
}
404+
405+
public static String ExtensionFromPath(String fullPathWithExtension) {
406+
String extension = fullPathWithExtension;
407+
if (fullPathWithExtension.contains(".")) {
408+
extension = fullPathWithExtension.substring(fullPathWithExtension.lastIndexOf(".") + 1);
409+
}
410+
return extension;
411+
}
412+
404413
}

test/java/org/entityc/compiler/EntityCompilerTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private void runTemplateTest(String testName) throws IOException {
2828
final String TestResourceDir = BASE_RESOURCE_DIR + "/" + testName;
2929
String strTmp = System.getProperty("java.io.tmpdir");
3030
String sourceFile = TestResourceDir + "/" + testName + ".edl";
31-
EntityCompiler.main(new String[]{"build", "Config", sourceFile, "-tp", TestResourceDir, "-D", "TEMP_DIR=" + strTmp});
31+
EntityCompiler.main(new String[]{"build", "Config", sourceFile, "-tp", TestResourceDir, "-D", "TEMP_DIR=" + strTmp, "-D", "TEST_DIR=" + TestResourceDir});
3232
String actual = FileUtils.readFileToString(new File(strTmp + File.separator + testName + ".txt"));
3333
String expected = FileUtils.readFileToString(new File(TestResourceDir + "/" + testName + "Expected.txt"));
3434
assertEquals(expected, actual);
@@ -106,4 +106,16 @@ void templateFormatting() throws IOException {
106106
String expected = FileUtils.readFileToString(new File(expectedFile));
107107
assertEquals(expected, actual);
108108
}
109+
110+
@Test
111+
void templateInstall() throws IOException {
112+
final String testName = "TemplateInstall";
113+
final String TestResourceDir = BASE_RESOURCE_DIR + "/" + testName;
114+
String strTmp = System.getProperty("java.io.tmpdir");
115+
String sourceFile = TestResourceDir + "/" + testName + ".edl";
116+
EntityCompiler.main(new String[]{"build", "Config", sourceFile, "-tp", TestResourceDir, "-D", "TEMP_DIR=" + strTmp, "-D", "TEST_DIR=" + TestResourceDir});
117+
String actual = FileUtils.readFileToString(new File(strTmp + File.separator + "main/java/Source.java"));
118+
String expected = FileUtils.readFileToString(new File(TestResourceDir + File.separator + "Source.java"));
119+
assertEquals(expected, actual);
120+
}
109121
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$[domain Code]
2+
$[let destDir = "main/java"]
3+
$[install "Source.java" destDir]
4+
$[log info]Installed Source.java in ${destDir}$[/log]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.entityc.code;
2+
3+
public class Source {
4+
5+
String entity1() {
6+
return "Entity1";
7+
}
8+
9+
String entity2() {
10+
return "Entity2";
11+
}
12+
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ${domain.namespace};
2+
3+
public class Source {
4+
5+
$[ foreach entity in space.entities ]
6+
String ${entity.name|uncapitalize}() {
7+
return "${entity.name}";
8+
}
9+
10+
$[/ foreach ]
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
space outer {
2+
}
3+
4+
module test {
5+
6+
entity Entity1 {
7+
primarykey uuid entity1_id
8+
9+
attributes {
10+
int32 someint
11+
}
12+
}
13+
14+
entity Entity2 {
15+
primarykey uuid entity2_id
16+
17+
attributes {
18+
float somefloat
19+
}
20+
}
21+
}
22+
23+
domain Code {
24+
namespace org.entityc.code
25+
}
26+
27+
configuration Config {
28+
repository Local {
29+
type local
30+
path $(TEST_DIR)
31+
}
32+
33+
output testOutput {
34+
path $(TEMP_DIR:"/tmp")
35+
}
36+
37+
templates {
38+
import from Local
39+
40+
template Install {
41+
output primary testOutput
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)