-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTool.java
More file actions
221 lines (197 loc) · 7.87 KB
/
Tool.java
File metadata and controls
221 lines (197 loc) · 7.87 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
/*
* Copyright (c) 2024 Christian Stein
* Licensed under the Universal Permissive License v 1.0 -> https://opensource.org/license/upl
*/
package run.bach;
import java.io.PrintWriter;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;
/**
* Links tool-identifying names to an instance of a tool provider interface implementation.
*
* @param identifier the nominal representation of this tool
* @param provider the tool provider instance to link
*/
public record Tool(Identifier identifier, ToolProvider provider) {
/**
* {@return an instance of a tool specified its name}
*
* @param name the name of the tool to look up
* @throws ToolNotFoundException when no tool could be found for the given name
* @see ToolProvider#findFirst(String)
* @see ToolProgram#findJavaDevelopmentKitTool(String, String...)
*/
public static Tool of(String name) throws ToolNotFoundException {
// Try with loading tool provider implementations using the system class loader first.
var provider = ToolProvider.findFirst(name);
if (provider.isPresent()) {
return Tool.of(provider.get());
}
// Find executable tool program in JDK's binary directory.
var program = ToolProgram.findJavaDevelopmentKitTool(name);
if (program.isPresent()) {
var version = String.valueOf(Runtime.version().feature());
var identifier = Identifier.of("jdk.home/bin/" + name + '@' + version);
return Tool.of(identifier, program.get());
}
// Try with loading a tool provider from modules in lib/ directory.
var loaded = ToolFinder.of(ModuleFinder.of(Path.of("lib"))).find(name);
if (loaded.isPresent()) {
return loaded.get();
}
// Try with treating the name argument as a URI.
var installer = ToolInstaller.find(name);
if (installer.isPresent()) {
return Tool.of(installer.get(), ToolInstaller.Mode.INSTALL_IMMEDIATE);
}
// Still here? Not so good...
throw new ToolNotFoundException("Tool not found for name: " + name);
}
/**
* {@return an instance of tool linking the given tool provider instance}
*
* @param provider the tool provider instance to link and extract tool-identifiable names from
* @see Identifier#of(ToolProvider)
*/
public static Tool of(ToolProvider provider) {
return new Tool(Identifier.of(provider), provider);
}
/**
* {@return an instance of tool for the given identifier and provider}
*
* @param identifier the nominal representation of the tool
* @param provider the tool provider instance to link
*/
public static Tool of(Identifier identifier, ToolProvider provider) {
return new Tool(identifier, provider);
}
/**
* {@return an instance of tool for the given identifier and provider}
*
* @param id the nominal representation of the tool
* @param provider the tool provider instance to link
*/
public static Tool of(String id, ToolProvider provider) {
return new Tool(Identifier.of(id), provider);
}
/**
* {@return an instance of tool for the given identifier and tool provider supplier}
*
* @param id the nominal representation of the tool
* @param supplier the supplier of the tool provider instance to wrap
*/
public static Tool of(String id, ToolProviderSupplier supplier) {
return Tool.of(Identifier.of(id), supplier);
}
/**
* {@return an instance of tool for the given identifier and tool provider supplier}
*
* @param identifier the nominal representation of the tool
* @param supplier the supplier of the tool provider instance to wrap
*/
public static Tool of(Identifier identifier, ToolProviderSupplier supplier) {
return new Tool(identifier, new ProviderFacade(identifier.name(), supplier));
}
public static Tool of(ToolInstaller installer) {
return Tool.of(installer, ToolInstaller.Mode.DEFAULT);
}
public static Tool of(ToolInstaller installer, ToolInstaller.Mode mode) {
return installer.install(mode);
}
public Tool {
Objects.requireNonNull(identifier);
Objects.requireNonNull(provider);
}
public void run(String... args) {
ToolCall.of(this, args).run();
}
public void run(UnaryOperator<ToolCall> operator) {
operator.apply(ToolCall.of(this)).run();
}
/**
* Describes a tool by its name in a possibly empty namespace and an optional version string.
*
* <p>Example: {@code "jdk.compiler/javac@99"}
*
* @param namespace a path-like group identifier of this tool or an empty string
* @param name the name of this tool
* @param version an optional version of this tool
* @see ToolProvider#name()
*/
public record Identifier(String namespace, String name, Optional<String> version) {
public static Identifier of(String namespace, String name, String version) {
return new Identifier(namespace, name, Optional.ofNullable(version));
}
public static Identifier of(ToolProvider provider) {
var type = provider.getClass();
var module = type.getModule();
var namespace = module.isNamed() ? module.getName() : type.getPackageName();
var name = provider.name();
var version =
module.isNamed()
? module.getDescriptor().version().map(Object::toString).orElse(null)
: null;
return Identifier.of(namespace, name, version);
}
public static Identifier of(String id) { // ["namespace" "/"] "name" ["@" "version"]
if (id == null) throw new NullPointerException("id must not be null");
if (id.isBlank()) throw new IllegalArgumentException("id must not be blank");
var path = Path.of(id).normalize();
var elements = path.getNameCount();
if (elements == 0) throw new IllegalArgumentException("only redundant elements in: " + id);
var namespace = elements == 1 ? "" : path.getParent().toString().replace('\\', '/');
var file = path.getFileName().toString();
var separator = file.indexOf('@');
var name = separator == -1 ? file : file.substring(0, separator);
var version = separator == -1 ? null : file.substring(separator + 1);
return Identifier.of(namespace, name, version);
}
public Identifier {
if (Stream.of("/", "\\").anyMatch(namespace::endsWith))
throw new IllegalArgumentException("Namespace must not end with / \\");
if (Stream.of("/", "\\", "@").anyMatch(name::contains))
throw new IllegalArgumentException("Name must not contain / \\ @");
}
public boolean matches(String string) {
if (name.equals(string)) return true; // "javac"
if (toNamespaceAndName().equals(string)) return true; // "jdk.compiler/javac"
if (version.isPresent()) {
if (toNameAndVersion().equals(string)) return true; // "javac@99"
return toNamespaceAndNameAndVersion().equals(string); // "jdk.compiler/javac@99"
}
return false;
}
public String toNameAndVersion() {
return version.map(version -> name + '@' + version).orElse(name);
}
public String toNamespaceAndName() {
return namespace + '/' + name;
}
public String toNamespaceAndNameAndVersion() {
return namespace + '/' + toNameAndVersion();
}
}
/** A supplier for tool provider instances. */
@FunctionalInterface
public interface ToolProviderSupplier {
ToolProvider supplyToolProvider() throws Exception;
}
/** A tool provider implementation delegating to another provider at run-time. */
record ProviderFacade(String name, ToolProviderSupplier delegate) implements ToolProvider {
@Override
public int run(PrintWriter out, PrintWriter err, String... args) {
try {
var provider = delegate.supplyToolProvider();
return provider.run(out, err, args);
} catch (Exception exception) {
exception.printStackTrace(err);
return -1;
}
}
}
}