Repository navigation
Expand file tree
/
Copy pathupdate_external_resources.java
More file actions
144 lines (124 loc) · 6.02 KB
/
Copy pathupdate_external_resources.java
File metadata and controls
144 lines (124 loc) · 6.02 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.google.guava:guava:33.4.0-jre
//DEPS commons-io:commons-io:2.18.0
import com.google.common.io.MoreFiles;
import org.apache.commons.io.FileUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class update_external_resources {
private record ExternalRepo(String url, String directory, String... sparsePaths) {
}
public static void main(String[] args) {
Path tempDir = null;
try {
// 1. Create a temporary directory
tempDir = Files.createTempDirectory("jbang-git-clone-");
System.out.println("Created temporary directory: " + tempDir);
List<String> commitLogs = new ArrayList<>();
// 2. Repositories to fetch
ExternalRepo html5libTests = new ExternalRepo(
"https://github.com/html5lib/html5lib-tests.git",
"html5lib-tests",
"/tokenizer/**");
ExternalRepo wpt = new ExternalRepo(
"https://github.com/web-platform-tests/wpt.git",
"wpt",
"/html/syntax/parsing/resources/**");
for (ExternalRepo repo : List.of(html5libTests, wpt)) {
cloneRepo(tempDir, repo);
String hash = getCommitHash(tempDir.resolve(repo.directory()));
commitLogs.add(repo.url() + " " + hash);
}
Path externalResourcesDir = Path.of("external-resources");
if (Files.exists(externalResourcesDir)) {
MoreFiles.deleteRecursively(externalResourcesDir);
}
// 3. Copy html5lib-tests/tokenizer
copyDirectoryIfExists(
tempDir.resolve("html5lib-tests").resolve("tokenizer"),
externalResourcesDir.resolve("html5lib-tests").resolve("tokenizer"));
// 4. Copy wpt/html/syntax/parsing/resources
copyDirectoryIfExists(
tempDir.resolve("wpt").resolve("html").resolve("syntax").resolve("parsing").resolve("resources"),
externalResourcesDir.resolve("wpt").resolve("html").resolve("syntax").resolve("parsing").resolve("resources"));
// 5. Write external-resources/commit.md
Files.createDirectories(externalResourcesDir);
Path commitFile = externalResourcesDir.resolve("commit.md");
System.out.println("\nWriting commit info to " + commitFile.toAbsolutePath() + " ...");
Files.write(commitFile, commitLogs, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
System.out.println("Successfully wrote commit.md.");
} catch (Exception e) {
System.err.println("An error occurred during execution: " + e.getMessage());
e.printStackTrace();
} finally {
// 5. Clean up temporary directory using Guava
if (tempDir != null && Files.exists(tempDir)) {
System.out.println("\nCleaning up temporary directory: " + tempDir);
try {
MoreFiles.deleteRecursively(tempDir);
System.out.println("Temporary directory deleted successfully.");
} catch (IOException e) {
System.err.println("Failed to delete temp directory: " + e.getMessage());
}
}
}
}
/**
* Clones only the requested sparse paths using a partial clone.
*/
private static void cloneRepo(Path workDir, ExternalRepo repo) throws IOException, InterruptedException {
System.out.println("\nCloning: " + repo.url() + " -> " + repo.directory() + " ...");
runCommand(workDir, List.of(
"git", "clone",
"--depth", "1",
"--filter=blob:none",
"--sparse",
repo.url(),
repo.directory()));
if (repo.sparsePaths().length > 0) {
System.out.println("Sparse-checkout paths: " + Arrays.toString(repo.sparsePaths()));
List<String> command = new ArrayList<>(List.of("git", "sparse-checkout", "set", "--no-cone"));
command.addAll(Arrays.asList(repo.sparsePaths()));
runCommand(workDir.resolve(repo.directory()), command);
}
}
private static void runCommand(Path directory, List<String> command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(directory.toFile());
pb.inheritIO();
Process process = pb.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Command failed (exit code " + exitCode + "): " + String.join(" ", command));
}
}
private static void copyDirectoryIfExists(Path source, Path target) throws IOException {
System.out.println("\nCopying directory: " + source + " -> " + target.toAbsolutePath() + " ...");
if (Files.exists(source)) {
FileUtils.copyDirectory(source.toFile(), target.toFile());
System.out.println("Successfully copied.");
} else {
System.err.println("Source path does not exist: " + source);
}
}
private static String getCommitHash(Path repoDir) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("git", "rev-parse", "HEAD");
pb.directory(repoDir.toFile());
Process process = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String hash = reader.readLine();
int exitCode = process.waitFor();
if (exitCode != 0 || hash == null) {
throw new RuntimeException("Failed to get commit hash for " + repoDir);
}
return hash.trim();
}
}
}