• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 12084849850

29 Nov 2024 12:34PM UTC coverage: 67.031% (-0.4%) from 67.412%
12084849850

push

github

web-flow
#758: improve status commandlet (#816)

2500 of 4078 branches covered (61.3%)

Branch coverage included in aggregate %.

6515 of 9371 relevant lines covered (69.52%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.69
cli/src/main/java/com/devonfw/tools/ide/environment/EnvironmentVariables.java
1
package com.devonfw.tools.ide.environment;
2

3
import java.nio.file.Path;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.Locale;
7

8
import com.devonfw.tools.ide.context.IdeContext;
9
import com.devonfw.tools.ide.variable.VariableDefinition;
10
import com.devonfw.tools.ide.variable.VariableSyntax;
11
import com.devonfw.tools.ide.version.VersionIdentifier;
12

13
/**
14
 * Interface for the environment with the variables.
15
 */
16
public interface EnvironmentVariables {
17

18
  /** Filename of the default variable configuration file. {@value} */
19
  String DEFAULT_PROPERTIES = "ide.properties";
20

21
  /** Filename of the legacy variable configuration file. {@value} */
22
  String LEGACY_PROPERTIES = "devon.properties";
23

24
  /**
25
   * @param name the name of the environment variable to get.
26
   * @return the value of the variable with the given {@code name}. Will be {@code null} if no such variable is defined.
27
   */
28
  default String get(String name) {
29

30
    return get(name, false);
5✔
31
  }
32

33
  /**
34
   * @param name the name of the environment variable to get.
35
   * @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default value} of a potential
36
   *     {@link VariableDefinition} shall be ignored, {@code false} to return default instead of {@code null}.
37
   * @return the value of the variable with the given {@code name}. Will be {@code null} if no such variable is defined.
38
   */
39
  default String get(String name, boolean ignoreDefaultValue) {
40

41
    String value = getFlat(name);
4✔
42
    if (value == null) {
2✔
43
      EnvironmentVariables parent = getParent();
3✔
44
      if (parent != null) {
2✔
45
        value = parent.get(name);
4✔
46
      }
47
    }
48
    return value;
2✔
49
  }
50

51
  /**
52
   * @param name the name of the environment variable to get.
53
   * @return the value of the variable with the given {@code name} as {@link Path}. Will be {@code null} if no such variable is defined.
54
   */
55
  default Path getPath(String name) {
56

57
    String value = get(name);
4✔
58
    if (value == null) {
2!
59
      return null;
×
60
    }
61
    return Path.of(value);
5✔
62
  }
63

64
  /**
65
   * @param name the name of the environment variable to get.
66
   * @return the value of the variable with the given {@code name} without {@link #getParent() inheritance from parent}. Will be {@code null} if no such
67
   *     variable is defined.
68
   */
69
  String getFlat(String name);
70

71
  /**
72
   * @param tool the name of the tool (e.g. "java").
73
   * @return the edition of the tool to use.
74
   */
75
  default String getToolEdition(String tool) {
76

77
    String variable = tool.toUpperCase(Locale.ROOT) + "_EDITION";
5✔
78
    String value = get(variable);
4✔
79
    if (value == null) {
2!
80
      value = tool;
2✔
81
    }
82
    return value;
2✔
83
  }
84

85
  /**
86
   * @param tool the name of the tool (e.g. "java").
87
   * @return the {@link VersionIdentifier} with the version of the tool to use. May also be a {@link VersionIdentifier#isPattern() version pattern}. Will be
88
   *     {@link VersionIdentifier#LATEST} if undefined.
89
   */
90
  VersionIdentifier getToolVersion(String tool);
91

92
  /**
93
   * @return the {@link EnvironmentVariablesType type} of this {@link EnvironmentVariables}.
94
   */
95
  EnvironmentVariablesType getType();
96

97
  /**
98
   * @param type the {@link #getType() type} of the requested {@link EnvironmentVariables}.
99
   * @return the {@link EnvironmentVariables} with the given {@link #getType() type} from this {@link EnvironmentVariables} along the
100
   *     {@link #getParent() parent} hierarchy or {@code null} if not found.
101
   */
102
  default EnvironmentVariables getByType(EnvironmentVariablesType type) {
103

104
    if (type == getType()) {
4✔
105
      return this;
2✔
106
    }
107
    EnvironmentVariables parent = getParent();
3✔
108
    if (parent == null) {
2!
109
      return null;
×
110
    } else {
111
      return parent.getByType(type);
4✔
112
    }
113
  }
114

115
  /**
116
   * @return the {@link Path} to the underlying properties file or {@code null} if not based on such file (e.g. for EVS or
117
   *     {@link EnvironmentVariablesResolved}).
118
   */
119
  Path getPropertiesFilePath();
120

121
  /**
122
   * @return the {@link Path} to the {@link #LEGACY_PROPERTIES} if they exist for this {@link EnvironmentVariables} or {@code null} otherwise (does not exist).
123
   */
124
  Path getLegacyPropertiesFilePath();
125

126
  /**
127
   * @return the {@link VariableSource} of this {@link EnvironmentVariables}.
128
   */
129
  VariableSource getSource();
130

131
  /**
132
   * @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the {@link EnvironmentVariablesType#SYSTEM root}
133
   *     {@link EnvironmentVariables} instance.
134
   */
135
  default EnvironmentVariables getParent() {
136

137
    return null;
×
138
  }
139

140
  /**
141
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
142
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
143
   * @param export - {@code true} if the variable needs to be exported, {@code false} otherwise.
144
   * @return the old variable value.
145
   */
146
  default String set(String name, String value, boolean export) {
147

148
    throw new UnsupportedOperationException();
×
149
  }
150

151
  /**
152
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
153
   */
154
  default void save() {
155

156
    throw new UnsupportedOperationException("Not yet implemented!");
×
157
  }
158

159
  /**
160
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to search for.
161
   * @return the closest {@link EnvironmentVariables} instance that defines the variable with the given {@code name} or {@code null} if the variable is not
162
   *     defined.
163
   */
164
  default EnvironmentVariables findVariable(String name) {
165

166
    String value = getFlat(name);
4✔
167
    if (value != null) {
2✔
168
      return this;
2✔
169
    }
170
    EnvironmentVariables parent = getParent();
3✔
171
    if (parent == null) {
2✔
172
      return null;
2✔
173
    } else {
174
      return parent.findVariable(name);
4✔
175
    }
176
  }
177

178
  /**
179
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
180
   */
181
  List<VariableLine> collectVariables();
182

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

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

199
  /**
200
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax}. Those will be resolved by this method and replaced with
201
   *     their {@link #get(String) value}.
202
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
203
   *     that will be used in error or log messages if a variable could not be resolved.
204
   * @param legacySupport - {@code true} for legacy support with {@link VariableSyntax#CURLY} as fallback, {@code false} otherwise.
205
   * @return the given {@link String} with the variables resolved.
206
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
207
   */
208
  String resolve(String string, Object source, boolean legacySupport);
209

210
  /**
211
   * The inverse operation of {@link #resolve(String, Object, boolean)}. Please note that the {@link #resolve(String, Object, boolean) resolve} operation is not
212
   * 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
213
   * {@link #get(String) variable value}. This method does its best to implement the inverse resolution based on some heuristics.
214
   *
215
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
216
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
217
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
218
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
219
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according {@link com.devonfw.tools.ide.variable.VariableSyntax}
220
   *     expressions.
221
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
222
   */
223
  default String inverseResolve(String string, Object source) {
224

225
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
226
  }
227

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

240
  /**
241
   * @param context the {@link IdeContext}.
242
   * @return the system {@link EnvironmentVariables} building the root of the {@link EnvironmentVariables} hierarchy.
243
   */
244
  static AbstractEnvironmentVariables ofSystem(IdeContext context) {
245

246
    return EnvironmentVariablesSystem.of(context);
3✔
247
  }
248

249
  /**
250
   * @param tool the name of the tool.
251
   * @return the name of the version variable.
252
   */
253
  static String getToolVersionVariable(String tool) {
254

255
    return tool.toUpperCase(Locale.ROOT) + "_VERSION";
5✔
256
  }
257

258
  /**
259
   * @param tool the name of the tool.
260
   * @return the name of the edition variable.
261
   */
262
  static String getToolEditionVariable(String tool) {
263

264
    return tool.toUpperCase(Locale.ROOT) + "_EDITION";
5✔
265
  }
266

267
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc