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

devonfw / IDEasy / 29098863710

10 Jul 2026 02:11PM UTC coverage: 72.291% (+0.07%) from 72.225%
29098863710

Pull #2149

github

web-flow
Merge cab6001a3 into 365f379a1
Pull Request #2149: #2102 set ideRoot during msi installation correctly

4887 of 7458 branches covered (65.53%)

Branch coverage included in aggregate %.

12585 of 16711 relevant lines covered (75.31%)

3.19 hits per line

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

70.32
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.FileHandler;
22
import java.util.logging.LogManager;
23
import java.util.logging.SimpleFormatter;
24

25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

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

92
/**
93
 * Abstract base implementation of {@link IdeContext}.
94
 */
95
public abstract class AbstractIdeContext implements IdeContext, IdeLogArgFormatter {
96

97
  static final Logger LOG = LoggerFactory.getLogger(AbstractIdeContext.class);
3✔
98

99
  /** The default shell bash (Bourne Again SHell). */
100
  public static final String BASH = "bash";
101

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

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

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

108
  private static final String OPTION_DETAILS_START = "([";
109

110
  private final IdeStartContextImpl startContext;
111

112
  private Path ideHome;
113

114
  private Path ideRoot;
115

116
  private Path confPath;
117

118
  protected Path settingsPath;
119

120
  private Path settingsCommitIdPath;
121

122
  protected Path pluginsPath;
123

124
  private Path workspacePath;
125

126
  private Path workspacesBasePath;
127

128
  private String workspaceName;
129

130
  private Path cwd;
131

132
  private Path downloadPath;
133

134
  private Path userHome;
135

136
  private Path userHomeIde;
137

138
  private SystemPath path;
139

140
  private WindowsPathSyntax pathSyntax;
141

142
  private final SystemInfo systemInfo;
143

144
  private EnvironmentVariables variables;
145

146
  private final FileAccess fileAccess;
147

148
  protected CommandletManager commandletManager;
149

150
  protected ToolRepository defaultToolRepository;
151

152
  private CustomToolRepository customToolRepository;
153

154
  private MvnRepository mvnRepository;
155

156
  private NpmRepository npmRepository;
157

158
  private PipRepository pipRepository;
159

160
  private UvRepository uvRepository;
161

162
  private DirectoryMerger workspaceMerger;
163

164
  protected UrlMetadata urlMetadata;
165

166
  protected Path defaultExecutionDirectory;
167

168
  private StepImpl currentStep;
169

170
  private NetworkStatus networkStatus;
171

172
  protected IdeSystem system;
173

174
  private WindowsHelper windowsHelper;
175

176
  private final Map<String, String> privacyMap;
177

178
  private Path bash;
179

180
  private boolean julConfigured;
181

182
  private Path logfile;
183

184
  private CliSuggester cliSuggester;
185

186
  /**
187
   * The constructor.
188
   *
189
   * @param startContext the {@link IdeStartContextImpl}.
190
   * @param workingDirectory the optional {@link Path} to current working directory.
191
   */
192
  public AbstractIdeContext(IdeStartContextImpl startContext, Path workingDirectory) {
193

194
    super();
2✔
195
    this.startContext = startContext;
3✔
196
    this.startContext.setArgFormatter(this);
4✔
197
    this.privacyMap = new HashMap<>();
5✔
198
    this.systemInfo = SystemInfoImpl.INSTANCE;
3✔
199
    if (isTest()) {
3!
200
      configureJavaUtilLogging(null);
3✔
201
    }
202
    this.commandletManager = new CommandletManagerImpl(this);
6✔
203
    this.fileAccess = new FileAccessImpl(this);
6✔
204
    String userHomeProperty = getSystem().getProperty("user.home");
5✔
205
    if (userHomeProperty != null) {
2!
206
      this.userHome = Path.of(userHomeProperty);
×
207
    }
208
    if (workingDirectory == null) {
2!
209
      workingDirectory = Path.of(System.getProperty("user.dir"));
×
210
    }
211
    workingDirectory = workingDirectory.toAbsolutePath();
3✔
212
    if (Files.isDirectory(workingDirectory)) {
5✔
213
      workingDirectory = this.fileAccess.toCanonicalPath(workingDirectory);
6✔
214
    } else {
215
      LOG.warn("Current working directory does not exist: {}", workingDirectory);
4✔
216
    }
217
    this.cwd = workingDirectory;
3✔
218
    // detect IDE_HOME and WORKSPACE
219
    String workspace = null;
2✔
220
    Path ideHomeDir = null;
2✔
221
    IdeHomeAndWorkspace ideHomeAndWorkspace = findIdeHome(workingDirectory);
4✔
222
    if (ideHomeAndWorkspace != null) {
2!
223
      ideHomeDir = ideHomeAndWorkspace.home();
3✔
224
      workspace = ideHomeAndWorkspace.workspace();
3✔
225
    }
226

227
    // detection completed, initializing variables
228
    this.ideRoot = findIdeRoot(ideHomeDir);
5✔
229

230
    setCwd(workingDirectory, workspace, ideHomeDir);
5✔
231

232
    if (this.ideRoot != null) {
3✔
233
      Path tempDownloadPath = getTempDownloadPath();
3✔
234
      if (Files.isDirectory(tempDownloadPath)) {
6✔
235
        // TODO delete all files older than 1 day here...
236
      } else {
237
        this.fileAccess.mkdirs(tempDownloadPath);
4✔
238
      }
239
    }
240
    this.defaultToolRepository = new DefaultToolRepository(this);
6✔
241
  }
1✔
242

243
  /**
244
   * 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
245
   * add additional validation or boundary checks.
246
   *
247
   * @param workingDirectory the starting directory for the search.
248
   * @return an instance of {@link IdeHomeAndWorkspace} where the IDE_HOME was found or {@code null} if not found.
249
   */
250
  protected IdeHomeAndWorkspace findIdeHome(Path workingDirectory) {
251

252
    Path currentDir = workingDirectory;
2✔
253
    String name1 = "";
2✔
254
    String name2 = "";
2✔
255
    String workspace = WORKSPACE_MAIN;
2✔
256
    Path ideRootPath = getIdeRootPathFromEnv(false);
4✔
257

258
    while (currentDir != null) {
2✔
259
      LOG.trace("Looking for IDE_HOME in {}", currentDir);
4✔
260
      if (isIdeHome(currentDir)) {
4✔
261
        if (FOLDER_WORKSPACES.equals(name1) && !name2.isEmpty()) {
7✔
262
          workspace = name2;
3✔
263
        }
264
        break;
265
      }
266
      name2 = name1;
2✔
267
      int nameCount = currentDir.getNameCount();
3✔
268
      if (nameCount >= 1) {
3✔
269
        name1 = currentDir.getName(nameCount - 1).toString();
7✔
270
      }
271
      currentDir = currentDir.getParent();
3✔
272
      if ((ideRootPath != null) && (ideRootPath.equals(currentDir))) {
2!
273
        // prevent that during tests we traverse to the real IDE project of IDEasy developer
274
        currentDir = null;
×
275
      }
276
    }
1✔
277

278
    return new IdeHomeAndWorkspace(currentDir, workspace);
6✔
279
  }
280

281
  /**
282
   * @return a new {@link MvnRepository}
283
   */
284
  protected MvnRepository createMvnRepository() {
285
    return new MvnRepository(this);
5✔
286
  }
287

288
  /**
289
   * @return a new {@link NpmRepository}
290
   */
291
  protected NpmRepository createNpmRepository() {
292
    return new NpmRepository(this);
×
293
  }
294

295
  /**
296
   * @return a new {@link PipRepository}
297
   */
298
  protected PipRepository createPipRepository() {
299
    return new PipRepository(this);
×
300
  }
301

302
  /**
303
   * @return a new {@link UvRepository}
304
   */
305
  protected UvRepository createUvRepository() {
306
    return new UvRepository(this);
×
307
  }
308

309
  private Path findIdeRoot(Path ideHomePath) {
310

311
    Path ideRootPath = null;
2✔
312
    if (ideHomePath != null) {
2✔
313
      Path ideRootPathFromEnv = getIdeRootPathFromEnv(true);
4✔
314
      ideRootPath = ideHomePath.getParent();
3✔
315
      if ((ideRootPathFromEnv != null) && !ideRootPath.toString().equals(ideRootPathFromEnv.toString())) {
2!
316
        LOG.warn(
×
317
            "Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.\n"
318
                + "Please check your 'user.dir' or working directory setting and make sure that it matches your IDE_ROOT variable.",
319
            ideRootPathFromEnv,
320
            ideHomePath.getFileName(), ideRootPath);
×
321
      }
322
    } else if (!isTest()) {
4!
323
      ideRootPath = getIdeRootPathFromEnv(true);
×
324
    }
325
    return ideRootPath;
2✔
326
  }
327

328
  /**
329
   * @return the {@link #getIdeRoot() IDE_ROOT} from the system environment.
330
   */
331
  protected Path getIdeRootPathFromEnv(boolean withSanityCheck) {
332

333
    String root = getSystem().getEnv(IdeVariables.IDE_ROOT.getName());
×
334
    if (root != null) {
×
335
      Path rootPath = Path.of(root);
×
336
      if (Files.isDirectory(rootPath)) {
×
337
        Path absoluteRootPath = getFileAccess().toCanonicalPath(rootPath);
×
338
        if (withSanityCheck) {
×
339
          int nameCount = rootPath.getNameCount();
×
340
          int absoluteNameCount = absoluteRootPath.getNameCount();
×
341
          int delta = absoluteNameCount - nameCount;
×
342
          if (delta >= 0) {
×
343
            for (int nameIndex = 0; nameIndex < nameCount; nameIndex++) {
×
344
              String rootName = rootPath.getName(nameIndex).toString();
×
345
              String absoluteRootName = absoluteRootPath.getName(nameIndex + delta).toString();
×
346
              if (!rootName.equals(absoluteRootName)) {
×
347
                LOG.warn("IDE_ROOT is set to {} but was expanded to absolute path {} and does not match for segment {} and {} - fix your IDEasy installation!",
×
348
                    rootPath, absoluteRootPath, rootName, absoluteRootName);
349
                break;
×
350
              }
351
            }
352
          } else {
353
            LOG.warn("IDE_ROOT is set to {} but was expanded to a shorter absolute path {}", rootPath,
×
354
                absoluteRootPath);
355
          }
356
        }
357
        return absoluteRootPath;
×
358
      } else if (withSanityCheck) {
×
359
        LOG.warn("IDE_ROOT is set to {} that is not an existing directory - fix your IDEasy installation!", rootPath);
×
360
      }
361
    }
362
    return null;
×
363
  }
364

365
  @Override
366
  public void setCwd(Path userDir, String workspace, Path ideHome) {
367

368
    this.cwd = userDir;
3✔
369
    this.workspaceName = workspace;
3✔
370
    this.ideHome = ideHome;
3✔
371
    if (ideHome == null) {
2✔
372
      this.workspacesBasePath = null;
3✔
373
      this.workspacePath = null;
3✔
374
      this.confPath = null;
3✔
375
      this.settingsPath = null;
3✔
376
      this.pluginsPath = null;
4✔
377
    } else {
378
      this.workspacesBasePath = this.ideHome.resolve(FOLDER_WORKSPACES);
6✔
379
      this.workspacePath = this.workspacesBasePath.resolve(this.workspaceName);
7✔
380
      this.confPath = this.ideHome.resolve(FOLDER_CONF);
6✔
381
      this.settingsPath = this.ideHome.resolve(FOLDER_SETTINGS);
6✔
382
      this.settingsCommitIdPath = this.ideHome.resolve(IdeContext.SETTINGS_COMMIT_ID);
6✔
383
      this.pluginsPath = this.ideHome.resolve(FOLDER_PLUGINS);
6✔
384
    }
385
    if (isTest()) {
3!
386
      // only for testing...
387
      if (this.ideHome == null) {
3✔
388
        this.userHome = Path.of("/non-existing-user-home-for-testing");
7✔
389
      } else {
390
        this.userHome = this.ideHome.resolve("home");
6✔
391
      }
392
    }
393
    this.userHomeIde = this.userHome.resolve(FOLDER_DOT_IDE);
6✔
394
    this.downloadPath = computeDownloadPath(this.userHome);
6✔
395
    resetPrivacyMap();
2✔
396
    this.path = computeSystemPath();
4✔
397
  }
1✔
398

399
  /**
400
   * On macOS, {@code ~/Downloads} is protected by the OS (TCC) and the CLI may not be allowed to delete it, so we put the cache under {@code ~/Library/Caches}
401
   * instead. Tests still use {@code ~/Downloads/ide} so existing fixtures keep working.
402
   */
403
  private Path computeDownloadPath(Path home) {
404

405
    if (!isTest() && this.systemInfo.isMac()) {
3!
406
      return home.resolve("Library/Caches/IDEasy/downloads");
×
407
    }
408
    return home.resolve("Downloads/ide");
4✔
409
  }
410

411
  private String getMessageIdeHomeFound() {
412

413
    String wks = this.workspaceName;
3✔
414
    if (isPrivacyMode() && !WORKSPACE_MAIN.equals(wks)) {
3!
415
      wks = "*".repeat(wks.length());
×
416
    }
417
    return "IDE environment variables have been set for " + formatArgument(this.ideHome) + " in workspace " + wks;
7✔
418
  }
419

420
  private String getMessageNotInsideIdeProject() {
421

422
    return "You are not inside an IDE project: " + formatArgument(this.cwd);
6✔
423
  }
424

425
  private String getMessageIdeRootNotFound() {
426

427
    String root = getSystem().getEnv("IDE_ROOT");
5✔
428
    if (root == null) {
2!
429
      return "The environment variable IDE_ROOT is undefined. Please reinstall IDEasy or manually repair IDE_ROOT variable.";
2✔
430
    } else {
431
      return "The environment variable IDE_ROOT is pointing to an invalid path " + formatArgument(root)
×
432
          + ". Please reinstall IDEasy or manually repair IDE_ROOT variable.";
433
    }
434
  }
435

436
  /**
437
   * @return {@code true} if this is a test context for JUnits, {@code false} otherwise.
438
   */
439
  public boolean isTest() {
440

441
    return false;
×
442
  }
443

444
  protected SystemPath computeSystemPath() {
445

446
    return new SystemPath(this);
×
447
  }
448

449
  /**
450
   * Checks if the given directory is a valid IDE home by verifying it contains both 'workspaces' and 'settings' directories.
451
   *
452
   * @param dir the directory to check.
453
   * @return {@code true} if the directory is a valid IDE home, {@code false} otherwise.
454
   */
455
  protected boolean isIdeHome(Path dir) {
456

457
    if (!Files.isDirectory(dir.resolve("workspaces"))) {
7✔
458
      return false;
2✔
459
    } else if (!Files.isDirectory(dir.resolve("settings"))) {
7!
460
      return false;
×
461
    }
462
    return true;
2✔
463
  }
464

465
  private EnvironmentVariables createVariables() {
466

467
    AbstractEnvironmentVariables system = createSystemVariables();
3✔
468
    AbstractEnvironmentVariables user = system.extend(this.userHomeIde, EnvironmentVariablesType.USER);
6✔
469
    AbstractEnvironmentVariables settings = user.extend(this.settingsPath, EnvironmentVariablesType.SETTINGS);
6✔
470
    AbstractEnvironmentVariables workspace = settings.extend(this.workspacePath, EnvironmentVariablesType.WORKSPACE);
6✔
471
    AbstractEnvironmentVariables conf = workspace.extend(this.confPath, EnvironmentVariablesType.CONF);
6✔
472
    return conf.resolved();
3✔
473
  }
474

475
  protected AbstractEnvironmentVariables createSystemVariables() {
476

477
    return EnvironmentVariables.ofSystem(this);
3✔
478
  }
479

480
  @Override
481
  public SystemInfo getSystemInfo() {
482

483
    return this.systemInfo;
3✔
484
  }
485

486
  @Override
487
  public FileAccess getFileAccess() {
488

489
    return this.fileAccess;
3✔
490
  }
491

492
  @Override
493
  public CommandletManager getCommandletManager() {
494

495
    return this.commandletManager;
3✔
496
  }
497

498
  @Override
499
  public ToolRepository getDefaultToolRepository() {
500

501
    return this.defaultToolRepository;
3✔
502
  }
503

504
  @Override
505
  public MvnRepository getMvnRepository() {
506
    if (this.mvnRepository == null) {
3✔
507
      this.mvnRepository = createMvnRepository();
4✔
508
    }
509
    return this.mvnRepository;
3✔
510
  }
511

512
  @Override
513
  public NpmRepository getNpmRepository() {
514
    if (this.npmRepository == null) {
3✔
515
      this.npmRepository = createNpmRepository();
4✔
516
    }
517
    return this.npmRepository;
3✔
518
  }
519

520
  @Override
521
  public PipRepository getPipRepository() {
522
    if (this.pipRepository == null) {
3✔
523
      this.pipRepository = createPipRepository();
4✔
524
    }
525
    return this.pipRepository;
3✔
526
  }
527

528
  @Override
529
  public UvRepository getUvRepository() {
530
    if (this.uvRepository == null) {
3✔
531
      this.uvRepository = createUvRepository();
4✔
532
    }
533
    return this.uvRepository;
3✔
534
  }
535

536
  @Override
537
  public CustomToolRepository getCustomToolRepository() {
538

539
    if (this.customToolRepository == null) {
3✔
540
      this.customToolRepository = CustomToolRepositoryImpl.of(this);
4✔
541
    }
542
    return this.customToolRepository;
3✔
543
  }
544

545
  @Override
546
  public Path getIdeHome() {
547

548
    return this.ideHome;
3✔
549
  }
550

551
  @Override
552
  public String getProjectName() {
553

554
    if (this.ideHome != null) {
3!
555
      return this.ideHome.getFileName().toString();
5✔
556
    }
557
    return "";
×
558
  }
559

560
  @Override
561
  public VersionIdentifier getProjectVersion() {
562

563
    if (this.ideHome != null) {
3!
564
      Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
565
      if (Files.exists(versionFile)) {
5✔
566
        String version = this.fileAccess.readFileContent(versionFile).trim();
6✔
567
        return VersionIdentifier.of(version);
3✔
568
      }
569
    }
570
    return IdeMigrator.START_VERSION;
2✔
571
  }
572

573
  @Override
574
  public void setProjectVersion(VersionIdentifier version) {
575

576
    if (this.ideHome == null) {
3!
577
      throw new IllegalStateException("IDE_HOME not available!");
×
578
    }
579
    Objects.requireNonNull(version);
3✔
580
    Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
581
    this.fileAccess.writeFileContent(version.toString(), versionFile);
6✔
582
  }
1✔
583

584
  @Override
585
  public Path getIdeRoot() {
586

587
    return this.ideRoot;
3✔
588
  }
589

590
  @Override
591
  public void setIdeRoot(Path ideRoot) {
592

593
    this.ideRoot = ideRoot;
3✔
594
  }
1✔
595

596
  @Override
597
  public Path getIdePath() {
598

599
    Path myIdeRoot = getIdeRoot();
3✔
600
    if (myIdeRoot == null) {
2✔
601
      return null;
2✔
602
    }
603
    return myIdeRoot.resolve(FOLDER_UNDERSCORE_IDE);
4✔
604
  }
605

606
  @Override
607
  public Path getCwd() {
608

609
    return this.cwd;
3✔
610
  }
611

612
  @Override
613
  public Path getTempPath() {
614

615
    Path idePath = getIdePath();
3✔
616
    if (idePath == null) {
2!
617
      return null;
×
618
    }
619
    return idePath.resolve("tmp");
4✔
620
  }
621

622
  @Override
623
  public Path getTempDownloadPath() {
624

625
    Path tmp = getTempPath();
3✔
626
    if (tmp == null) {
2!
627
      return null;
×
628
    }
629
    return tmp.resolve(FOLDER_DOWNLOADS);
4✔
630
  }
631

632
  @Override
633
  public Path getUserHome() {
634

635
    return this.userHome;
3✔
636
  }
637

638
  /**
639
   * This method should only be used for tests to mock user home.
640
   *
641
   * @param userHome the new value of {@link #getUserHome()}.
642
   */
643
  protected void setUserHome(Path userHome) {
644

645
    this.userHome = userHome;
3✔
646
    this.userHomeIde = userHome.resolve(FOLDER_DOT_IDE);
5✔
647
    this.downloadPath = computeDownloadPath(userHome);
5✔
648
    this.variables = null;
3✔
649
    resetPrivacyMap();
2✔
650
  }
1✔
651

652
  @Override
653
  public Path getUserHomeIde() {
654

655
    return this.userHomeIde;
3✔
656
  }
657

658
  @Override
659
  public Path getSettingsPath() {
660

661
    return this.settingsPath;
3✔
662
  }
663

664
  @Override
665
  public Path getSettingsGitRepository() {
666

667
    Path settingsPath = getSettingsPath();
3✔
668
    // check whether the settings path has a .git folder only if its not a symbolic link or junction
669
    if ((settingsPath != null) && !Files.exists(settingsPath.resolve(".git")) && !isSettingsCodeRepository()) {
12!
670
      LOG.error("Settings repository exists but is not a git repository.");
3✔
671
      return null;
2✔
672
    }
673
    return settingsPath;
2✔
674
  }
675

676
  @Override
677
  public boolean isSettingsCodeRepository() {
678

679
    Path settingsPath = getSettingsPath();
3✔
680
    if (settingsPath != null) {
2!
681
      boolean settingsIsLink = Files.isSymbolicLink(settingsPath) || getFileAccess().isJunction(settingsPath);
10!
682
      if (settingsIsLink) {
2!
683
        Path realPath = getFileAccess().toRealPath(this.settingsPath);
×
684
        if (realPath != null) {
×
685
          return getGitContext().isGitRepo(realPath.getParent());
×
686
        }
687
        return true;
×
688
      }
689
    }
690
    return false;
2✔
691
  }
692

693
  @Override
694
  public Path getSettingsCommitIdPath() {
695

696
    return this.settingsCommitIdPath;
3✔
697
  }
698

699
  @Override
700
  public Path getConfPath() {
701

702
    return this.confPath;
3✔
703
  }
704

705
  @Override
706
  public Path getSoftwarePath() {
707

708
    if (this.ideHome == null) {
3✔
709
      return null;
2✔
710
    }
711
    return this.ideHome.resolve(FOLDER_SOFTWARE);
5✔
712
  }
713

714
  @Override
715
  public Path getSoftwareExtraPath() {
716

717
    Path softwarePath = getSoftwarePath();
3✔
718
    if (softwarePath == null) {
2✔
719
      return null;
2✔
720
    }
721
    return softwarePath.resolve(FOLDER_EXTRA);
4✔
722
  }
723

724
  @Override
725
  public Path getSoftwareRepositoryPath() {
726

727
    Path idePath = getIdePath();
3✔
728
    if (idePath == null) {
2!
729
      return null;
×
730
    }
731
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
732
  }
733

734
  @Override
735
  public Path getPluginsPath() {
736

737
    return this.pluginsPath;
3✔
738
  }
739

740
  @Override
741
  public String getWorkspaceName() {
742

743
    return this.workspaceName;
3✔
744
  }
745

746
  @Override
747
  public Path getWorkspacesBasePath() {
748

749
    return this.workspacesBasePath;
3✔
750
  }
751

752
  @Override
753
  public Path getWorkspacePath() {
754

755
    return this.workspacePath;
3✔
756
  }
757

758
  @Override
759
  public Path getWorkspacePath(String workspace) {
760

761
    if (this.workspacesBasePath == null) {
3!
762
      throw new IllegalStateException("Failed to access workspace " + workspace + " without IDE_HOME in " + this.cwd);
×
763
    }
764
    return this.workspacesBasePath.resolve(workspace);
5✔
765
  }
766

767
  @Override
768
  public Path getDownloadPath() {
769

770
    return this.downloadPath;
3✔
771
  }
772

773
  @Override
774
  public Path getUrlsPath() {
775

776
    Path idePath = getIdePath();
3✔
777
    if (idePath == null) {
2!
778
      return null;
×
779
    }
780
    return idePath.resolve(FOLDER_URLS);
4✔
781
  }
782

783
  @Override
784
  public Path getToolRepositoryPath() {
785

786
    Path idePath = getIdePath();
3✔
787
    if (idePath == null) {
2!
788
      return null;
×
789
    }
790
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
791
  }
792

793
  @Override
794
  public SystemPath getPath() {
795

796
    return this.path;
3✔
797
  }
798

799
  @Override
800
  public EnvironmentVariables getVariables() {
801

802
    if (this.variables == null) {
3✔
803
      this.variables = createVariables();
4✔
804
    }
805
    return this.variables;
3✔
806
  }
807

808
  @Override
809
  public UrlMetadata getUrls() {
810

811
    if (this.urlMetadata == null) {
3✔
812
      if (!isTest()) {
3!
813
        getGitContext().pullOrCloneAndResetIfNeeded(IDE_URLS_GIT, getUrlsPath(), null);
×
814
      }
815
      this.urlMetadata = new UrlMetadata(this);
6✔
816
    }
817
    return this.urlMetadata;
3✔
818
  }
819

820
  @Override
821
  public boolean isQuietMode() {
822

823
    return this.startContext.isQuietMode();
4✔
824
  }
825

826
  @Override
827
  public boolean isBatchMode() {
828

829
    return this.startContext.isBatchMode();
4✔
830
  }
831

832
  @Override
833
  public boolean isForceMode() {
834

835
    return this.startContext.isForceMode();
4✔
836
  }
837

838
  @Override
839
  public boolean isForcePull() {
840

841
    return this.startContext.isForcePull();
4✔
842
  }
843

844
  @Override
845
  public boolean isForcePlugins() {
846

847
    return this.startContext.isForcePlugins();
4✔
848
  }
849

850
  @Override
851
  public boolean isForceRepositories() {
852

853
    return this.startContext.isForceRepositories();
4✔
854
  }
855

856
  @Override
857
  public boolean isOfflineMode() {
858

859
    return this.startContext.isOfflineMode();
4✔
860
  }
861

862
  @Override
863
  public boolean isPrivacyMode() {
864
    return this.startContext.isPrivacyMode();
4✔
865
  }
866

867
  @Override
868
  public boolean isSkipUpdatesMode() {
869

870
    return this.startContext.isSkipUpdatesMode();
4✔
871
  }
872

873
  @Override
874
  public boolean isNoColorsMode() {
875

876
    return this.startContext.isNoColorsMode();
×
877
  }
878

879
  @Override
880
  public NetworkStatus getNetworkStatus() {
881

882
    if (this.networkStatus == null) {
×
883
      this.networkStatus = new NetworkStatusImpl(this);
×
884
    }
885
    return this.networkStatus;
×
886
  }
887

888
  @Override
889
  public Locale getLocale() {
890

891
    Locale locale = this.startContext.getLocale();
4✔
892
    if (locale == null) {
2✔
893
      locale = Locale.getDefault();
2✔
894
    }
895
    return locale;
2✔
896
  }
897

898
  @Override
899
  public DirectoryMerger getWorkspaceMerger() {
900

901
    if (this.workspaceMerger == null) {
3✔
902
      this.workspaceMerger = new DirectoryMerger(this);
6✔
903
    }
904
    return this.workspaceMerger;
3✔
905
  }
906

907
  /**
908
   * @return the default execution directory in which a command process is executed.
909
   */
910
  @Override
911
  public Path getDefaultExecutionDirectory() {
912

913
    return this.defaultExecutionDirectory;
×
914
  }
915

916
  /**
917
   * @param defaultExecutionDirectory new value of {@link #getDefaultExecutionDirectory()}.
918
   */
919
  public void setDefaultExecutionDirectory(Path defaultExecutionDirectory) {
920

921
    if (defaultExecutionDirectory != null) {
×
922
      this.defaultExecutionDirectory = defaultExecutionDirectory;
×
923
    }
924
  }
×
925

926
  @Override
927
  public GitContext getGitContext() {
928

929
    return new GitContextImpl(this);
×
930
  }
931

932
  @Override
933
  public ProcessContext newProcess() {
934

935
    ProcessContext processContext = createProcessContext();
3✔
936
    if (this.defaultExecutionDirectory != null) {
3!
937
      processContext.directory(this.defaultExecutionDirectory);
×
938
    }
939
    return processContext;
2✔
940
  }
941

942
  @Override
943
  public IdeSystem getSystem() {
944

945
    if (this.system == null) {
×
946
      this.system = new IdeSystemImpl();
×
947
    }
948
    return this.system;
×
949
  }
950

951
  /**
952
   * @return a new instance of {@link ProcessContext}.
953
   * @see #newProcess()
954
   */
955
  protected ProcessContext createProcessContext() {
956

957
    return new ProcessContextImpl(this);
×
958
  }
959

960
  @Override
961
  public IdeLogLevel getLogLevelConsole() {
962

963
    return this.startContext.getLogLevelConsole();
4✔
964
  }
965

966
  @Override
967
  public IdeLogLevel getLogLevelLogger() {
968

969
    return this.startContext.getLogLevelLogger();
×
970
  }
971

972
  @Override
973
  public IdeLogListener getLogListener() {
974

975
    return this.startContext.getLogListener();
×
976
  }
977

978
  @Override
979
  public void logIdeHomeAndRootStatus() {
980
    if (this.ideRoot != null) {
3✔
981
      IdeLogLevel.SUCCESS.log(LOG, "IDE_ROOT is set to {}", this.ideRoot);
11✔
982
    }
983
    if (this.ideHome == null) {
3✔
984
      LOG.warn(getMessageNotInsideIdeProject());
5✔
985
    } else {
986
      IdeLogLevel.SUCCESS.log(LOG, "IDE_HOME is set to {}", this.ideHome);
11✔
987
    }
988
  }
1✔
989

990
  @Override
991
  public String formatArgument(Object argument) {
992

993
    if (argument == null) {
2✔
994
      return null;
2✔
995
    }
996
    String result = argument.toString();
3✔
997
    if (isPrivacyMode()) {
3✔
998
      if ((this.ideRoot != null) && this.privacyMap.isEmpty()) {
7!
999
        initializePrivacyMap(this.userHome, "~");
5✔
1000
        String projectName = getProjectName();
3✔
1001
        if (!projectName.isEmpty()) {
3!
1002
          this.privacyMap.put(projectName, "project");
6✔
1003
        }
1004
      }
1005
      for (Entry<String, String> entry : this.privacyMap.entrySet()) {
12✔
1006
        result = result.replace(entry.getKey(), entry.getValue());
9✔
1007
      }
1✔
1008
      result = PrivacyUtil.removeSensitivePathInformation(result);
3✔
1009
    }
1010
    return result;
2✔
1011
  }
1012

1013
  /**
1014
   * @param path the sensitive {@link Path} to
1015
   * @param replacement the replacement to mask the {@link Path} in log output.
1016
   */
1017
  protected void initializePrivacyMap(Path path, String replacement) {
1018

1019
    if (path == null) {
2!
1020
      return;
×
1021
    }
1022
    if (this.systemInfo.isWindows()) {
4!
1023
      this.privacyMap.put(WindowsPathSyntax.WINDOWS.format(path), replacement);
×
1024
      this.privacyMap.put(WindowsPathSyntax.MSYS.format(path), replacement);
×
1025
    } else {
1026
      this.privacyMap.put(path.toString(), replacement);
7✔
1027
    }
1028
  }
1✔
1029

1030
  /**
1031
   * Resets the privacy map in case fundamental values have changed.
1032
   */
1033
  private void resetPrivacyMap() {
1034

1035
    this.privacyMap.clear();
3✔
1036
  }
1✔
1037

1038

1039
  @Override
1040
  public String askForInput(String message, String defaultValue) {
1041

1042
    while (true) {
1043
      if (!message.isBlank()) {
3!
1044
        IdeLogLevel.INTERACTION.log(LOG, message);
4✔
1045
      }
1046
      if (isBatchMode()) {
3!
1047
        if (isForceMode()) {
×
1048
          return defaultValue;
×
1049
        } else {
1050
          throw new CliAbortException();
×
1051
        }
1052
      }
1053
      String input = readLine().trim();
4✔
1054
      if (!input.isEmpty()) {
3!
1055
        return input;
2✔
1056
      } else {
1057
        if (defaultValue != null) {
×
1058
          return defaultValue;
×
1059
        }
1060
      }
1061
    }
×
1062
  }
1063

1064
  @Override
1065
  public <O> O question(O[] options, String question, Object... args) {
1066

1067
    assert (options.length > 0);
4!
1068
    IdeLogLevel.INTERACTION.log(LOG, question, args);
5✔
1069
    LOG.warn(question, args);
4✔
1070
    return displayOptionsAndGetAnswer(options);
4✔
1071
  }
1072

1073
  private <O> O displayOptionsAndGetAnswer(O[] options) {
1074
    Map<String, O> mapping = new HashMap<>(options.length);
6✔
1075
    int i = 0;
2✔
1076
    for (O option : options) {
16✔
1077
      i++;
1✔
1078
      String title = "" + option;
4✔
1079
      String key = computeOptionKey(title);
3✔
1080
      addMapping(mapping, key, option);
4✔
1081
      String numericKey = Integer.toString(i);
3✔
1082
      if (numericKey.equals(key)) {
4!
1083
        LOG.trace("Options should not be numeric: {}", key);
×
1084
      } else {
1085
        addMapping(mapping, numericKey, option);
4✔
1086
      }
1087
      IdeLogLevel.INTERACTION.log(LOG, "Option {}: {}", numericKey, title);
14✔
1088
    }
1089
    if (options.length == 1) {
4✔
1090
      mapping.put("", options[0]);
7✔
1091
    }
1092
    O option = null;
2✔
1093
    if (isBatchMode()) {
3!
1094
      if (isForceMode()) {
×
1095
        option = options[0];
×
1096
        IdeLogLevel.INTERACTION.log(LOG, "" + option);
×
1097
      }
1098
    } else {
1099
      while (option == null) {
2✔
1100
        String answer = readLine();
3✔
1101
        option = mapping.get(answer);
4✔
1102
        if (option == null) {
2!
1103
          LOG.warn("Invalid answer: '{}' - please try again.", answer);
×
1104
        }
1105
      }
1✔
1106
    }
1107
    return option;
2✔
1108
  }
1109

1110
  private static String computeOptionKey(String option) {
1111
    String key = option;
2✔
1112
    int index = -1;
2✔
1113
    for (char c : OPTION_DETAILS_START.toCharArray()) {
17✔
1114
      int currentIndex = key.indexOf(c);
4✔
1115
      if (currentIndex != -1) {
3✔
1116
        if ((index == -1) || (currentIndex < index)) {
3!
1117
          index = currentIndex;
2✔
1118
        }
1119
      }
1120
    }
1121
    if (index > 0) {
2✔
1122
      key = key.substring(0, index).trim();
6✔
1123
    }
1124
    return key;
2✔
1125
  }
1126

1127
  /**
1128
   * @return the input from the end-user (e.g. read from the console).
1129
   */
1130
  protected abstract String readLine();
1131

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

1134
    O duplicate = mapping.put(key, option);
5✔
1135
    if (duplicate != null) {
2!
1136
      throw new IllegalArgumentException("Duplicated option " + key);
×
1137
    }
1138
  }
