-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolInstaller.java
More file actions
167 lines (144 loc) · 5.17 KB
/
ToolInstaller.java
File metadata and controls
167 lines (144 loc) · 5.17 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
/*
* 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.IOException;
import java.net.URI;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;
import run.bach.internal.JavaApplicationInstaller;
import run.bach.internal.PathSupport;
/** An interface for installers to fetch required files and compose them into a tool provider. */
public interface ToolInstaller {
Path DEFAULT_INSTALLATION_HOME_DIRECTORY = Path.of(".bach", "tmp", "tool");
/**
* {@return the default namespace used by this tool installer}
*
* @see Tool.Identifier#namespace()
*/
default String namespace() {
var type = getClass();
var module = type.getModule();
return module.isNamed() ? module.getName() : type.getPackageName();
}
/**
* {@return the default name used by this tool installer}
*
* @see Tool.Identifier#name()
*/
default String name() {
return getClass().getSimpleName().toLowerCase(Locale.ROOT);
}
/**
* {@return the version for this tool installer}
*
* @see Tool.Identifier#version()
*/
String version();
ToolProvider install(Path into) throws Exception;
default Tool install() {
return install(Mode.DEFAULT);
}
default Tool install(Mode mode) {
var identifier = Tool.Identifier.of(namespace(), name(), version());
return install(DEFAULT_INSTALLATION_HOME_DIRECTORY, identifier, mode);
}
private Tool install(Path base, Tool.Identifier identifier, Mode mode) {
// resolve installation folder
var path = Path.of(identifier.namespace());
if (path.getRoot() != null) {
path = path.getRoot().relativize(path); // strip root component from namespace
}
var folder = base.resolve(path).resolve(identifier.toNameAndVersion()).normalize();
return switch (mode) {
case INSTALL_IMMEDIATE -> Tool.of(identifier, install(this, folder));
case INSTALL_ON_DEMAND -> Tool.of(identifier, () -> install(this, folder));
};
}
default void download(Path target, URI source) throws IOException {
PathSupport.copy(target, source);
}
private static ToolProvider install(ToolInstaller installer, Path directory) {
try {
Files.createDirectories(directory);
var lockfile = directory.resolve(".lockfile");
try {
try {
Files.createFile(lockfile);
} catch (FileAlreadyExistsException exception) {
while (Files.exists(lockfile)) {
// noinspection BusyWait
Thread.sleep(1234);
}
}
return installer.install(directory);
} finally {
Files.deleteIfExists(lockfile);
}
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
static Optional<ToolInstaller> find(String string) {
return JavaApplicationInstaller.find(string);
}
static ToolInstaller ofJavaApplication(String id, String uri) {
var identifier = Tool.Identifier.of(id);
var source = URI.create(uri);
return new JavaApplicationInstaller(identifier, source);
}
static Finder finder(Mode mode) {
return new Finder(List.of(), mode, DEFAULT_INSTALLATION_HOME_DIRECTORY);
}
/** Tool installation mode. */
enum Mode {
/**
* Install tools eagerly while composing the finder by using the provider-taking tool factory.
*
* @see Tool#of(String, ToolProvider)
*/
INSTALL_IMMEDIATE,
/**
* Install tools lazily only when running them by using the supplier-taking tool factory.
*
* @see Tool#of(String, Tool.ToolProviderSupplier)
*/
INSTALL_ON_DEMAND;
/** Default to on-demand installation of tools. */
public static final Mode DEFAULT = INSTALL_ON_DEMAND;
}
record Finder(List<Tool> tools, Mode mode, Path installationHomeDirectory) implements ToolFinder {
public Finder with(ToolInstaller installer) {
var namespace = installer.namespace();
var name = installer.name();
var version = installer.version();
var identifier = Tool.Identifier.of(namespace, name, version);
return with(identifier, installer);
}
public Finder with(String string) {
return with(ToolInstaller.find(string).orElseThrow(() -> new ToolNotFoundException(string)));
}
public Finder with(String id, ToolInstaller installer) {
var identifier = Tool.Identifier.of(id);
return with(identifier, installer);
}
public Finder withJavaApplication(String id, String uri, String... more) {
var identifier = Tool.Identifier.of(id);
var source = URI.create(uri);
var installer = new JavaApplicationInstaller(identifier, source);
return with(identifier, installer);
}
private Finder with(Tool.Identifier identifier, ToolInstaller installer) {
var tool = installer.install(installationHomeDirectory, identifier, mode);
var tools = Stream.concat(tools().stream(), Stream.of(tool)).toList();
return new Finder(tools, mode, installationHomeDirectory);
}
}
}