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

devonfw / IDEasy / 22315552991

23 Feb 2026 04:40PM UTC coverage: 70.563% (+0.09%) from 70.474%
22315552991

Pull #1714

github

web-flow
Merge 7d88da975 into 379acdc9d
Pull Request #1714: #404: #1713: advanced logging

4081 of 6382 branches covered (63.95%)

Branch coverage included in aggregate %.

10644 of 14486 relevant lines covered (73.48%)

3.09 hits per line

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

66.21
cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java
1
package com.devonfw.tools.ide.context;
2

3
import static com.devonfw.tools.ide.variable.IdeVariables.IDE_MIN_VERSION;
4

5
import java.io.ByteArrayInputStream;
6
import java.io.ByteArrayOutputStream;
7
import java.io.IOException;
8
import java.nio.file.Files;
9
import java.nio.file.Path;
10
import java.time.LocalDateTime;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Locale;
16
import java.util.Map;
17
import java.util.Map.Entry;
18
import java.util.Objects;
19
import java.util.Properties;
20
import java.util.function.Predicate;
21
import java.util.logging.LogManager;
22

23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
import com.devonfw.tools.ide.cli.CliAbortException;
27
import com.devonfw.tools.ide.cli.CliArgument;
28
import com.devonfw.tools.ide.cli.CliArguments;
29
import com.devonfw.tools.ide.cli.CliException;
30
import com.devonfw.tools.ide.commandlet.Commandlet;
31
import com.devonfw.tools.ide.commandlet.CommandletManager;
32
import com.devonfw.tools.ide.commandlet.CommandletManagerImpl;
33
import com.devonfw.tools.ide.commandlet.ContextCommandlet;
34
import com.devonfw.tools.ide.commandlet.EnvironmentCommandlet;
35
import com.devonfw.tools.ide.commandlet.HelpCommandlet;
36
import com.devonfw.tools.ide.common.SystemPath;
37
import com.devonfw.tools.ide.completion.CompletionCandidate;
38
import com.devonfw.tools.ide.completion.CompletionCandidateCollector;
39
import com.devonfw.tools.ide.completion.CompletionCandidateCollectorDefault;
40
import com.devonfw.tools.ide.environment.AbstractEnvironmentVariables;
41
import com.devonfw.tools.ide.environment.EnvironmentVariables;
42
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
43
import com.devonfw.tools.ide.environment.IdeSystem;
44
import com.devonfw.tools.ide.environment.IdeSystemImpl;
45
import com.devonfw.tools.ide.git.GitContext;
46
import com.devonfw.tools.ide.git.GitContextImpl;
47
import com.devonfw.tools.ide.git.GitUrl;
48
import com.devonfw.tools.ide.io.FileAccess;
49
import com.devonfw.tools.ide.io.FileAccessImpl;
50
import com.devonfw.tools.ide.log.IdeLogArgFormatter;
51
import com.devonfw.tools.ide.log.IdeLogLevel;
52
import com.devonfw.tools.ide.log.IdeLogListener;
53
import com.devonfw.tools.ide.merge.DirectoryMerger;
54
import com.devonfw.tools.ide.migration.IdeMigrator;
55
import com.devonfw.tools.ide.network.NetworkStatus;
56
import com.devonfw.tools.ide.network.NetworkStatusImpl;
57
import com.devonfw.tools.ide.os.SystemInfo;
58
import com.devonfw.tools.ide.os.SystemInfoImpl;
59
import com.devonfw.tools.ide.os.WindowsHelper;
60
import com.devonfw.tools.ide.os.WindowsHelperImpl;
61
import com.devonfw.tools.ide.os.WindowsPathSyntax;
62
import com.devonfw.tools.ide.process.ProcessContext;
63
import com.devonfw.tools.ide.process.ProcessContextImpl;
64
import com.devonfw.tools.ide.process.ProcessResult;
65
import com.devonfw.tools.ide.property.Property;
66
import com.devonfw.tools.ide.step.Step;
67
import com.devonfw.tools.ide.step.StepImpl;
68
import com.devonfw.tools.ide.tool.custom.CustomToolRepository;
69
import com.devonfw.tools.ide.tool.custom.CustomToolRepositoryImpl;
70
import com.devonfw.tools.ide.tool.mvn.MvnRepository;
71
import com.devonfw.tools.ide.tool.npm.NpmRepository;
72
import com.devonfw.tools.ide.tool.pip.PipRepository;
73
import com.devonfw.tools.ide.tool.repository.DefaultToolRepository;
74
import com.devonfw.tools.ide.tool.repository.ToolRepository;
75
import com.devonfw.tools.ide.url.model.UrlMetadata;
76
import com.devonfw.tools.ide.util.DateTimeUtil;
77
import com.devonfw.tools.ide.util.PrivacyUtil;
78
import com.devonfw.tools.ide.validation.ValidationResult;
79
import com.devonfw.tools.ide.validation.ValidationResultValid;
80
import com.devonfw.tools.ide.validation.ValidationState;
81
import com.devonfw.tools.ide.variable.IdeVariables;
82
import com.devonfw.tools.ide.version.IdeVersion;
83
import com.devonfw.tools.ide.version.VersionIdentifier;
84

85
/**
86
 * Abstract base implementation of {@link IdeContext}.
87
 */
