2
2
3
3
import java .nio .file .Files ;
4
4
import java .nio .file .Path ;
5
+ import java .util .Arrays ;
5
6
import java .util .List ;
6
- import java .util .Map ;
7
7
import java .util .Set ;
8
8
9
9
import com .devonfw .tools .ide .common .Tag ;
@@ -26,7 +26,7 @@ public abstract class GlobalToolCommandlet extends ToolCommandlet {
26
26
* @param context the {@link IdeContext}.
27
27
* @param tool the {@link #getName() tool name}.
28
28
* @param tags the {@link #getTags() tags} classifying the tool. Should be created via {@link Set#of(Object) Set.of}
29
- * method.
29
+ * method.
30
30
*/
31
31
public GlobalToolCommandlet (IdeContext context , String tool , Set <Tag > tags ) {
32
32
@@ -36,57 +36,63 @@ public GlobalToolCommandlet(IdeContext context, String tool, Set<Tag> tags) {
36
36
/**
37
37
* Performs the installation of the {@link #getName() tool} via a package manager.
38
38
*
39
- * @param silent - {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
40
- * @param commands - A {@link Map} containing the commands used to perform the installation for each package manager.
41
- * @return {@code true} if the tool was newly installed, {@code false} if the tool was already installed before and
42
- * nothing has changed.
39
+ * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
40
+ * @param commandStrings commandStrings The package manager command strings to execute.
41
+ * @return {@code true} if installation succeeds with any of the package manager commands, {@code false} otherwise.
43
42
*/
44
- protected boolean installWithPackageManger ( Map < PackageManager , List < String >> commands , boolean silent ) {
43
+ protected boolean installWithPackageManager ( boolean silent , String ... commandStrings ) {
45
44
46
- Path binaryPath = this .context .getPath ().findBinary (Path .of (getBinaryName ()));
47
-
48
- if (binaryPath != null && Files .exists (binaryPath ) && !this .context .isForceMode ()) {
49
- IdeLogLevel level = silent ? IdeLogLevel .DEBUG : IdeLogLevel .INFO ;
50
- this .context .level (level ).log ("{} is already installed at {}" , this .tool , binaryPath );
51
- return false ;
52
- }
45
+ List <PackageManagerCommand > pmCommands = Arrays .stream (commandStrings ).map (PackageManagerCommand ::of ).toList ();
46
+ return installWithPackageManager (silent , pmCommands );
47
+ }
53
48
54
- Path bashPath = this .context .getPath ().findBinary (Path .of ("bash" ));
55
- if (bashPath == null || !Files .exists (bashPath )) {
56
- context .warning ("Bash was not found on this machine. Not Proceeding with installation of tool " + this .tool );
57
- return false ;
58
- }
49
+ /**
50
+ * Performs the installation of the {@link #getName() tool} via a package manager.
51
+ *
52
+ * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
53
+ * @param pmCommands A list of {@link PackageManagerCommand} to be used for installation.
54
+ * @return {@code true} if installation succeeds with any of the package manager commands, {@code false} otherwise.
55
+ */
56
+ protected boolean installWithPackageManager (boolean silent , List <PackageManagerCommand > pmCommands ) {
57
+
58
+ for (PackageManagerCommand pmCommand : pmCommands ) {
59
+ PackageManager packageManager = pmCommand .packageManager ();
60
+ Path packageManagerPath = this .context .getPath ().findBinary (Path .of (packageManager .getBinaryName ()));
61
+ if (packageManagerPath == null || !Files .exists (packageManagerPath )) {
62
+ this .context .debug ("{} is not installed" , packageManager .toString ());
63
+ continue ; // Skip to the next package manager command
64
+ }
59
65
60
- PackageManager foundPackageManager = null ;
61
- for (PackageManager pm : commands .keySet ()) {
62
- if (Files .exists (this .context .getPath ().findBinary (Path .of (pm .toString ().toLowerCase ())))) {
63
- foundPackageManager = pm ;
64
- break ;
66
+ if (executePackageManagerCommand (pmCommand , silent )) {
67
+ return true ; // Successfully installed
65
68
}
66
69
}
70
+ return false ; // None of the package manager commands were successful
71
+ }
67
72
68
- int finalExitCode = 0 ;
69
- if (foundPackageManager == null ) {
70
- context .warning ("No supported Package Manager found for installation" );
71
- return false ;
72
- } else {
73
- List <String > commandList = commands .get (foundPackageManager );
74
- if (commandList != null ) {
75
- for (String command : commandList ) {
76
- ProcessContext pc = this .context .newProcess ().errorHandling (ProcessErrorHandling .WARNING ).executable (bashPath )
77
- .addArgs ("-c" , command );
78
- finalExitCode = pc .run ();
79
- }
73
+ /**
74
+ * Executes the provided package manager command.
75
+ *
76
+ * @param pmCommand The {@link PackageManagerCommand} containing the commands to execute.
77
+ * @param silent {@code true} if called recursively to suppress verbose logging, {@code false} otherwise.
78
+ * @return {@code true} if the package manager commands execute successfully, {@code false} otherwise.
79
+ */
80
+ private boolean executePackageManagerCommand (PackageManagerCommand pmCommand , boolean silent ) {
81
+
82
+ String bashPath = this .context .findBash ();
83
+ for (String command : pmCommand .commands ()) {
84
+ ProcessContext pc = this .context .newProcess ().errorHandling (ProcessErrorHandling .WARNING ).executable (bashPath )
85
+ .addArgs ("-c" , command );
86
+ int exitCode = pc .run ();
87
+ if (exitCode != 0 ) {
88
+ this .context .warning ("{} command did not execute successfully" , command );
89
+ return false ;
80
90
}
81
91
}
82
92
83
- if (finalExitCode == 0 ) {
93
+ if (! silent ) {
84
94
this .context .success ("Successfully installed {}" , this .tool );
85
- } else {
86
- this .context .warning ("{} was not successfully installed" , this .tool );
87
- return false ;
88
95
}
89
- postInstall ();
90
96
return true ;
91
97
}
92
98
@@ -122,7 +128,7 @@ protected boolean doInstall(boolean silent) {
122
128
tmpDir = fileAccess .createTempDir (getName ());
123
129
Path downloadBinaryPath = tmpDir .resolve (target .getFileName ());
124
130
fileAccess .extract (target , downloadBinaryPath );
125
- downloadBinaryPath = fileAccess .findFirst (downloadBinaryPath , Files ::isExecutable , false );
131
+ executable = fileAccess .findFirst (downloadBinaryPath , Files ::isExecutable , false );
126
132
}
127
133
ProcessContext pc = this .context .newProcess ().errorHandling (ProcessErrorHandling .WARNING ).executable (executable );
128
134
int exitCode = pc .run ();
0 commit comments