forked from devonfw/IDEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolCommandlet.java
More file actions
551 lines (462 loc) · 19 KB
/
ToolCommandlet.java
File metadata and controls
551 lines (462 loc) · 19 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package com.devonfw.tools.ide.tool;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import com.devonfw.tools.ide.commandlet.Commandlet;
import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.common.Tags;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesFiles;
import com.devonfw.tools.ide.nls.NlsBundle;
import com.devonfw.tools.ide.os.MacOsHelper;
import com.devonfw.tools.ide.process.EnvironmentContext;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
import com.devonfw.tools.ide.process.ProcessMode;
import com.devonfw.tools.ide.process.ProcessResult;
import com.devonfw.tools.ide.property.StringProperty;
import com.devonfw.tools.ide.tool.repository.ToolRepository;
import com.devonfw.tools.ide.version.GenericVersionRange;
import com.devonfw.tools.ide.version.VersionIdentifier;
/**
* {@link Commandlet} for a tool integrated into the IDE.
*/
public abstract class ToolCommandlet extends Commandlet implements Tags {
/** @see #getName() */
protected final String tool;
private final Set<Tag> tags;
/** The commandline arguments to pass to the tool. */
public final StringProperty arguments;
private Path executionDirectory;
private MacOsHelper macOsHelper;
/**
* The constructor.
*
* @param context the {@link IdeContext}.
* @param tool the {@link #getName() tool name}.
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of} method.
*/
public ToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
super(context);
this.tool = tool;
this.tags = tags;
addKeyword(tool);
this.arguments = new StringProperty("", false, true, "args");
initProperties();
}
/**
* Add initial Properties to the tool
*/
protected void initProperties() {
add(this.arguments);
}
/**
* @return the name of the tool (e.g. "java", "mvn", "npm", "node").
*/
@Override
public final String getName() {
return this.tool;
}
/**
* @return the name of the binary executable for this tool.
*/
protected String getBinaryName() {
return this.tool;
}
@Override
public final Set<Tag> getTags() {
return this.tags;
}
/**
* @return the execution directory where the tool will be executed. Will be {@code null} by default leading to execution in the users current working
* directory where IDEasy was called.
* @see #setExecutionDirectory(Path)
*/
public Path getExecutionDirectory() {
return this.executionDirectory;
}
/**
* @param executionDirectory the new value of {@link #getExecutionDirectory()}.
*/
public void setExecutionDirectory(Path executionDirectory) {
this.executionDirectory = executionDirectory;
}
/**
* @return the {@link EnvironmentVariables#getToolVersion(String) tool version}.
*/
public VersionIdentifier getConfiguredVersion() {
return this.context.getVariables().getToolVersion(getName());
}
/**
* @return the {@link EnvironmentVariables#getToolEdition(String) tool edition}.
*/
public String getConfiguredEdition() {
return this.context.getVariables().getToolEdition(getName());
}
/**
* @return the {@link #getName() tool} with its {@link #getConfiguredEdition() edition}. The edition will be omitted if same as tool.
* @see #getToolWithEdition(String, String)
*/
protected final String getToolWithEdition() {
return getToolWithEdition(getName(), getConfiguredEdition());
}
/**
* @param tool the tool name.
* @param edition the edition.
* @return the {@link #getName() tool} with its {@link #getConfiguredEdition() edition}. The edition will be omitted if same as tool.
*/
protected static String getToolWithEdition(String tool, String edition) {
if (tool.equals(edition)) {
return tool;
}
return tool + "/" + edition;
}
@Override
public void run() {
runTool(this.arguments.asArray());
}
/**
* @param args the command-line arguments to run the tool.
* @see ToolCommandlet#runTool(ProcessMode, GenericVersionRange, String...)
*/
public void runTool(String... args) {
runTool(ProcessMode.DEFAULT, null, args);
}
/**
* Ensures the tool is installed and then runs this tool with the given arguments.
*
* @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
* @param toolVersion the explicit {@link GenericVersionRange version} to run. Typically {@code null} to run the
* {@link #getConfiguredVersion() configured version}. Otherwise, the specified version will be used (from the software repository, if not compatible).
* @param args the command-line arguments to run the tool.
*/
public final void runTool(ProcessMode processMode, GenericVersionRange toolVersion, String... args) {
runTool(processMode, toolVersion, ProcessErrorHandling.THROW_CLI, args);
}
/**
* Ensures the tool is installed and then runs this tool with the given arguments.
*
* @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
* @param toolVersion the explicit {@link GenericVersionRange version} to run. Typically {@code null} to run the
* {@link #getConfiguredVersion() configured version}. Otherwise, the specified version will be used (from the software repository, if not compatible).
* @param errorHandling the {@link ProcessErrorHandling}.
* @param args the command-line arguments to run the tool.
* @return the {@link ProcessResult result}.
*/
public ProcessResult runTool(ProcessMode processMode, GenericVersionRange toolVersion, ProcessErrorHandling errorHandling, String... args) {
ProcessContext pc = this.context.newProcess().errorHandling(errorHandling);
install(true, pc);
return runTool(processMode, errorHandling, pc, args);
}
/**
* @param processMode the {@link ProcessMode}. Should typically be {@link ProcessMode#DEFAULT} or {@link ProcessMode#BACKGROUND}.
* @param errorHandling the {@link ProcessErrorHandling}.
* @param pc the {@link ProcessContext}.
* @param args the command-line arguments to run the tool.
* @return the {@link ProcessResult result}.
*/
public ProcessResult runTool(ProcessMode processMode, ProcessErrorHandling errorHandling, ProcessContext pc, String... args) {
if (this.executionDirectory != null) {
pc.directory(this.executionDirectory);
}
configureToolBinary(pc, processMode, errorHandling);
configureToolArgs(pc, processMode, errorHandling, args);
return pc.run(processMode);
}
/**
* @param pc the {@link ProcessContext}.
* @param processMode the {@link ProcessMode}.
* @param errorHandling the {@link ProcessErrorHandling}.
*/
protected void configureToolBinary(ProcessContext pc, ProcessMode processMode, ProcessErrorHandling errorHandling) {
pc.executable(Path.of(getBinaryName()));
}
/**
* @param pc the {@link ProcessContext}.
* @param processMode the {@link ProcessMode}.
* @param errorHandling the {@link ProcessErrorHandling}.
* @param args the command-line arguments to {@link ProcessContext#addArgs(Object...) add}.
*/
protected void configureToolArgs(ProcessContext pc, ProcessMode processMode, ProcessErrorHandling errorHandling, String... args) {
pc.addArgs(args);
}
/**
* Creates a new {@link ProcessContext} from the given executable with the provided arguments attached.
*
* @param binaryPath path to the binary executable for this process
* @param args the command-line arguments for this process
* @return {@link ProcessContext}
*/
protected ProcessContext createProcessContext(Path binaryPath, String... args) {
return this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_ERR).executable(binaryPath).addArgs(args);
}
/**
* Installs or updates the managed {@link #getName() tool}.
*
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
public boolean install() {
return install(true);
}
/**
* Performs the installation of the {@link #getName() tool} managed by this {@link com.devonfw.tools.ide.commandlet.Commandlet}.
*
* @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
public boolean install(boolean silent) {
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI);
return install(silent, pc);
}
/**
* Installs or updates the managed {@link #getName() tool}.
*
* @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
* @param processContext the {@link ProcessContext} used to
* {@link LocalToolCommandlet#setEnvironment(EnvironmentContext, ToolInstallation, boolean) configure environment variables}.
* @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and nothing has changed.
*/
public abstract boolean install(boolean silent, ProcessContext processContext);
/**
* @return {@code true} to extract (unpack) the downloaded binary file, {@code false} otherwise.
*/
protected boolean isExtract() {
return true;
}
/**
* @return the {@link MacOsHelper} instance.
*/
protected MacOsHelper getMacOsHelper() {
if (this.macOsHelper == null) {
this.macOsHelper = new MacOsHelper(this.context);
}
return this.macOsHelper;
}
/**
* @return the currently installed {@link VersionIdentifier version} of this tool or {@code null} if not installed.
*/
public abstract VersionIdentifier getInstalledVersion();
/**
* @return the installed edition of this tool or {@code null} if not installed.
*/
public abstract String getInstalledEdition();
/**
* @return the path of the installed tool inside the software repo folder or {@code null} if not installed.
*/
public abstract Path getInstalledSoftwareRepoPath();
/**
* Uninstalls the {@link #getName() tool}.
*/
public abstract void uninstall();
/**
* Uninstalls the {@link #getName() tool} and the real tool version inside the software repository.
*/
public abstract void forceUninstall();
/**
* @return the {@link ToolRepository}.
*/
public ToolRepository getToolRepository() {
return this.context.getDefaultToolRepository();
}
/**
* List the available editions of this tool.
*/
public void listEditions() {
List<String> editions = getToolRepository().getSortedEditions(getName());
for (String edition : editions) {
this.context.info(edition);
}
}
/**
* List the available versions of this tool.
*/
public void listVersions() {
List<VersionIdentifier> versions = getToolRepository().getSortedVersions(getName(), getConfiguredEdition(), this);
for (VersionIdentifier vi : versions) {
this.context.info(vi.toString());
}
}
/**
* Sets the tool version in the environment variable configuration file.
*
* @param version the version (pattern) to set.
*/
public void setVersion(String version) {
if ((version == null) || version.isBlank()) {
throw new IllegalStateException("Version has to be specified!");
}
VersionIdentifier configuredVersion = VersionIdentifier.of(version);
if (!configuredVersion.isPattern() && !configuredVersion.isValid()) {
this.context.warning("Version {} seems to be invalid", version);
}
setVersion(configuredVersion, true);
}
/**
* Sets the tool version in the environment variable configuration file.
*
* @param version the version to set. May also be a {@link VersionIdentifier#isPattern() version pattern}.
* @param hint - {@code true} to print the installation hint, {@code false} otherwise.
*/
public void setVersion(VersionIdentifier version, boolean hint) {
setVersion(version, hint, null);
}
/**
* Sets the tool version in the environment variable configuration file.
*
* @param version the version to set. May also be a {@link VersionIdentifier#isPattern() version pattern}.
* @param hint - {@code true} to print the installation hint, {@code false} otherwise.
* @param destination - the destination for the property to be set
*/
public void setVersion(VersionIdentifier version, boolean hint, EnvironmentVariablesFiles destination) {
String edition = getConfiguredEdition();
ToolRepository toolRepository = getToolRepository();
VersionIdentifier versionIdentifier = toolRepository.resolveVersion(this.tool, edition, version, this);
Objects.requireNonNull(versionIdentifier);
EnvironmentVariables variables = this.context.getVariables();
if (destination == null) {
//use default location
destination = EnvironmentVariablesFiles.SETTINGS;
}
EnvironmentVariables settingsVariables = variables.getByType(destination.toType());
String name = EnvironmentVariables.getToolVersionVariable(this.tool);
VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool, edition, version, this);
if (version.isPattern()) {
this.context.debug("Resolved version {} to {} for tool {}/{}", version, resolvedVersion, this.tool, edition);
}
settingsVariables.set(name, resolvedVersion.toString(), false);
settingsVariables.save();
this.context.info("{}={} has been set in {}", name, version, settingsVariables.getSource());
EnvironmentVariables declaringVariables = variables.findVariable(name);
if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
declaringVariables.getSource());
}
if (hint) {
this.context.info("To install that version call the following command:");
this.context.info("ide install {}", this.tool);
}
}
/**
* Sets the tool edition in the environment variable configuration file.
*
* @param edition the edition to set.
*/
public void setEdition(String edition) {
setEdition(edition, true);
}
/**
* Sets the tool edition in the environment variable configuration file.
*
* @param edition the edition to set
* @param hint - {@code true} to print the installation hint, {@code false} otherwise.
*/
public void setEdition(String edition, boolean hint) {
setEdition(edition, hint, null);
}
/**
* Sets the tool edition in the environment variable configuration file.
*
* @param edition the edition to set
* @param hint - {@code true} to print the installation hint, {@code false} otherwise.
* @param destination - the destination for the property to be set
*/
public void setEdition(String edition, boolean hint, EnvironmentVariablesFiles destination) {
if ((edition == null) || edition.isBlank()) {
throw new IllegalStateException("Edition has to be specified!");
}
if (destination == null) {
//use default location
destination = EnvironmentVariablesFiles.SETTINGS;
}
if (!getToolRepository().getSortedEditions(this.tool).contains(edition)) {
this.context.warning("Edition {} seems to be invalid", edition);
}
EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables settingsVariables = variables.getByType(destination.toType());
String name = EnvironmentVariables.getToolEditionVariable(this.tool);
settingsVariables.set(name, edition, false);
settingsVariables.save();
this.context.info("{}={} has been set in {}", name, edition, settingsVariables.getSource());
EnvironmentVariables declaringVariables = variables.findVariable(name);
if ((declaringVariables != null) && (declaringVariables != settingsVariables)) {
this.context.warning("The variable {} is overridden in {}. Please remove the overridden declaration in order to make the change affect.", name,
declaringVariables.getSource());
}
if (hint) {
this.context.info("To install that edition call the following command:");
this.context.info("ide install {}", this.tool);
}
}
/**
* Runs the tool's help command to provide the user with usage information.
*/
@Override
public void printHelp(NlsBundle bundle) {
super.printHelp(bundle);
String toolHelpArgs = getToolHelpArguments();
if (toolHelpArgs != null && getInstalledVersion() != null) {
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.LOG_WARNING)
.executable(Path.of(getBinaryName())).addArgs(toolHelpArgs);
pc.run(ProcessMode.DEFAULT);
}
}
/**
* @return the tool's specific help command. Usually help, --help or -h. Return null if not applicable.
*/
public String getToolHelpArguments() {
return null;
}
/**
* Creates a start script for the tool using the tool name.
*
* @param targetDir the {@link Path} of the installation where to create the script. If a "bin" sub-folder is present, the script will be created there
* instead.
* @param binary name of the binary to execute from the start script.
*/
protected void createStartScript(Path targetDir, String binary) {
createStartScript(targetDir, binary, false);
}
/**
* Creates a start script for the tool using the tool name.
*
* @param targetDir the {@link Path} of the installation where to create the script. If a "bin" sub-folder is present, the script will be created there
* instead.
* @param binary name of the binary to execute from the start script.
* @param background {@code true} to run the {@code binary} in background, {@code false} otherwise (foreground).
*/
protected void createStartScript(Path targetDir, String binary, boolean background) {
Path binFolder = targetDir.resolve("bin");
if (!Files.exists(binFolder)) {
if (this.context.getSystemInfo().isMac()) {
MacOsHelper macOsHelper = getMacOsHelper();
Path appDir = macOsHelper.findAppDir(targetDir);
binFolder = macOsHelper.findLinkDir(appDir, binary);
} else {
binFolder = targetDir;
}
assert (Files.exists(binFolder));
}
Path bashFile = binFolder.resolve(getName());
String bashFileContentStart = "#!/usr/bin/env bash\n\"$(dirname \"$0\")/";
String bashFileContentEnd = "\" $@";
if (background) {
bashFileContentEnd += " &";
}
try {
Files.writeString(bashFile, bashFileContentStart + binary + bashFileContentEnd);
} catch (IOException e) {
throw new RuntimeException(e);
}
assert (Files.exists(bashFile));
context.getFileAccess().makeExecutable(bashFile);
}
@Override
public void reset() {
super.reset();
this.executionDirectory = null;
}
}