88
public abstract class AbstractIdeContext implements IdeContext, IdeLogArgFormatter {
89

90
  static final Logger LOG = LoggerFactory.getLogger(AbstractIdeContext.class);
3✔
91

92
  /** The default shell bash (Bourne Again SHell). */
93
  public static final String BASH = "bash";
94

95
  private static final GitUrl IDE_URLS_GIT = new GitUrl("https://github.com/devonfw/ide-urls.git", null);
7✔
96

97
  private static final String LICENSE_URL = "https://github.com/devonfw/IDEasy/blob/main/documentation/LICENSE.adoc";
98

99
  private static final String DEFAULT_WINDOWS_GIT_PATH = "C:\\Program Files\\Git\\bin\\bash.exe";
100

101
  private static final String OPTION_DETAILS_START = "([";
102

103
  private final IdeStartContextImpl startContext;
104

105
  private Path ideHome;
106

107
  private final Path ideRoot;
108

109
  private Path confPath;
110

111
  protected Path settingsPath;
112

113
  private Path settingsCommitIdPath;
114

115
  protected Path pluginsPath;
116

117
  private Path workspacePath;
118

119
  private String workspaceName;
120

121
  private Path cwd;
122

123
  private Path downloadPath;
124

125
  private Path userHome;
126

127
  private Path userHomeIde;
128

129
  private SystemPath path;
130

131
  private WindowsPathSyntax pathSyntax;
132

133
  private final SystemInfo systemInfo;
134

135
  private EnvironmentVariables variables;
136

137
  private final FileAccess fileAccess;
138

139
  protected CommandletManager commandletManager;
140

141
  protected ToolRepository defaultToolRepository;
142

143
  private CustomToolRepository customToolRepository;
144

145
  private MvnRepository mvnRepository;
146

147
  private NpmRepository npmRepository;
148

149
  private PipRepository pipRepository;
150

151
  private DirectoryMerger workspaceMerger;
152

153
  protected UrlMetadata urlMetadata;
154

155
  protected Path defaultExecutionDirectory;
156

157
  private StepImpl currentStep;
158

159
  private NetworkStatus networkStatus;
160

161
  protected IdeSystem system;
162

163
  private WindowsHelper windowsHelper;
164

165
  private final Map<String, String> privacyMap;
166

167
  private Path bash;
168

169
  private boolean julConfigured;
170

171
  private Path logfile;
172

173
  /**
174
   * The constructor.
175
   *
176
   * @param startContext the {@link IdeStartContextImpl}.
177
   * @param workingDirectory the optional {@link Path} to current working directory.
178
   */
179
  public AbstractIdeContext(IdeStartContextImpl startContext, Path workingDirectory) {
180

181
    super();
2✔
182

183
    this.startContext = startContext;
3✔
184
    this.startContext.setArgFormatter(this);
4✔
185
    this.privacyMap = new HashMap<>();
5✔
186
    this.systemInfo = SystemInfoImpl.INSTANCE;
3✔
187
    this.commandletManager = new CommandletManagerImpl(this);
6✔
188
    this.fileAccess = new FileAccessImpl(this);
6✔
189
    String userHomeProperty = getSystem().getProperty("user.home");
5✔
190
    if (userHomeProperty != null) {
2!
191
      this.userHome = Path.of(userHomeProperty);
×
192
    }
193
    if (workingDirectory == null) {
2✔
194
      workingDirectory = Path.of(System.getProperty("user.dir"));
6✔
195
    }
196
    workingDirectory = workingDirectory.toAbsolutePath();
3✔
197
    if (Files.isDirectory(workingDirectory)) {
5✔
198
      workingDirectory = this.fileAccess.toCanonicalPath(workingDirectory);
6✔
199
    } else {
200
      LOG.warn("Current working directory does not exist: {}", workingDirectory);
4✔
201
    }
202
    this.cwd = workingDirectory;
3✔
203
    // detect IDE_HOME and WORKSPACE
204
    String workspace = null;
2✔
205
    Path ideHomeDir = null;
2✔
206
    IdeHomeAndWorkspace ideHomeAndWorkspace = findIdeHome(workingDirectory);
4✔
207
    if (ideHomeAndWorkspace != null) {
2!
208
      ideHomeDir = ideHomeAndWorkspace.home();
3✔
209
      workspace = ideHomeAndWorkspace.workspace();
3✔
210
    }
211

212
    // detection completed, initializing variables
213
    this.ideRoot = findIdeRoot(ideHomeDir);
5✔
214

215
    setCwd(workingDirectory, workspace, ideHomeDir);
5✔
216

217
    if (this.ideRoot != null) {
3✔
218
      Path tempDownloadPath = getTempDownloadPath();
3✔
219
      if (Files.isDirectory(tempDownloadPath)) {
6✔
220
        // TODO delete all files older than 1 day here...
221
      } else {
222
        this.fileAccess.mkdirs(tempDownloadPath);
4✔
223
      }
224
    }
225
    this.defaultToolRepository = new DefaultToolRepository(this);
6✔
226
  }
1✔
227

228
  /**
229
   * Searches for the IDE home directory by traversing up the directory tree from the given working directory. This method can be overridden in test contexts to
230
   * add additional validation or boundary checks.
231
   *
232
   * @param workingDirectory the starting directory for the search.
233
   * @return an instance of {@link IdeHomeAndWorkspace} where the IDE_HOME was found or {@code null} if not found.
234
   */
235
  protected IdeHomeAndWorkspace findIdeHome(Path workingDirectory) {
236

237
    Path currentDir = workingDirectory;
2✔
238
    String name1 = "";
2✔
239
    String name2 = "";
2✔
240
    String workspace = WORKSPACE_MAIN;
2✔
241
    Path ideRootPath = getIdeRootPathFromEnv(false);
4✔
242

243
    while (currentDir != null) {
2✔
244
      LOG.trace("Looking for IDE_HOME in {}", currentDir);
4✔
245
      if (isIdeHome(currentDir)) {
4✔
246
        if (FOLDER_WORKSPACES.equals(name1) && !name2.isEmpty()) {
7✔
247
          workspace = name2;
3✔
248
        }
249
        break;
250
      }
251
      name2 = name1;
2✔
252
      int nameCount = currentDir.getNameCount();
3✔
253
      if (nameCount >= 1) {
3✔
254
        name1 = currentDir.getName(nameCount - 1).toString();
7✔
255
      }
256
      currentDir = currentDir.getParent();
3✔
257
      if ((ideRootPath != null) && (ideRootPath.equals(currentDir))) {
2!
258
        // prevent that during tests we traverse to the real IDE project of IDEasy developer
259
        currentDir = null;
×
260
      }
261
    }
1✔
262

263
    return new IdeHomeAndWorkspace(currentDir, workspace);
6✔
264
  }
265

266
  /**
267
   * @return a new {@link MvnRepository}
268
   */
269
  protected MvnRepository createMvnRepository() {
270
    return new MvnRepository(this);
5✔
271
  }
272

273
  /**
274
   * @return a new {@link NpmRepository}
275
   */
276
  protected NpmRepository createNpmRepository() {
277
    return new NpmRepository(this);
×
278
  }
279

280
  /**
281
   * @return a new {@link PipRepository}
282
   */
283
  protected PipRepository createPipRepository() {
284
    return new PipRepository(this);
×
285
  }
286

287
  private Path findIdeRoot(Path ideHomePath) {
288

289
    Path ideRootPath = null;
2✔
290
    if (ideHomePath != null) {
2✔
291
      Path ideRootPathFromEnv = getIdeRootPathFromEnv(true);
4✔
292
      ideRootPath = ideHomePath.getParent();
3✔
293
      if ((ideRootPathFromEnv != null) && !ideRootPath.toString().equals(ideRootPathFromEnv.toString())) {
2!
294
        LOG.warn(
×
295
            "Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.\n"
296
                + "Please check your 'user.dir' or working directory setting and make sure that it matches your IDE_ROOT variable.",
297
            ideRootPathFromEnv,
298
            ideHomePath.getFileName(), ideRootPath);
×
299
      }
300
    } else if (!isTest()) {
4!
301
      ideRootPath = getIdeRootPathFromEnv(true);
×
302
    }
303
    return ideRootPath;
2✔
304
  }
305

306
  /**
307
   * @return the {@link #getIdeRoot() IDE_ROOT} from the system environment.
308
   */
309
  protected Path getIdeRootPathFromEnv(boolean withSanityCheck) {
310

311
    String root = getSystem().getEnv(IdeVariables.IDE_ROOT.getName());
×
312
    if (root != null) {
×
313
      Path rootPath = Path.of(root);
×
314
      if (Files.isDirectory(rootPath)) {
×
315
        Path absoluteRootPath = getFileAccess().toCanonicalPath(rootPath);
×
316
        if (withSanityCheck) {
×
317
          int nameCount = rootPath.getNameCount();
×
318
          int absoluteNameCount = absoluteRootPath.getNameCount();
×
319
          int delta = absoluteNameCount - nameCount;
×
320
          if (delta >= 0) {
×
321
            for (int nameIndex = 0; nameIndex < nameCount; nameIndex++) {
×
322
              String rootName = rootPath.getName(nameIndex).toString();
×
323
              String absoluteRootName = absoluteRootPath.getName(nameIndex + delta).toString();
×
324
              if (!rootName.equals(absoluteRootName)) {
×
325
                LOG.warn("IDE_ROOT is set to {} but was expanded to absolute path {} and does not match for segment {} and {} - fix your IDEasy installation!",
×
326
                    rootPath, absoluteRootPath, rootName, absoluteRootName);
327
                break;
×
328
              }
329
            }
330
          } else {
331
            LOG.warn("IDE_ROOT is set to {} but was expanded to a shorter absolute path {}", rootPath,
×
332
                absoluteRootPath);
333
          }
334
        }
335
        return absoluteRootPath;
×
336
      } else if (withSanityCheck) {
×
337
        LOG.warn("IDE_ROOT is set to {} that is not an existing directory - fix your IDEasy installation!", rootPath);
×
338
      }
339
    }
340
    return null;
×
341
  }
342

343
  @Override
344
  public void setCwd(Path userDir, String workspace, Path ideHome) {
345

346
    this.cwd = userDir;
3✔
347
    this.workspaceName = workspace;
3✔
348
    this.ideHome = ideHome;
3✔
349
    if (ideHome == null) {
2✔
350
      this.workspacePath = null;
3✔
351
      this.confPath = null;
3✔
352
      this.settingsPath = null;
3✔
353
      this.pluginsPath = null;
4✔
354
    } else {
355
      this.workspacePath = this.ideHome.resolve(FOLDER_WORKSPACES).resolve(this.workspaceName);
9✔
356
      this.confPath = this.ideHome.resolve(FOLDER_CONF);
6✔
357
      this.settingsPath = this.ideHome.resolve(FOLDER_SETTINGS);
6✔
358
      this.settingsCommitIdPath = this.ideHome.resolve(IdeContext.SETTINGS_COMMIT_ID);
6✔
359
      this.pluginsPath = this.ideHome.resolve(FOLDER_PLUGINS);
6✔
360
    }
361
    if (isTest()) {
3!
362
      // only for testing...
363
      if (this.ideHome == null) {
3✔
364
        this.userHome = Path.of("/non-existing-user-home-for-testing");
7✔
365
      } else {
366
        this.userHome = this.ideHome.resolve("home");
6✔
367
      }
368
    }
369
    this.userHomeIde = this.userHome.resolve(FOLDER_DOT_IDE);
6✔
370
    this.downloadPath = this.userHome.resolve("Downloads/ide");
6✔
371
    resetPrivacyMap();
2✔
372
    this.path = computeSystemPath();
4✔
373
  }
1✔
374

375
  private String getMessageIdeHomeFound() {
376

377
    String wks = this.workspaceName;
3✔
378
    if (isPrivacyMode() && !WORKSPACE_MAIN.equals(wks)) {
3!
379
      wks = "*".repeat(wks.length());
×
380
    }
381
    return "IDE environment variables have been set for " + formatArgument(this.ideHome) + " in workspace " + wks;
7✔
382
  }
383

384
  private String getMessageNotInsideIdeProject() {
385

386
    return "You are not inside an IDE project: " + formatArgument(this.cwd);
6✔
387
  }
388

389
  private String getMessageIdeRootNotFound() {
390

391
    String root = getSystem().getEnv("IDE_ROOT");
5✔
392
    if (root == null) {
2!
393
      return "The environment variable IDE_ROOT is undefined. Please reinstall IDEasy or manually repair IDE_ROOT variable.";
2✔
394
    } else {
395
      return "The environment variable IDE_ROOT is pointing to an invalid path " + formatArgument(root)
×
396
          + ". Please reinstall IDEasy or manually repair IDE_ROOT variable.";
397
    }
398
  }
399

400
  /**
401
   * @return {@code true} if this is a test context for JUnits, {@code false} otherwise.
402
   */
403
  public boolean isTest() {
404

405
    return false;
×
406
  }
407

408
  protected SystemPath computeSystemPath() {
409

410
    return new SystemPath(this);
×
411
  }
412

413
  /**
414
   * Checks if the given directory is a valid IDE home by verifying it contains both 'workspaces' and 'settings' directories.
415
   *
416
   * @param dir the directory to check.
417
   * @return {@code true} if the directory is a valid IDE home, {@code false} otherwise.
418
   */
419
  protected boolean isIdeHome(Path dir) {
420

421
    if (!Files.isDirectory(dir.resolve("workspaces"))) {
7✔
422
      return false;
2✔
423
    } else if (!Files.isDirectory(dir.resolve("settings"))) {
7!
424
      return false;
×
425
    }
426
    return true;
2✔
427
  }
428

429
  private EnvironmentVariables createVariables() {
430

431
    AbstractEnvironmentVariables system = createSystemVariables();
3✔
432
    AbstractEnvironmentVariables user = system.extend(this.userHomeIde, EnvironmentVariablesType.USER);
6✔
433
    AbstractEnvironmentVariables settings = user.extend(this.settingsPath, EnvironmentVariablesType.SETTINGS);
6✔
434
    AbstractEnvironmentVariables workspace = settings.extend(this.workspacePath, EnvironmentVariablesType.WORKSPACE);
6✔
435
    AbstractEnvironmentVariables conf = workspace.extend(this.confPath, EnvironmentVariablesType.CONF);
6✔
436
    return conf.resolved();
3✔
437
  }
438

439
  protected AbstractEnvironmentVariables createSystemVariables() {
440

441
    return EnvironmentVariables.ofSystem(this);
3✔
442
  }
443

444
  @Override
445
  public SystemInfo getSystemInfo() {
446

447
    return this.systemInfo;
3✔
448
  }
449

450
  @Override
451
  public FileAccess getFileAccess() {
452

453
    return this.fileAccess;
3✔
454
  }
455

456
  @Override
457
  public CommandletManager getCommandletManager() {
458

459
    return this.commandletManager;
3✔
460
  }
461

462
  @Override
463
  public ToolRepository getDefaultToolRepository() {
464

465
    return this.defaultToolRepository;
3✔
466
  }
467

468
  @Override
469
  public MvnRepository getMvnRepository() {
470
    if (this.mvnRepository == null) {
3✔
471
      this.mvnRepository = createMvnRepository();
4✔
472
    }
473
    return this.mvnRepository;
3✔
474
  }
475

476
  @Override
477
  public NpmRepository getNpmRepository() {
478
    if (this.npmRepository == null) {
3✔
479
      this.npmRepository = createNpmRepository();
4✔
480
    }
481
    return this.npmRepository;
3✔
482
  }
483

484
  @Override
485
  public PipRepository getPipRepository() {
486
    if (this.pipRepository == null) {
3✔
487
      this.pipRepository = createPipRepository();
4✔
488
    }
489
    return this.pipRepository;
3✔
490
  }
491

492
  @Override
493
  public CustomToolRepository getCustomToolRepository() {
494

495
    if (this.customToolRepository == null) {
3!
496
      this.customToolRepository = CustomToolRepositoryImpl.of(this);
4✔
497
    }
498
    return this.customToolRepository;
3✔
499
  }
500

501
  @Override
502
  public Path getIdeHome() {
503

504
    return this.ideHome;
3✔
505
  }
506

507
  @Override
508
  public String getProjectName() {
509

510
    if (this.ideHome != null) {
3!
511
      return this.ideHome.getFileName().toString();
5✔
512
    }
513
    return "";
×
514
  }
515

516
  @Override
517
  public VersionIdentifier getProjectVersion() {
518

519
    if (this.ideHome != null) {
3!
520
      Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
521
      if (Files.exists(versionFile)) {
5✔
522
        String version = this.fileAccess.readFileContent(versionFile).trim();
6✔
523
        return VersionIdentifier.of(version);
3✔
524
      }
525
    }
526
    return IdeMigrator.START_VERSION;
2✔
527
  }
528

529
  @Override
530
  public void setProjectVersion(VersionIdentifier version) {
531

532
    if (this.ideHome == null) {
3!
533
      throw new IllegalStateException("IDE_HOME not available!");
×
534
    }
535
    Objects.requireNonNull(version);
3✔
536
    Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
537
    this.fileAccess.writeFileContent(version.toString(), versionFile);
6✔
538
  }
1✔
539

540
  @Override
541
  public Path getIdeRoot() {
542

543
    return this.ideRoot;
3✔
544
  }
545

546
  @Override
547
  public Path getIdePath() {
548

549
    Path myIdeRoot = getIdeRoot();
3✔
550
    if (myIdeRoot == null) {
2✔
551
      return null;
2✔
552
    }
553
    return myIdeRoot.resolve(FOLDER_UNDERSCORE_IDE);
4✔
554
  }
555

556
  @Override
557
  public Path getCwd() {
558

559
    return this.cwd;
3✔
560
  }
561

562
  @Override
563
  public Path getTempPath() {
564

565
    Path idePath = getIdePath();
3✔
566
    if (idePath == null) {
2!
567
      return null;
×
568
    }
569
    return idePath.resolve("tmp");
4✔
570
  }
571

572
  @Override
573
  public Path getTempDownloadPath() {
574

575
    Path tmp = getTempPath();
3✔
576
    if (tmp == null) {
2!
577
      return null;
×
578
    }
579
    return tmp.resolve(FOLDER_DOWNLOADS);
4✔
580
  }
581

582
  @Override
583
  public Path getUserHome() {
584

585
    return this.userHome;
3✔
586
  }
587

588
  /**
589
   * This method should only be used for tests to mock user home.
590
   *
591
   * @param userHome the new value of {@link #getUserHome()}.
592
   */
593
  protected void setUserHome(Path userHome) {
594

595
    this.userHome = userHome;
3✔
596
    resetPrivacyMap();
2✔
597
  }
1✔
598

599
  @Override
600
  public Path getUserHomeIde() {
601

602
    return this.userHomeIde;
3✔
603
  }
604

605
  @Override
606
  public Path getSettingsPath() {
607

608
    return this.settingsPath;
3✔
609
  }
610

611
  @Override
612
  public Path getSettingsGitRepository() {
613

614
    Path settingsPath = getSettingsPath();
3✔
615
    // check whether the settings path has a .git folder only if its not a symbolic link or junction
616
    if ((settingsPath != null) && !Files.exists(settingsPath.resolve(".git")) && !isSettingsRepositorySymlinkOrJunction()) {
12!
617
      LOG.error("Settings repository exists but is not a git repository.");
3✔
618
      return null;
2✔
619
    }
620
    return settingsPath;
2✔
621
  }
622

623
  @Override
624
  public boolean isSettingsRepositorySymlinkOrJunction() {
625

626
    Path settingsPath = getSettingsPath();
3✔
627
    if (settingsPath == null) {
2!
628
      return false;
×
629
    }
630
    return Files.isSymbolicLink(settingsPath) || getFileAccess().isJunction(settingsPath);
10!
631
  }
632

633
  @Override
634
  public Path getSettingsCommitIdPath() {
635

636
    return this.settingsCommitIdPath;
3✔
637
  }
638

639
  @Override
640
  public Path getConfPath() {
641

642
    return this.confPath;
3✔
643
  }
644

645
  @Override
646
  public Path getSoftwarePath() {
647

648
    if (this.ideHome == null) {
3✔
649
      return null;
2✔
650
    }
651
    return this.ideHome.resolve(FOLDER_SOFTWARE);
5✔
652
  }
653

654
  @Override
655
  public Path getSoftwareExtraPath() {
656

657
    Path softwarePath = getSoftwarePath();
3✔
658
    if (softwarePath == null) {
2!
659
      return null;
×
660
    }
661
    return softwarePath.resolve(FOLDER_EXTRA);
4✔
662
  }
663

664
  @Override
665
  public Path getSoftwareRepositoryPath() {
666

667
    Path idePath = getIdePath();
3✔
668
    if (idePath == null) {
2!
669
      return null;
×
670
    }
671
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
672
  }
673

674
  @Override
675
  public Path getPluginsPath() {
676

677
    return this.pluginsPath;
3✔
678
  }
679

680
  @Override
681
  public String getWorkspaceName() {
682

683
    return this.workspaceName;
3✔
684
  }
685

686
  @Override
687
  public Path getWorkspacePath() {
688

689
    return this.workspacePath;
3✔
690
  }
691

692
  @Override
693
  public Path getDownloadPath() {
694

695
    return this.downloadPath;
3✔
696
  }
697

698
  @Override
699
  public Path getUrlsPath() {
700

701
    Path idePath = getIdePath();
3✔
702
    if (idePath == null) {
2!
703
      return null;
×
704
    }
705
    return idePath.resolve(FOLDER_URLS);
4✔
706
  }
707

708
  @Override
709
  public Path getToolRepositoryPath() {
710

711
    Path idePath = getIdePath();
3✔
712
    if (idePath == null) {
2!
713
      return null;
×
714
    }
715
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
716
  }
717

718
  @Override
719
  public SystemPath getPath() {
720

721
    return this.path;
3✔
722
  }
723

724
  @Override
725
  public EnvironmentVariables getVariables() {
726

727
    if (this.variables == null) {
3✔
728
      this.variables = createVariables();
4✔
729
    }
730
    return this.variables;
3✔
731
  }
732

733
  @Override
734
  public UrlMetadata getUrls() {
735

736
    if (this.urlMetadata == null) {
3✔
737
      if (!isTest()) {
3!
738
        getGitContext().pullOrCloneAndResetIfNeeded(IDE_URLS_GIT, getUrlsPath(), null);
×
739
      }
740
      this.urlMetadata = new UrlMetadata(this);
6✔
741
    }
742
    return this.urlMetadata;
3✔
743
  }
744

745
  @Override
746
  public boolean isQuietMode() {
747

748
    return this.startContext.isQuietMode();
4✔
749
  }
750

751
  @Override
752
  public boolean isBatchMode() {
753

754
    return this.startContext.isBatchMode();
4✔
755
  }
756

757
  @Override
758
  public boolean isForceMode() {
759

760
    return this.startContext.isForceMode();
4✔
761
  }
762

763
  @Override
764
  public boolean isForcePull() {
765

766
    return this.startContext.isForcePull();
4✔
767
  }
768

769
  @Override
770
  public boolean isForcePlugins() {
771

772
    return this.startContext.isForcePlugins();
4✔
773
  }
774

775
  @Override
776
  public boolean isForceRepositories() {
777

778
    return this.startContext.isForceRepositories();
4✔
779
  }
780

781
  @Override
782
  public boolean isOfflineMode() {
783

784
    return this.startContext.isOfflineMode();
4✔
785
  }
786

787
  @Override
788
  public boolean isPrivacyMode() {
789
    return this.startContext.isPrivacyMode();
4✔
790
  }
791

792
  @Override
793
  public boolean isSkipUpdatesMode() {
794

795
    return this.startContext.isSkipUpdatesMode();
4✔
796
  }
797

798
  @Override
799
  public boolean isNoColorsMode() {
800

801
    return this.startContext.isNoColorsMode();
×
802
  }
803

804
  @Override
805
  public NetworkStatus getNetworkStatus() {
806

807
    if (this.networkStatus == null) {
×
808
      this.networkStatus = new NetworkStatusImpl(this);
×
809
    }
810
    return this.networkStatus;
×
811
  }
812

813
  @Override
814
  public Locale getLocale() {
815

816
    Locale locale = this.startContext.getLocale();
4✔
817
    if (locale == null) {
2✔
818
      locale = Locale.getDefault();
2✔
819
    }
820
    return locale;
2✔
821
  }
822

823
  @Override
824
  public DirectoryMerger getWorkspaceMerger() {
825

826
    if (this.workspaceMerger == null) {
3✔
827
      this.workspaceMerger = new DirectoryMerger(this);
6✔
828
    }
829
    return this.workspaceMerger;
3✔
830
  }
831

832
  /**
833
   * @return the default execution directory in which a command process is executed.
834
   */
835
  @Override
836
  public Path getDefaultExecutionDirectory() {
837

838
    return this.defaultExecutionDirectory;
×
839
  }
840

841
  /**
842
   * @param defaultExecutionDirectory new value of {@link #getDefaultExecutionDirectory()}.
843
   */
844
  public void setDefaultExecutionDirectory(Path defaultExecutionDirectory) {
845

846
    if (defaultExecutionDirectory != null) {
×
847
      this.defaultExecutionDirectory = defaultExecutionDirectory;
×
848
    }
849
  }
×
850

851
  @Override
852
  public GitContext getGitContext() {
853

854
    return new GitContextImpl(this);
×
855
  }
856

857
  @Override
858
  public ProcessContext newProcess() {
859

860
    ProcessContext processContext = createProcessContext();
3✔
861
    if (this.defaultExecutionDirectory != null) {
3!
862
      processContext.directory(this.defaultExecutionDirectory);
×
863
    }
864
    return processContext;
2✔
865
  }
866

867
  @Override
868
  public IdeSystem getSystem() {
869

870
    if (this.system == null) {
×
871
      this.system = new IdeSystemImpl();
×
872
    }
873
    return this.system;
×
874
  }
875

876
  /**
877
   * @return a new instance of {@link ProcessContext}.
878
   * @see #newProcess()
879
   */
880
  protected ProcessContext createProcessContext() {
881

882
    return new ProcessContextImpl(this);
×
883
  }
884

885
  @Override
886
  public IdeLogLevel getLogLevelConsole() {
887

888
    return this.startContext.getLogLevelConsole();
×
889
  }
890

891
  @Override
892
  public IdeLogLevel getLogLevelLogger() {
893

894
    return this.startContext.getLogLevelLogger();
×
895
  }
896

897
  @Override
898
  public IdeLogListener getLogListener() {
899

900
    return this.startContext.getLogListener();
×
901
  }
902

903
  @Override
904
  public void logIdeHomeAndRootStatus() {
905
    if (this.ideRoot != null) {
3!
906
      IdeLogLevel.SUCCESS.log(LOG, "IDE_ROOT is set to {}", this.ideRoot);
×
907
    }
908
    if (this.ideHome == null) {
3✔
909
      LOG.warn(getMessageNotInsideIdeProject());
5✔
910
    } else {
911
      IdeLogLevel.SUCCESS.log(LOG, "IDE_HOME is set to {}", this.ideHome);
11✔
912
    }
913
  }
1✔
914

915
  @Override
916
  public String formatArgument(Object argument) {
917

918
    if (argument == null) {
2✔
919
      return null;
2✔
920
    }
921
    String result = argument.toString();
3✔
922
    if (isPrivacyMode()) {
3✔
923
      if ((this.ideRoot != null) && this.privacyMap.isEmpty()) {
3!
924
        initializePrivacyMap(this.userHome, "~");
×
925
        String projectName = getProjectName();
×
926
        if (!projectName.isEmpty()) {
×
927
          this.privacyMap.put(projectName, "project");
×
928
        }
929
      }
930
      for (Entry<String, String> entry : this.privacyMap.entrySet()) {
8!
931
        result = result.replace(entry.getKey(), entry.getValue());
×
932
      }
×
933
      result = PrivacyUtil.removeSensitivePathInformation(result);
3✔
934
    }
935
    return result;
2✔
936
  }
937

938
  /**
939
   * @param path the sensitive {@link Path} to
940
   * @param replacement the replacement to mask the {@link Path} in log output.
941
   */
942
  protected void initializePrivacyMap(Path path, String replacement) {
943

944
    if (path == null) {
×
945
      return;
×
946
    }
947
    if (this.systemInfo.isWindows()) {
×
948
      this.privacyMap.put(WindowsPathSyntax.WINDOWS.format(path), replacement);
×
949
      this.privacyMap.put(WindowsPathSyntax.MSYS.format(path), replacement);
×
950
    } else {
951
      this.privacyMap.put(path.toString(), replacement);
×
952
    }
953
  }
×
954

955
  /**
956
   * Resets the privacy map in case fundamental values have changed.
957
   */
958
  private void resetPrivacyMap() {
959

960
    this.privacyMap.clear();
3✔
961
  }
1✔
962

963

964
  @Override
965
  public String askForInput(String message, String defaultValue) {
966

967
    while (true) {
968
      if (!message.isBlank()) {
3!
969
        IdeLogLevel.INTERACTION.log(LOG, message);
4✔
970
      }
971
      if (isBatchMode()) {
3!
972
        if (isForceMode()) {
×
973
          return defaultValue;
×
974
        } else {
975
          throw new CliAbortException();
×
976
        }
977
      }
978
      String input = readLine().trim();
4✔
979
      if (!input.isEmpty()) {
3!
980
        return input;
2✔
981
      } else {
982
        if (defaultValue != null) {
×
983
          return defaultValue;
×
984
        }
985
      }
986
    }
×
987
  }
988

989
  @Override
990
  public <O> O question(O[] options, String question, Object... args) {
991

992
    assert (options.length > 0);
4!
993
    IdeLogLevel.INTERACTION.log(LOG, question, args);
5✔
994
    return displayOptionsAndGetAnswer(options);
4✔
995
  }
996

997
  private <O> O displayOptionsAndGetAnswer(O[] options) {
998
    Map<String, O> mapping = new HashMap<>(options.length);
6✔
999
    int i = 0;
2✔
1000
    for (O option : options) {
16✔
1001
      i++;
1✔
1002
      String title = "" + option;
4✔
1003
      String key = computeOptionKey(title);
3✔
1004
      addMapping(mapping, key, option);
4✔
1005
      String numericKey = Integer.toString(i);
3✔
1006
      if (numericKey.equals(key)) {
4!
1007
        LOG.trace("Options should not be numeric: {}", key);
×
1008
      } else {
1009
        addMapping(mapping, numericKey, option);
4✔
1010
      }
1011
      IdeLogLevel.INTERACTION.log(LOG, "Option {}: {}", numericKey, title);
14✔
1012
    }
1013
    O option = null;
2✔
1014
    if (isBatchMode()) {
3!
1015
      if (isForceMode()) {
×
1016
        option = options[0];
×
1017
        IdeLogLevel.INTERACTION.log(LOG, "" + option);
×
1018
      }
1019
    } else {
1020
      while (option == null) {
2✔
1021
        String answer = readLine();
3✔
1022
        option = mapping.get(answer);
4✔
1023
        if (option == null) {
2!
1024
          LOG.warn("Invalid answer: '{}' - please try again.", answer);
×
1025
        }
1026
      }
1✔
1027
    }
1028
    return option;
2✔
1029
  }
1030

1031
  private static String computeOptionKey(String option) {
1032
    String key = option;
2✔
1033
    int index = -1;
2✔
1034
    for (char c : OPTION_DETAILS_START.toCharArray()) {
17✔
1035
      int currentIndex = key.indexOf(c);
4✔
1036
      if (currentIndex != -1) {
3✔
1037
        if ((index == -1) || (currentIndex < index)) {
3!
1038
          index = currentIndex;
2✔
1039
        }
1040
      }
1041
    }
1042
    if (index > 0) {
2✔
1043
      key = key.substring(0, index).trim();
6✔
1044
    }
1045
    return key;
2✔
1046
  }
1047

1048
  /**
1049
   * @return the input from the end-user (e.g. read from the console).
1050
   */
1051
  protected abstract String readLine();
1052

1053
  private static <O> void addMapping(Map<String, O> mapping, String key, O option) {
1054

1055
    O duplicate = mapping.put(key, option);
5✔
1056
    if (duplicate != null) {
2!
1057
      throw new IllegalArgumentException("Duplicated option " + key);
×
1058
    }
1059
  }
1✔
1060

1061
  @Override
1062
  public Step getCurrentStep() {
1063

1064
    return this.currentStep;
×
1065
  }
1066

1067
  @Override
1068
  public StepImpl newStep(boolean silent, String name, Object... parameters) {
1069

1070
    this.currentStep = new StepImpl(this, this.currentStep, name, silent, parameters);
11✔
1071
    return this.currentStep;
3✔
1072
  }
1073

1074
  /**
1075
   * Internal method to end the running {@link Step}.
1076
   *
1077
   * @param step the current {@link Step} to end.
1078
   */
1079
  public void endStep(StepImpl step) {
1080

1081
    if (step == this.currentStep) {
4!
1082
      this.currentStep = this.currentStep.getParent();
6✔
1083
    } else {
1084
      String currentStepName = "null";
×
1085
      if (this.currentStep != null) {
×
1086
        currentStepName = this.currentStep.getName();
×
1087
      }
1088
      LOG.warn("endStep called with wrong step '{}' but expected '{}'", step.getName(), currentStepName);
×
1089
    }
1090
  }
1✔
1091

1092
  /**
1093
   * Finds the matching {@link Commandlet} to run, applies {@link CliArguments} to its {@link Commandlet#getProperties() properties} and will execute it.
1094
   *
1095
   * @param arguments the {@link CliArgument}.
1096
   * @return the return code of the execution.
1097
   */
1098
  public int run(CliArguments arguments) {
1099

1100
    CliArgument current = arguments.current();
3✔
1101
    assert (this.currentStep == null);
4!
1102
    boolean supressStepSuccess = false;
2✔
1103
    StepImpl step = newStep(true, "ide", (Object[]) current.asArray());
8✔
1104
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, null);
6✔
1105
    Commandlet cmd = null;
2✔
1106
    ValidationResult result = null;
2✔
1107
    try {
1108
      while (commandletIterator.hasNext()) {
3✔
1109
        cmd = commandletIterator.next();
4✔
1110
        result = applyAndRun(arguments.copy(), cmd);
6✔
1111
        if (result.isValid()) {
3!
1112
          supressStepSuccess = cmd.isSuppressStepSuccess();
3✔
1113
          step.success();
2✔
1114
          return ProcessResult.SUCCESS;
4✔
1115
        }
1116
      }
1117
      activateLogging(cmd);
3✔
1118
      verifyIdeMinVersion(false);
3✔
1119
      if (result != null) {
2!
1120
        LOG.error(result.getErrorMessage());
×
1121
      }
1122
      step.error("Invalid arguments: {}", current.getArgs());
10✔
1123
      HelpCommandlet help = this.commandletManager.getCommandlet(HelpCommandlet.class);
6✔
1124
      if (cmd != null) {
2!
1125
        help.commandlet.setValue(cmd);
×
1126
      }
1127
      help.run();
2✔
1128
      return 1;
4✔
1129
    } catch (Throwable t) {
1✔
1130
      activateLogging(cmd);
3✔
1131
      step.error(t, true);
4✔
1132
      if (this.logfile != null) {
3!
1133
        System.err.println("Logfile can be found at " + this.logfile); // do not use logger
×
1134
      }
1135
      throw t;
2✔
1136
    } finally {
1137
      step.close();
2✔
1138
      assert (this.currentStep == null);
4!
1139
      step.logSummary(supressStepSuccess);
3✔
1140
    }
1141
  }