1✔
1139

1140
  @Override
1141
  public Step getCurrentStep() {
1142

1143
    return this.currentStep;
×
1144
  }
1145

1146
  @Override
1147
  public StepImpl newStep(boolean silent, String name, Object... parameters) {
1148

1149
    this.currentStep = new StepImpl(this, this.currentStep, name, silent, parameters);
11✔
1150
    return this.currentStep;
3✔
1151
  }
1152

1153
  /**
1154
   * Internal method to end the running {@link Step}.
1155
   *
1156
   * @param step the current {@link Step} to end.
1157
   */
1158
  public void endStep(StepImpl step) {
1159

1160
    if (step == this.currentStep) {
4!
1161
      this.currentStep = this.currentStep.getParent();
6✔
1162
    } else {
1163
      String currentStepName = "null";
×
1164
      if (this.currentStep != null) {
×
1165
        currentStepName = this.currentStep.getName();
×
1166
      }
1167
      LOG.warn("endStep called with wrong step '{}' but expected '{}'", step.getName(), currentStepName);
×
1168
    }
1169
  }
1✔
1170

1171
  /**
1172
   * Finds the matching {@link Commandlet} to run, applies {@link CliArguments} to its {@link Commandlet#getProperties() properties} and will execute it.
1173
   *
1174
   * @param arguments the {@link CliArgument}.
1175
   * @return the return code of the execution.
1176
   */
