Skip to content

Commit

Permalink
#430: Add source of env variable for debugging (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
slskiba authored Aug 2, 2024
1 parent d30694b commit c89f8af
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.tools.ide.commandlet;

import java.util.Collection;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.context.AbstractIdeContext;
import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -59,12 +60,26 @@ public void run() {
}
((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
Collection<VariableLine> variables = this.context.getVariables().collectVariables();
for (VariableLine line : variables) {
String lineValue = line.getValue();
lineValue = "\"" + lineValue + "\"";
line = line.withValue(lineValue);
this.context.info(line.toString());
if (this.context.debug().isEnabled()) {
for (String source : variables.stream().map(VariableLine::getSource).collect(Collectors.toSet())) {
this.context.debug("from {}:", source);
for (VariableLine line : variables) {
if (line.getSource().equals(source)) {
printEnvLine(line);
}
}
}
} else {
for (VariableLine line : variables) {
printEnvLine(line);
}
}
}

private void printEnvLine(VariableLine line) {
String lineValue = line.getValue();
lineValue = "\"" + lineValue + "\"";
line = line.withValue(lineValue);
this.context.info(line.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.regex.Matcher;

import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -114,25 +114,25 @@ public final Collection<VariableLine> collectExportedVariables() {

private final Collection<VariableLine> collectVariables(boolean onlyExported) {

Set<String> variableNames = new HashSet<>();
Map<String, String> variableNames = new HashMap<>();
collectVariables(variableNames);
List<VariableLine> variables = new ArrayList<>(variableNames.size());
for (String name : variableNames) {
for (String name : variableNames.keySet()) {
boolean export = isExported(name);
if (!onlyExported || export) {
String value = get(name, false);
if (value != null) {
variables.add(VariableLine.of(export, name, value));
variables.add(VariableLine.of(export, name, value, variableNames.get(name)));
}
}
}
return variables;
}

/**
* @param variables the {@link Set} where to add the names of the variables defined here.
* @param variables the {@link Map} where to add the names of the variables defined here as keys, and their corresponding source as value.
*/
protected void collectVariables(Set<String> variables) {
protected void collectVariables(Map<String, String> variables) {

if (this.parent != null) {
this.parent.collectVariables(variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ protected Map<String, String> getVariables() {
}

@Override
protected void collectVariables(Set<String> variableNames) {
protected void collectVariables(Map<String, String> variableNames) {

for (String key : this.variables.keySet()) {
variableNames.put(key, this.propertiesFilePath.toString());
}

variableNames.addAll(this.variables.keySet());
super.collectVariables(variableNames);
}

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

import java.util.Set;
import java.util.Map;

import com.devonfw.tools.ide.variable.IdeVariables;
import com.devonfw.tools.ide.variable.VariableDefinition;
Expand Down Expand Up @@ -49,11 +49,11 @@ public EnvironmentVariables resolved() {
}

@Override
protected void collectVariables(Set<String> variables) {
protected void collectVariables(Map<String, String> variables) {

for (VariableDefinition<?> var : IdeVariables.VARIABLES) {
if (var.isExport() || var.isForceDefaultValue()) {
variables.add(var.getName());
variables.put(var.getName(), "defaults");
}
}
super.collectVariables(variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public String getComment() {
return null;
}

/**
* @return the source of the variable.
*/
public String getSource() {

return null;
}

/**
* @param newName the new variable {@link #getName() name}.
* @return the new {@link VariableLine} with the modified {@link #getName() name}.
Expand Down Expand Up @@ -77,12 +85,14 @@ static final class Variable extends VariableLine {

private final String line;

private final Object source;

Variable(boolean export, String name, String value) {

this(export, name, value, null);
this(export, name, value, null, null);
}

private Variable(boolean export, String name, String value, String line) {
private Variable(boolean export, String name, String value, String line, Object source) {

super();
this.export = export;
Expand All @@ -100,6 +110,7 @@ private Variable(boolean export, String name, String value, String line) {
} else {
this.line = line;
}
this.source = source;
}

@Override
Expand All @@ -120,6 +131,11 @@ public String getValue() {
return this.value;
}

@Override
public String getSource() {
return source.toString();
}

@Override
public VariableLine withName(String newName) {

Expand Down Expand Up @@ -269,7 +285,7 @@ public static VariableLine of(String line, IdeLogger logger, Object source) {
} else if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
return new Variable(export, name, value, line);
return new Variable(export, name, value, line, source);
}
end++;
}
Expand All @@ -288,4 +304,16 @@ public static VariableLine of(boolean export, String name, String value) {
return new Variable(export, name, value);
}

/**
* @param export the {@link #isExport() export flag}.
* @param name the {@link #getName() name}.
* @param value the {@link #getValue() value}.
* @param source the {@link #getSource() source} of the variable.
* @return the {@link VariableLine} for the given values.
*/
public static VariableLine of(boolean export, String name, String value, Object source) {

return new Variable(export, name, value, null, source);
}

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

import java.nio.file.Path;

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
Expand All @@ -22,6 +24,8 @@ public void testRun() {
String path = "project/workspaces/foo-test/my-git-repo";
IdeTestContext context = newContext(PROJECT_BASIC, path, false);
EnvironmentCommandlet env = context.getCommandletManager().getCommandlet(EnvironmentCommandlet.class);
Path settingsIdeProperties = context.getSettingsPath().resolve("ide.properties");
Path confIdeProperties = context.getConfPath().resolve("ide.properties");
// act
env.run();
// assert
Expand All @@ -34,6 +38,11 @@ public void testRun() {
assertLogMessage(context, IdeLogLevel.INFO, "JAVA_VERSION=\"17*\"");
assertLogMessage(context, IdeLogLevel.INFO, "INTELLIJ_EDITION=\"ultimate\"");
assertLogMessage(context, IdeLogLevel.INFO, "DOCKER_EDITION=\"docker\"");
//assert messages of debug level grouping of environment variable are present
assertLogMessage(context, IdeLogLevel.DEBUG, "from defaults:");
assertLogMessage(context, IdeLogLevel.DEBUG, "from " + settingsIdeProperties + ":");
assertLogMessage(context, IdeLogLevel.DEBUG, "from " + confIdeProperties + ":");

}

/**
Expand Down

0 comments on commit c89f8af

Please sign in to comment.