Skip to content

Commit 68bd056

Browse files
committed
Fix extraction
1 parent 438ac86 commit 68bd056

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

sass-embedded-host/src/main/java/de/larsgrefer/sass/embedded/connection/DartSassPackageProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ synchronized File extractPackage() throws IOException {
3737

3838
Path targetPath = getTargetPath();
3939

40-
try {
41-
IOUtils.extract(dist, targetPath);
42-
} catch (IOException e) {
43-
throw new IOException(String.format("Failed to extract %s into %s", dist, targetPath), e);
40+
if (IOUtils.isEmpty(targetPath)) {
41+
try {
42+
IOUtils.extract(dist, targetPath);
43+
} catch (IOException e) {
44+
throw new IOException(String.format("Failed to extract %s into %s", dist, targetPath), e);
45+
}
4446
}
4547

4648
File execDir = targetPath.resolve("dart-sass").toFile();

sass-embedded-host/src/main/java/de/larsgrefer/sass/embedded/util/IOUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.nio.file.Path;
1414
import java.nio.file.StandardCopyOption;
1515
import java.util.concurrent.TimeUnit;
16+
import java.util.stream.Stream;
1617
import java.util.zip.ZipEntry;
1718
import java.util.zip.ZipInputStream;
1819

@@ -23,6 +24,20 @@
2324
@RequiresApi(10000)
2425
public class IOUtils {
2526

27+
public static boolean isEmpty(Path path) throws IOException {
28+
if (Files.notExists(path)) {
29+
return true;
30+
}
31+
32+
if (Files.isDirectory(path)) {
33+
try (Stream<Path> walk = Files.walk(path)) {
34+
return walk.noneMatch(Files::isRegularFile);
35+
}
36+
}
37+
38+
return false;
39+
}
40+
2641
public static void extract(URL archiveUrl, Path destinationDir) throws IOException {
2742
String file = archiveUrl.getPath();
2843
if (file.endsWith(".zip")) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.larsgrefer.sass.embedded.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Path;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class IOUtilsTest {
14+
15+
@Test
16+
void isEmpty(@TempDir Path tempDir) throws IOException {
17+
18+
assertThat(IOUtils.isEmpty(tempDir)).isTrue();
19+
20+
File subfolder1 = new File(tempDir.toFile(), "foo");
21+
subfolder1.mkdir();
22+
23+
assertThat(IOUtils.isEmpty(tempDir)).isTrue();
24+
25+
File subfolder2 = new File(tempDir.toFile(), "bar");
26+
subfolder2.mkdir();
27+
28+
assertThat(IOUtils.isEmpty(tempDir)).isTrue();
29+
30+
File file1 = new File(subfolder1, "dummy.txt");
31+
file1.createNewFile();
32+
33+
assertThat(IOUtils.isEmpty(tempDir)).isFalse();
34+
35+
}
36+
}

0 commit comments

Comments
 (0)