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

devonfw / IDEasy / 12764387666

14 Jan 2025 09:07AM UTC coverage: 68.077% (+0.5%) from 67.541%
12764387666

Pull #820

github

web-flow
Merge e2ff4216d into 875fbff84
Pull Request #820: #759: upgrade settings commandlet

2689 of 4311 branches covered (62.38%)

Branch coverage included in aggregate %.

6946 of 9842 relevant lines covered (70.58%)

3.1 hits per line

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

79.63
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
   * @return the old variable value.
144
   */
145
  default String set(String name, String value) {
146

147
    throw new UnsupportedOperationException();
×
148
  }
149

150
  /**
151
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to set.
152
   * @param value the new {@link #get(String) value} of the variable to set. May be {@code null} to unset the variable.
153
   * @param export - {@code true} if the variable needs to be exported, {@code false} otherwise.
154
   * @return the old variable value.
155
   */
156
  default String set(String name, String value, boolean export) {
157

158
    throw new UnsupportedOperationException();
×
159
  }
160

161
  /**
162
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
163
   */
164
  default void save() {
165

166
    throw new UnsupportedOperationException("Not yet implemented!");
×
167
  }
168

169
  /**
170
   * @param name the {@link com.devonfw.tools.ide.variable.VariableDefinition#getName() name} of the variable to search for.
171
   * @return the closest {@link EnvironmentVariables} instance that defines the variable with the given {@code name} or {@code null} if the variable is not
172
   *     defined.
173
   */
174
  default EnvironmentVariables findVariable(String name) {
175

176
    String value = getFlat(name);
4✔
177
    if (value != null) {
2✔
178
      return this;
2✔
179
    }
180
    EnvironmentVariables parent = getParent();
3✔
181
    if (parent == null) {
2✔
182
      return null;
2✔
183
    } else {
184
      return parent.findVariable(name);
4✔
185
    }
186
  }
187

188
  /**
189
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
190
   */
191
  List<VariableLine> collectVariables();
192

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

199
  /**
200
   * @param string the {@link String} that potentially contains variables in {@link VariableSyntax#CURLY} ("${«variable«}"). Those will be resolved by this
201
   *     method and replaced with 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
   * @return the given {@link String} with the variables resolved.
205
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
206
   */
207
  String resolve(String string, Object source);
208

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

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

235
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
236
  }
237

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

250
  /**
251
   * @param context the {@link IdeContext}.
252
   * @return the system {@link EnvironmentVariables} building the root of the {@link EnvironmentVariables} hierarchy.
253
   */
254
  static AbstractEnvironmentVariables ofSystem(IdeContext context) {
255

256
    return EnvironmentVariablesSystem.of(context);
3✔
257
  }
258

259
  /**
260
   * @param tool the name of the tool.
261
   * @return the name of the version variable.
262
   */
263
  static String getToolVersionVariable(String tool) {
264

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

268
  /**
269
   * @param tool the name of the tool.
270
   * @return the name of the edition variable.
271
   */
272
  static String getToolEditionVariable(String tool) {
273

274
    return tool.toUpperCase(Locale.ROOT) + "_EDITION";
5✔
275
  }
276

277
  /**
278
   * Removes a property.
279
   *
280
   * @param string name of the property to remove.
281
   */
282
  default void remove(String string) {
283
    throw new UnsupportedOperationException("Not yet implemented!");
×
284
  }
285
}
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

© 2026 Coveralls, Inc