1177
  public int run(CliArguments arguments) {
1178

1179
    CliArgument current = arguments.current();
3✔
1180
    if (current.isStart()) {
3✔
1181
      arguments.next();
3✔
1182
      current = arguments.current();
3✔
1183
    }
1184
    assert (this.currentStep == null);
4!
1185
    boolean supressStepSuccess = false;
2✔
1186
    StepImpl step = newStep(true, "ide", (Object[]) current.asArray());
8✔
1187
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, null);
6✔
1188
    Commandlet cmd = null;
2✔
1189
    ValidationResult result = null;
2✔
1190
    try {
1191
      while (commandletIterator.hasNext()) {
3✔
1192
        cmd = commandletIterator.next();
4✔
1193
        result = applyAndRun(arguments.copy(), cmd);
6✔
1194
        if (result.isValid()) {
3✔
1195
          supressStepSuccess = cmd.isSuppressStepSuccess();
3✔
1196
          step.success();
2✔
1197
          return ProcessResult.SUCCESS;
4✔
1198
        }
1199
      }
1200
      activateLogging(cmd);
3✔
1201
      String commandKey = current.getKey();
3✔
1202

1203
      if (commandKey == null || commandKey.isBlank()) {
5!
1204
        return 0;
×
1205
      }
1206
      Commandlet commandletByName = this.commandletManager.getCommandlet(commandKey);
5✔
1207
      // Missing commandlet
1208
      if (commandletByName == null) {
2✔
1209
        if (getCliSuggester().isMissingCommandletHandled(commandKey, step)) {
6!
1210
          return 1;
4✔
1211
        }
1212
        return 0;
×
1213
      }
1214
      // Missing project context
1215
      if (getCliSuggester().isMissingProjectContextHandled(commandletByName, step)) {
6✔
1216
        return 1;
4✔
1217
      }
1218
      // Only validate options/arguments if same commandlet and proper type
1219
      if (cmd != commandletByName || !(result instanceof ValidationState validationState)) {
10!
1220
        return 0;
×
1221
      }
1222
      // Invalid option
1223
      if (getCliSuggester().isInvalidOptionHandled(validationState, commandletByName, step)) {
7✔
1224
        return 1;
4✔
1225
      }
1226
      // Invalid argument
1227
      if (getCliSuggester().isInvalidArgumentHandled(validationState, commandletByName)) {
6!
1228
        return 1;
4✔
1229
      }
1230
      LOG.error(result.getErrorMessage());
×
1231
      step.error("Invalid arguments: {}", current.getArgs());
×
1232
      IdeLogLevel.INTERACTION.log(LOG, "For additional details run ide help {}", cmd == null ? "" : cmd.getName());
×
1233
      return 1;
×
1234
    } catch (Throwable t) {
1✔
1235
      activateLogging(cmd);
3✔
1236
      step.error(t, true);
4✔
1237
      if (this.logfile != null) {
3!
1238
        System.err.println("Logfile can be found at " + this.logfile); // do not use logger
×
1239
      }
1240
      throw t;
2✔
1241
    } finally {
1242
      step.close();
2✔
1243
      assert (this.currentStep == null);
4!
1244
      step.logSummary(supressStepSuccess);
3✔
1245
    }
1246
  }
1247

1248

1249
  /**
1250
   * @return the {@link CliSuggester} for CLI suggestions.
1251
   */
1252
  private CliSuggester getCliSuggester() {
1253
    if (this.cliSuggester == null) {
3✔
1254
      this.cliSuggester = new CliSuggester(this);
6✔
1255
    }
1256
    return this.cliSuggester;
3✔
1257
  }
1258

1259
  /**
1260
   * Ensure the logging system is initialized.
1261
   */
1262
  private void activateLogging(Commandlet cmd) {
1263

1264
    configureJavaUtilLogging(cmd);
3✔
1265
    this.startContext.activateLogging();
3✔
1266
  }
1✔
1267

1268
  /**
1269
   * Configures the logging system (JUL).
1270
   *
1271
   * @param cmd the {@link Commandlet} to be called. May be {@code null}.
1272
   */
1273
  public void configureJavaUtilLogging(Commandlet cmd) {
1274

1275
    if (this.julConfigured) {
3✔
1276
      return;
1✔
1277
    }
1278
    boolean writeLogfile = isWriteLogfile(cmd);
4✔
1279
    this.startContext.setWriteLogfile(writeLogfile);
4✔
1280
    Properties properties = createJavaUtilLoggingProperties(writeLogfile, cmd);
5✔
1281
    try {
1282
      ByteArrayOutputStream out = new ByteArrayOutputStream(512);
5✔
1283
      properties.store(out, null);
4✔
1284
      out.flush();
2✔
1285
      ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
6✔
1286
      LogManager.getLogManager().readConfiguration(in);
3✔
1287
      this.julConfigured = true;
3✔
1288
      this.startContext.activateLogging();
3✔
1289
    } catch (IOException e) {
×
1290
      LOG.error("Failed to configure logging: {}", e.toString(), e);
×
1291
    }
1✔
1292
  }
