Skip to content

Commit

Permalink
improve logging, step debugging, various fixes (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Apr 12, 2024
1 parent d8c158e commit 43c1a70
Show file tree
Hide file tree
Showing 46 changed files with 2,641 additions and 1,664 deletions.
34 changes: 18 additions & 16 deletions cli/src/main/java/com/devonfw/tools/ide/cli/CliAbortException.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.devonfw.tools.ide.cli;

/**
* {@link CliException} that is thrown if the user aborted further processing due
*/
public final class CliAbortException extends CliException {

/**
* The constructor.
*/
public CliAbortException() {

super("Aborted by end-user.", 22);
}

}
package com.devonfw.tools.ide.cli;

import com.devonfw.tools.ide.process.ProcessResult;

/**
* {@link CliException} that is thrown if the user aborted further processing due
*/
public final class CliAbortException extends CliException {

/**
* The constructor.
*/
public CliAbortException() {

super("Aborted by end-user.", ProcessResult.ABORT);
}

}
16 changes: 16 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/cli/CliArgument.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.devonfw.tools.ide.cli;

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

/**
Expand Down Expand Up @@ -256,6 +258,20 @@ private CliArgument createStart() {
return new CliArgument(NAME_START, this);
}

/**
* @return a {@link String} array with all arguments starting from this one.
*/
public String[] asArray() {

List<String> args = new ArrayList<>();
CliArgument current = this;
while (!current.isEnd()) {
args.add(current.arg);
current = current.next;
}
return args.toArray(size -> new String[size]);
}

@Override
public String toString() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.devonfw.tools.ide.cli;

import com.devonfw.tools.ide.process.ProcessResult;

/**
* {@link CliException} that is thrown if further processing requires network but the user if offline.
*/
public final class CliOfflineException extends CliException {

/**
* The constructor.
*/
public CliOfflineException() {

super("You are offline but network connection is required to perform the operation.", ProcessResult.OFFLINE);
}

/**
* The constructor.
*
* @param message the {@link #getMessage() message}.
*/
public CliOfflineException(String message) {

super(message, ProcessResult.OFFLINE);
}

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

import com.devonfw.tools.ide.common.StepContainer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.devonfw.tools.ide.context.GitContext;
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.step.Step;
import com.devonfw.tools.ide.tool.CustomToolCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.variable.IdeVariables;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Abstract {@link Commandlet} base-class for both {@link UpdateCommandlet} and {@link CreateCommandlet}.
*/
public abstract class AbstractUpdateCommandlet extends Commandlet {

/** {@link StringProperty} for the settings repository URL. */
protected final StringProperty settingsRepo;

/**
Expand All @@ -26,7 +31,7 @@ public abstract class AbstractUpdateCommandlet extends Commandlet {
public AbstractUpdateCommandlet(IdeContext context) {

super(context);
settingsRepo = new StringProperty("", false, "settingsRepository");
this.settingsRepo = new StringProperty("", false, "settingsRepository");
}

@Override
Expand All @@ -45,7 +50,13 @@ public void run() {
}
}

setupConf(templatesFolder, this.context.getIdeHome());
Step step = this.context.newStep("Copy configuration templates", templatesFolder);
try {
setupConf(templatesFolder, this.context.getIdeHome());
step.success();
} finally {
step.end();
}
updateSoftware();
}

Expand Down Expand Up @@ -77,72 +88,77 @@ private void setupConf(Path template, Path conf) {

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) {
String message = "Missing your settings at " + settingsPath + " and no SETTINGS_URL is defined.\n" +
"Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.asciidoc\n" +
"Please contact the technical lead of your project to get the SETTINGS_URL for your project.\n" +
"In case you just want to test IDEasy you may simply hit return to install the default settings.\n" +
"Settings URL [" + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
GitContext gitContext = this.context.getGitContext();
Step step = null;
try {
// here we do not use pullOrClone to prevent asking a pointless question for repository URL...
if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
step = this.context.newStep("Pull settings repository");
gitContext.pull(settingsPath);
} else {
step = this.context.newStep("Clone settings repository");
// check if a settings repository is given, otherwise prompt user for a repository.
String repository = this.settingsRepo.getValue();
if (repository == null) {
String message = "Missing your settings at " + settingsPath + " and no SETTINGS_URL is defined.\n"
+ "Further details can be found here: https://github.com/devonfw/IDEasy/blob/main/documentation/settings.asciidoc\n"
+ "Please contact the technical lead of your project to get the SETTINGS_URL for your project.\n"
+ "In case you just want to test IDEasy you may simply hit return to install the default settings.\n"
+ "Settings URL [" + IdeContext.DEFAULT_SETTINGS_REPO_URL + "]:";
repository = this.context.askForInput(message, IdeContext.DEFAULT_SETTINGS_REPO_URL);
} else if ("-".equals(repository)) {
repository = IdeContext.DEFAULT_SETTINGS_REPO_URL;
}
gitContext.pullOrClone(repository, settingsPath);
}
step.success("Successfully updated settings repository.");
} finally {
if (step != null) {
step.end();
}
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);
Step step = this.context.newStep("Install or update software");
try {
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));
// 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 in ide-custom-tools.json
for (CustomTool customTool : this.context.getCustomToolRepository().getTools()) {
CustomToolCommandlet customToolCommandlet = new CustomToolCommandlet(this.context, customTool);
toolCommandlets.add(customToolCommandlet);
}
// custom tools in ide-custom-tools.json
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());
// update/install the toolCommandlets
for (ToolCommandlet toolCommandlet : toolCommandlets) {
toolCommandlet.install(false);
container.endStep(toolCommandlet.getName(), true, null);
} catch (Exception e) {
container.endStep(toolCommandlet.getName(), false, e);
}
}
// summary
if (!toolCommandlets.isEmpty()) {
container.complete();
step.success();
} finally {
step.end();
}
}

}

Loading

0 comments on commit 43c1a70

Please sign in to comment.