1142

1143
  /**
1144
   * Ensure the logging system is initialized.
1145
   */
1146
  private void activateLogging(Commandlet cmd) {
1147

1148
    configureJavaUtilLogging(cmd);
3✔
1149
    this.startContext.activateLogging();
3✔
1150
  }
1✔
1151

1152
  /**
1153
   * Configures the logging system (JUL).
1154
   *
1155
   * @param cmd the {@link Commandlet} to be called. May be {@code null}.
1156
   */
1157
  public void configureJavaUtilLogging(Commandlet cmd) {
1158

1159
    if (this.julConfigured) {
3✔
1160
      return;
1✔
1161
    }
1162
    boolean writeLogfile = isWriteLogfile(cmd);
4✔
1163
    this.startContext.setWriteLogfile(writeLogfile);
4✔
1164
    Properties properties = createJavaUtilLoggingProperties(writeLogfile, cmd);
5✔
1165
    try {
1166
      ByteArrayOutputStream out = new ByteArrayOutputStream(512);
5✔
1167
      properties.store(out, null);
4✔
1168
      out.flush();
2✔
1169
      ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
6✔
1170
      LogManager.getLogManager().readConfiguration(in);
3✔
1171
      this.julConfigured = true;
3✔
1172
      this.startContext.activateLogging();
3✔
1173
    } catch (IOException e) {
×
1174
      LOG.error("Failed to configure logging: {}", e.toString(), e);
×
1175
    }
1✔
1176
  }
