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

devonfw / IDEasy / 11783967420

11 Nov 2024 06:14PM UTC coverage: 67.267% (+0.07%) from 67.195%
11783967420

push

github

web-flow
#745: fix loading and evaluation of variables (#746)

2457 of 3995 branches covered (61.5%)

Branch coverage included in aggregate %.

6398 of 9169 relevant lines covered (69.78%)

3.08 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 VariableSource} of this {@link EnvironmentVariables}.
123
   */
124
  VariableSource getSource();
125

126
  /**
127
   * @return the parent {@link EnvironmentVariables} to inherit from or {@code null} if this is the {@link EnvironmentVariablesType#SYSTEM root}
128
   *     {@link EnvironmentVariables} instance.
129
   */
130
  default EnvironmentVariables getParent() {
131

132
    return null;
×
133
  }
134

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

143
    throw new UnsupportedOperationException();
×
144
  }
145

146
  /**
147
   * Saves any potential {@link #set(String, String, boolean) changes} of this {@link EnvironmentVariables}.
148
   */
149
  default void save() {
150

151
    throw new UnsupportedOperationException("Not yet implemented!");
×
152
  }
153

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

161
    String value = getFlat(name);
4✔
162
    if (value != null) {
2✔
163
      return this;
2✔
164
    }
165
    EnvironmentVariables parent = getParent();
3✔
166
    if (parent == null) {
2✔
167
      return null;
2✔
168
    } else {
169
      return parent.findVariable(name);
4✔
170
    }
171
  }
172

173
  /**
174
   * @return the {@link Collection} of the {@link VariableLine}s defined by this {@link EnvironmentVariables} including inheritance.
175
   */
176
  List<VariableLine> collectVariables();
177

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

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

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

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

220
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
221
  }
222

223
  /**
224
   * @param string the {@link String} where to find {@link #get(String) variable values} and replace them with according
225
   *     {@link com.devonfw.tools.ide.variable.VariableSyntax} expressions.
226
   * @param source the source where the {@link String} to inverse resolve originates from. Should have a reasonable
227
   *     {@link Object#toString() string representation} that will be used in error or log messages if the inverse resolving was not working as expected.
228
   * @param syntax the explicit {@link VariableSyntax} to use.
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 #inverseResolve(String, Object)
232
   */
233
  String inverseResolve(String string, Object source, VariableSyntax syntax);
234

235
  /**
236
   * @param context the {@link IdeContext}.
237
   * @return the system {@link EnvironmentVariables} building the root of the {@link EnvironmentVariables} hierarchy.
238
   */
239
  static AbstractEnvironmentVariables ofSystem(IdeContext context) {
240

241
    return EnvironmentVariablesSystem.of(context);
3✔
242
  }
243

244
  /**
245
   * @param tool the name of the tool.
246
   * @return the name of the version variable.
247
   */
248
  static String getToolVersionVariable(String tool) {
249

250
    return tool.toUpperCase(Locale.ROOT) + "_VERSION";
5✔
251
  }
252

253
  /**
254
   * @param tool the name of the tool.
255
   * @return the name of the edition variable.
256
   */
257
  static String getToolEditionVariable(String tool) {
258

259
    return tool.toUpperCase(Locale.ROOT) + "_EDITION";
5✔
260
  }
261

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