Skip to content

Commit

Permalink
#507: improve collect variables (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Aug 6, 2024
1 parent 5061acf commit c10b48d
Show file tree
Hide file tree
Showing 27 changed files with 439 additions and 154 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.devonfw.tools.ide.commandlet;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.context.AbstractIdeContext;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.environment.VariableLine;
import com.devonfw.tools.ide.os.WindowsPathSyntax;
import com.devonfw.tools.ide.property.FlagProperty;
Expand Down Expand Up @@ -59,23 +62,35 @@ public void run() {
}
}
((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
Collection<VariableLine> variables = this.context.getVariables().collectVariables();
List<VariableLine> variables = this.context.getVariables().collectVariables();
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)) {
Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variables.stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
List<VariableLine> lines = type2lines.get(type);
if (lines != null) {
boolean sourcePrinted = false;
sortVariables(lines);
for (VariableLine line : lines) {
if (!sourcePrinted) {
this.context.debug("from {}:", line.getSource());
sourcePrinted = true;
}
printEnvLine(line);
}
}
}
} else {
sortVariables(variables);
for (VariableLine line : variables) {
printEnvLine(line);
}
}
}

private static void sortVariables(List<VariableLine> lines) {
Collections.sort(lines, (c1, c2) -> c1.getName().compareTo(c2.getName()));
}

