Skip to content

Commit

Permalink
Merge branch 'main' into feature/399-uninstall-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Jun 25, 2024
2 parents 79b50cf + 79ff36f commit 85f6a89
Show file tree
Hide file tree
Showing 25 changed files with 564 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void updateConf() {
setupConf(templatesFolder, this.context.getIdeHome());
step.success();
} finally {
step.end();
step.close();
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ private void updateSettings() {
step.success("Successfully updated settings repository.");
} finally {
if (step != null) {
step.end();
step.close();
}
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ private void updateSoftware() {
}
step.success();
} finally {
step.end();
step.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public boolean isIdeHomeRequired() {
return true;
}

@Override
public boolean isSuppressStepSuccess() {

return true;
}

@Override
public void run() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ public int run(CliArguments arguments) {
step.error(t, true);
throw t;
} finally {
step.end();
step.close();
assert (this.currentStep == null);
step.logSummary(supressStepSuccess);
}
Expand Down
32 changes: 18 additions & 14 deletions cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,25 @@ default void requireOnline(String purpose) {
*/
default String getMavenArgs() {

if (getIdeHome() != null) {
Path mvnSettingsFile = getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
if (Files.exists(mvnSettingsFile)) {
String settingsPath;
WindowsPathSyntax pathSyntax = null; // getPathSyntax();
if (pathSyntax == null) {
settingsPath = mvnSettingsFile.toString();
} else {
settingsPath = pathSyntax.format(mvnSettingsFile);
}
return "-s " + settingsPath;
if (getIdeHome() == null) {
return null;
}
Path confFolder = getConfPath();
Path mvnSettingsFile = confFolder.resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
if (!Files.exists(mvnSettingsFile)) {
mvnSettingsFile = confFolder.resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER).resolve(Mvn.SETTINGS_FILE);
if (!Files.exists(mvnSettingsFile)) {
return null;
}
}
return null;

String settingsPath;
WindowsPathSyntax pathSyntax = getPathSyntax();
if (pathSyntax == null) {
settingsPath = mvnSettingsFile.toString();
} else {
settingsPath = pathSyntax.format(mvnSettingsFile);
}
return "-s " + settingsPath;
}

/**
Expand All @@ -459,7 +463,7 @@ default String getMavenArgs() {
default Path getMavenRepoEnvVariable() {

if (getIdeHome() != null) {
return getConfPath().resolve(Mvn.M2_CONFIG_FOLDER).resolve("repository");
return getConfPath().resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER).resolve("repository");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.devonfw.tools.ide.environment;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.variable.IdeVariables;
import com.devonfw.tools.ide.variable.VariableDefinition;
import com.devonfw.tools.ide.variable.VariableSyntax;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -11,7 +13,6 @@
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Abstract base implementation of {@link EnvironmentVariables}.
Expand All @@ -24,17 +25,10 @@ public abstract class AbstractEnvironmentVariables implements EnvironmentVariabl
*/
private static final int EXTRA_CAPACITY = 8;

// Variable surrounded with "${" and "}" such as "${JAVA_HOME}" 1......2........
private static final Pattern VARIABLE_SYNTAX = Pattern.compile("(\\$\\{([^}]+)})");

private static final String SELF_REFERENCING_NOT_FOUND = "";

private static final int MAX_RECURSION = 9;

private static final String VARIABLE_PREFIX = "${";

private static final String VARIABLE_SUFFIX = "}";

/**
* @see #getParent()
*/
Expand Down Expand Up @@ -164,21 +158,24 @@ public EnvironmentVariables resolved() {
}

@Override
public String resolve(String string, Object src) {
public String resolve(String string, Object source) {
return resolveRecursive(string, source, 0, this, new ResolveContext(source, string, false, VariableSyntax.CURLY));
}

@Override
public String resolve(String string, Object source, boolean legacySupport) {

return resolve(string, src, 0, src, string, this);
return resolveRecursive(string, source, 0, this, new ResolveContext(source, string, legacySupport, null));
}

/**
* This method is called recursively. This allows you to resolve variables that are defined by other variables.
*
* @param value the {@link String} that potentially contains variables in the syntax "${«variable«}". Those will be resolved by this method and replaced with
* their {@link #get(String) value}.
* @param src the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation} that
* @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation} that
* will be used in error or log messages if a variable could not be resolved.
* @param recursion the current recursion level. This is used to interrupt endless recursion.
* @param rootSrc the root source where the {@link String} to resolve originates from.
* @param rootValue the root value to resolve.
* @param resolvedVars this is a reference to an object of {@link EnvironmentVariablesResolved} being the lowest level in the
* {@link EnvironmentVariablesType hierarchy} of variables. In case of a self-referencing variable {@code x} the resolving has to continue one level higher in
* the {@link EnvironmentVariablesType hierarchy} to avoid endless recursion. The {@link EnvironmentVariablesResolved} is then used if another variable
Expand All @@ -187,34 +184,58 @@ public String resolve(String string, Object src) {
* first resolved at level {@code l1} and then up the {@link EnvironmentVariablesType hierarchy} at {@code l2} to avoid endless recursion. However, {@code y}
* must be resolved starting from the lowest level in the {@link EnvironmentVariablesType hierarchy} and therefore {@link EnvironmentVariablesResolved} is
* used.
* @param context the {@link ResolveContext}.
* @return the given {@link String} with the variables resolved.
*/
private String resolve(String value, Object src, int recursion, Object rootSrc, String rootValue, AbstractEnvironmentVariables resolvedVars) {
private String resolveRecursive(String value, Object source, int recursion, AbstractEnvironmentVariables resolvedVars, ResolveContext context) {

if (value == null) {
return null;
}
if (recursion > MAX_RECURSION) {
throw new IllegalStateException("Reached maximum recursion resolving " + value + " for root variable " + rootSrc + " with value '" + rootValue + "'.");
throw new IllegalStateException("Reached maximum recursion resolving " + value + " for root variable " + context.rootSrc + " with value '" + context.rootValue + "'.");
}
recursion++;

Matcher matcher = VARIABLE_SYNTAX.matcher(value);
String resolved;
if (context.syntax == null) {
resolved = resolveWithSyntax(value, source, recursion, resolvedVars, context, VariableSyntax.SQUARE);
if (context.legacySupport) {
resolved = resolveWithSyntax(value, source, recursion, resolvedVars, context, VariableSyntax.CURLY);
}
} else {
resolved = resolveWithSyntax(value, source, recursion, resolvedVars, context, context.syntax);
}
return resolved;
}

private String resolveWithSyntax(final String value, final Object src, final int recursion, final AbstractEnvironmentVariables resolvedVars, final ResolveContext context, final VariableSyntax syntax) {

Matcher matcher = syntax.getPattern().matcher(value);
if (!matcher.find()) {
return value;
}
StringBuilder sb = new StringBuilder(value.length() + EXTRA_CAPACITY);
do {
String variableName = matcher.group(2);
String variableName = syntax.getVariable(matcher);
String variableValue = resolvedVars.getValue(variableName, false);
if (variableValue == null) {
this.context.warning("Undefined variable {} in '{}={}' for root '{}={}'", variableName, src, value, rootSrc, rootValue);
IdeLogLevel logLevel = IdeLogLevel.WARNING;
if (context.legacySupport && (syntax == VariableSyntax.CURLY)) {
logLevel = IdeLogLevel.INFO;
}
String var = matcher.group();
if (recursion > 1) {
this.context.level(logLevel).log("Undefined variable {} in '{}' at '{}={}'", var, context.rootSrc, src, value);
} else {
this.context.level(logLevel).log("Undefined variable {} in '{}'", var, src);
}
continue;
}
EnvironmentVariables lowestFound = findVariable(variableName);
if ((lowestFound == null) || !lowestFound.getFlat(variableName).equals(value)) {
// looking for "variableName" starting from resolved upwards the hierarchy
String replacement = resolvedVars.resolve(variableValue, variableName, recursion, rootSrc, rootValue, resolvedVars);
String replacement = resolvedVars.resolveRecursive(variableValue, variableName, recursion, resolvedVars, context);
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
} else { // is self referencing
// finding next occurrence of "variableName" up the hierarchy of EnvironmentVariablesType
Expand All @@ -231,8 +252,8 @@ private String resolve(String value, Object src, int recursion, Object rootSrc,
}
// resolving a self referencing variable one level up the hierarchy of EnvironmentVariablesType, i.e. at "next",
// to avoid endless recursion
String replacement = ((AbstractEnvironmentVariables) next).resolve(next.getFlat(variableName), variableName, recursion, rootSrc, rootValue,
resolvedVars);
String replacement = ((AbstractEnvironmentVariables) next).resolveRecursive(next.getFlat(variableName), variableName, recursion, resolvedVars, context
);
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));

}
Expand Down Expand Up @@ -278,7 +299,7 @@ protected String getValue(String name, boolean ignoreDefaultValue) {
}

@Override
public String inverseResolve(String string, Object src) {
public String inverseResolve(String string, Object src, VariableSyntax syntax) {

String result = string;
// TODO add more variables to IdeVariables like JAVA_HOME
Expand All @@ -290,7 +311,7 @@ public String inverseResolve(String string, Object src) {
value = variable.getDefaultValueAsString(this.context);
}
if (value != null) {
result = result.replace(value, VARIABLE_PREFIX + name + VARIABLE_SUFFIX);
result = result.replace(value, syntax.create(name));
}
}
}
Expand All @@ -306,4 +327,16 @@ public String toString() {
return getSource();
}

/**
* Simple record for the immutable arguments of recursive resolve methods.
*
* @param rootSrc the root source where the {@link String} to resolve originates from.
* @param rootValue the root value to resolve.
* @param legacySupport flag for legacy support (see {@link #resolve(String, Object, boolean)}). Only considered if {@link #syntax()} is {@code null}.
* @param syntax the explicit {@link VariableSyntax} to use.
*/
private static record ResolveContext(Object rootSrc, String rootValue, boolean legacySupport, VariableSyntax syntax) {

}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.devonfw.tools.ide.environment;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Locale;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.variable.VariableDefinition;
import com.devonfw.tools.ide.variable.VariableSyntax;
import com.devonfw.tools.ide.version.VersionIdentifier;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Locale;

/**
* Interface for the environment with the variables.
*/
Expand Down Expand Up @@ -193,32 +194,60 @@ default EnvironmentVariables findVariable(String name) {
Collection<VariableLine> collectExportedVariables();

/**
* @param string the {@link String} that potentially contains variables in the syntax "${«variable«}". Those will be
* resolved by this method and replaced with their {@link #get(String) value}.
* @param string the {@link String} that potentially contains variables in {@link VariableSyntax#CURLY} ("${«variable«}"). Those will be
* resolved by this method and replaced with their {@link #get(String) value}.
* @param source the source where the {@link String} to resolve originates from. Should have a reasonable
* {@link Object#toString() string representation} that will be used in error or log messages if a variable
* could not be resolved.
* {@link Object#toString() string representation} that will be used in error or log messages if a variable
* could not be resolved.
* @return the given {@link String} with the variables resolved.
* @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
*/
String resolve(String string, Object source);

/**
* The inverse operation of {@link #resolve(String, Object)}. Please note that the {@link #resolve(String, Object)
* @param string the {@link String} that potentially contains variables in {@link VariableSyntax}. Those will be
* resolved by this method and replaced with their {@link #get(String) value}.
* @param source the source where the {@link String} to resolve originates from. Should have a reasonable
* {@link Object#toString() string representation} that will be used in error or log messages if a variable
* could not be resolved.
* @param legacySupport
* @return the given {@link String} with the variables resolved.
* @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
*/
String resolve(String string, Object source, boolean legacySupport);

/**
* The inverse operation of {@link #resolve(String, Object, boolean)}. Please note that the {@link #resolve(String, Object, boolean)
* resolve} operation is not fully bijective. There may be multiple variables holding the same {@link #get(String)
* value} or there may be static text that can be equal to a {@link #get(String) variable value}. This method does its
* best to implement the inverse resolution based on some heuristics.
*
* @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
* "${«variable«}" expressions.
* {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
* @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
* {@link Object#toString() string representation} that will be used in error or log messages if the inverse
* resolving was not working as expected.
* @return the given {@link String} with {@link #get(String) variable values} replaced with according "${«variable«}"
* expressions.
* {@link Object#toString() string representation} that will be used in error or log messages if the inverse
* resolving was not working as expected.
* @return the given {@link String} with {@link #get(String) variable values} replaced with according
* {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
* @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
*/
String inverseResolve(String string, Object source);
default String inverseResolve(String string, Object source) {

return inverseResolve(string, source, VariableSyntax.SQUARE);
}

/**
* @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
* {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
* @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
* {@link Object#toString() string representation} that will be used in error or log messages if the inverse
* resolving was not working as expected.
* @param syntax the explicit {@link VariableSyntax} to use.
* @return the given {@link String} with {@link #get(String) variable values} replaced with according
* {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
* @see #inverseResolve(String, Object)
*/
String inverseResolve(String string, Object source, VariableSyntax syntax);

/**
* @param context the {@link IdeContext}.
Expand Down
4 changes: 4 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/merge/FileMerger.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.tools.ide.merge;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.variable.IdeVariables;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -11,6 +12,8 @@
*/
public abstract class FileMerger extends AbstractWorkspaceMerger {

protected final boolean legacySupport;

/**
* The constructor.
*
Expand All @@ -19,6 +22,7 @@ public abstract class FileMerger extends AbstractWorkspaceMerger {
public FileMerger(IdeContext context) {

super(context);
this.legacySupport = IdeVariables.IDE_VARIABLE_SYNTAX_LEGACY_SUPPORT_ENABLED.get(context).booleanValue();
}

/**
Expand Down
Loading

0 comments on commit 85f6a89

Please sign in to comment.