1✔
1293

1294
  protected boolean isWriteLogfile(Commandlet cmd) {
1295
    if ((cmd == null) || !cmd.isWriteLogFile()) {
×
1296
      return false;
×
1297
    }
1298
    Boolean writeLogfile = IdeVariables.IDE_WRITE_LOGFILE.get(this);
×
1299
    return Boolean.TRUE.equals(writeLogfile);
×
1300
  }
1301

1302
  private Properties createJavaUtilLoggingProperties(boolean writeLogfile, Commandlet cmd) {
1303

1304
    Path idePath = getIdePath();
3✔
1305
    if (writeLogfile && (idePath == null)) {
2!
1306
      writeLogfile = false;
×
1307
      LOG.error("Cannot enable log-file since IDE_ROOT is undefined.");
×
1308
    }
1309
    Properties properties = new Properties();
4✔
1310
    // prevent 3rd party (e.g. java.lang.ProcessBuilder) logging into our console via JUL
1311
    // see JulLogLevel for the trick we did to workaround JUL flaws
1312
    properties.setProperty(".level", "SEVERE");
5✔
1313
    if (writeLogfile) {
2!
1314
      this.startContext.setLogLevelLogger(IdeLogLevel.TRACE);
×
1315
      String fileHandlerName = FileHandler.class.getName();
×
1316
      properties.setProperty("handlers", JulConsoleHandler.class.getName() + "," + fileHandlerName);
×
1317
      properties.setProperty(fileHandlerName + ".formatter", SimpleFormatter.class.getName());
×
1318
      properties.setProperty(fileHandlerName + ".encoding", "UTF-8");
×
1319
      this.logfile = createLogfilePath(idePath, cmd);
×
1320
      getFileAccess().mkdirs(this.logfile.getParent());
×
1321
      properties.setProperty(fileHandlerName + ".pattern", this.logfile.toString());
×
1322
    } else {
×
1323
      properties.setProperty("handlers", JulConsoleHandler.class.getName());
6✔
1324
    }
1325
    properties.setProperty(SimpleFormatter.class.getName() + ".format", "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL [%4$s] [%3$s] %5$s%6$s%n");
7✔
1326
    return properties;
2✔
1327
  }
1328

1329
  private Path createLogfilePath(Path idePath, Commandlet cmd) {
1330
    LocalDateTime now = LocalDateTime.now();
×
1331
    Path logsPath = idePath.resolve(FOLDER_LOGS).resolve(DateTimeUtil.formatDate(now, true));
×
1332
    StringBuilder sb = new StringBuilder(32);
×
1333
    if (this.ideHome == null || ((cmd != null) && !cmd.isIdeHomeRequired())) {
×
1334
      sb.append("_ide-");
×
1335
    } else {
1336
      sb.append(this.ideHome.getFileName().toString());
×
1337
      sb.append('-');
×
1338
    }
1339
    sb.append("ide-");
×
1340
    if (cmd != null) {
×
1341
      sb.append(cmd.getName());
×
1342
      sb.append('-');
×
1343
    }
1344
    sb.append(DateTimeUtil.formatTime(now));
×
1345
    sb.append(".log");
×
1346
    return logsPath.resolve(sb.toString());
×
1347
  }
1348

1349
  @Override
1350
  public void runWithoutLogging(Runnable lambda, IdeLogLevel threshold) {
1351

1352
    this.startContext.deactivateLogging(threshold);
4✔
1353
    lambda.run();
2✔
1354
    this.startContext.activateLogging();
3✔
1355
  }
