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

devonfw / IDEasy / 9660998058

25 Jun 2024 10:43AM UTC coverage: 60.42% (-0.06%) from 60.475%
9660998058

push

github

web-flow
#171: support for multiple VariableSyntax styles (#407)

1898 of 3450 branches covered (55.01%)

Branch coverage included in aggregate %.

4985 of 7942 relevant lines covered (62.77%)

2.74 hits per line

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

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

3
import com.devonfw.tools.ide.context.IdeContext;
4
import com.devonfw.tools.ide.variable.VariableDefinition;
5
import com.devonfw.tools.ide.variable.VariableSyntax;
6
import com.devonfw.tools.ide.version.VersionIdentifier;
7

8
import java.nio.file.Path;
9
import java.util.Collection;
10
import java.util.Locale;
11

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

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

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

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

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

32
  /**
33
   * @param name the name of the environment variable to get.
34
   * @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default
35
   *        value} of a potential {@link VariableDefinition} shall be ignored, {@code false} to return default instead
36
   *        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
54
   *         variable is defined.
55
   */
56
  default Path getPath(String name) {
57

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

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

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

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

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

94
    String variable = getToolVersionVariable(tool);
3✔
95
    String value = get(variable);
4✔
96
    if (value == null) {
2!
97
      return VersionIdentifier.LATEST;
×
98
    }
99
    return VersionIdentifier.of(value);
3✔
100
  }
101

102
  /**
103
   * @return the {@link EnvironmentVariablesType type} of this {@link EnvironmentVariables}.
104
   */
105
  EnvironmentVariablesType getType();
106

107
  /**
108
   * @param type the {@link #getType() type} of the requested {@link EnvironmentVariables}.
109
   * @return the {@link EnvironmentVariables} with the given {@link #getType() type} from this
110
   *         {@link EnvironmentVariables} along the {@link #getParent() parent} hierarchy or {@code null} if not found.
111
   */
112
  default EnvironmentVariables getByType(EnvironmentVariablesType type) {
113

114
    if (type == getType()) {
4✔
115
      return this;
2✔
116
    }
117
    EnvironmentVariables parent = getParent();
3✔
118
    if (parent == null) {
2!
119
      return null;
×
120
    } else {
121
      return parent.getByType(type);
4✔
122
    }
123
  }
124

125
  /**
126
   * @return the {@link Path} to the underlying properties file or {@code null} if not based on such file (e.g. for EVS
127
   *         or {@link EnvironmentVariablesResolved}).
128
   */
129
  Path getPropertiesFilePath();
130

131
  /**
132
   * @return the source identifier describing this {@link EnvironmentVariables} for debugging.
133
   */
134
  String getSource();
135

136
  /**
137
   * @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the
138
   *         {@link EnvironmentVariablesType#SYSTEM root} {@link EnvironmentVariables} instance.
139
   */
140
  default EnvironmentVariables getParent() {
141

142
    return null;
×
143
  }
144

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

153
    throw new UnsupportedOperationException();
×
154
  }
155

156
  /**
157
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
158
   */
159
  default void save() {
160

161
    throw new UnsupportedOperationException("Not yet implemented!");
×
162
  }
163

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

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

184
  /**
185
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including
186
   *         inheritance.
187
   */
188
  Collection<VariableLine> collectVariables();
189

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

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

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

219
  /**
220
   * The inverse operation of {@link #resolve(String, Object, boolean)}. Please note that the {@link #resolve(String, Object, boolean)
221
   * resolve} operation is not fully bijective. There may be multiple variables holding the same {@link #get(String)
222
   * value} or there may be static text that can be equal to a {@link #get(String) variable value}. This method does its
223
   * 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
229
   *               resolving was not working as expected.
230
   * @return the given {@link String} with {@link #get(String) variable values} replaced with according
231
   * {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
232
   * @see com.devonfw.tools.ide.tool.ide.IdeToolCommandlet
233
   */
234
  default String inverseResolve(String string, Object source) {
235

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

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

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

258
    return EnvironmentVariablesSystem.of(context);
3✔
259
  }
260

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

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

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

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

279
}
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