1✔
1177

1178
  protected boolean isWriteLogfile(Commandlet cmd) {
1179
    if (!cmd.isWriteLogFile()) {
×
1180
      return false;
×
1181
    }
1182
    Boolean writeLogfile = IdeVariables.IDE_WRITE_LOGFILE.get(this);
×
1183
    return Boolean.TRUE.equals(writeLogfile);
×
1184
  }
1185

1186
  private Properties createJavaUtilLoggingProperties(boolean writeLogfile, Commandlet cmd) {
1187

1188
    Path idePath = getIdePath();
3✔
1189
    if (writeLogfile && (idePath == null)) {
2!
1190
      writeLogfile = false;
×
1191
      LOG.error("Cannot enable log-file since IDE_ROOT is undefined.");
×
1192
    }
1193
    Properties properties = new Properties();
4✔
1194
    // prevent 3rd party (e.g. java.lang.ProcessBuilder) logging into our console via JUL
1195
    // see JulLogLevel for the trick we did to workaround JUL flaws
1196
    properties.setProperty(".level", "SEVERE");
5✔
1197
    if (writeLogfile) {
2!
1198
      this.startContext.setLogLevelLogger(IdeLogLevel.TRACE);
×
1199
      properties.setProperty("handlers", "com.devonfw.tools.ide.log.JulConsoleHandler,java.util.logging.FileHandler");
×
1200
      properties.setProperty("java.util.logging.FileHandler.formatter", "java.util.logging.SimpleFormatter");
×
1201
      properties.setProperty("java.util.logging.FileHandler.encoding", "UTF-8");
×
1202
      this.logfile = createLogfilePath(idePath, cmd);
×
1203
      getFileAccess().mkdirs(this.logfile.getParent());
×
1204
      properties.setProperty("java.util.logging.FileHandler.pattern", this.logfile.toString());
×
1205
    } else {
1206
      properties.setProperty("handlers", "com.devonfw.tools.ide.log.JulConsoleHandler");
5✔
1207
    }
1208
    properties.setProperty("java.util.logging.SimpleFormatter.format", "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL [%4$s] [%3$s] %5$s%6$s%n");
5✔
1209
    return properties;
2✔
1210
  }
