Repository navigation
Expand file tree
/
Copy pathinstallSkills
More file actions
executable file
·404 lines (351 loc) · 14 KB
/
Copy pathinstallSkills
File metadata and controls
executable file
·404 lines (351 loc) · 14 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env -S java --source 25
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;
String name = MethodHandles.lookup().lookupClass().getName();
String version = "2026-07-05.4";
Path defaultTargetDirectory = Path.of(System.getProperty("user.home"), ".agents", "skills");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
void main(String... args) {
var status = 1;
try {
status = run(args);
} catch (Exception exception) {
reportFailure("installation aborted", exception);
}
System.exit(status);
}
int run(String... args) throws IOException {
var options = parse(args);
if (options.help()) {
usage();
return 0;
}
if (options.version()) {
IO.println(name + " " + version);
return 0;
}
var sourceSkillsDirectory = sourceSkillsDirectory();
if (!Files.isDirectory(sourceSkillsDirectory)) {
fail(1,
"Error: skills source directory not found: " + sourceSkillsDirectory,
"Place this script in the repository root before running it.");
}
var skills = discoverSkills();
if (skills.isEmpty()) {
fail(1,
"Error: no skills found under: " + sourceSkillsDirectory,
"Each skill directory must contain a SKILL.md file.");
}
createTargetDirectory(options.targetDirectory());
IO.println();
IO.println("Skills development install");
IO.println("Source: " + sourceSkillsDirectory);
IO.println("Target: " + options.targetDirectory());
IO.println("Mode: " + options.mode());
IO.println();
var summary = InstallSummary.NONE;
for (var skill : skills) {
if (confirmInstall(skill)) {
try {
summary = summary.plus(InstallSummary.INSTALLED_SKILL).plus(installSkill(skill, options));
} catch (IOException | UncheckedIOException exception) {
reportFailure("skill '" + skill.name() + "' was not installed", exception);
summary = summary.plus(InstallSummary.FAILED_SKILL);
}
} else {
printEntry("skip", skill.name());
summary = summary.plus(InstallSummary.SKIPPED_SKILL);
}
IO.println();
}
IO.println("Summary");
printCount("skills installed:", summary.skillsInstalled());
printCount("skills skipped:", summary.skillsSkipped());
printCount("skills failed:", summary.skillsFailed());
printCount("symlinks removed:", summary.removedLinks());
printCount("directories removed:", summary.removedDirectories());
printCount("files skipped:", summary.skippedFiles());
printCount("links created:", summary.linksCreated());
printCount("copies created:", summary.copiesCreated());
IO.println();
if (summary.skillsFailed() > 0) {
System.err.println("Error: " + summary.skillsFailed() + " skill(s) failed to install.");
return 1;
}
IO.println("Done.");
return 0;
}
Path repositoryRoot() {
var sourceFile = System.getProperty("jdk.launcher.sourcefile");
if (sourceFile == null || sourceFile.isBlank()) {
throw new IllegalStateException(
"cannot resolve the repository root: run the installer as './installSkills'"
+ " or 'java --source 25 installSkills'");
}
var root = Path.of(sourceFile).toAbsolutePath().normalize().getParent();
if (root == null) {
throw new IllegalStateException("cannot resolve the repository root from: " + sourceFile);
}
return root;
}
Path sourceSkillsDirectory() {
return repositoryRoot().resolve("skills");
}
void createTargetDirectory(Path targetDirectory) throws IOException {
try {
Files.createDirectories(targetDirectory);
} catch (IOException exception) {
throw new IOException("cannot create the target skills directory: " + targetDirectory, exception);
}
}
Options parse(String... args) {
var targetDirectory = defaultTargetDirectory;
var mode = InstallMode.symlink;
for (var index = 0; index < args.length; index++) {
switch (args[index]) {
case "--copy" -> mode = InstallMode.copy;
case "--target" -> {
if (index + 1 >= args.length || args[index + 1].isBlank() || args[index + 1].startsWith("-")) {
failUsage("Error: --target requires a directory path.");
}
targetDirectory = Path.of(args[++index]).toAbsolutePath().normalize();
}
case "--help", "-h" -> {
return new Options(targetDirectory, mode, true, false);
}
case "--version", "-version" -> {
return new Options(targetDirectory, mode, false, true);
}
default -> failUsage("Error: unknown option or argument: " + args[index]);
}
}
return new Options(targetDirectory, mode, false, false);
}
void failUsage(String message) {
fail(2, message, "", usageText());
}
void fail(int exitCode, String... messages) {
for (var message : messages) {
System.err.println(message);
}
System.exit(exitCode);
}
void reportFailure(String context, Throwable throwable) {
System.err.println("Error: " + context + ": " + describe(throwable));
}
String describe(Throwable throwable) {
var reasons = new ArrayList<String>();
var rootCause = throwable;
for (var current = throwable; current != null && current != current.getCause(); current = current.getCause()) {
rootCause = current;
var message = current.getMessage();
var reason = message == null || message.isBlank() ? current.getClass().getSimpleName() : message;
if (reasons.isEmpty() || !reasons.getLast().contains(reason)) {
reasons.add(reason);
}
}
var description = new StringBuilder(String.join(" -> ", reasons))
.append(" [")
.append(rootCause.getClass().getSimpleName())
.append(']');
for (var suppressed : throwable.getSuppressed()) {
description.append(" (also failed: ").append(describe(suppressed)).append(')');
}
return description.toString();
}
void printEntry(String label, Object value) {
IO.println(" %-16s%s".formatted(label, value));
}
void printCount(String label, int count) {
IO.println(" %-21s%s".formatted(label, count));
}
void usage() {
IO.println(usageText());
}
String usageText() {
return """
Usage: %s [--copy] [--target DIR] [--help] [--version]
Install selected local skills from the repository skills directory as symbolic links by default.
The installer prompts before installing each discovered skill.
Options:
--copy Copy the skill files instead of creating symbolic links.
--target DIR Destination skills directory. Default: %s
--help Show this help message.
--version Show version information.
""".formatted(name, defaultTargetDirectory);
}
List<Skill> discoverSkills() throws IOException {
return listEntries(sourceSkillsDirectory(), this::isSkillDirectory)
.stream()
.map(Skill::of)
.toList();
}
List<Path> listEntries(Path directory, Predicate<Path> filter) throws IOException {
try (var paths = Files.list(directory)) {
return paths
.filter(filter)
.sorted(Comparator.comparing(Path::getFileName))
.toList();
}
}
boolean isSkillDirectory(Path path) {
return Files.isDirectory(path) && Files.isRegularFile(path.resolve("SKILL.md"));
}
boolean confirmInstall(Skill skill) throws IOException {
IO.println("Install skill '%s'? [y/N]".formatted(skill.name()));
var answer = input.readLine();
if (answer == null) {
throw new IllegalStateException(
"standard input closed before the prompt for skill '" + skill.name()
+ "' was answered: run the installer interactively");
}
return List.of("y", "yes").contains(answer.strip().toLowerCase(Locale.ROOT));
}
InstallSummary installSkill(Skill skill, Options options) throws IOException {
IO.println("Installing skill: " + skill.name());
IO.println("Cleaning previous entries for " + skill.name());
var removal = cleanPreviousEntries(options.targetDirectory(), skill);
var creation = switch (options.mode()) {
case symlink -> createSymbolicLink(skill, options.targetDirectory());
case copy -> copySkill(skill, options.targetDirectory());
};
return removal.plus(creation);
}
InstallSummary cleanPreviousEntries(Path targetDirectory, Skill skill) throws IOException {
var entries = listEntries(targetDirectory, skill::isPreviousEntry);
if (entries.isEmpty()) {
IO.println(" none");
return InstallSummary.NONE;
}
var summary = InstallSummary.NONE;
for (var entry : entries) {
var entryName = entry.getFileName();
if (Files.isSymbolicLink(entry)) {
printEntry("remove symlink", entryName);
Files.delete(entry);
summary = summary.plus(InstallSummary.REMOVED_LINK);
} else if (Files.isDirectory(entry)) {
printEntry("remove dir", entryName);
deleteDirectory(entry);
summary = summary.plus(InstallSummary.REMOVED_DIRECTORY);
} else {
printEntry("skip file", entryName);
summary = summary.plus(InstallSummary.SKIPPED_FILE);
}
}
return summary;
}
InstallSummary createSymbolicLink(Skill skill, Path targetDirectory) throws IOException {
IO.println("Creating symbolic link");
var link = targetDirectory.resolve(skill.name());
try {
Files.createSymbolicLink(link, skill.path());
} catch (UnsupportedOperationException | FileSystemException exception) {
throw new IOException("cannot create symbolic link " + link + ": retry with --copy", exception);
}
printEntry("link", skill.name());
return InstallSummary.CREATED_LINK;
}
InstallSummary copySkill(Skill skill, Path targetDirectory) throws IOException {
IO.println("Copying skill files");
copyDirectory(skill.path(), targetDirectory.resolve(skill.name()));
printEntry("copy", skill.name());
return InstallSummary.CREATED_COPY;
}
void copyDirectory(Path source, Path target) throws IOException {
try {
for (var path : walk(source, Comparator.naturalOrder())) {
var relative = source.relativize(path);
if (relative.getNameCount() > 0 && "evals".equals(relative.getName(0).toString())) {
continue;
}
var destination = target.resolve(relative);
if (Files.isDirectory(path)) {
Files.createDirectories(destination);
} else {
Files.copy(path, destination);
}
}
} catch (IOException | UncheckedIOException exception) {
throw rollback(target, new IOException("cannot copy " + source + " to " + target, exception));
}
}
IOException rollback(Path target, IOException failure) {
try {
if (Files.exists(target)) {
deleteDirectory(target);
}
} catch (IOException cleanupFailure) {
failure.addSuppressed(cleanupFailure);
}
return failure;
}
void deleteDirectory(Path directory) throws IOException {
for (var path : walk(directory, Comparator.reverseOrder())) {
try {
Files.delete(path);
} catch (IOException exception) {
throw new IOException("cannot delete " + path, exception);
}
}
}
List<Path> walk(Path root, Comparator<Path> order) throws IOException {
try (var paths = Files.walk(root)) {
return paths.sorted(order).toList();
}
}
record Options(Path targetDirectory, InstallMode mode, boolean help, boolean version) {
}
record Skill(String name, Path path) {
static Skill of(Path path) {
return new Skill(path.getFileName().toString(), path);
}
boolean isPreviousEntry(Path path) {
return path.getFileName().toString().equals(name);
}
}
record InstallSummary(
int skillsInstalled,
int skillsSkipped,
int skillsFailed,
int removedLinks,
int removedDirectories,
int skippedFiles,
int linksCreated,
int copiesCreated) {
static final InstallSummary NONE = new InstallSummary(0, 0, 0, 0, 0, 0, 0, 0);
static final InstallSummary INSTALLED_SKILL = new InstallSummary(1, 0, 0, 0, 0, 0, 0, 0);
static final InstallSummary SKIPPED_SKILL = new InstallSummary(0, 1, 0, 0, 0, 0, 0, 0);
static final InstallSummary FAILED_SKILL = new InstallSummary(0, 0, 1, 0, 0, 0, 0, 0);
static final InstallSummary REMOVED_LINK = new InstallSummary(0, 0, 0, 1, 0, 0, 0, 0);
static final InstallSummary REMOVED_DIRECTORY = new InstallSummary(0, 0, 0, 0, 1, 0, 0, 0);
static final InstallSummary SKIPPED_FILE = new InstallSummary(0, 0, 0, 0, 0, 1, 0, 0);
static final InstallSummary CREATED_LINK = new InstallSummary(0, 0, 0, 0, 0, 0, 1, 0);
static final InstallSummary CREATED_COPY = new InstallSummary(0, 0, 0, 0, 0, 0, 0, 1);
InstallSummary plus(InstallSummary other) {
return new InstallSummary(
this.skillsInstalled + other.skillsInstalled,
this.skillsSkipped + other.skillsSkipped,
this.skillsFailed + other.skillsFailed,
this.removedLinks + other.removedLinks,
this.removedDirectories + other.removedDirectories,
this.skippedFiles + other.skippedFiles,
this.linksCreated + other.linksCreated,
this.copiesCreated + other.copiesCreated);
}
}
enum InstallMode {
symlink,
copy
}