1✔
1356

1357
  /**
1358
   * @param cmd the potential {@link Commandlet} to {@link #apply(CliArguments, Commandlet) apply} and {@link Commandlet#run() run}.
1359
   * @return {@code true} if the given {@link Commandlet} matched and did {@link Commandlet#run() run} successfully, {@code false} otherwise (the
1360
   *     {@link Commandlet} did not match and we have to try a different candidate).
1361
   */
1362
  private ValidationResult applyAndRun(CliArguments arguments, Commandlet cmd) {
1363

1364
    IdeLogLevel previousLogLevel = null;
2✔
1365
    cmd.reset();
2✔
1366
    ValidationResult result = apply(arguments, cmd);
5✔
1367
    if (result.isValid()) {
3✔
1368
      result = cmd.validate();
3✔
1369
    }
1370
    if (result.isValid()) {
3✔
1371
      LOG.debug("Running commandlet {}", cmd);
4✔
1372
      if (cmd.isIdeHomeRequired() && (this.ideHome == null)) {
6!
1373
        throw new CliException(getMessageNotInsideIdeProject(), ProcessResult.NO_IDE_HOME);
×
1374
      } else if (cmd.isIdeRootRequired() && (this.ideRoot == null)) {
6✔
1375
        throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
7✔
1376
      }
1377
      try {
1378
        if (cmd.isProcessableOutput()) {
3✔
1379
          if (!isLogLevelEnabled(IdeLogLevel.DEBUG)) {
4!
1380
            // unless --debug or --trace was supplied, processable output commandlets will disable all log-levels except INFO to prevent other logs interfere
1381
            previousLogLevel = this.startContext.setLogLevelConsole(IdeLogLevel.PROCESSABLE);
×
1382
          }
1383
        } else {
1384
          if (cmd.isIdeHomeRequired()) {
3✔
1385
            LOG.debug(getMessageIdeHomeFound());
4✔
1386
          }
1387
          if (!(cmd instanceof UpgradeCommandlet)) {
3!
1388
            verifyIdeMinVersion(false);
3✔
1389
          }
1390
          Path settingsRepository = getSettingsGitRepository();
3✔
1391
          if (settingsRepository != null) {
2!
1392
            if (getGitContext().isRepositoryUpdateAvailable(settingsRepository, getSettingsCommitIdPath()) || (
8✔
1393
                getGitContext().fetchIfNeeded(settingsRepository) && getGitContext().isRepositoryUpdateAvailable(
4!
1394
                    settingsRepository, getSettingsCommitIdPath()))) {
×
1395

1396
              // Inform the user that an update is available. The update message is suppressed if we are already running the update
1397
              String msg = determineSettingsUpdateMessage(cmd);
4✔
1398
              if (msg != null) {
2!
1399
                IdeLogLevel.INTERACTION.log(LOG, msg);
4✔
1400
              }
1401
            }
1402
          }
1403
        }
1404
        boolean success = ensureLicenseAgreement(cmd);
4✔
1405
        if (!success) {
2!
1406
          return ValidationResultValid.get();
×
1407
        }
1408
        cmd.run();
2✔
1409
      } finally {
1410
        if (previousLogLevel != null) {
2!
1411
          this.startContext.setLogLevelConsole(previousLogLevel);
×
1412
        }
1413
      }
1✔
1414
    } else {
1415
      LOG.trace("Commandlet did not match");
3✔
1416
    }
1417
    return result;
2✔
1418
  }
1419

1420

1421
  /**
1422
   * When an update is available for the settings repository, we log a message to the console, reminding the user to run {@code ide update}. This method
1423
   * determines the correct message to log, depending on whether the settings repository is a symlink/junction, or not. Should the user already be running the
1424
   * appropriate {@code ide update} command, the message is suppressed to avoid confusion.
1425
   *
1426
   * @param cmd the {@link Commandlet}.
1427
   * @return {@code msg} to log to the console. {@code null} if the message is suppressed.
1428
   */
1429
  private String determineSettingsUpdateMessage(Commandlet cmd) {
1430
    boolean update = cmd instanceof UpdateCommandlet;
3✔
1431
    if (isSettingsCodeRepository()) {
3!
1432
      if (update && (isForceMode() || isForcePull())) {
×
1433
        return null;
×
1434
      }
1435
      return "Updates are available for the settings repository. Please pull the latest changes by yourself or by calling \"ide -f update\" to apply them.";
×
1436
    } else {
1437
      if (update) {
2!
1438
        return null;
×
1439
      }
1440
      return "Updates are available for the settings repository. If you want to apply the latest changes, call \"ide update\"";
2✔
1441
    }
1442
  }
1443

1444
  private boolean ensureLicenseAgreement(Commandlet cmd) {
1445

1446
    if (isTest()) {
3!
1447
      return true; // ignore for tests
2✔
1448
    }
1449
    getFileAccess().mkdirs(this.userHomeIde);
×
1450
    Path licenseAgreement = this.userHomeIde.resolve(FILE_LICENSE_AGREEMENT);
×
1451
    if (Files.isRegularFile(licenseAgreement)) {
×
1452
      return true; // success, license already accepted
×
1453
    }
1454
    if (cmd instanceof EnvironmentCommandlet) {
×
1455
      // 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
1456
      // 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
1457
      // printing anything anymore in such case.
1458
      return false;
×
1459
    }
1460
    activateLogging(cmd);
×
1461
    IdeLogLevel oldLogLevel = this.startContext.getLogLevelConsole();
×
1462
    IdeLogLevel newLogLevel = oldLogLevel;
×
1463
    if (oldLogLevel.ordinal() > IdeLogLevel.INFO.ordinal()) {
×
1464
      newLogLevel = IdeLogLevel.INFO;
×
1465
      this.startContext.setLogLevelConsole(newLogLevel);
×
1466
    }
1467
    StringBuilder sb = new StringBuilder(1180);
×
1468
    sb.append(LOGO).append("""
×
1469
        Welcome to IDEasy!
1470
        This product (with its included 3rd party components) is open-source software and can be used free (also commercially).
1471
        It supports automatic download and installation of arbitrary 3rd party tools.
1472
        By default only open-source 3rd party tools are used (downloaded, installed, executed).
1473
        But if explicitly configured, also commercial software that requires an additional license may be used.
1474
        This happens e.g. if you configure "ultimate" edition of IntelliJ or "docker" edition of Docker (Docker Desktop).
1475
        You are solely responsible for all risks implied by using this software.
1476
        Before using IDEasy you need to read and accept the license agreement with all involved licenses.
1477
        You will be able to find it online under the following URL:
1478
        """).append(LICENSE_URL);
×
1479
    if (this.ideRoot != null) {
×
1480
      sb.append("\n\nAlso it is included in the documentation that you can find here:\n").
×
1481
          append(getIdePath().resolve("IDEasy.pdf").toString()).append("\n");
×
1482
    }
1483
    LOG.info(sb.toString());
×
1484
    askToContinue("Do you accept these terms of use and all license agreements?");
×
1485

1486
    sb.setLength(0);
×
1487
    LocalDateTime now = LocalDateTime.now();
×
1488
    sb.append("On ").append(DateTimeUtil.formatDate(now, false)).append(" at ").append(DateTimeUtil.formatTime(now))
×
1489
        .append(" you accepted the IDEasy license.\n").append(LICENSE_URL);
×
1490
    try {
1491
      Files.writeString(licenseAgreement, sb);
×
1492
    } catch (Exception e) {
×
1493
      throw new RuntimeException("Failed to save license agreement!", e);
×
1494
    }
×
1495
    if (oldLogLevel != newLogLevel) {
×
1496
      this.startContext.setLogLevelConsole(oldLogLevel);
×
1497
    }
1498
    return true;
×
1499
  }
1500

1501
  @Override
1502
  public void verifyIdeMinVersion(boolean throwException) {
1503
    VersionIdentifier minVersion = IDE_MIN_VERSION.get(this);
5✔
1504
    if (minVersion == null) {
2✔
1505
      return;
1✔
1506
    }
1507
    VersionIdentifier versionIdentifier = IdeVersion.getVersionIdentifier();
2✔
1508
    if (versionIdentifier.compareVersion(minVersion).isLess() && !IdeVersion.isUndefined()) {
7!
1509
      String warning = String.format("Your version of IDEasy is currently %s\n"
13✔
1510
          + "However, this is too old as your project requires at latest version %s", versionIdentifier, minVersion);
1511
      String interaction = "Please run the following command to update to the latest version of IDEasy and fix the problem:\n"
2✔
1512
          + "ide upgrade";
1513
      if (throwException) {
2✔
1514
        throw new CliException(warning + "\n" + interaction);
7✔
1515
      } else {
1516
        LOG.warn(warning);
3✔
1517
        IdeLogLevel.INTERACTION.log(LOG, interaction);
4✔
1518
      }
1519
    }
1520
  }
1✔
1521

1522
  /**
1523
   * @param arguments the {@link CliArguments#ofCompletion(String...) completion arguments}.
1524
   * @param includeContextOptions to include the options of {@link ContextCommandlet}.
1525
   * @return the {@link List} of {@link CompletionCandidate}s to suggest.
1526
   */