1211

1212
  private Path createLogfilePath(Path idePath, Commandlet cmd) {
1213
    LocalDateTime now = LocalDateTime.now();
×
1214
    Path logsPath = idePath.resolve(FOLDER_LOGS).resolve(DateTimeUtil.formatDate(now, true));
×
1215
    StringBuilder sb = new StringBuilder(32);
×
1216
    if (this.ideHome == null || ((cmd != null) && !cmd.isIdeHomeRequired())) {
×
1217
      sb.append("_ide-");
×
1218
    } else {
1219
      sb.append(this.ideHome.getFileName().toString());
×
1220
      sb.append('-');
×
1221
    }
1222
    sb.append("ide-");
×
1223
    if (cmd != null) {
×
1224
      sb.append(cmd.getName());
×
1225
      sb.append('-');
×
1226
    }
1227
    sb.append(DateTimeUtil.formatTime(now));
×
1228
    sb.append(".log");
×
1229
    return logsPath.resolve(sb.toString());
×
1230
  }
1231

1232
  @Override
1233
  public void runWithoutLogging(Runnable lambda, IdeLogLevel threshold) {
1234

1235
    this.startContext.deactivateLogging(threshold);
4✔
1236
    lambda.run();
2✔
1237
    this.startContext.activateLogging();
3✔
1238
  }
