44import java .io .IOException ;
55import java .io .UncheckedIOException ;
66import java .nio .charset .StandardCharsets ;
7+ import java .nio .file .DirectoryStream ;
78import java .nio .file .Files ;
89import java .nio .file .Path ;
910import java .nio .file .Paths ;
11+ import java .nio .file .StandardCopyOption ;
1012import java .text .SimpleDateFormat ;
1113import java .util .ArrayList ;
1214import java .util .HashMap ;
@@ -56,6 +58,7 @@ public final class UpdaterUtils {
5658
5759 private static final Logger _LOG = LoggerFactory .getLogger (UpdaterUtils .class );
5860
61+ private static final Path TEST_ALGORITHMS_DIR = Paths .get ("src" , "test" , "resources" , "algorithms" );
5962 private static final Pattern _ID_CHARACTERS = Pattern .compile ("[a-z0-9_]+" );
6063
6164 /**
@@ -208,12 +211,10 @@ else if (unusedTableIds.contains(id) && !id.startsWith("conversion_"))
208211
209212 // Zip up schemas/, tables/, glossary/
210213 Path versionDir = Paths .get (baseDirectory );
211- zipAlgorithm (algorithm , version , versionDir );
214+ Path zipPath = zipAlgorithm (algorithm , version , versionDir );
215+ _LOG .info ("Created ZIP: {}/{}-{}.zip" , versionDir .getParent ().getParent (), algorithm , version );
212216
213- _LOG .info ("Created ZIP: {}/{}-{}.zip" ,
214- versionDir .getParent ().getParent (),
215- algorithm ,
216- version );
217+ copyZipToProject (algorithm , zipPath );
217218
218219 stopwatch .stop ();
219220 _LOG .info ("Completed in {}" , stopwatch );
@@ -246,7 +247,8 @@ private static int purgeDirectory(File dir) {
246247 /**
247248 * Zip the algorithm
248249 */
249- private static void zipAlgorithm (String algorithm , String version , Path versionDir ) throws IOException {
250+ private static Path zipAlgorithm (String algorithm , String version , Path versionDir ) throws IOException {
251+ // versionDir = .../algorithms/<algorithm>/<version>
250252 Path algorithmsDir = versionDir .getParent ().getParent (); // .../algorithms
251253 Path zipPath = algorithmsDir .resolve (algorithm + "-" + version + ".zip" );
252254
@@ -273,6 +275,36 @@ private static void zipAlgorithm(String algorithm, String version, Path versionD
273275 }
274276 }
275277 }
278+
279+ return zipPath ;
280+ }
281+
282+ /**
283+ * Move zip file into project deleting the existing one; only one version of an algorithm should exist in the project at once
284+ */
285+ private static void copyZipToProject (String algorithm , Path zipPath ) throws IOException {
286+ if (!Files .exists (TEST_ALGORITHMS_DIR )) {
287+ _LOG .info ("Test algorithms directory {} does not exist; skipping copy of {}" , TEST_ALGORITHMS_DIR .toAbsolutePath (), zipPath .getFileName ());
288+ return ;
289+ }
290+
291+ if (!Files .isDirectory (TEST_ALGORITHMS_DIR ))
292+ throw new IllegalStateException ("Not a directory: " + TEST_ALGORITHMS_DIR .toAbsolutePath ());
293+
294+ // delete existing zips for this algorithm
295+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (TEST_ALGORITHMS_DIR , algorithm + "-*.zip" )) {
296+ for (Path oldZip : stream ) {
297+ _LOG .info ("Deleting old ZIP {}" , oldZip .toAbsolutePath ());
298+ Files .deleteIfExists (oldZip );
299+ }
300+ }
301+
302+ // copy new zip
303+ Path dest = TEST_ALGORITHMS_DIR .resolve (zipPath .getFileName ());
304+ Files .createDirectories (TEST_ALGORITHMS_DIR );
305+ Files .copy (zipPath , dest , StandardCopyOption .REPLACE_EXISTING );
306+
307+ _LOG .info ("Installed {} into {}" , dest .getFileName (), dest .toAbsolutePath ());
276308 }
277309
278310}
0 commit comments