private void printEnvLine(VariableLine line) {
String lineValue = line.getValue();
lineValue = "\"" + lineValue + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ private static String getTool(Path path, Path ideRoot) {
return null;
}
if (path.startsWith(ideRoot)) {
int i = ideRoot.getNameCount();
if (path.getNameCount() > i) {
return path.getName(i).toString();
Path relativized = ideRoot.relativize(path);
int count = relativized.getNameCount();
if (count >= 3) {
if (relativized.getName(1).toString().equals("software")) {
return relativized.getName(2).toString();
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public boolean isMock() {
return false;
}

private SystemPath computeSystemPath() {
protected SystemPath computeSystemPath() {

return new SystemPath(this);
}
Expand Down Expand Up @@ -352,7 +352,7 @@ private Path getParentPath(Path dir) {

private EnvironmentVariables createVariables() {

AbstractEnvironmentVariables system = EnvironmentVariables.ofSystem(this);
AbstractEnvironmentVariables system = createSystemVariables();
AbstractEnvironmentVariables user = extendVariables(system, this.userHomeIde, EnvironmentVariablesType.USER);
AbstractEnvironmentVariables settings = extendVariables(user, this.settingsPath, EnvironmentVariablesType.SETTINGS);
// TODO should we keep this workspace properties? Was this feature ever used?
Expand All @@ -361,7 +361,12 @@ private EnvironmentVariables createVariables() {
return conf.resolved();
}

private AbstractEnvironmentVariables extendVariables(AbstractEnvironmentVariables envVariables, Path propertiesPath, EnvironmentVariablesType type) {
protected AbstractEnvironmentVariables createSystemVariables() {

return EnvironmentVariables.ofSystem(this);
}

protected AbstractEnvironmentVariables extendVariables(AbstractEnvironmentVariables envVariables, Path propertiesPath, EnvironmentVariablesType type) {

Path propertiesFile = null;
if (propertiesPath == null) {
Expand Down
41 changes: 33 additions & 8 deletions cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@ default void requireOnline(String purpose) {
*/
Path getSettingsPath();

/**
* @return the {@link Path} to the templates folder inside the {@link #getSettingsPath() settings}. The relative directory structure in this templates folder
* is to be applied to {@link #getIdeHome() IDE_HOME} when the project is set up.
*/
default Path getSettingsTemplatePath() {
Path settingsFolder = getSettingsPath();
Path templatesFolder = settingsFolder.resolve(IdeContext.FOLDER_TEMPLATES);
if (!Files.isDirectory(templatesFolder)) {
Path templatesFolderLegacy = settingsFolder.resolve(IdeContext.FOLDER_LEGACY_TEMPLATES);
if (Files.isDirectory(templatesFolderLegacy)) {
templatesFolder = templatesFolderLegacy;
} else {
warning("No templates found in settings git repo neither in {} nor in {} - configuration broken", templatesFolder, templatesFolderLegacy);
return null;
}
}
return templatesFolder;
}

/**
* @return the {@link Path} to the {@code conf} folder with instance specific tool configurations and the
* {@link EnvironmentVariablesType#CONF user specific project configuration}.
Expand Down Expand Up @@ -439,13 +458,11 @@ default String getMavenArgs() {
if (getIdeHome() == null) {
return null;
}
Path confFolder = getConfPath();
Path mvnSettingsFile = confFolder.resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
Mvn mvn = getCommandletManager().getCommandlet(Mvn.class);
Path mavenConfFolder = mvn.getMavenConfFolder(false);
Path mvnSettingsFile = mavenConfFolder.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();
Expand All @@ -460,10 +477,18 @@ default String getMavenArgs() {
/**
* @return the String value for the variable M2_REPO, or null if called outside an IDEasy installation.
*/
default Path getMavenRepoEnvVariable() {
default Path getMavenRepository() {

if (getIdeHome() != null) {
return getConfPath().resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER).resolve("repository");
Path confPath = getConfPath();
Path m2Folder = confPath.resolve(Mvn.MVN_CONFIG_FOLDER);
if (!Files.isDirectory(m2Folder)) {
Path m2LegacyFolder = confPath.resolve(Mvn.MVN_CONFIG_LEGACY_FOLDER);
if (Files.isDirectory(m2LegacyFolder)) {
m2Folder = m2LegacyFolder;
}
}
return m2Folder.resolve("repository");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -39,7 +38,7 @@ public abstract class AbstractEnvironmentVariables implements EnvironmentVariabl
*/
protected final IdeContext context;

private String source;
private VariableSource source;

/**
* The constructor.
Expand Down Expand Up @@ -74,14 +73,10 @@ public Path getPropertiesFilePath() {
}

@Override
public String getSource() {
public VariableSource getSource() {

if (this.source == null) {
this.source = getType().toString();
Path propertiesPath = getPropertiesFilePath();
if (propertiesPath != null) {
this.source = this.source + "@" + propertiesPath;
}
this.source = new VariableSource(getType(), getPropertiesFilePath());
}
return this.source;
}
Expand All @@ -101,44 +96,46 @@ protected boolean isExported(String name) {
}

@Override
public final Collection<VariableLine> collectVariables() {
public final List<VariableLine> collectVariables() {

return collectVariables(false);
}

@Override
public final Collection<VariableLine> collectExportedVariables() {
public final List<VariableLine> collectExportedVariables() {

return collectVariables(true);
}

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

Map<String, String> variableNames = new HashMap<>();
collectVariables(variableNames);
List<VariableLine> variables = new ArrayList<>(variableNames.size());
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, variableNames.get(name)));
}
}
}
return variables;
Map<String, VariableLine> variables = new HashMap<>();
collectVariables(variables, onlyExported, this);
return new ArrayList<>(variables.values());
}

/**
* @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(Map<String, String> variables) {
protected void collectVariables(Map<String, VariableLine> variables, boolean onlyExported, AbstractEnvironmentVariables resolver) {

if (this.parent != null) {
this.parent.collectVariables(variables);
this.parent.collectVariables(variables, onlyExported, resolver);
}
}

protected VariableLine createVariableLine(String name, boolean onlyExported, AbstractEnvironmentVariables resolver) {

boolean export = resolver.isExported(name);
if (!onlyExported || export) {
String value = resolver.get(name, false);
if (value != null) {
return VariableLine.of(export, name, value, getSource());
}
}
return null;
}

/**
* @param propertiesFilePath the {@link #getPropertiesFilePath() propertiesFilePath} of the child {@link EnvironmentVariables}.
* @param type the {@link #getType() type}.
Expand Down Expand Up @@ -326,7 +323,7 @@ public String inverseResolve(String string, Object src, VariableSyntax syntax) {
@Override
public String toString() {

return getSource();
return getSource().toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -126,9 +127,9 @@ default EnvironmentVariables getByType(EnvironmentVariablesType type) {
Path getPropertiesFilePath();

/**
* @return the source identifier describing this {@link EnvironmentVariables} for debugging.
* @return the {@link VariableSource} of this {@link EnvironmentVariables}.
*/
String getSource();
VariableSource getSource();

/**
* @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the {@link EnvironmentVariablesType#SYSTEM root}
Expand Down Expand Up @@ -180,13 +181,13 @@ default EnvironmentVariables findVariable(String name) {
/**
* @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
*/
Collection<VariableLine> collectVariables();
List<VariableLine> collectVariables();

/**
* @return the {@link Collection} of the {@link VariableLine#isExport() exported} {@link VariableLine}s defined by this {@link EnvironmentVariables} including
* inheritance.
*/
Collection<VariableLine> collectExportedVariables();
List<VariableLine> collectExportedVariables();

/**
* @param string the {@link String} that potentially contains variables in {@link VariableSyntax#CURLY} ("${«variable«}"). Those will be resolved by this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Implementation of {@link EnvironmentVariables}.
*/
final class EnvironmentVariablesPropertiesFile extends EnvironmentVariablesMap {
public final class EnvironmentVariablesPropertiesFile extends EnvironmentVariablesMap {

private static final String NEWLINE = "\n";

Expand All @@ -40,11 +40,10 @@ final class EnvironmentVariablesPropertiesFile extends EnvironmentVariablesMap {
*
* @param parent the parent {@link EnvironmentVariables} to inherit from.
* @param type the {@link #getType() type}.
* @param source the {@link #getSource() source}.
* @param propertiesFilePath the {@link #getSource() source}.
* @param context the {@link IdeContext}.
* @param variables the underlying variables.
*/
EnvironmentVariablesPropertiesFile(AbstractEnvironmentVariables parent, EnvironmentVariablesType type,
public EnvironmentVariablesPropertiesFile(AbstractEnvironmentVariables parent, EnvironmentVariablesType type,
Path propertiesFilePath, IdeContext context) {

super(parent, context);
Expand Down Expand Up @@ -73,7 +72,7 @@ private void load() {
do {
line = reader.readLine();
if (line != null) {
VariableLine variableLine = VariableLine.of(line, this.context, this.propertiesFilePath);
VariableLine variableLine = VariableLine.of(line, this.context, getSource());
String name = variableLine.getName();
if (name != null) {
variableLine = migrateLine(variableLine, false);
Expand Down Expand Up @@ -110,7 +109,7 @@ public void save() {
do {
line = reader.readLine();
if (line != null) {
VariableLine variableLine = VariableLine.of(line, this.context, reader);
VariableLine variableLine = VariableLine.of(line, this.context, getSource());
lines.add(variableLine);
}
} while (line != null);
Expand Down Expand Up @@ -189,13 +188,12 @@ protected Map<String, String> getVariables() {
}

@Override
protected void collectVariables(Map<String, String> variableNames) {
protected void collectVariables(Map<String, VariableLine> variables, boolean onlyExported, AbstractEnvironmentVariables resolver) {

for (String key : this.variables.keySet()) {
variableNames.put(key, this.propertiesFilePath.toString());
variables.computeIfAbsent(key, k -> createVariableLine(key, onlyExported, resolver));
}

super.collectVariables(variableNames);
super.collectVariables(variables, onlyExported, resolver);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public EnvironmentVariables resolved() {
}

@Override
protected void collectVariables(Map<String, String> variables) {
protected void collectVariables(Map<String, VariableLine> variables, boolean onlyExported, AbstractEnvironmentVariables resolver) {

for (VariableDefinition<?> var : IdeVariables.VARIABLES) {
if (var.isExport() || var.isForceDefaultValue()) {
variables.put(var.getName(), "defaults");
variables.computeIfAbsent(var.getName(), k -> createVariableLine(k, onlyExported, resolver));
}
}
super.collectVariables(variables);
super.collectVariables(variables, onlyExported, resolver);
}

@Override
Expand Down
Loading

0 comments on commit c10b48d

Please sign in to comment.