@@ -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 }
0 commit comments