• 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

91.67
cli/src/main/java/com/devonfw/tools/ide/environment/VariableLine.java
1
package com.devonfw.tools.ide.environment;
2

3
import java.util.ArrayList;
4
import java.util.List;
5

6
import com.devonfw.tools.ide.log.IdeLogger;
7

8
/**
9
 * Container that represents a line from a properties (ide.properties) file. We do not use {@link java.util.Properties} as we need support for exported
10
 * variables, lists/arrays, and saving changes without loosing comments, etc.
11
 */
12
public abstract class VariableLine {
3✔
13

14
  /**
15
   * @return {@code true} if the variable is exported (e.g. "export MAVEN_OPTS=-Xmx20248m"), {@code false} otherwise.
16
   */
17
  public boolean isExport() {
18

19
    return false;
2✔
20
  }
21

22
  /**
23
   * @return the name of the variable. Will be {@code null} if not a regular variable line (e.g. a comment or empty line).
24
   */
25
  public String getName() {
26

27
    return null;
2✔
28
  }
29

30
  /**
31
   * @return the value of the variable. Will be {@code null} if not a regular variable line (e.g. a comment or empty line).
32
   */
33
  public String getValue() {
34

35
    return null;
2✔
36
  }
37

38
  /**
39
   * @return the comment line (including the '#' character). Will be {@code null} if not a comment line.
40
   */
41
  public String getComment() {
42

43
    return null;
2✔
44
  }
45

46
  /**
47
   * @return the {@link VariableSource} of the variable.
48
   */
49
  public VariableSource getSource() {
50

51
    return null;
×
52
  }
53

54
  /**
55
   * @param newName the new variable {@link #getName() name}.
56
   * @return the new {@link VariableLine} with the modified {@link #getName() name}.
57
   */
58
  public VariableLine withName(String newName) {
59

60
    throw new UnsupportedOperationException();
×
61
  }
62

63
  /**
64
   * @param newValue the new variable {@link #getValue() value}.
65
   * @return the new {@link VariableLine} with the modified {@link #getValue() value}.
66
   */
67
  public VariableLine withValue(String newValue) {
68

69
    throw new UnsupportedOperationException();
×
70
  }
71

72
  /**
73
   * @param newExport the new {@link #isExport() export} flag.
74
   * @return the new {@link VariableLine} with the modified {@link #isExport() export} flag.
75
   */
76
  public VariableLine withExport(boolean newExport) {
77

78
    throw new UnsupportedOperationException();
×
79
  }
80

81
  static final class Variable extends VariableLine {
82

83
    private boolean export;
84

85
    private final String name;
86

87
    private final String value;
88

89
    private final String line;
90

91
    private final VariableSource source;
92

93
    Variable(boolean export, String name, String value) {
94

95
      this(export, name, value, null, null);
7✔
96
    }
1✔
97

98
    private Variable(boolean export, String name, String value, String line, VariableSource source) {
99

100
      super();
2✔
101
      this.export = export;
3✔
102
      this.name = name;
3✔
103
      this.value = value;
3✔
104
      if (line == null) {
2✔
105
        StringBuilder sb = new StringBuilder();
4✔
106
        if (export) {
2✔
107
          sb.append("export ");
4✔
108
        }
109
        sb.append(this.name);
5✔
110
        sb.append('=');
4✔
111
        sb.append(this.value);
5✔
112
        this.line = sb.toString();
4✔
113
      } else {
1✔
114
        this.line = line;
3✔
115
      }
116
      this.source = source;
3✔
117
    }
1✔
118

119
    @Override
120
    public boolean isExport() {
121

122
      return this.export;
3✔
123
    }
124

125
    @Override
126
    public String getName() {
127

128
      return this.name;
3✔
129
    }
130

131
    @Override
132
    public String getValue() {
133

134
      return this.value;
3✔
135
    }
136

137
    @Override
138
    public VariableSource getSource() {
139
      return source;
3✔
140
    }
141

142
    @Override
143
    public VariableLine withName(String newName) {
144

145
      if (newName.equals(this.name)) {
5!
146
        return this;
×
147
      }
148
      return new Variable(this.export, newName, this.value);
9✔
149
    }
150

151
    @Override
152
    public VariableLine withValue(String newValue) {
153

154
      if (newValue.equals(this.value)) {
5✔
155
        return this;
2✔
156
      }
157
      return new Variable(this.export, this.name, newValue);
9✔
158
    }
159

160
    @Override
161
    public VariableLine withExport(boolean newExport) {
162

163
      if (newExport == this.export) {
4!
164
        return this;
×
165
      }
166
      return new Variable(newExport, this.name, this.value);
9✔
167
    }
168

169
    @Override
170
    public String toString() {
171

172
      return this.line;
3✔
173
    }
174

175
  }
176

177
  static final class Comment extends VariableLine {
178

179
    private final String line;
180

181
    private Comment(String line) {
182

183
      super();
2✔
184
      this.line = line;
3✔
185
    }
1✔
186

187
    @Override
188
    public String getComment() {
189

190
      return this.line;
3✔
191
    }
192

193
    @Override
194
    public String toString() {
195

196
      return this.line;
3✔
197
    }
198

199
  }
200

201
  static final class Garbage extends VariableLine {
202

203
    private final String line;
204

205
    Garbage(String line) {
206

207
      super();
2✔
208
      this.line = line;
3✔
209
    }
1✔
210

211
    @Override
212
    public String toString() {
213

214
      return this.line;
3✔
215
    }
216

217
  }
218

219
  static final class Empty extends VariableLine {
220

221
    private final String line;
222

223
    Empty(String line) {
224

225
      super();
2✔
226
      this.line = line;
3✔
227
    }
1✔
228

229
    @Override
230
    public String toString() {
231

232
      return this.line;
3✔
233
    }
234

235
  }
236

237
  /**
238
   * Parses a {@link VariableLine} from {@link String}.
239
   *
240
   * @param line the {@link VariableLine} as {@link String} to parse.
241
   * @param logger the {@link IdeLogger}.
242
   * @param source the source where the given {@link String} to parse is from (e.g. the file path).
243
   * @return the parsed {@link VariableLine}.
244
   */
245
  public static VariableLine of(String line, IdeLogger logger, VariableSource source) {
246

247
    int len = line.length();
3✔
248
    int start = 0;
2✔
249
    while (start < len) {
3✔
250
      char c = line.charAt(start);
4✔
251
      if (c == ' ') {
3✔
252
        start++; // ignore leading spaces
2✔
253
      } else if (c == '#') {
3✔
254
        return new Comment(line);
5✔
255
      } else {
256
        break;
257
      }
258
    }
1✔
259
    if (start >= len) {
3✔
260
      return new Empty(line);
5✔
261
    }
262
    boolean export = false;
2✔
263
    int end = start;
2✔
264
    int space = -1;
2✔
265
    while (end < len) {
3✔
266
      char c = line.charAt(end);
4✔
267
      if (c == ' ') {
3✔
268
        if (space == -1) {
3✔
269
          space = end;
3✔
270
        }
271
      } else if (c == '=') {
3✔
272
        // .0123456789
273
        // "export ...="
274
        String name;
275
        if ((space == start + 6) && (end > start + 7) && line.substring(start, space).equals("export")) {
17!
276
          name = line.substring(space + 1, end).trim();
8✔
277
          if (name.isEmpty()) {
3✔
278
            name = line.substring(start, end).trim();
7✔
279
          } else {
280
            export = true;
3✔
281
          }
282
        } else {
283
          name = line.substring(start, end).trim();
6✔
284
        }
285
        String value = line.substring(end + 1).trim();
7✔
286
        if (value.startsWith("\"") && value.endsWith("\"")) {
8!
287
          value = value.substring(1, value.length() - 1);
8✔
288
        }
289
        return new Variable(export, name, value, line, source);
9✔
290
      }
291
      end++;
1✔
292
    }
1✔
293
    logger.warning("Ignoring corrupted line '{}' in {}", line, source);
13✔
294
    return new Garbage(line);
5✔
295
  }
296

297
  /**
298
   * @param export the {@link #isExport() export flag}.
299
   * @param name the {@link #getName() name}.
300
   * @param value the {@link #getValue() value}.
301
   * @return the {@link VariableLine} for the given values.
302
   */
303
  public static VariableLine of(boolean export, String name, String value) {
304

305
    return new Variable(export, name, value);
7✔
306
  }
307

308
  /**
309
   * @param export the {@link #isExport() export flag}.
310
   * @param name the {@link #getName() name}.
311
   * @param value the {@link #getValue() value}.
312
   * @param source the {@link #getSource() source} of the variable.
313
   * @return the {@link VariableLine} for the given values.
314
   */
315
  public static VariableLine of(boolean export, String name, String value, VariableSource source) {
316

317
    return new Variable(export, name, value, null, source);
9✔
318
  }
319

320
  /**
321
   * Returns a list of String Variables.
322
   *
323
   * @param value String to parse
324
   * @return List of variables.
325
   */
326
  public static List<String> parseArray(String value) {
327
    String csv = value;
2✔
328
    String separator = ",";
2✔
329
    // TODO: refactor with isBashArray method from VariableDefinitionStringList
330
    if (value.startsWith("(") && value.endsWith(")")) {
8!
331
      csv = value.substring(1, value.length() - 1);
8✔
332
      separator = " ";
2✔
333
    }
334
    String[] items = csv.split(separator);
4✔
335
    List<String> list = new ArrayList<>(items.length);
6✔
336
    for (String item : items) {
16✔
337
      list.add(item.trim());
5✔
338
    }
339
    return list;
2✔
340
  }
341

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