From f3e62584ff98ff60d64aafe73df0f2fefe2d6343 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Sat, 8 Jun 2024 01:37:53 +0200 Subject: [PATCH] If WebJar version is a "-snapshot", overwrite existing files when extracing --- src/main/java/org/webjars/WebJarExtractor.java | 6 ++++-- src/test/java/org/webjars/WebJarAssetLocatorTest.java | 3 ++- src/test/java/org/webjars/WebJarExtractorTest.java | 10 ++++++++++ .../resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js | 1 + .../webjars/wip/1.0.0-SNAPSHOT/caramba.js.map | 0 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js create mode 100644 src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js.map diff --git a/src/main/java/org/webjars/WebJarExtractor.java b/src/main/java/org/webjars/WebJarExtractor.java index 265575b..725be85 100644 --- a/src/main/java/org/webjars/WebJarExtractor.java +++ b/src/main/java/org/webjars/WebJarExtractor.java @@ -15,6 +15,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFilePermission; import java.util.Map; @@ -138,10 +139,11 @@ private static void extractResource(@Nonnull String webJarName, @Nonnull WebJarI final String newPath = resource.getPath().substring(prefix.length()); final String relativeName = String.format("%s%s%s", webJarId, File.separator, newPath); final File newFile = new File(to, relativeName); - if (!newFile.exists()) { + final boolean allowOverwrite = webJarInfo.getVersion() != null && webJarInfo.getVersion().toLowerCase().endsWith("-snapshot"); + if (!newFile.exists() || allowOverwrite) { try { newFile.getParentFile().mkdirs(); - Files.copy(inputStream, newFile.toPath()); + Files.copy(inputStream, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); inputStream.close(); Set resourcePerms = resource.getPosixFilePermissions(); if (resourcePerms != null && Files.getFileStore(newFile.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) { diff --git a/src/test/java/org/webjars/WebJarAssetLocatorTest.java b/src/test/java/org/webjars/WebJarAssetLocatorTest.java index c010a4e..714920f 100644 --- a/src/test/java/org/webjars/WebJarAssetLocatorTest.java +++ b/src/test/java/org/webjars/WebJarAssetLocatorTest.java @@ -248,12 +248,13 @@ public void invalid_webjar_path_should_return_null() { public void should_get_a_list_of_webjars() { Map webjars = new WebJarAssetLocator().getWebJars(); - assertThat(webjars.size(), is(38)); // this is the pom.xml ones plus the test resources (spaces, foo, bar-node, multiple) + assertThat(webjars.size(), is(39)); // this is the pom.xml ones plus the test resources (spaces, foo, bar-node, multiple) assertThat(webjars.get("bootstrap"), is("3.1.1")); assertThat(webjars.get("less-node"), is("1.6.0")); assertThat(webjars.get("jquery"), is("2.1.0")); assertThat(webjars.get("angularjs"), is("1.2.11")); assertThat(webjars.get("virtual-keyboard"), is("1.30.1")); + assertThat(webjars.get("wip"), is("1.0.0-SNAPSHOT")); } @Test diff --git a/src/test/java/org/webjars/WebJarExtractorTest.java b/src/test/java/org/webjars/WebJarExtractorTest.java index 4015247..f57dd01 100644 --- a/src/test/java/org/webjars/WebJarExtractorTest.java +++ b/src/test/java/org/webjars/WebJarExtractorTest.java @@ -63,6 +63,16 @@ public void extractWebJarShouldNotExtractWhenFileExists() throws Exception { assertFileContains(file, "Hello"); } + @Test + public void extractWebJarShouldExtractWhenFileExistsAndWebJarVersionIsSnapshot() throws Exception { + WebJarExtractor extractor = new WebJarExtractor(createClassLoader()); + File cacheDir = createTmpDir(); + File file = new File(cacheDir, "wip/caramba.js"); + createFile(file, "Hello"); + extractor.extractWebJarTo("wip", cacheDir); + assertFileContains(file, "var just = 'do it';"); + } + @Test public void extractAllWebJarsShouldExtractWhenFileDoesntExist() throws Exception { WebJarExtractor extractor = new WebJarExtractor(createClassLoader()); diff --git a/src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js b/src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js new file mode 100644 index 0000000..eb84477 --- /dev/null +++ b/src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js @@ -0,0 +1 @@ +var just = 'do it'; \ No newline at end of file diff --git a/src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js.map b/src/test/resources/META-INF/resources/webjars/wip/1.0.0-SNAPSHOT/caramba.js.map new file mode 100644 index 0000000..e69de29