Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit cc0725b

Browse files
committed
#128 Adding diff'ing for generated files
1 parent e7d7228 commit cc0725b

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

src/main/java/com/marklogic/appdeployer/command/es/GenerateModelArtifactsCommand.java

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,14 @@ protected void generateInstanceConverter(AppConfig appConfig, GeneratedCode code
9999
File out = new File(esDir, code.getTitle() + "-" + code.getVersion() + ".xqy");
100100
String logMessage = "Wrote instance converter to: ";
101101
if (out.exists()) {
102+
if (!fileHasDifferentContent(out, instanceConverter)) {
103+
if (logger.isInfoEnabled()) {
104+
logger.info("Instance converter matches file, so not modifying: " + out.getAbsolutePath());
105+
}
106+
return;
107+
}
102108
out = new File(esDir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xqy");
103-
logMessage = "Instance converter file already exists; writing new generated copy to: ";
109+
logMessage = "Instance converter does not match existing file, so writing to: ";
104110
}
105111
try {
106112
FileCopyUtils.copy(instanceConverter.getBytes(), out);
@@ -121,8 +127,14 @@ protected void generateSearchOptions(GeneratedCode code, File modulesDir) {
121127
File out = new File(optionsDir, code.getTitle() + ".xml");
122128
String logMessage = "Wrote search options to: ";
123129
if (out.exists()) {
130+
if (!fileHasDifferentContent(out, searchOptions)) {
131+
if (logger.isInfoEnabled()) {
132+
logger.info("Search options matches file, so not modifying: " + out.getAbsolutePath());
133+
}
134+
return;
135+
}
124136
out = new File(optionsDir, code.getTitle() + "-GENERATED.xml");
125-
logMessage = "Search options file already exists; writing new generated copy to: ";
137+
logMessage = "Search options does not match existing file, so writing to: ";
126138
}
127139
try {
128140
FileCopyUtils.copy(searchOptions.getBytes(), out);
@@ -150,8 +162,14 @@ protected void generateDatabaseProperties(AppConfig appConfig, GeneratedCode cod
150162
File out = new File(dbDir, "content-database.json");
151163
String logMessage = "Wrote database properties to: ";
152164
if (out.exists()) {
165+
if (!fileHasDifferentContent(out, props)) {
166+
if (logger.isInfoEnabled()) {
167+
logger.info("Database properties matches file, so not modifying: " + out.getAbsolutePath());
168+
}
169+
return;
170+
}
153171
out = new File(dbDir, "content-database-GENERATED.json");
154-
logMessage = "Database properties file already exists; writing new generated copy to: ";
172+
logMessage = "Database properties does not match existing file, so writing to: ";
155173
}
156174
try {
157175
FileCopyUtils.copy(props.getBytes(), out);
@@ -183,8 +201,14 @@ protected void generateSchema(AppConfig appConfig, GeneratedCode code) {
183201
File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".xsd");
184202
String logMessage = "Wrote schema to: ";
185203
if (out.exists()) {
204+
if (!fileHasDifferentContent(out, schema)) {
205+
if (logger.isInfoEnabled()) {
206+
logger.info("Schema matches file, so not modifying: " + out.getAbsolutePath());
207+
}
208+
return;
209+
}
186210
out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xsd");
187-
logMessage = "Schema file already exists; writing new generated copy to: ";
211+
logMessage = "Schema does not match existing file, so writing to: ";
188212
}
189213
try {
190214
FileCopyUtils.copy(schema.getBytes(), out);
@@ -205,8 +229,14 @@ protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode cod
205229
File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".tdex");
206230
String logMessage = "Wrote extraction template to: ";
207231
if (out.exists()) {
232+
if (!fileHasDifferentContent(out, template)) {
233+
if (logger.isInfoEnabled()) {
234+
logger.info("Extraction template matches file, so not modifying: " + out.getAbsolutePath());
235+
}
236+
return;
237+
}
208238
out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.tdex");
209-
logMessage = "Extraction template already exists; writing new generated copy to: ";
239+
logMessage = "Extraction template does not match existing file, so writing to: ";
210240
}
211241
try {
212242
FileCopyUtils.copy(template.getBytes(), out);
@@ -219,6 +249,42 @@ protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode cod
219249
}
220250
}
221251

252+
/**
253+
* Determines if a file matches generated code, in which case we don't need to do anything further with the
254+
* generated code.
255+
*
256+
* @param existingFile
257+
* @param content
258+
* @return
259+
*/
260+
protected boolean fileHasDifferentContent(File existingFile, String content) {
261+
try {
262+
String fileContent = new String(FileCopyUtils.copyToByteArray(existingFile));
263+
fileContent = removeGeneratedAtTimestamp(fileContent);
264+
content = removeGeneratedAtTimestamp(content);
265+
return !fileContent.equals(content);
266+
} catch (IOException e) {
267+
// Shouldn't occur, but if it does, treat it as the file having different content
268+
return true;
269+
}
270+
}
271+
272+
/**
273+
* The instance converter module has a timestamp in it that has to be removed in order to tell if its contents match
274+
* that of an existing instance converter; the timestamp will of course nearly always be different.
275+
*
276+
* @param content
277+
* @return
278+
*/
279+
protected String removeGeneratedAtTimestamp(String content) {
280+
int pos = content.indexOf("Generated at timestamp");
281+
if (pos > -1) {
282+
int end = content.indexOf(":)", pos);
283+
return content.substring(0, pos) + content.substring(end + 2);
284+
}
285+
return content;
286+
}
287+
222288
public void setOptionsPath(String optionsPath) {
223289
this.optionsPath = optionsPath;
224290
}

src/test/java/com/marklogic/appdeployer/command/es/GenerateModelArtifactsTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class GenerateModelArtifactsTest extends AbstractAppDeployerTest {
1818
public void tearDown() {
1919
initializeAppDeployer(new DeployContentDatabasesCommand(), new DeploySchemasDatabaseCommand(),
2020
new DeployRestApiServersCommand());
21-
undeploySampleApp();
21+
//undeploySampleApp();
2222
}
2323

2424
@Test
@@ -48,15 +48,16 @@ public void test() {
4848

4949
deploySampleApp();
5050

51-
assertTrue(new File(projectPath, "src/main/ml-modules/ext/entity-services/Race-0.0.1-GENERATED.xqy").exists());
52-
assertTrue(new File(projectPath, "src/main/ml-modules/options/Race-GENERATED.xml").exists());
53-
assertTrue(new File(projectPath, "src/main/ml-config/databases/content-database-GENERATED.json").exists());
54-
assertTrue(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.xsd").exists());
55-
assertTrue(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.tdex").exists());
51+
// These shouldn't exist because the content is the same
52+
assertFalse(new File(projectPath, "src/main/ml-modules/ext/entity-services/Race-0.0.1-GENERATED.xqy").exists());
53+
assertFalse(new File(projectPath, "src/main/ml-modules/options/Race-GENERATED.xml").exists());
54+
assertFalse(new File(projectPath, "src/main/ml-config/databases/content-database-GENERATED.json").exists());
55+
assertFalse(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.xsd").exists());
56+
assertFalse(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.tdex").exists());
5657

5758
// Make sure none of these files break when they're deployed
5859
initializeAppDeployer(new DeployContentDatabasesCommand(), new DeploySchemasDatabaseCommand(),
5960
new LoadSchemasCommand(), new LoadModulesCommand());
60-
deploySampleApp();
61+
//deploySampleApp();
6162
}
6263
}

0 commit comments

Comments
 (0)