1✔
1239

1240
  /**
1241
   * @param cmd the potential {@link Commandlet} to {@link #apply(CliArguments, Commandlet) apply} and {@link Commandlet#run() run}.
1242
   * @return {@code true} if the given {@link Commandlet} matched and did {@link Commandlet#run() run} successfully, {@code false} otherwise (the
1243
   *     {@link Commandlet} did not match and we have to try a different candidate).
1244
   */
1245
  private ValidationResult applyAndRun(CliArguments arguments, Commandlet cmd) {
1246

1247
    IdeLogLevel previousLogLevel = null;
2✔
1248
    cmd.reset();
2✔
1249
    ValidationResult result = apply(arguments, cmd);
5✔
1250
    if (result.isValid()) {
3!
1251
      result = cmd.validate();
3✔
1252
    }
1253
    if (result.isValid()) {
3!
1254
      LOG.debug("Running commandlet {}", cmd);
4✔
1255
      if (cmd.isIdeHomeRequired() && (this.ideHome == null)) {
6!
1256
        throw new CliException(getMessageNotInsideIdeProject(), ProcessResult.NO_IDE_HOME);
×
1257
      } else if (cmd.isIdeRootRequired() && (this.ideRoot == null)) {
6!
1258
        throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
7✔
1259
      }
1260
      try {
1261
        if (cmd.isProcessableOutput()) {
3!
1262
          if (!LOG.isDebugEnabled()) {
×
1263
            // unless --debug or --trace was supplied, processable output commandlets will disable all log-levels except INFO to prevent other logs interfere
1264
            previousLogLevel = this.startContext.setLogLevelConsole(IdeLogLevel.PROCESSABLE);
×
1265
          }
1266
        } else {
1267
          if (cmd.isIdeHomeRequired()) {
3!
1268
            LOG.debug(getMessageIdeHomeFound());
4✔
1269
          }
1270
          Path settingsRepository = getSettingsGitRepository();
3✔
1271
          if (settingsRepository != null) {
2!
1272
            if (getGitContext().isRepositoryUpdateAvailable(settingsRepository, getSettingsCommitIdPath()) || (
×
1273
                getGitContext().fetchIfNeeded(settingsRepository) && getGitContext().isRepositoryUpdateAvailable(
×
1274
                    settingsRepository, getSettingsCommitIdPath()))) {
×
1275
              String msg;
1276
              if (isSettingsRepositorySymlinkOrJunction()) {
×
1277
                msg = "Updates are available for the settings repository. Please pull the latest changes by yourself or by calling \"ide -f update\" to apply them.";
×
1278
              } else {
1279
                msg = "Updates are available for the settings repository. If you want to apply the latest changes, call \"ide update\"";
×
1280
              }
1281
              IdeLogLevel.INTERACTION.log(LOG, msg);
×
1282
            }
1283
          }
1284
        }
1285
        boolean success = ensureLicenseAgreement(cmd);
4✔
1286
        if (!success) {
2!
1287
          return ValidationResultValid.get();
×
1288
        }
1289
        cmd.run();
2✔
1290
      } finally {
1291
        if (previousLogLevel != null) {
2!
1292
          this.startContext.setLogLevelConsole(previousLogLevel);
×
1293
        }
1294
      }
1✔
1295
    } else {
1296
      LOG.trace("Commandlet did not match");
×
1297
    }
1298
    return result;
2✔
1299
  }
1300

1301
  private boolean ensureLicenseAgreement(Commandlet cmd) {
1302

1303
    if (isTest()) {
3!
1304
      return true; // ignore for tests
2✔
1305
    }
1306
    getFileAccess().mkdirs(this.userHomeIde);
×
1307
    Path licenseAgreement = this.userHomeIde.resolve(FILE_LICENSE_AGREEMENT);
×
1308
    if (Files.isRegularFile(licenseAgreement)) {
×
1309
      return true; // success, license already accepted
×
1310
    }
1311
    if (cmd instanceof EnvironmentCommandlet) {
×
1312
      // if the license was not accepted, "$(ideasy env --bash)" that is written into a variable prevents the user from seeing the question he is asked
1313
      // in such situation the user could not open a bash terminal anymore and gets blocked what would really annoy the user so we exit here without doing or
1314
      // printing anything anymore in such case.
1315
      return false;
×
1316
    }
1317
    IdeLogLevel oldLogLevel = this.startContext.getLogLevelConsole();
×
1318
    IdeLogLevel newLogLevel = oldLogLevel;
×
1319
    if (oldLogLevel.ordinal() > IdeLogLevel.INFO.ordinal()) {
×
1320
      newLogLevel = IdeLogLevel.INFO;
×
1321
      this.startContext.setLogLevelConsole(newLogLevel);
×
1322
    }
1323
    StringBuilder sb = new StringBuilder(1180);
×
1324
    sb.append(LOGO).append("""
×
1325
        Welcome to IDEasy!
1326
        This product (with its included 3rd party components) is open-source software and can be used free (also commercially).
1327
        It supports automatic download and installation of arbitrary 3rd party tools.
1328
        By default only open-source 3rd party tools are used (downloaded, installed, executed).
1329
        But if explicitly configured, also commercial software that requires an additional license may be used.
1330
        This happens e.g. if you configure "ultimate" edition of IntelliJ or "docker" edition of Docker (Docker Desktop).
1331
        You are solely responsible for all risks implied by using this software.
1332
        Before using IDEasy you need to read and accept the license agreement with all involved licenses.
1333
        You will be able to find it online under the following URL:
1334
        """).append(LICENSE_URL);
×
1335
    if (this.ideRoot != null) {
×
1336
      sb.append("\n\nAlso it is included in the documentation that you can find here:\n").
×
1337
          append(getIdePath().resolve("IDEasy.pdf").toString()).append("\n");
×
1338
    }
1339
    LOG.info(sb.toString());
×
1340
    askToContinue("Do you accept these terms of use and all license agreements?");
×
1341

1342
    sb.setLength(0);
×
1343
    LocalDateTime now = LocalDateTime.now();
×
1344
    sb.append("On ").append(DateTimeUtil.formatDate(now, false)).append(" at ").append(DateTimeUtil.formatTime(now))
×
1345
        .append(" you accepted the IDEasy license.\n").append(LICENSE_URL);
×
1346
    try {
1347
      Files.writeString(licenseAgreement, sb);
×
1348
    } catch (Exception e) {
×
1349
      throw new RuntimeException("Failed to save license agreement!", e);
×
1350
    }
×
1351
    if (oldLogLevel != newLogLevel) {
×
1352
      this.startContext.setLogLevelConsole(oldLogLevel);
×
1353
    }
1354
    return true;
×
1355
  }
