-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#180: commandlet to set tool edition #181
Changes from all commits
81027c6
13f3da2
2a7a3b8
910ba25
5b36863
f2a7223
8ac3902
d43ded2
41b5595
59cafd8
205268e
9c34191
36f75b5
cadfd7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.devonfw.tools.ide.commandlet; | ||
|
||
import com.devonfw.tools.ide.cli.CliException; | ||
import com.devonfw.tools.ide.context.IdeContext; | ||
import com.devonfw.tools.ide.property.ToolProperty; | ||
import com.devonfw.tools.ide.tool.ToolCommandlet; | ||
import com.devonfw.tools.ide.version.VersionIdentifier; | ||
|
||
import static com.devonfw.tools.ide.process.ProcessResult.TOOL_NOT_INSTALLED; | ||
|
||
/** | ||
* An internal {@link Commandlet} to get the installed edition for a tool. | ||
* | ||
* @see ToolCommandlet#getInstalledEdition() | ||
*/ | ||
public class EditionGetCommandlet extends Commandlet { | ||
|
||
/** The tool to get the edition of. */ | ||
public final ToolProperty tool; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param context the {@link IdeContext}. | ||
*/ | ||
public EditionGetCommandlet(IdeContext context) { | ||
|
||
super(context); | ||
addKeyword(getName()); | ||
this.tool = add(new ToolProperty("", true, "tool")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return "get-edition"; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
ToolCommandlet commandlet = this.tool.getValue(); | ||
VersionIdentifier installedVersion = commandlet.getInstalledVersion(); | ||
if (installedVersion == null) { | ||
throw new CliException("Tool " + commandlet.getName() + " is not installed!", TOOL_NOT_INSTALLED); | ||
} | ||
|
||
try { | ||
String installedEdition = commandlet.getInstalledEdition(); | ||
this.context.info(installedEdition); | ||
} catch (IllegalStateException e) { | ||
String configuredEdition = this.context.getVariables().getToolEdition(getName()); | ||
this.context.info("The configured edition for tool {} is {}.", getName(), configuredEdition); | ||
|
||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.devonfw.tools.ide.commandlet; | ||
|
||
import com.devonfw.tools.ide.context.IdeContext; | ||
import com.devonfw.tools.ide.property.ToolProperty; | ||
import com.devonfw.tools.ide.tool.ToolCommandlet; | ||
|
||
/** | ||
* An internal {@link Commandlet} to list editions for a tool. | ||
* | ||
* @see ToolCommandlet#listEditions() | ||
*/ | ||
public class EditionListCommandlet extends Commandlet { | ||
|
||
/** The tool to list the editions of. */ | ||
public final ToolProperty tool; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param context the {@link IdeContext}. | ||
*/ | ||
public EditionListCommandlet(IdeContext context) { | ||
|
||
super(context); | ||
addKeyword(getName()); | ||
this.tool = add(new ToolProperty("", true, "tool")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return "list-editions"; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
ToolCommandlet commandlet = this.tool.getValue(); | ||
commandlet.listEditions(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.devonfw.tools.ide.commandlet; | ||
|
||
import com.devonfw.tools.ide.context.IdeContext; | ||
import com.devonfw.tools.ide.property.EditionProperty; | ||
import com.devonfw.tools.ide.property.ToolProperty; | ||
import com.devonfw.tools.ide.tool.ToolCommandlet; | ||
|
||
/** | ||
* An internal {@link Commandlet} to set a tool edition. | ||
*/ | ||
public class EditionSetCommandlet extends Commandlet { | ||
|
||
/** The tool to set the edition of. */ | ||
public final ToolProperty tool; | ||
|
||
/** The edition to set. */ | ||
public final EditionProperty edition; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param context the {@link IdeContext}. | ||
*/ | ||
public EditionSetCommandlet(IdeContext context) { | ||
|
||
super(context); | ||
addKeyword(getName()); | ||
this.tool = add(new ToolProperty("", true, "tool")); | ||
this.edition = add(new EditionProperty("", true, "edition")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return "set-edition"; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
ToolCommandlet commandlet = this.tool.getValue(); | ||
String edition = this.edition.getValue(); | ||
|
||
commandlet.setEdition(edition); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.devonfw.tools.ide.property; | ||
|
||
import com.devonfw.tools.ide.context.IdeContext; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class EditionProperty extends Property<String> { | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param name the {@link #getName() property name}. | ||
* @param required the {@link #isRequired() required flag}. | ||
* @param alias the {@link #getAlias() property alias}. | ||
*/ | ||
public EditionProperty(String name, boolean required, String alias) { | ||
|
||
this(name, required, alias, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param name the {@link #getName() property name}. | ||
* @param required the {@link #isRequired() required flag}. | ||
* @param alias the {@link #getAlias() property alias}. | ||
* @param validator the {@link Consumer} used to {@link #validate() validate} the {@link #getValue() value}. | ||
*/ | ||
public EditionProperty(String name, boolean required, String alias, Consumer<String> validator) { | ||
|
||
super(name, required, alias, validator); | ||
} | ||
|
||
@Override | ||
public Class<String> getValueType() { | ||
|
||
return String.class; | ||
} | ||
|
||
@Override | ||
public String parse(String valueAsString, IdeContext context) { | ||
|
||
return valueAsString; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,6 +330,52 @@ protected VersionIdentifier getInstalledVersion(Path toolPath) { | |
} | ||
} | ||
|
||
/** | ||
* @return the installed edition of this tool or {@code null} if not installed. | ||
*/ | ||
public String getInstalledEdition() { | ||
|
||
return getInstalledEdition(this.context.getSoftwarePath().resolve(getName())); | ||
} | ||
|
||
/** | ||
* @param toolPath the installation {@link Path} where to find currently installed tool. The name of the parent | ||
* directory of the real path corresponding to the passed {@link Path path} must be the name of the edition. | ||
* @return the installed edition of this tool or {@code null} if not installed. | ||
*/ | ||
public String getInstalledEdition(Path toolPath) { | ||
|
||
if (!Files.isDirectory(toolPath)) { | ||
this.context.debug("Tool {} not installed in {}", getName(), toolPath); | ||
return null; | ||
} | ||
try { | ||
String edition = toolPath.toRealPath().getParent().getFileName().toString(); | ||
if (!this.context.getUrls().getSortedEditions(getName()).contains(edition)) { | ||
this.context.warning("The determined edition \"{}\" of tool {} is not among the editions provided by IDEasy", | ||
edition, getName()); | ||
} | ||
return edition; | ||
Comment on lines
+353
to
+358
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the tool was installed via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. See #194 |
||
} catch (IOException e) { | ||
throw new IllegalStateException("Couldn't determine the edition of " + getName() | ||
+ " from the directory structure of its software path " + toolPath | ||
+ ", assuming the name of the parent directory of the real path of the software path to be the edition " | ||
+ "of the tool.", e); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* List the available editions of this tool. | ||
*/ | ||
public void listEditions() { | ||
|
||
List<String> editions = this.context.getUrls().getSortedEditions(getName()); | ||
for (String edition : editions) { | ||
this.context.info(edition); | ||
} | ||
} | ||
|
||
/** | ||
* List the available versions of this tool. | ||
*/ | ||
|
@@ -389,4 +435,49 @@ public void setVersion(VersionIdentifier version, boolean hint) { | |
} | ||
} | ||
|
||
/** | ||
* Sets the tool edition in the environment variable configuration file. | ||
* | ||
* @param edition the edition to set. | ||
*/ | ||
public void setEdition(String edition) { | ||
MattesMrzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ((edition == null) || edition.isBlank()) { | ||
throw new IllegalStateException("Edition has to be specified!"); | ||
} | ||
|
||
if (!Files.exists(this.context.getUrls().getEdition(getName(), edition).getPath())) { | ||
this.context.warning("Edition {} seems to be invalid", 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) { | ||
|
||
EnvironmentVariables variables = this.context.getVariables(); | ||
EnvironmentVariables settingsVariables = variables.getByType(EnvironmentVariablesType.SETTINGS); | ||
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); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO it would make much more sense to print the configured edition instead of failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, adjusted the unit test accordingly. See #194