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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 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 java.nio.file.Path;
4
import java.util.Collection;
5
import java.util.Locale;
6

7
import com.devonfw.tools.ide.context.IdeContext;
8
import com.devonfw.tools.ide.variable.VariableDefinition;
9
import com.devonfw.tools.ide.variable.VariableSyntax;
10
import com.devonfw.tools.ide.version.VersionIdentifier;
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 value} of a potential
35
   * {@link VariableDefinition} shall be ignored, {@code false} to return default instead of {@code null}.
36
   * @return the value of the variable with the given {@code name}. Will be {@code null} if no such variable is defined.
37
   */
38
  default String get(String name, boolean ignoreDefaultValue) {
39

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

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

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

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

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

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

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

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

99
  /**
100
   * @return the {@link EnvironmentVariablesType type} of this {@link EnvironmentVariables}.
101
   */
102
  EnvironmentVariablesType getType();
103

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

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

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

128
  /**
129
   * @return the source identifier describing this {@link EnvironmentVariables} for debugging.
130
   */
131
  String getSource();
132

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

139
    return null;
×
140
  }
141

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

150
    throw new UnsupportedOperationException();
×
151
  }
152

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

158
    throw new UnsupportedOperationException("Not yet implemented!");
×
159
  }
160

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

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

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

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

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

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

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

227
    return inverseResolve(string, source, VariableSyntax.SQUARE);
×
228
  }
229

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

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

248
    return EnvironmentVariablesSystem.of(context);
3✔
249
  }
250

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

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

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

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

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