1356

1357
  @Override
1358
  public void verifyIdeMinVersion(boolean throwException) {
1359
    VersionIdentifier minVersion = IDE_MIN_VERSION.get(this);
5✔
1360
    if (minVersion == null) {
2✔
1361
      return;
1✔
1362
    }
1363
    if (IdeVersion.getVersionIdentifier().compareVersion(minVersion).isLess()) {
5✔
1364
      String message = String.format("Your version of IDEasy is currently %s\n"
7✔
1365
          + "However, this is too old as your project requires at latest version %s\n"
1366
          + "Please run the following command to update to the latest version of IDEasy and fix the problem:\n"
1367
          + "ide upgrade", IdeVersion.getVersionIdentifier().toString(), minVersion.toString());
8✔
1368
      if (throwException) {
2✔
1369
        throw new CliException(message);
5✔
1370
      } else {
1371
        LOG.warn(message);
3✔
1372
      }
1373
    }
1374
  }
1✔
1375

1376
  /**
1377
   * @param arguments the {@link CliArguments#ofCompletion(String...) completion arguments}.
1378
   * @param includeContextOptions to include the options of {@link ContextCommandlet}.
1379
   * @return the {@link List} of {@link CompletionCandidate}s to suggest.
1380
   */
1381
  public List<CompletionCandidate> complete(CliArguments arguments, boolean includeContextOptions) {
1382

1383
    CompletionCandidateCollector collector = new CompletionCandidateCollectorDefault(this);
5✔
1384
    if (arguments.current().isStart()) {
4✔
1385
      arguments.next();
3✔
1386
    }
1387
    if (includeContextOptions) {
2✔
1388
      ContextCommandlet cc = new ContextCommandlet();
4✔
1389
      for (Property<?> property : cc.getProperties()) {
11✔
1390
        assert (property.isOption());
4!
1391
        property.apply(arguments, this, cc, collector);
7✔
1392
      }
1✔
1393
    }
1394
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, collector);
6✔
1395
    CliArgument current = arguments.current();
3✔
1396
    if (current.isCompletion() && current.isCombinedShortOption()) {
6✔
1397
      collector.add(current.get(), null, null, null);
7✔
1398
    }
1399
    arguments.next();
3✔
1400
    while (commandletIterator.hasNext()) {
3✔
1401
      Commandlet cmd = commandletIterator.next();
4✔
1402
      if (!arguments.current().isEnd()) {
4✔
1403
        completeCommandlet(arguments.copy(), cmd, collector);
6✔
1404
      }
1405
    }
1✔
1406
    return collector.getSortedCandidates();
3✔
1407
  }
1408

1409
  private void completeCommandlet(CliArguments arguments, Commandlet cmd, CompletionCandidateCollector collector) {
1410

1411
    LOG.trace("Trying to match arguments for auto-completion for commandlet {}", cmd.getName());
5✔
1412
    Iterator<Property<?>> valueIterator = cmd.getValues().iterator();
4✔
1413
    valueIterator.next(); // skip first property since this is the keyword property that already matched to find the commandlet
3✔
1414
    List<Property<?>> properties = cmd.getProperties();
3✔
1415
    // we are creating our own list of options and remove them when matched to avoid duplicate suggestions
1416
    List<Property<?>> optionProperties = new ArrayList<>(properties.size());
6✔
1417
    for (Property<?> property : properties) {
10✔
1418
      if (property.isOption()) {
3✔
1419
        optionProperties.add(property);
4✔
1420
      }
1421
    }
1✔
1422
    CliArgument currentArgument = arguments.current();
3✔
1423
    while (!currentArgument.isEnd()) {
3✔
1424
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1425
      if (currentArgument.isOption() && !arguments.isEndOptions()) {
6!
1426
        if (currentArgument.isCompletion()) {
3✔
1427
          Iterator<Property<?>> optionIterator = optionProperties.iterator();
3✔
1428
          while (optionIterator.hasNext()) {
3✔
1429
            Property<?> option = optionIterator.next();
4✔
1430
            boolean success = option.apply(arguments, this, cmd, collector);
7✔
1431
            if (success) {
2✔
1432
              optionIterator.remove();
2✔
1433
              arguments.next();
3✔
1434
            }
1435
          }
1✔
1436
        } else {
1✔
1437
          Property<?> option = cmd.getOption(currentArgument.get());
5✔
1438
          if (option != null) {
2✔
1439
            arguments.next();
3✔
1440
            boolean removed = optionProperties.remove(option);
4✔
1441
            if (!removed) {
2!
1442
              option = null;
×
1443
            }
1444
          }
1445
          if (option == null) {
2✔
1446
            LOG.trace("No such option was found.");
3✔
1447
            return;
1✔
1448
          }
1449
        }
1✔
1450
      } else {
1451
        if (valueIterator.hasNext()) {
3✔
1452
          Property<?> valueProperty = valueIterator.next();
4✔
1453
          boolean success = valueProperty.apply(arguments, this, cmd, collector);
7✔
1454
          if (!success) {
2✔
1455
            LOG.trace("Completion cannot match any further.");
3✔
1456
            return;
1✔
1457
          }
1458
        } else {
1✔
1459
          LOG.trace("No value left for completion.");
3✔
1460
          return;
1✔
1461
        }
1462
      }
1463
      currentArgument = arguments.current();
4✔
1464
    }
1465
  }
1✔
1466

1467
  /**
1468
   * @param arguments the {@link CliArguments} to apply. Will be {@link CliArguments#next() consumed} as they are matched. Consider passing a
1469
   *     {@link CliArguments#copy() copy} as needed.
1470
   * @param cmd the potential {@link Commandlet} to match.
1471
   * @return the {@link ValidationResult} telling if the {@link CliArguments} can be applied successfully or if validation errors ocurred.
1472
   */
1473
  public ValidationResult apply(CliArguments arguments, Commandlet cmd) {
1474

1475
    LOG.trace("Trying to match arguments to commandlet {}", cmd.getName());
5✔
1476
    CliArgument currentArgument = arguments.current();
3✔
1477
    Iterator<Property<?>> propertyIterator = cmd.getValues().iterator();
4✔
1478
    Property<?> property = null;
2✔
1479
    if (propertyIterator.hasNext()) {
3!
1480
      property = propertyIterator.next();
4✔
1481
    }
1482
    while (!currentArgument.isEnd()) {
3✔
1483
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1484
      Property<?> currentProperty = property;
2✔
1485
      if (!arguments.isEndOptions()) {
3!
1486
        Property<?> option = cmd.getOption(currentArgument.getKey());
5✔
1487
        if (option != null) {
2!
1488
          currentProperty = option;
×
1489
        }
1490
      }
1491
      if (currentProperty == null) {
2!
1492
        LOG.trace("No option or next value found");
×
1493
        ValidationState state = new ValidationState(null);
×
1494
        state.addErrorMessage("No matching property found");
×
1495
        return state;
×
1496
      }
1497
      LOG.trace("Next property candidate to match argument is {}", currentProperty);
4✔
1498
      if (currentProperty == property) {
3!
1499
        if (!property.isMultiValued()) {
3✔
1500
          if (propertyIterator.hasNext()) {
3✔
1501
            property = propertyIterator.next();
5✔
1502
          } else {
1503
            property = null;
2✔
1504
          }
1505
        }
1506
        if ((property != null) && property.isValue() && property.isMultiValued()) {
8!
1507
          arguments.stopSplitShortOptions();
2✔
1508
        }
1509
      }
1510
      boolean matches = currentProperty.apply(arguments, this, cmd, null);
7✔
1511
      if (!matches) {
2!
1512
        ValidationState state = new ValidationState(null);
×
1513
        state.addErrorMessage("No matching property found");
×
1514
        return state;
×
1515
      }
1516
      currentArgument = arguments.current();
3✔
1517
    }
1✔
1518
    return ValidationResultValid.get();
2✔
1519
  }
1520

1521
  @Override
