-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDartSassPackageProvider.java
More file actions
104 lines (81 loc) · 3.36 KB
/
Copy pathDartSassPackageProvider.java
File metadata and controls
104 lines (81 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package de.larsgrefer.sass.embedded.connection;
import androidx.annotation.RequiresApi;
import de.larsgrefer.sass.embedded.util.DirCleaner;
import de.larsgrefer.sass.embedded.util.IOUtils;
import de.larsgrefer.sass.embedded.util.PropertyUtils;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@Slf4j
@RequiresApi(1000)
public abstract class DartSassPackageProvider {
private File dartSassExecutable;
@Nullable
public File getDartSassExecutable() throws IOException {
if (dartSassExecutable == null || !dartSassExecutable.exists()) {
this.dartSassExecutable = extractPackage();
}
return dartSassExecutable;
}
synchronized File extractPackage() throws IOException {
URL dist = getPackageUrl();
if (dist == null) {
return null;
}
Path targetPath = getTargetPath();
if (IOUtils.isEmpty(targetPath)) {
try {
IOUtils.extract(dist, targetPath);
} catch (IOException e) {
throw new IOException(String.format("Failed to extract %s into %s", dist, targetPath), e);
}
}
File execDir = targetPath.resolve("dart-sass").toFile();
File result = findExecutable(execDir);
if (result == null && !IOUtils.isEmpty(targetPath)) {
log.info("No unique dart-sass executable found in {}, extracting {} again", execDir, dist);
try {
IOUtils.extract(dist, targetPath);
} catch (IOException e) {
throw new IOException(String.format("Failed to extract %s into %s", dist, targetPath), e);
}
result = findExecutable(execDir);
}
if (result == null) {
throw new IllegalStateException("No (unique) executable file found in " + execDir);
}
result.setExecutable(true, true);
return result;
}
private File findExecutable(File execDir) {
File[] execFile = execDir.listFiles(pathname -> pathname.isFile() && pathname.getName().startsWith("sass"));
if (execFile == null || execFile.length != 1) {
return null;
}
return execFile[0];
}
Path getTargetPath() throws IOException {
String dartSassVersion = PropertyUtils.getDartSassVersion();
String hostVersion = PropertyUtils.getHostVersion();
if (hostVersion == null || dartSassVersion == null) {
// No version information available, use random Path and cleanup afterward.
Path tempDirectory = Files.createTempDirectory("dart-sass");
Runtime.getRuntime().addShutdownHook(new Thread(new DirCleaner(tempDirectory)));
return tempDirectory;
}
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
String target = String.format("%s/%s/dart-sass/%s", this.getClass().getName(), hostVersion, dartSassVersion);
File targetDir = new File(tmpDir, target);
if (targetDir.isDirectory() || targetDir.mkdirs()) {
return targetDir.toPath();
} else {
throw new IOException(targetDir + " is not directory or can't be created");
}
}
@Nullable
protected abstract URL getPackageUrl() throws IOException;
}