Skip to content
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

#101: implement create & update commandlet #186

Closed
wants to merge 146 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
e9d574f
added class RepositoryCommandlet and its help function
salimbouch Nov 8, 2023
76eba65
implemented run method when project is given as argument
salimbouch Nov 9, 2023
0e16de6
implemented loadProperties
salimbouch Nov 10, 2023
69835e3
added record RepositoryConfig, implemented run method, implemented lo…
salimbouch Nov 11, 2023
90145a6
implemented build functionality, import missing
salimbouch Nov 13, 2023
c738a0e
minor improvements
salimbouch Nov 13, 2023
c3de09e
javadoc
salimbouch Nov 14, 2023
c0fbd26
fixed bug: tool installed but not found
salimbouch Nov 16, 2023
89061ae
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Nov 16, 2023
edc7b1d
created class UpdateCommandlet, implemented updateSettings method
salimbouch Nov 23, 2023
7b43974
created class UpdateCommandlet, implemented updateSettings method
salimbouch Nov 23, 2023
3fc8c23
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Nov 24, 2023
15bdb62
implemented repository update
salimbouch Nov 26, 2023
8063580
solved bug in CustomToolRepositoryImpl, created class CustomToolComma…
salimbouch Nov 28, 2023
1c729ae
removed useless improt
salimbouch Nov 28, 2023
60e72bc
implemented install/update of custom tools
salimbouch Nov 28, 2023
01cb2b9
minor change
salimbouch Nov 28, 2023
db8008a
Update RepositoryCommandlet.java
salimbouch Nov 28, 2023
ec96f35
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Nov 28, 2023
a64b959
made variable constant
salimbouch Nov 28, 2023
6f265e1
added the help function
salimbouch Nov 28, 2023
77fd651
changes after review
salimbouch Nov 30, 2023
82cc617
changes after review
salimbouch Nov 30, 2023
5440755
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Nov 30, 2023
f1fede6
reviewed changes
salimbouch Nov 30, 2023
dbc3bd3
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
a582601
Update cli/src/main/resources/nls/Ide_de.properties
salimbouch Dec 13, 2023
7e3bdd9
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
d71f68e
Update cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java
salimbouch Dec 13, 2023
1e5aba8
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
c8cf771
Update cli/src/main/resources/nls/Ide.properties
salimbouch Dec 13, 2023
4b60135
Update cli/src/main/resources/nls/Ide.properties
salimbouch Dec 13, 2023
061cfa5
Update cli/src/main/resources/nls/Ide_de.properties
salimbouch Dec 13, 2023
c314c08
reviewed changes
salimbouch Dec 13, 2023
e7a8323
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
07d4a34
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Dec 13, 2023
37da950
updates
salimbouch Dec 13, 2023
45e16e8
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
05bcde1
reviewed changes
salimbouch Dec 13, 2023
e055a6a
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Dec 13, 2023
f2ee210
reviewed changes
salimbouch Dec 13, 2023
268a067
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Dec 13, 2023
5773121
implemented repositoryProperty
salimbouch Dec 15, 2023
fe607d2
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/UpdateComma…
salimbouch Dec 19, 2023
91d0570
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/UpdateComma…
salimbouch Dec 19, 2023
d047d62
removed useless comment
salimbouch Dec 19, 2023
260750a
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Dec 19, 2023
6397213
merged #100
salimbouch Dec 19, 2023
13578e9
reviewed changes, new class StepContainer
salimbouch Dec 19, 2023
a0eb88f
small change
salimbouch Dec 19, 2023
c53d072
javadoc for StepContainer class
salimbouch Dec 19, 2023
c55c4a8
small changes
salimbouch Dec 19, 2023
68ddfa8
removed useless imports
salimbouch Dec 19, 2023
c40042f
updated updateSettings
salimbouch Dec 25, 2023
25e4167
added stepContainer, update settings asks for link when no settingsfo…
salimbouch Jan 2, 2024
48db7c3
added null check
salimbouch Jan 2, 2024
94c7b73
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Jan 2, 2024
4c2291a
Update RepositoryProperty.java
salimbouch Jan 2, 2024
dcecb50
Update RepositoryProperty.java
salimbouch Jan 2, 2024
f89b889
created CreateCommandlet
salimbouch Jan 2, 2024
4880d12
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Jan 7, 2024
de061fe
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Jan 7, 2024
e62ea03
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
8a35bbd
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
e8ba8f7
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Jan 7, 2024
21f14df
reviewed changes
salimbouch Jan 8, 2024
b030586
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Jan 8, 2024
f847820
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Jan 11, 2024
aa1fcca
init instance
salimbouch Jan 11, 2024
d6c3b4c
created setupConf
salimbouch Jan 17, 2024
60d1e7b
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Jan 17, 2024
da4949d
bug fixes
salimbouch Jan 17, 2024
3181cde
comments
salimbouch Jan 18, 2024
6ea0cc9
updated ide.properties
salimbouch Jan 18, 2024
3bb3926
Merge branch 'main' into 101-implement-create-commandlet
salimbouch Jan 18, 2024
bb2beb8
Merge branch 'main' of https://github.com/devonfw/IDEasy into 100-imp…
salimbouch Jan 18, 2024
7c6de4c
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Jan 18, 2024
7dfdf17
Merge branch '100-implement-repository-commandlet' into 101-implement…
salimbouch Jan 18, 2024
7047ebd
Update UpdateCommandlet.java
salimbouch Jan 18, 2024
e494943
javadoc
salimbouch Jan 18, 2024
fae8577
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Jan 18, 2024
cbeee6b
Update CreateCommandlet.java
salimbouch Jan 19, 2024
ff0d192
fixed conflicts
salimbouch Jan 23, 2024
1420396
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Jan 23, 2024
75a1f01
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Jan 23, 2024
10ca262
Merge branch '101-implement-create-commandlet' of https://github.com/…
salimbouch Jan 23, 2024
bc7b48a
wrote test for repositoryCom
salimbouch Jan 25, 2024
e21f222
changed repositoryProperty to FileProperty inheritance
salimbouch Jan 26, 2024
bf64dfb
Update CreateCommandlet.java
salimbouch Jan 31, 2024
d06a970
Merge branch 'main' of https://github.com/devonfw/IDEasy into 100-imp…
salimbouch Feb 2, 2024
df05c7c
Update Ide_de.properties
salimbouch Feb 2, 2024
edb59cd
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Feb 2, 2024
f1ae20d
Update CommandletManagerImpl.java
salimbouch Feb 2, 2024
e4b885d
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Feb 2, 2024
ea4b410
typo
salimbouch Feb 15, 2024
b911a22
Merge branch 'main' into 101-implement-create-commandlet
salimbouch Feb 15, 2024
7812831
Merge branch 'main' into 102-implement-update-commandlet
salimbouch Feb 15, 2024
3c0584b
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 15, 2024
a218748
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Feb 19, 2024
0769f2c
Merge branch 'main' into 101-implement-create-commandlet
hohwille Feb 19, 2024
e0a2b50
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 20, 2024
7b119df
Update cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
salimbouch Feb 21, 2024
a60cf62
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Feb 21, 2024
128dbcf
minor change
salimbouch Feb 27, 2024
216260c
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Feb 27, 2024
8991d5e
Merge branch 'main' of https://github.com/devonfw/ideasy into 102-imp…
salimbouch Feb 29, 2024
03f2f73
resolved conflicts
salimbouch Feb 29, 2024
b76c336
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Feb 29, 2024
9f0bd6c
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Feb 29, 2024
572ffb1
resolved conflicts
salimbouch Feb 29, 2024
c7671c5
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Feb 29, 2024
7a3c3c2
added tests for repo commandlet
salimbouch Feb 29, 2024
e5b794a
removed my stupid fix
salimbouch Feb 29, 2024
ec515f9
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Feb 29, 2024
cd0075f
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Feb 29, 2024
9254184
removed * import
salimbouch Mar 4, 2024
b490f16
adjusted tests
salimbouch Mar 6, 2024
424887f
Merge branch 'main' into 100-implement-repository-commandlet
hohwille Mar 12, 2024
2b99a21
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Mar 13, 2024
6129a5a
Update cli/src/main/java/com/devonfw/tools/ide/property/RepositoryPro…
salimbouch Mar 13, 2024
4b5b005
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/RepositoryC…
salimbouch Mar 13, 2024
5dd5d49
Merge branch 'main' of https://github.com/devonfw/ideasy into 100-imp…
salimbouch Mar 13, 2024
d079c78
reviewed changes
salimbouch Mar 13, 2024
34408ea
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Mar 13, 2024
7a9bb0b
minor change
salimbouch Mar 13, 2024
4236eae
crlf diff
salimbouch Mar 13, 2024
862eabc
diff war
salimbouch Mar 13, 2024
de2c3f9
solved minor bug
salimbouch Mar 13, 2024
6a1eefc
cleanup
salimbouch Mar 13, 2024
62fe8f2
Merge branch 'main' into 100-implement-repository-commandlet
salimbouch Mar 13, 2024
8a06af5
again crlf switching ..
salimbouch Mar 13, 2024
1752f0e
Merge branch '100-implement-repository-commandlet' of https://github.…
salimbouch Mar 13, 2024
b7e76d0
Update AbstractIdeContext.java
salimbouch Mar 13, 2024
6765d89
Update GitContextImpl.java
salimbouch Mar 13, 2024
6a8bf0e
Update GitContextImpl.java
salimbouch Mar 13, 2024
7b4bc57
Update GitContextImpl.java
salimbouch Mar 13, 2024
b73e83d
small git api improvements
salimbouch Mar 15, 2024
f513dfa
Update GitContextImpl.java
salimbouch Mar 15, 2024
ee986bd
Merge branch '100-implement-repository-commandlet' into 102-implement…
salimbouch Mar 18, 2024
2a31288
Merge branch 'main' into 102-implement-update-commandlet
salimbouch Mar 18, 2024
6315f7d
reviewed changes
salimbouch Mar 18, 2024
4efaccc
Merge branch '102-implement-update-commandlet' of https://github.com/…
salimbouch Mar 18, 2024
af7aeb2
Merge branch 'main' of https://github.com/devonfw/ideasy into 102-imp…
salimbouch Mar 22, 2024
96beaa7
Merge branch '102-implement-update-commandlet' into 101-implement-cre…
salimbouch Mar 22, 2024
caa0be6
adjusting to changes to Update cmdlt
salimbouch Mar 22, 2024
252f633
Merge branch 'main' into 101-implement-create-commandlet
salimbouch Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public interface CommandletManager {

/**
* @param name the {@link Commandlet#getName() name} of the requested {@link ToolCommandlet}.
* @return the requested {@link ToolCommandlet} or {@code null} if not found.
* @return the requested {@link ToolCommandlet} if found.
* @throws IllegalArgumentException if the commandlet with the given name is not a {@link ToolCommandlet}
*/
default ToolCommandlet getToolCommandlet(String name) {

Expand All @@ -50,4 +51,16 @@ default ToolCommandlet getToolCommandlet(String name) {
throw new IllegalArgumentException("The commandlet " + name + " is not a ToolCommandlet!");
}

/**
* @param name the {@link Commandlet#getName() name} of the requested {@link ToolCommandlet}.
* @return the requested {@link ToolCommandlet} or {@code null} if not found.
*/
default ToolCommandlet getToolCommandletOrNull(String name) {

Commandlet commandlet = getCommandlet(name);
if (commandlet instanceof ToolCommandlet) {
return (ToolCommandlet) commandlet;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public CommandletManagerImpl(IdeContext context) {
add(new EditionListCommandlet(context));
add(new VersionCommandlet(context));
add(new RepositoryCommandlet(context));
add(new UpdateCommandlet(context));
add(new CreateCommandlet(context));
add(new Gh(context));
add(new Helm(context));
add(new Java(context));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessResult;
import com.devonfw.tools.ide.property.StringProperty;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* {@link Commandlet} to create a new IDEasy instance
*/
public class CreateCommandlet extends Commandlet {

private final StringProperty newInstance;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public CreateCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
newInstance = add(new StringProperty("", false, "newInstance"));
}

@Override
public String getName() {

return "create";
}

@Override
public void run() {

String newInstanceName = newInstance.getValue();
Path newInstancePath;

if (newInstanceName == null) {
newInstancePath = this.context.getCwd();
} else {
newInstancePath = this.context.getIdeRoot().resolve(newInstanceName);
this.context.getFileAccess().mkdirs(newInstancePath);
}

this.context.info("Creating new IDEasy instance in {}", newInstancePath);
if (!this.context.getFileAccess().isEmptyDir(newInstancePath)) {
this.context.askToContinue("Directory is not empty, continue?");
}

initializeInstance(newInstancePath);
ProcessContext pc = this.context.newProcess().executable("ideasy");
pc.addArgs("update");
pc.directory(newInstancePath);
if (pc.run() == ProcessResult.SUCCESS) {
this.context.success("IDEasy Instance successfully created in {}", newInstancePath);
} else {
this.context.warning("Could not create IDEasy Instance.");
}
}

private void initializeInstance(Path newInstancePath) {

FileAccess fileAccess = this.context.getFileAccess();
fileAccess.mkdirs(newInstancePath.resolve(IdeContext.FOLDER_SOFTWARE));
fileAccess.mkdirs(newInstancePath.resolve(IdeContext.FOLDER_UPDATES));
fileAccess.mkdirs(newInstancePath.resolve(IdeContext.FOLDER_PLUGINS));
fileAccess.mkdirs(newInstancePath.resolve(IdeContext.FOLDER_WORKSPACES));
fileAccess.mkdirs(newInstancePath.resolve(IdeContext.FOLDER_SETTINGS));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.common.StepContainer;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.StringProperty;
import com.devonfw.tools.ide.repo.CustomTool;
import com.devonfw.tools.ide.tool.CustomToolCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.variable.IdeVariables;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Stream;

/**
* {@link Commandlet} to update settings, software and repositories
*/
public class UpdateCommandlet extends Commandlet {

private static final String DEFAULT_SETTINGS_REPO_URL = "https://github.com/devonfw/ide-settings";

private final StringProperty settingsRepo;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public UpdateCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
settingsRepo = add(new StringProperty("", false, "settingsRepository"));
}

@Override
public String getName() {

return "update";
}

@Override
public void run() {

updateSettings();
this.context.getFileAccess().mkdirs(this.context.getWorkspacePath());
Path templatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_TEMPLATES);
if (!Files.exists(templatesFolder)) {
Path legacyTemplatesFolder = this.context.getSettingsPath().resolve(IdeContext.FOLDER_LEGACY_TEMPLATES);
if (Files.exists(legacyTemplatesFolder)) {
templatesFolder = legacyTemplatesFolder;
} else {
this.context.warning("Templates folder is missing in settings folder.");
return;
}
}
setupConf(templatesFolder, this.context.getIdeHome());
updateSoftware();
updateRepositories();
}

private void setupConf(Path template, Path conf) {

List<Path> children = this.context.getFileAccess().listChildren(template, f -> true);
for (Path child : children) {

String basename = child.getFileName().toString();
Path confPath = conf.resolve(basename);

if (Files.isDirectory(child)) {
if (!Files.isDirectory(confPath)) {
this.context.getFileAccess().mkdirs(confPath);
}
setupConf(child, confPath);
} else if (Files.isRegularFile(child)) {
if (Files.isRegularFile(confPath)) {
this.context.debug("Configuration {} already exists - skipping to copy from {}", confPath, child);
} else {
if (!basename.equals("settings.xml")) {
this.context.info("Copying template {} to {}.", child, confPath);
this.context.getFileAccess().copy(child, confPath);
}
}
}
}
}

private void updateSettings() {

this.context.info("Updating settings repository ...");
Path settingsPath = this.context.getSettingsPath();
if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
// perform git pull on the settings repo
this.context.getGitContext().pull(settingsPath);
this.context.success("Successfully updated settings repository.");
} else {
// check if a settings repository is given then clone, otherwise prompt user for a repository.
String repository = settingsRepo.getValue();
if (repository == null) {
if (this.context.isBatchMode()) {
repository = DEFAULT_SETTINGS_REPO_URL;
} else {
this.context.info("Missing your settings at {} and no SETTINGS_URL is defined.", settingsPath);
this.context.info("Further details can be found here:");
this.context.info("https://github.com/devonfw/IDEasy/blob/main/documentation/settings.asciidoc");
this.context.info("Please contact the technical lead of your project to get the SETTINGS_URL for your project.");
this.context.info("In case you just want to test IDEasy you may simply hit return to install the default settings.");
this.context.info("");
this.context.info("Settings URL [{}]:", DEFAULT_SETTINGS_REPO_URL);
repository = this.context.readLine();
}
}
if (repository.isBlank()) {
repository = DEFAULT_SETTINGS_REPO_URL;
}
this.context.getGitContext().pullOrClone(repository, settingsPath);
this.context.success("Successfully cloned settings repository.");
}
}

private void updateSoftware() {

Set<ToolCommandlet> toolCommandlets = new HashSet<>();

// installed tools in IDE_HOME/software
List<Path> softwares = this.context.getFileAccess().listChildren(this.context.getSoftwarePath(), Files::isDirectory);
for (Path software : softwares) {
String toolName = software.getFileName().toString();
ToolCommandlet toolCommandlet = this.context.getCommandletManager().getToolCommandletOrNull(toolName);
if (toolCommandlet != null) {
toolCommandlets.add(toolCommandlet);
}
}

// regular tools in $IDE_TOOLS
List<String> regularTools = IdeVariables.IDE_TOOLS.get(this.context);
if (regularTools != null) {
for (String regularTool : regularTools) {
toolCommandlets.add(this.context.getCommandletManager().getToolCommandlet(regularTool));
}
}

// custom tools
for (CustomTool customTool : this.context.getCustomToolRepository().getTools()) {
CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
toolCommandlets.add(customToolCommandlet);
}

// update/install the toolCommandlets
StepContainer container = new StepContainer(this.context);
for (ToolCommandlet toolCommandlet : toolCommandlets) {
try {
container.startStep(toolCommandlet.getName());
toolCommandlet.install(false);
container.endStep(toolCommandlet.getName(), true, null);
} catch (Exception e) {
container.endStep(toolCommandlet.getName(), false, e);
}
}
// summary
container.complete();
}

private void updateRepositories() {
this.context.getCommandletManager().getCommandlet(RepositoryCommandlet.class).run();
}
}
89 changes: 89 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/common/StepContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.devonfw.tools.ide.common;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.IdeContext;

import java.util.ArrayList;
import java.util.List;

/**
* A utility class to manage and log the progress of steps in a process.
* Each step can be started, ended with success or failure, and the overall completion
* status can be checked.
* @throws CliException if one or more steps fail.
*/
public class StepContainer {

private final IdeContext context;

/** List of steps that ended successfully. */
private List<String> successfulSteps;

/** List of steps that failed. */
private List<String> failedSteps;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public StepContainer(IdeContext context) {

this.context = context;
successfulSteps = new ArrayList<>();
failedSteps = new ArrayList<>();
}

/**
* Logs the start of a step.
*
* @param stepName the name of the step.
*/
public void startStep(String stepName) {

this.context.step("Starting step: {}", stepName);
}

/**
* Logs the end of a step, indicating success or failure.
*
* @param stepName the name of the step.
* @param success {@code true} if the step succeeded, {@code false} otherwise.
* @param e the exception associated with the failure, or {@code null} if the step succeeded.
*/
public void endStep(String stepName, boolean success, Throwable e) {

if (success) {
successfulSteps.add(stepName);
this.context.success("Step '{}' succeeded.", stepName);
} else {
failedSteps.add(stepName);
this.context.warning("Step '{}' failed.", stepName);
if (e != null) {
this.context.error(e);
}
}
}

/**
* Checks the overall completion status of all steps.
*
* @throws CliException if one or more steps fail, providing a detailed summary.
*/
public void complete() {

if (failedSteps.isEmpty()) {
this.context.success("All {} steps ended successfully!", successfulSteps.size());
} else {
throw new CliException(String.format("%d step(s) failed (%d%%) and %d step(s) succeeded (%d%%) out of %d step(s)!",
failedSteps.size(), calculatePercentage(failedSteps.size()), successfulSteps.size(),
100 - calculatePercentage(failedSteps.size()), successfulSteps.size() + failedSteps.size()));
}
}

private int calculatePercentage(int count) {

return (count * 100) / (successfulSteps.size() + failedSteps.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public <O> O question(String question, O... options) {
/**
* @return the input from the end-user (e.g. read from the console).
*/
protected abstract String readLine();
public abstract String readLine();

private static <O> void addMapping(Map<String, O> mapping, String key, O option) {

Expand Down
Loading
Loading