1522
  public Path findBash() {
1523
    if (this.bash != null) {
3✔
1524
      return this.bash;
3✔
1525
    }
1526
    Path bashPath = findBashOnBashPath();
3✔
1527
    if (bashPath == null) {
2✔
1528
      bashPath = findBashInPath();
3✔
1529
      if (bashPath == null && (getSystemInfo().isWindows() || SystemInfoImpl.INSTANCE.isWindows())) {
6!
1530
        bashPath = findBashOnWindowsDefaultGitPath();
3✔
1531
        if (bashPath == null) {
2!
1532
          bashPath = findBashInWindowsRegistry();
3✔
1533
        }
1534
      }
1535
    }
1536
    if (bashPath == null) {
2✔
1537
      LOG.error("No bash executable could be found on your system.");
4✔
1538
    } else {
1539
      this.bash = bashPath;
3✔
1540
    }
1541
    return bashPath;
2✔
1542
  }
1543

1544
  private Path findBashOnBashPath() {
1545
    LOG.trace("Trying to find BASH_PATH environment variable.");
3✔
1546
    Path bash;
1547
    String bashPathVariableName = IdeVariables.BASH_PATH.getName();
3✔
1548
    String bashVariable = getVariables().get(bashPathVariableName);
5✔
1549
    if (bashVariable != null) {
2✔
1550
      bash = Path.of(bashVariable);
5✔
1551
      if (Files.exists(bash)) {
5✔
1552
        LOG.debug("{} environment variable was found and points to: {}", bashPathVariableName, bash);
5✔
1553
        return bash;
2✔
1554
      } else {
1555
        LOG.error("The environment variable {} points to a non existing file: {}", bashPathVariableName, bash);
5✔
1556
        return null;
2✔
1557
      }
1558
    } else {
1559
      LOG.debug("{} environment variable was not found", bashPathVariableName);
4✔
1560
      return null;
2✔
1561
    }
1562
  }
1563

1564
  /**
1565
   * @param path the path to check.
1566
   * @param toIgnore the String sequence which needs to be checked and ignored.
1567
   * @return {@code true} if the sequence to ignore was not found, {@code false} if the path contained the sequence to ignore.
1568
   */
1569
  private boolean checkPathToIgnoreLowercase(Path path, String toIgnore) {
1570
    String s = path.toAbsolutePath().toString().toLowerCase(Locale.ROOT);
6✔
1571
    return !s.contains(toIgnore);
7!
1572
  }
1573

1574
  /**
1575
   * Tries to find the bash.exe within the PATH environment variable.
1576
   *
1577
   * @return Path to bash.exe if found in PATH environment variable, {@code null} if bash.exe was not found.
1578
   */
1579
  private Path findBashInPath() {
1580
    LOG.trace("Trying to find bash in PATH environment variable.");
3✔
1581
    Path bash;
1582
    String pathVariableName = IdeVariables.PATH.getName();
3✔
1583
    if (pathVariableName != null) {
2!
1584
      Path plainBash = Path.of(BASH);
5✔
1585
      Predicate<Path> pathsToIgnore = p -> checkPathToIgnoreLowercase(p, "\\appdata\\local\\microsoft\\windowsapps") && checkPathToIgnoreLowercase(p,
16!
1586
          "\\windows\\system32");
1587
      Path bashPath = getPath().findBinary(plainBash, pathsToIgnore);
6✔
1588
      bash = bashPath.toAbsolutePath();
3✔
1589
      if (bashPath.equals(plainBash)) {
4✔
1590
        LOG.warn("No usable bash executable was found in your PATH environment variable!");
3✔
1591
        bash = null;
3✔
1592
      } else {
1593
        if (Files.exists(bashPath)) {
5!
1594
          LOG.debug("A proper bash executable was found in your PATH environment variable at: {}", bash);
5✔
1595
        } else {
1596
          bash = null;
×
1597
          LOG.error("A path to a bash executable was found in your PATH environment variable at: {} but the file is not existing.", bash);
×
1598
        }
1599
      }
1600
    } else {
1✔
1601
      bash = null;
×
1602
      // this should never happen...
1603
      LOG.error("PATH environment variable was not found");
×
1604
    }
1605
    return bash;
2✔
1606
  }
1607

1608
  /**
1609
   * Tries to find the bash.exe within the Windows registry.
1610
   *
1611
   * @return Path to bash.exe if found in registry, {@code null} if bash.exe was found.
1612
   */
1613
  protected Path findBashInWindowsRegistry() {
1614
    LOG.trace("Trying to find bash in Windows registry");
×
1615
    // If not found in the default location, try the registry query
1616
    String[] bashVariants = { "GitForWindows", "Cygwin\\setup" };
×
1617
    String[] registryKeys = { "HKEY_LOCAL_MACHINE", "HKEY_CURRENT_USER" };
×
1618
    for (String bashVariant : bashVariants) {
×
1619
      LOG.trace("Trying to find bash variant: {}", bashVariant);
×
1620
      for (String registryKey : registryKeys) {
×
1621
        LOG.trace("Trying to find bash from registry key: {}", registryKey);
×
1622
        String toolValueName = ("GitForWindows".equals(bashVariant)) ? "InstallPath" : "rootdir";
×
1623
        String registryPath = registryKey + "\\Software\\" + bashVariant;
×
1624

1625
        String path = getWindowsHelper().getRegistryValue(registryPath, toolValueName);
×
1626
        if (path != null) {
×
1627
          Path bashPath = Path.of(path + "\\bin\\bash.exe");
×
1628
          if (Files.exists(bashPath)) {
×
1629
            LOG.debug("Found bash at: {}", bashPath);
×
1630
            return bashPath;
×
1631
          } else {
1632
            LOG.error("Found bash at: {} but it is not pointing to an existing file", bashPath);
×
1633
            return null;
×
1634
          }
1635
        } else {
1636
          LOG.info("No bash executable could be found in the Windows registry.");
×
1637
        }
1638
      }
1639
    }
1640
    // no bash found
1641
    return null;
×
1642
  }
1643

1644
  private Path findBashOnWindowsDefaultGitPath() {
1645
    // Check if Git Bash exists in the default location
1646
    LOG.trace("Trying to find bash on the Windows default git path.");
3✔
1647
    Path defaultPath = Path.of(getDefaultWindowsGitPath());
6✔
1648
    if (!defaultPath.toString().isEmpty() && Files.exists(defaultPath)) {
4!
1649
      LOG.trace("Found default path to git bash on Windows at: {}", getDefaultWindowsGitPath());
×
1650
      return defaultPath;
×
1651
    }
1652
    LOG.debug("No bash was found on the Windows default git path.");
3✔
1653
    return null;
2✔
1654
  }
1655

1656
  @Override
1657
  public WindowsPathSyntax getPathSyntax() {
1658

1659
    return this.pathSyntax;
3✔
1660
  }
1661

1662
  /**
1663
   * @param pathSyntax new value of {@link #getPathSyntax()}.
1664
   */
1665
  public void setPathSyntax(WindowsPathSyntax pathSyntax) {
1666

1667
    this.pathSyntax = pathSyntax;
3✔
1668
  }
1✔
1669

1670
  /**
1671
   * @return the {@link IdeStartContextImpl}.
1672
   */
1673
  public IdeStartContextImpl getStartContext() {
1674

1675
    return startContext;
3✔
1676
  }
1677

1678
  /**
1679
   * @return the {@link WindowsHelper}.
1680
   */
1681
  public final WindowsHelper getWindowsHelper() {
1682

1683
    if (this.windowsHelper == null) {
3✔
1684
      this.windowsHelper = createWindowsHelper();
4✔
1685
    }
1686
    return this.windowsHelper;
3✔
1687
  }
1688

1689
  /**
1690
   * @return the new {@link WindowsHelper} instance.
1691
   */
1692
  protected WindowsHelper createWindowsHelper() {
1693

1694
    return new WindowsHelperImpl(this);
×
1695
  }
1696

1697
  /**
1698
   * Reloads this context and re-initializes the {@link #getVariables() variables}.
1699
   */
1700
  public void reload() {
1701

1702
    this.variables = null;
3✔
1703
    this.customToolRepository = null;
3✔
1704
  }
1✔
1705

1706
  @Override
1707
  public void writeVersionFile(VersionIdentifier version, Path installationPath) {
1708

1709
    assert (Files.isDirectory(installationPath));
6!
1710
    Path versionFile = installationPath.resolve(FILE_SOFTWARE_VERSION);
4✔
1711
    getFileAccess().writeFileContent(version.toString(), versionFile);
6✔
1712
  }
1✔
1713

1714
  /*
1715
   * @param home the IDE_HOME directory.
1716
   * @param workspace the name of the active workspace folder.
1717
   */
1718
  protected static record IdeHomeAndWorkspace(Path home, String workspace) {
9✔
1719

1720
  }
1721

1722
  /**
1723
   * Returns the default git path on Windows. Required to be overwritten in tests.
1724
   *
1725
   * @return default path to git on Windows.
1726
   */
1727
  public String getDefaultWindowsGitPath() {
1728
    return DEFAULT_WINDOWS_GIT_PATH;
×
1729
  }
1730

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