1527
  public List<CompletionCandidate> complete(CliArguments arguments, boolean includeContextOptions) {
1528

1529
    CompletionCandidateCollector collector = new CompletionCandidateCollectorDefault(this);
5✔
1530
    if (arguments.current().isStart()) {
4✔
1531
      arguments.next();
3✔
1532
    }
1533
    if (includeContextOptions) {
2✔
1534
      ContextCommandlet cc = new ContextCommandlet();
4✔
1535
      for (Property<?> property : cc.getProperties()) {
11✔
1536
        assert (property.isOption());
4!
1537
        property.apply(arguments, this, cc, collector);
7✔
1538
      }
1✔
1539
    }
1540
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, collector);
6✔
1541
    CliArgument current = arguments.current();
3✔
1542
    if (current.isCompletion() && current.isCombinedShortOption()) {
6✔
1543
      collector.add(current.get(), null, null, null);
7✔
1544
    }
1545
    arguments.next();
3✔
1546
    while (commandletIterator.hasNext()) {
3✔
1547
      Commandlet cmd = commandletIterator.next();
4✔
1548
      if (!arguments.current().isEnd()) {
4✔
1549
        completeCommandlet(arguments.copy(), cmd, collector);
6✔
1550
      }
1551
    }
1✔
1552
    return collector.getSortedCandidates();
3✔
1553
  }
1554

1555
  /**
1556
   * Gets the next value property and applies its implicit end-options behavior if required.
1557
   *
1558
   * @param valueIterator the iterator over the commandlet value properties
1559
   * @param arguments the CLI arguments whose option parsing state may be updated
1560
   * @return the next value property or {@code null} if no further value property exists
1561
   */
1562
  private Property<?> nextValueProperty(Iterator<Property<?>> valueIterator, CliArguments arguments) {
1563

1564
    if (!valueIterator.hasNext()) {
3✔
1565
      return null;
2✔
1566
    }
1567

1568
    Property<?> valueProperty = valueIterator.next();
4✔
1569
    if (valueProperty.isEndOptions()) {
3✔
1570
      // Tool argument properties should accept values starting with "-" so stop option parsing here
1571
      arguments.endOptions();
2✔
1572
    }
1573
    return valueProperty;
2✔
1574
  }
1575

1576
  private void completeCommandlet(CliArguments arguments, Commandlet cmd, CompletionCandidateCollector collector) {
1577

1578
    LOG.trace("Trying to match arguments for auto-completion for commandlet {}", cmd.getName());
5✔
1579
    Iterator<Property<?>> valueIterator = cmd.getValues().iterator();
4✔
1580
    valueIterator.next(); // skip first property since this is the keyword property that already matched to find the commandlet
3✔
1581
    Property<?> currentValueProperty = nextValueProperty(valueIterator, arguments);
5✔
1582
    List<Property<?>> properties = cmd.getProperties();
3✔
1583
    // we are creating our own list of options and remove them when matched to avoid duplicate suggestions
1584
    List<Property<?>> optionProperties = new ArrayList<>(properties.size());
6✔
1585
    for (Property<?> property : properties) {
10✔
1586
      if (property.isOption()) {
3✔
1587
        optionProperties.add(property);
4✔
1588
      }
1589
    }
1✔
1590
    CliArgument currentArgument = arguments.current();
3✔
1591
    while (!currentArgument.isEnd()) {
3✔
1592
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1593
      if (currentArgument.isOption() && !arguments.isEndOptions()) {
6✔
1594
        if (currentArgument.isCompletion()) {
3✔
1595
          Iterator<Property<?>> optionIterator = optionProperties.iterator();
3✔
1596
          while (optionIterator.hasNext()) {
3✔
1597
            Property<?> option = optionIterator.next();
4✔
1598
            boolean success = option.apply(arguments, this, cmd, collector);
7✔
1599
            if (success) {
2✔
1600
              optionIterator.remove();
2✔
1601
              arguments.next();
3✔
1602
            }
1603
          }
1✔
1604
        } else {
1✔
1605
          Property<?> option = cmd.getOption(currentArgument.get());
5✔
1606
          if (option != null) {
2✔
1607
            arguments.next();
3✔
1608
            boolean removed = optionProperties.remove(option);
4✔
1609
            if (!removed) {
2!
1610
              option = null;
×
1611
            }
1612
          }
1613
          if (option == null) {
2✔
1614
            LOG.trace("No such option was found.");
3✔
1615
            return;
1✔
1616
          }
1617
        }
1✔
1618
      } else {
1619
        if (currentValueProperty != null) {
2✔
1620
          boolean success = currentValueProperty.apply(arguments, this, cmd, collector);
7✔
1621
          if (!success) {
2✔
1622
            LOG.trace("Completion cannot match any further.");
3✔
1623
            return;
1✔
1624
          }
1625
          if (!currentValueProperty.isMultiValued()) {
3✔
1626
            currentValueProperty = nextValueProperty(valueIterator, arguments);
5✔
1627
          }
1628
        } else {
1✔
1629
          LOG.trace("No value left for completion.");
3✔
1630
          return;
1✔
1631
        }
1632
      }
1633
      currentArgument = arguments.current();
4✔
1634
    }
1635
  }
1✔
1636

1637
  /**
1638
   * @param arguments the {@link CliArguments} to apply. Will be {@link CliArguments#next() consumed} as they are matched. Consider passing a
1639
   *     {@link CliArguments#copy() copy} as needed.
1640
   * @param cmd the potential {@link Commandlet} to match.
1641
   * @return the {@link ValidationResult} telling if the {@link CliArguments} can be applied successfully or if validation errors ocurred.
1642
   */
1643
  public ValidationResult apply(CliArguments arguments, Commandlet cmd) {
1644

1645
    LOG.trace("Trying to match arguments to commandlet {}", cmd.getName());
5✔
1646
    CliArgument currentArgument = arguments.current();
3✔
1647
    Iterator<Property<?>> propertyIterator = cmd.getValues().iterator();
4✔
1648
    Property<?> property = null;
2✔
1649
    if (propertyIterator.hasNext()) {
3!
1650
      property = propertyIterator.next();
4✔
1651
    }
1652
    while (!currentArgument.isEnd()) {
3✔
1653
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1654
      Property<?> currentProperty = property;
2✔
1655
      if (!arguments.isEndOptions()) {
3✔
1656
        Property<?> option = cmd.getOption(currentArgument.getKey());
5✔
1657
        if (option != null) {
2✔
1658
          currentProperty = option;
3✔
1659
        } else {
1660
          boolean allowDashedValue = (property != null) && property.isValue()
6!
1661
              && (property.isMultiValued() || "-".equals(currentArgument.get()));
11!
1662
          boolean allowKeywordOption = (currentProperty instanceof KeywordProperty keywordProperty) && keywordProperty.matches(currentArgument.getKey());
15!
1663
          if (!allowDashedValue && !allowKeywordOption && currentArgument.isOption()) {
7✔
1664
            ValidationState state = new ValidationState(null);
5✔
1665
            state.addInvalidOption(currentArgument.getKey());
4✔
1666
            state.addErrorMessage("Invalid option \"" + currentArgument.getKey() + "\"");
5✔
1667
            return state;
2✔
1668
          }
1669
        }
1670
      }
1671
      if (currentProperty == null) {
2!
1672
        LOG.trace("No option or next value found");
×
1673
        ValidationState state = new ValidationState(null);
×
1674
        state.addErrorMessage("No matching property found");
×
1675
        return state;
×
1676
      }
1677
      LOG.trace("Next property candidate to match argument is {}", currentProperty);
4✔
1678
      if (currentProperty == property) {
3✔
1679
        if (!property.isMultiValued()) {
3✔
1680
          if (propertyIterator.hasNext()) {
3✔
1681
            property = propertyIterator.next();
5✔
1682
          } else {
1683
            property = null;
2✔
1684
          }
1685
        }
1686
        if ((property != null) && property.isValue() && property.isMultiValued()) {
8!
1687
          arguments.endOptions();
2✔
1688
        }
1689
      }
1690
      boolean matches = currentProperty.apply(arguments, this, cmd, null);
7✔
1691
      if (!matches) {
2✔
1692
        String invalidValue = currentProperty.getLastInvalidValue();
3✔
1693
        if (invalidValue != null) {
2!
1694
          ValidationState state = new ValidationState(null);
5✔
1695
          state.addInvalidArgument(invalidValue, currentProperty.getNameOrAlias());
5✔
1696
          state.addErrorMessage(
4✔
1697
              "Invalid CLI argument '" + invalidValue + "' for property '" + currentProperty.getNameOrAlias() + "' of commandlet '" + cmd.getName() + "'");
4✔
1698
          currentProperty.clearLastInvalidValue();
2✔
1699
          return state;
2✔
1700
        }
1701
        ValidationState state = new ValidationState(null);
×
1702
        state.addErrorMessage("No matching property found");
×
1703
        return state;
×
1704
      }
1705
      currentArgument = arguments.current();
3✔
1706
    }
1✔
1707
    return ValidationResultValid.get();
2✔
1708
  }
1709

1710
  @Override
1711
  public Path findBash() {
1712
    if (this.bash != null) {
3✔
1713
      return this.bash;
3✔
1714
    }
1715
    Path bashPath = findBashOnBashPath();
3✔
1716
    if (bashPath == null) {
2✔
1717
      bashPath = findBashInPath();
3✔
1718
      if (bashPath == null && (getSystemInfo().isWindows() || SystemInfoImpl.INSTANCE.isWindows())) {
6!
1719
        bashPath = findBashOnWindowsDefaultGitPath();
3✔
1720
        if (bashPath == null) {
2!
1721
          bashPath = findBashInWindowsRegistry();
3✔
1722
        }
1723
      }
1724
    }
1725
    if (bashPath == null) {
2✔
1726
      LOG.error("No bash executable could be found on your system.");
4✔
1727
    } else {
1728
      this.bash = bashPath;
3✔
1729
    }
1730
    return bashPath;
2✔
1731
  }
1732

1733
  private Path findBashOnBashPath() {
1734
    LOG.trace("Trying to find BASH_PATH environment variable.");
3✔
1735
    Path bash;
1736
    String bashPathVariableName = IdeVariables.BASH_PATH.getName();
3✔
1737
    String bashVariable = getVariables().get(bashPathVariableName);
5✔
1738
    if (bashVariable != null) {
2✔
1739
      bash = Path.of(bashVariable);
5✔
1740
      if (Files.exists(bash)) {
5✔
1741
        LOG.debug("{} environment variable was found and points to: {}", bashPathVariableName, bash);
5✔
1742
        return bash;
2✔
1743
      } else {
1744
        LOG.error("The environment variable {} points to a non existing file: {}", bashPathVariableName, bash);
5✔
1745
        return null;
2✔
1746
      }
1747
    } else {
1748
      LOG.debug("{} environment variable was not found", bashPathVariableName);
4✔
1749
      return null;
2✔
1750
    }
1751
  }
1752

1753
  /**
1754
   * @param path the path to check.
1755
   * @param toIgnore the String sequence which needs to be checked and ignored.
1756
   * @return {@code true} if the sequence to ignore was not found, {@code false} if the path contained the sequence to ignore.
1757
   */
1758
  private boolean checkPathToIgnoreLowercase(Path path, String toIgnore) {
1759
    String s = path.toAbsolutePath().toString().toLowerCase(Locale.ROOT);
6✔
1760
    return !s.contains(toIgnore);
7!
1761
  }
1762

1763
  /**
1764
   * Tries to find the bash.exe within the PATH environment variable.
1765
   *
1766
   * @return Path to bash.exe if found in PATH environment variable, {@code null} if bash.exe was not found.
1767
   */
1768
  private Path findBashInPath() {
1769
    LOG.trace("Trying to find bash in PATH environment variable.");
3✔
1770
    Path bash;
1771
    String pathVariableName = IdeVariables.PATH.getName();
3✔
1772
    if (pathVariableName != null) {
2!
1773
      Path plainBash = Path.of(BASH);
5✔
1774
      Predicate<Path> pathsToIgnore = p -> checkPathToIgnoreLowercase(p, "\\appdata\\local\\microsoft\\windowsapps") && checkPathToIgnoreLowercase(p,
16!
1775
          "\\windows\\system32");
1776
      Path bashPath = getPath().findBinary(plainBash, pathsToIgnore);
6✔
1777
      bash = bashPath.toAbsolutePath();
3✔
1778
      if (bashPath.equals(plainBash)) {
4✔
1779
        LOG.warn("No usable bash executable was found in your PATH environment variable!");
3✔
1780
        bash = null;
3✔
1781
      } else {
1782
        if (Files.exists(bashPath)) {
5!
1783
          LOG.debug("A proper bash executable was found in your PATH environment variable at: {}", bash);
5✔
1784
        } else {
1785
          bash = null;
×
1786
          LOG.error("A path to a bash executable was found in your PATH environment variable at: {} but the file is not existing.", bash);
×
1787
        }
1788
      }
1789
    } else {
1✔
1790
      bash = null;
×
1791
      // this should never happen...
1792
      LOG.error("PATH environment variable was not found");
×
1793
    }
1794
    return bash;
2✔
1795
  }
1796

1797
  /**
1798
   * Tries to find the bash.exe within the Windows registry.
1799
   *
1800
   * @return Path to bash.exe if found in registry, {@code null} if bash.exe was found.
1801
   */
1802
  protected Path findBashInWindowsRegistry() {
1803
    LOG.trace("Trying to find bash in Windows registry");
×
1804
    // If not found in the default location, try the registry query
1805
    String[] bashVariants = { "GitForWindows", "Cygwin\\setup" };
×
1806
    String[] registryKeys = { "HKEY_LOCAL_MACHINE", "HKEY_CURRENT_USER" };
×
1807
    for (String bashVariant : bashVariants) {
×
1808
      LOG.trace("Trying to find bash variant: {}", bashVariant);
×
1809
      for (String registryKey : registryKeys) {
×
1810
        LOG.trace("Trying to find bash from registry key: {}", registryKey);
×
1811
        String toolValueName = ("GitForWindows".equals(bashVariant)) ? "InstallPath" : "rootdir";
×
1812
        String registryPath = registryKey + "\\Software\\" + bashVariant;
×
1813

1814
        String path = getWindowsHelper().getRegistryValue(registryPath, toolValueName);
×
1815
        if (path != null) {
×
1816
          Path bashPath = Path.of(path + "\\bin\\bash.exe");
×
1817
          if (Files.exists(bashPath)) {
×
1818
            LOG.debug("Found bash at: {}", bashPath);
×
1819
            return bashPath;
×
1820
          } else {
1821
            LOG.error("Found bash at: {} but it is not pointing to an existing file", bashPath);
×
1822
            return null;
×
1823
          }
1824
        } else {
1825
          LOG.info("No bash executable could be found in the Windows registry.");
×
1826
        }
1827
      }
1828
    }
1829
    // no bash found
1830
    return null;
×
1831
  }
1832

1833
  private Path findBashOnWindowsDefaultGitPath() {
1834
    // Check if Git Bash exists in the default location
1835
    LOG.trace("Trying to find bash on the Windows default git path.");
3✔
1836
    Path defaultPath = Path.of(getDefaultWindowsGitPath());
6✔
1837
    if (!defaultPath.toString().isEmpty() && Files.exists(defaultPath)) {
4!
1838
      LOG.trace("Found default path to git bash on Windows at: {}", getDefaultWindowsGitPath());
×
1839
      return defaultPath;
×
1840
    }
1841
    LOG.debug("No bash was found on the Windows default git path.");
3✔
1842
    return null;
2✔
1843
  }
1844

1845
  @Override
1846
  public WindowsPathSyntax getPathSyntax() {
1847

1848
    return this.pathSyntax;
3✔
1849
  }
1850

1851
  /**
1852
   * @param pathSyntax new value of {@link #getPathSyntax()}.
1853
   */
1854
  public void setPathSyntax(WindowsPathSyntax pathSyntax) {
1855

1856
    this.pathSyntax = pathSyntax;
3✔
1857
  }
1✔
1858

1859
  /**
1860
   * @return the {@link IdeStartContextImpl}.
1861
   */
1862
  public IdeStartContextImpl getStartContext() {
1863

1864
    return startContext;
3✔
1865
  }
1866

1867
  /**
1868
   * @return the {@link WindowsHelper}.
1869
   */
1870
  public final WindowsHelper getWindowsHelper() {
1871

1872
    if (this.windowsHelper == null) {
3✔
1873
      this.windowsHelper = createWindowsHelper();
4✔
1874
    }
1875
    return this.windowsHelper;
3✔
1876
  }
1877

1878
  /**
1879
   * @return the new {@link WindowsHelper} instance.
1880
   */
1881
  protected WindowsHelper createWindowsHelper() {
1882

1883
    return new WindowsHelperImpl(this);
×
1884
  }
1885

1886
  /**
1887
   * Reloads this context and re-initializes the {@link #getVariables() variables}.
1888
   */
1889
  public void reload() {
1890

1891
    this.variables = null;
3✔
1892
    this.customToolRepository = null;
3✔
1893
  }
1✔
1894

1895
  @Override
1896
  public void writeVersionFile(VersionIdentifier version, Path installationPath) {
1897

1898
    assert (Files.isDirectory(installationPath));
6!
1899
    Path versionFile = installationPath.resolve(FILE_SOFTWARE_VERSION);
4✔
1900
    getFileAccess().writeFileContent(version.toString(), versionFile);
6✔
1901
  }
1✔
1902

1903
  /*
1904
   * @param home the IDE_HOME directory.
1905
   * @param workspace the name of the active workspace folder.
1906
   */
1907
  protected static record IdeHomeAndWorkspace(Path home, String workspace) {
9✔
1908

1909
  }
1910

1911
  /**
1912
   * Returns the default git path on Windows. Required to be overwritten in tests.
1913
   *
1914
   * @return default path to git on Windows.
1915
   */
1916
  public String getDefaultWindowsGitPath() {
1917
    return DEFAULT_WINDOWS_GIT_PATH;
×
1918
  }
1919

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