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

devonfw / IDEasy / 24235929298

10 Apr 2026 09:19AM UTC coverage: 70.466% (-0.004%) from 70.47%
24235929298

push

github

web-flow
#1760: accept empty input for one option (#1803)

4264 of 6690 branches covered (63.74%)

Branch coverage included in aggregate %.

11068 of 15068 relevant lines covered (73.45%)

3.1 hits per line

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

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

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

92
  static final Logger LOG = LoggerFactory.getLogger(AbstractIdeContext.class);
3✔
93

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

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

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

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

103
  private static final String OPTION_DETAILS_START = "([";
104

105
  private final IdeStartContextImpl startContext;
106

107
  private Path ideHome;
108

109
  private final Path ideRoot;
110

111
  private Path confPath;
112

113
  protected Path settingsPath;
114

115
  private Path settingsCommitIdPath;
116

117
  protected Path pluginsPath;
118

119
  private Path workspacePath;
120

121
  private Path workspacesBasePath;
122

123
  private String workspaceName;
124

125
  private Path cwd;
126

127
  private Path downloadPath;
128

129
  private Path userHome;
130

131
  private Path userHomeIde;
132

133
  private SystemPath path;
134

135
  private WindowsPathSyntax pathSyntax;
136

137
  private final SystemInfo systemInfo;
138

139
  private EnvironmentVariables variables;
140

141
  private final FileAccess fileAccess;
142

143
  protected CommandletManager commandletManager;
144

145
  protected ToolRepository defaultToolRepository;
146

147
  private CustomToolRepository customToolRepository;
148

149
  private MvnRepository mvnRepository;
150

151
  private NpmRepository npmRepository;
152

153
  private PipRepository pipRepository;
154

155
  private DirectoryMerger workspaceMerger;
156

157
  protected UrlMetadata urlMetadata;
158

159
  protected Path defaultExecutionDirectory;
160

161
  private StepImpl currentStep;
162

163
  private NetworkStatus networkStatus;
164

165
  protected IdeSystem system;
166

167
  private WindowsHelper windowsHelper;
168

169
  private final Map<String, String> privacyMap;
170

171
  private Path bash;
172

173
  private boolean julConfigured;
174

175
  private Path logfile;
176

177
  /**
178
   * The constructor.
179
   *
180
   * @param startContext the {@link IdeStartContextImpl}.
181
   * @param workingDirectory the optional {@link Path} to current working directory.
182
   */
183
  public AbstractIdeContext(IdeStartContextImpl startContext, Path workingDirectory) {
184

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

218
    // detection completed, initializing variables
219
    this.ideRoot = findIdeRoot(ideHomeDir);
5✔
220

221
    setCwd(workingDirectory, workspace, ideHomeDir);
5✔
222

223
    if (this.ideRoot != null) {
3✔
224
      Path tempDownloadPath = getTempDownloadPath();
3✔
225
      if (Files.isDirectory(tempDownloadPath)) {
6✔
226
        // TODO delete all files older than 1 day here...
227
      } else {
228
        this.fileAccess.mkdirs(tempDownloadPath);
4✔
229
      }
230
    }
231
    this.defaultToolRepository = new DefaultToolRepository(this);
6✔
232
  }
1✔
233

234
  /**
235
   * 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
236
   * add additional validation or boundary checks.
237
   *
238
   * @param workingDirectory the starting directory for the search.
239
   * @return an instance of {@link IdeHomeAndWorkspace} where the IDE_HOME was found or {@code null} if not found.
240
   */
241
  protected IdeHomeAndWorkspace findIdeHome(Path workingDirectory) {
242

243
    Path currentDir = workingDirectory;
2✔
244
    String name1 = "";
2✔
245
    String name2 = "";
2✔
246
    String workspace = WORKSPACE_MAIN;
2✔
247
    Path ideRootPath = getIdeRootPathFromEnv(false);
4✔
248

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

269
    return new IdeHomeAndWorkspace(currentDir, workspace);
6✔
270
  }
271

272
  /**
273
   * @return a new {@link MvnRepository}
274
   */
275
  protected MvnRepository createMvnRepository() {
276
    return new MvnRepository(this);
5✔
277
  }
278

279
  /**
280
   * @return a new {@link NpmRepository}
281
   */
282
  protected NpmRepository createNpmRepository() {
283
    return new NpmRepository(this);
×
284
  }
285

286
  /**
287
   * @return a new {@link PipRepository}
288
   */
289
  protected PipRepository createPipRepository() {
290
    return new PipRepository(this);
×
291
  }
292

293
  private Path findIdeRoot(Path ideHomePath) {
294

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

312
  /**
313
   * @return the {@link #getIdeRoot() IDE_ROOT} from the system environment.
314
   */
315
  protected Path getIdeRootPathFromEnv(boolean withSanityCheck) {
316

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

349
  @Override
350
  public void setCwd(Path userDir, String workspace, Path ideHome) {
351

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

383
  private String getMessageIdeHomeFound() {
384

385
    String wks = this.workspaceName;
3✔
386
    if (isPrivacyMode() && !WORKSPACE_MAIN.equals(wks)) {
3!
387
      wks = "*".repeat(wks.length());
×
388
    }
389
    return "IDE environment variables have been set for " + formatArgument(this.ideHome) + " in workspace " + wks;
7✔
390
  }
391

392
  private String getMessageNotInsideIdeProject() {
393

394
    return "You are not inside an IDE project: " + formatArgument(this.cwd);
6✔
395
  }
396

397
  private String getMessageIdeRootNotFound() {
398

399
    String root = getSystem().getEnv("IDE_ROOT");
5✔
400
    if (root == null) {
2!
401
      return "The environment variable IDE_ROOT is undefined. Please reinstall IDEasy or manually repair IDE_ROOT variable.";
2✔
402
    } else {
403
      return "The environment variable IDE_ROOT is pointing to an invalid path " + formatArgument(root)
×
404
          + ". Please reinstall IDEasy or manually repair IDE_ROOT variable.";
405
    }
406
  }
407

408
  /**
409
   * @return {@code true} if this is a test context for JUnits, {@code false} otherwise.
410
   */
411
  public boolean isTest() {
412

413
    return false;
×
414
  }
415

416
  protected SystemPath computeSystemPath() {
417

418
    return new SystemPath(this);
×
419
  }
420

421
  /**
422
   * Checks if the given directory is a valid IDE home by verifying it contains both 'workspaces' and 'settings' directories.
423
   *
424
   * @param dir the directory to check.
425
   * @return {@code true} if the directory is a valid IDE home, {@code false} otherwise.
426
   */
427
  protected boolean isIdeHome(Path dir) {
428

429
    if (!Files.isDirectory(dir.resolve("workspaces"))) {
7✔
430
      return false;
2✔
431
    } else if (!Files.isDirectory(dir.resolve("settings"))) {
7!
432
      return false;
×
433
    }
434
    return true;
2✔
435
  }
436

437
  private EnvironmentVariables createVariables() {
438

439
    AbstractEnvironmentVariables system = createSystemVariables();
3✔
440
    AbstractEnvironmentVariables user = system.extend(this.userHomeIde, EnvironmentVariablesType.USER);
6✔
441
    AbstractEnvironmentVariables settings = user.extend(this.settingsPath, EnvironmentVariablesType.SETTINGS);
6✔
442
    AbstractEnvironmentVariables workspace = settings.extend(this.workspacePath, EnvironmentVariablesType.WORKSPACE);
6✔
443
    AbstractEnvironmentVariables conf = workspace.extend(this.confPath, EnvironmentVariablesType.CONF);
6✔
444
    return conf.resolved();
3✔
445
  }
446

447
  protected AbstractEnvironmentVariables createSystemVariables() {
448

449
    return EnvironmentVariables.ofSystem(this);
3✔
450
  }
451

452
  @Override
453
  public SystemInfo getSystemInfo() {
454

455
    return this.systemInfo;
3✔
456
  }
457

458
  @Override
459
  public FileAccess getFileAccess() {
460

461
    return this.fileAccess;
3✔
462
  }
463

464
  @Override
465
  public CommandletManager getCommandletManager() {
466

467
    return this.commandletManager;
3✔
468
  }
469

470
  @Override
471
  public ToolRepository getDefaultToolRepository() {
472

473
    return this.defaultToolRepository;
3✔
474
  }
475

476
  @Override
477
  public MvnRepository getMvnRepository() {
478
    if (this.mvnRepository == null) {
3✔
479
      this.mvnRepository = createMvnRepository();
4✔
480
    }
481
    return this.mvnRepository;
3✔
482
  }
483

484
  @Override
485
  public NpmRepository getNpmRepository() {
486
    if (this.npmRepository == null) {
3✔
487
      this.npmRepository = createNpmRepository();
4✔
488
    }
489
    return this.npmRepository;
3✔
490
  }
491

492
  @Override
493
  public PipRepository getPipRepository() {
494
    if (this.pipRepository == null) {
3✔
495
      this.pipRepository = createPipRepository();
4✔
496
    }
497
    return this.pipRepository;
3✔
498
  }
499

500
  @Override
501
  public CustomToolRepository getCustomToolRepository() {
502

503
    if (this.customToolRepository == null) {
3!
504
      this.customToolRepository = CustomToolRepositoryImpl.of(this);
4✔
505
    }
506
    return this.customToolRepository;
3✔
507
  }
508

509
  @Override
510
  public Path getIdeHome() {
511

512
    return this.ideHome;
3✔
513
  }
514

515
  @Override
516
  public String getProjectName() {
517

518
    if (this.ideHome != null) {
3!
519
      return this.ideHome.getFileName().toString();
5✔
520
    }
521
    return "";
×
522
  }
523

524
  @Override
525
  public VersionIdentifier getProjectVersion() {
526

527
    if (this.ideHome != null) {
3!
528
      Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
529
      if (Files.exists(versionFile)) {
5✔
530
        String version = this.fileAccess.readFileContent(versionFile).trim();
6✔
531
        return VersionIdentifier.of(version);
3✔
532
      }
533
    }
534
    return IdeMigrator.START_VERSION;
2✔
535
  }
536

537
  @Override
538
  public void setProjectVersion(VersionIdentifier version) {
539

540
    if (this.ideHome == null) {
3!
541
      throw new IllegalStateException("IDE_HOME not available!");
×
542
    }
543
    Objects.requireNonNull(version);
3✔
544
    Path versionFile = this.ideHome.resolve(IdeContext.FILE_SOFTWARE_VERSION);
5✔
545
    this.fileAccess.writeFileContent(version.toString(), versionFile);
6✔
546
  }
1✔
547

548
  @Override
549
  public Path getIdeRoot() {
550

551
    return this.ideRoot;
3✔
552
  }
553

554
  @Override
555
  public Path getIdePath() {
556

557
    Path myIdeRoot = getIdeRoot();
3✔
558
    if (myIdeRoot == null) {
2✔
559
      return null;
2✔
560
    }
561
    return myIdeRoot.resolve(FOLDER_UNDERSCORE_IDE);
4✔
562
  }
563

564
  @Override
565
  public Path getCwd() {
566

567
    return this.cwd;
3✔
568
  }
569

570
  @Override
571
  public Path getTempPath() {
572

573
    Path idePath = getIdePath();
3✔
574
    if (idePath == null) {
2!
575
      return null;
×
576
    }
577
    return idePath.resolve("tmp");
4✔
578
  }
579

580
  @Override
581
  public Path getTempDownloadPath() {
582

583
    Path tmp = getTempPath();
3✔
584
    if (tmp == null) {
2!
585
      return null;
×
586
    }
587
    return tmp.resolve(FOLDER_DOWNLOADS);
4✔
588
  }
589

590
  @Override
591
  public Path getUserHome() {
592

593
    return this.userHome;
3✔
594
  }
595

596
  /**
597
   * This method should only be used for tests to mock user home.
598
   *
599
   * @param userHome the new value of {@link #getUserHome()}.
600
   */
601
  protected void setUserHome(Path userHome) {
602

603
    this.userHome = userHome;
3✔
604
    resetPrivacyMap();
2✔
605
  }
1✔
606

607
  @Override
608
  public Path getUserHomeIde() {
609

610
    return this.userHomeIde;
3✔
611
  }
612

613
  @Override
614
  public Path getSettingsPath() {
615

616
    return this.settingsPath;
3✔
617
  }
618

619
  @Override
620
  public Path getSettingsGitRepository() {
621

622
    Path settingsPath = getSettingsPath();
3✔
623
    // check whether the settings path has a .git folder only if its not a symbolic link or junction
624
    if ((settingsPath != null) && !Files.exists(settingsPath.resolve(".git")) && !isSettingsRepositorySymlinkOrJunction()) {
12!
625
      LOG.error("Settings repository exists but is not a git repository.");
3✔
626
      return null;
2✔
627
    }
628
    return settingsPath;
2✔
629
  }
630

631
  @Override
632
  public boolean isSettingsRepositorySymlinkOrJunction() {
633

634
    Path settingsPath = getSettingsPath();
3✔
635
    if (settingsPath == null) {
2!
636
      return false;
×
637
    }
638
    return Files.isSymbolicLink(settingsPath) || getFileAccess().isJunction(settingsPath);
10!
639
  }
640

641
  @Override
642
  public Path getSettingsCommitIdPath() {
643

644
    return this.settingsCommitIdPath;
3✔
645
  }
646

647
  @Override
648
  public Path getConfPath() {
649

650
    return this.confPath;
3✔
651
  }
652

653
  @Override
654
  public Path getSoftwarePath() {
655

656
    if (this.ideHome == null) {
3✔
657
      return null;
2✔
658
    }
659
    return this.ideHome.resolve(FOLDER_SOFTWARE);
5✔
660
  }
661

662
  @Override
663
  public Path getSoftwareExtraPath() {
664

665
    Path softwarePath = getSoftwarePath();
3✔
666
    if (softwarePath == null) {
2!
667
      return null;
×
668
    }
669
    return softwarePath.resolve(FOLDER_EXTRA);
4✔
670
  }
671

672
  @Override
673
  public Path getSoftwareRepositoryPath() {
674

675
    Path idePath = getIdePath();
3✔
676
    if (idePath == null) {
2!
677
      return null;
×
678
    }
679
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
680
  }
681

682
  @Override
683
  public Path getPluginsPath() {
684

685
    return this.pluginsPath;
3✔
686
  }
687

688
  @Override
689
  public String getWorkspaceName() {
690

691
    return this.workspaceName;
3✔
692
  }
693

694
  @Override
695
  public Path getWorkspacesBasePath() {
696

697
    return this.workspacesBasePath;
3✔
698
  }
699

700
  @Override
701
  public Path getWorkspacePath() {
702

703
    return this.workspacePath;
3✔
704
  }
705

706
  @Override
707
  public Path getWorkspacePath(String workspace) {
708

709
    if (this.workspacesBasePath == null) {
3!
710
      throw new IllegalStateException("Failed to access workspace " + workspace + " without IDE_HOME in " + this.cwd);
×
711
    }
712
    return this.workspacesBasePath.resolve(workspace);
5✔
713
  }
714

715
  @Override
716
  public Path getDownloadPath() {
717

718
    return this.downloadPath;
3✔
719
  }
720

721
  @Override
722
  public Path getUrlsPath() {
723

724
    Path idePath = getIdePath();
3✔
725
    if (idePath == null) {
2!
726
      return null;
×
727
    }
728
    return idePath.resolve(FOLDER_URLS);
4✔
729
  }
730

731
  @Override
732
  public Path getToolRepositoryPath() {
733

734
    Path idePath = getIdePath();
3✔
735
    if (idePath == null) {
2!
736
      return null;
×
737
    }
738
    return idePath.resolve(FOLDER_SOFTWARE);
4✔
739
  }
740

741
  @Override
742
  public SystemPath getPath() {
743

744
    return this.path;
3✔
745
  }
746

747
  @Override
748
  public EnvironmentVariables getVariables() {
749

750
    if (this.variables == null) {
3✔
751
      this.variables = createVariables();
4✔
752
    }
753
    return this.variables;
3✔
754
  }
755

756
  @Override
757
  public UrlMetadata getUrls() {
758

759
    if (this.urlMetadata == null) {
3✔
760
      if (!isTest()) {
3!
761
        getGitContext().pullOrCloneAndResetIfNeeded(IDE_URLS_GIT, getUrlsPath(), null);
×
762
      }
763
      this.urlMetadata = new UrlMetadata(this);
6✔
764
    }
765
    return this.urlMetadata;
3✔
766
  }
767

768
  @Override
769
  public boolean isQuietMode() {
770

771
    return this.startContext.isQuietMode();
4✔
772
  }
773

774
  @Override
775
  public boolean isBatchMode() {
776

777
    return this.startContext.isBatchMode();
4✔
778
  }
779

780
  @Override
781
  public boolean isForceMode() {
782

783
    return this.startContext.isForceMode();
4✔
784
  }
785

786
  @Override
787
  public boolean isForcePull() {
788

789
    return this.startContext.isForcePull();
4✔
790
  }
791

792
  @Override
793
  public boolean isForcePlugins() {
794

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

798
  @Override
799
  public boolean isForceRepositories() {
800

801
    return this.startContext.isForceRepositories();
4✔
802
  }
803

804
  @Override
805
  public boolean isOfflineMode() {
806

807
    return this.startContext.isOfflineMode();
4✔
808
  }
809

810
  @Override
811
  public boolean isPrivacyMode() {
812
    return this.startContext.isPrivacyMode();
4✔
813
  }
814

815
  @Override
816
  public boolean isSkipUpdatesMode() {
817

818
    return this.startContext.isSkipUpdatesMode();
4✔
819
  }
820

821
  @Override
822
  public boolean isNoColorsMode() {
823

824
    return this.startContext.isNoColorsMode();
×
825
  }
826

827
  @Override
828
  public NetworkStatus getNetworkStatus() {
829

830
    if (this.networkStatus == null) {
×
831
      this.networkStatus = new NetworkStatusImpl(this);
×
832
    }
833
    return this.networkStatus;
×
834
  }
835

836
  @Override
837
  public Locale getLocale() {
838

839
    Locale locale = this.startContext.getLocale();
4✔
840
    if (locale == null) {
2✔
841
      locale = Locale.getDefault();
2✔
842
    }
843
    return locale;
2✔
844
  }
845

846
  @Override
847
  public DirectoryMerger getWorkspaceMerger() {
848

849
    if (this.workspaceMerger == null) {
3✔
850
      this.workspaceMerger = new DirectoryMerger(this);
6✔
851
    }
852
    return this.workspaceMerger;
3✔
853
  }
854

855
  /**
856
   * @return the default execution directory in which a command process is executed.
857
   */
858
  @Override
859
  public Path getDefaultExecutionDirectory() {
860

861
    return this.defaultExecutionDirectory;
×
862
  }
863

864
  /**
865
   * @param defaultExecutionDirectory new value of {@link #getDefaultExecutionDirectory()}.
866
   */
867
  public void setDefaultExecutionDirectory(Path defaultExecutionDirectory) {
868

869
    if (defaultExecutionDirectory != null) {
×
870
      this.defaultExecutionDirectory = defaultExecutionDirectory;
×
871
    }
872
  }
×
873

874
  @Override
875
  public GitContext getGitContext() {
876

877
    return new GitContextImpl(this);
×
878
  }
879

880
  @Override
881
  public ProcessContext newProcess() {
882

883
    ProcessContext processContext = createProcessContext();
3✔
884
    if (this.defaultExecutionDirectory != null) {
3!
885
      processContext.directory(this.defaultExecutionDirectory);
×
886
    }
887
    return processContext;
2✔
888
  }
889

890
  @Override
891
  public IdeSystem getSystem() {
892

893
    if (this.system == null) {
×
894
      this.system = new IdeSystemImpl();
×
895
    }
896
    return this.system;
×
897
  }
898

899
  /**
900
   * @return a new instance of {@link ProcessContext}.
901
   * @see #newProcess()
902
   */
903
  protected ProcessContext createProcessContext() {
904

905
    return new ProcessContextImpl(this);
×
906
  }
907

908
  @Override
909
  public IdeLogLevel getLogLevelConsole() {
910

911
    return this.startContext.getLogLevelConsole();
×
912
  }
913

914
  @Override
915
  public IdeLogLevel getLogLevelLogger() {
916

917
    return this.startContext.getLogLevelLogger();
×
918
  }
919

920
  @Override
921
  public IdeLogListener getLogListener() {
922

923
    return this.startContext.getLogListener();
×
924
  }
925

926
  @Override
927
  public void logIdeHomeAndRootStatus() {
928
    if (this.ideRoot != null) {
3!
929
      IdeLogLevel.SUCCESS.log(LOG, "IDE_ROOT is set to {}", this.ideRoot);
×
930
    }
931
    if (this.ideHome == null) {
3✔
932
      LOG.warn(getMessageNotInsideIdeProject());
5✔
933
    } else {
934
      IdeLogLevel.SUCCESS.log(LOG, "IDE_HOME is set to {}", this.ideHome);
11✔
935
    }
936
  }
1✔
937

938
  @Override
939
  public String formatArgument(Object argument) {
940

941
    if (argument == null) {
2✔
942
      return null;
2✔
943
    }
944
    String result = argument.toString();
3✔
945
    if (isPrivacyMode()) {
3✔
946
      if ((this.ideRoot != null) && this.privacyMap.isEmpty()) {
3!
947
        initializePrivacyMap(this.userHome, "~");
×
948
        String projectName = getProjectName();
×
949
        if (!projectName.isEmpty()) {
×
950
          this.privacyMap.put(projectName, "project");
×
951
        }
952
      }
953
      for (Entry<String, String> entry : this.privacyMap.entrySet()) {
8!
954
        result = result.replace(entry.getKey(), entry.getValue());
×
955
      }
×
956
      result = PrivacyUtil.removeSensitivePathInformation(result);
3✔
957
    }
958
    return result;
2✔
959
  }
960

961
  /**
962
   * @param path the sensitive {@link Path} to
963
   * @param replacement the replacement to mask the {@link Path} in log output.
964
   */
965
  protected void initializePrivacyMap(Path path, String replacement) {
966

967
    if (path == null) {
×
968
      return;
×
969
    }
970
    if (this.systemInfo.isWindows()) {
×
971
      this.privacyMap.put(WindowsPathSyntax.WINDOWS.format(path), replacement);
×
972
      this.privacyMap.put(WindowsPathSyntax.MSYS.format(path), replacement);
×
973
    } else {
974
      this.privacyMap.put(path.toString(), replacement);
×
975
    }
976
  }
×
977

978
  /**
979
   * Resets the privacy map in case fundamental values have changed.
980
   */
981
  private void resetPrivacyMap() {
982

983
    this.privacyMap.clear();
3✔
984
  }
1✔
985

986

987
  @Override
988
  public String askForInput(String message, String defaultValue) {
989

990
    while (true) {
991
      if (!message.isBlank()) {
3!
992
        IdeLogLevel.INTERACTION.log(LOG, message);
4✔
993
      }
994
      if (isBatchMode()) {
3!
995
        if (isForceMode()) {
×
996
          return defaultValue;
×
997
        } else {
998
          throw new CliAbortException();
×
999
        }
1000
      }
1001
      String input = readLine().trim();
4✔
1002
      if (!input.isEmpty()) {
3!
1003
        return input;
2✔
1004
      } else {
1005
        if (defaultValue != null) {
×
1006
          return defaultValue;
×
1007
        }
1008
      }
1009
    }
×
1010
  }
1011

1012
  @Override
1013
  public <O> O question(O[] options, String question, Object... args) {
1014

1015
    assert (options.length > 0);
4!
1016
    IdeLogLevel.INTERACTION.log(LOG, question, args);
5✔
1017
    return displayOptionsAndGetAnswer(options);
4✔
1018
  }
1019

1020
  private <O> O displayOptionsAndGetAnswer(O[] options) {
1021
    Map<String, O> mapping = new HashMap<>(options.length);
6✔
1022
    int i = 0;
2✔
1023
    for (O option : options) {
16✔
1024
      i++;
1✔
1025
      String title = "" + option;
4✔
1026
      String key = computeOptionKey(title);
3✔
1027
      addMapping(mapping, key, option);
4✔
1028
      String numericKey = Integer.toString(i);
3✔
1029
      if (numericKey.equals(key)) {
4!
1030
        LOG.trace("Options should not be numeric: {}", key);
×
1031
      } else {
1032
        addMapping(mapping, numericKey, option);
4✔
1033
      }
1034
      IdeLogLevel.INTERACTION.log(LOG, "Option {}: {}", numericKey, title);
14✔
1035
    }
1036
    if (options.length == 1) {
4✔
1037
      mapping.put("", options[0]);
7✔
1038
    }
1039
    O option = null;
2✔
1040
    if (isBatchMode()) {
3!
1041
      if (isForceMode()) {
×
1042
        option = options[0];
×
1043
        IdeLogLevel.INTERACTION.log(LOG, "" + option);
×
1044
      }
1045
    } else {
1046
      while (option == null) {
2✔
1047
        String answer = readLine();
3✔
1048
        option = mapping.get(answer);
4✔
1049
        if (option == null) {
2!
1050
          LOG.warn("Invalid answer: '{}' - please try again.", answer);
×
1051
        }
1052
      }
1✔
1053
    }
1054
    return option;
2✔
1055
  }
1056

1057
  private static String computeOptionKey(String option) {
1058
    String key = option;
2✔
1059
    int index = -1;
2✔
1060
    for (char c : OPTION_DETAILS_START.toCharArray()) {
17✔
1061
      int currentIndex = key.indexOf(c);
4✔
1062
      if (currentIndex != -1) {
3✔
1063
        if ((index == -1) || (currentIndex < index)) {
3!
1064
          index = currentIndex;
2✔
1065
        }
1066
      }
1067
    }
1068
    if (index > 0) {
2✔
1069
      key = key.substring(0, index).trim();
6✔
1070
    }
1071
    return key;
2✔
1072
  }
1073

1074
  /**
1075
   * @return the input from the end-user (e.g. read from the console).
1076
   */
1077
  protected abstract String readLine();
1078

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

1081
    O duplicate = mapping.put(key, option);
5✔
1082
    if (duplicate != null) {
2!
1083
      throw new IllegalArgumentException("Duplicated option " + key);
×
1084
    }
1085
  }
1✔
1086

1087
  @Override
1088
  public Step getCurrentStep() {
1089

1090
    return this.currentStep;
×
1091
  }
1092

1093
  @Override
1094
  public StepImpl newStep(boolean silent, String name, Object... parameters) {
1095

1096
    this.currentStep = new StepImpl(this, this.currentStep, name, silent, parameters);
11✔
1097
    return this.currentStep;
3✔
1098
  }
1099

1100
  /**
1101
   * Internal method to end the running {@link Step}.
1102
   *
1103
   * @param step the current {@link Step} to end.
1104
   */
1105
  public void endStep(StepImpl step) {
1106

1107
    if (step == this.currentStep) {
4!
1108
      this.currentStep = this.currentStep.getParent();
6✔
1109
    } else {
1110
      String currentStepName = "null";
×
1111
      if (this.currentStep != null) {
×
1112
        currentStepName = this.currentStep.getName();
×
1113
      }
1114
      LOG.warn("endStep called with wrong step '{}' but expected '{}'", step.getName(), currentStepName);
×
1115
    }
1116
  }
1✔
1117

1118
  /**
1119
   * Finds the matching {@link Commandlet} to run, applies {@link CliArguments} to its {@link Commandlet#getProperties() properties} and will execute it.
1120
   *
1121
   * @param arguments the {@link CliArgument}.
1122
   * @return the return code of the execution.
1123
   */
1124
  public int run(CliArguments arguments) {
1125

1126
    CliArgument current = arguments.current();
3✔
1127
    assert (this.currentStep == null);
4!
1128
    boolean supressStepSuccess = false;
2✔
1129
    StepImpl step = newStep(true, "ide", (Object[]) current.asArray());
8✔
1130
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, null);
6✔
1131
    Commandlet cmd = null;
2✔
1132
    ValidationResult result = null;
2✔
1133
    try {
1134
      while (commandletIterator.hasNext()) {
3✔
1135
        cmd = commandletIterator.next();
4✔
1136
        result = applyAndRun(arguments.copy(), cmd);
6✔
1137
        if (result.isValid()) {
3!
1138
          supressStepSuccess = cmd.isSuppressStepSuccess();
3✔
1139
          step.success();
2✔
1140
          return ProcessResult.SUCCESS;
4✔
1141
        }
1142
      }
1143
      activateLogging(cmd);
3✔
1144
      verifyIdeMinVersion(false);
3✔
1145
      if (result != null) {
2!
1146
        LOG.error(result.getErrorMessage());
×
1147
      }
1148
      step.error("Invalid arguments: {}", current.getArgs());
10✔
1149
      IdeLogLevel.INTERACTION.log(LOG, "For additional details run ide help {}", cmd == null ? "" : cmd.getName());
13!
1150
      return 1;
4✔
1151
    } catch (Throwable t) {
1✔
1152
      activateLogging(cmd);
3✔
1153
      step.error(t, true);
4✔
1154
      if (this.logfile != null) {
3!
1155
        System.err.println("Logfile can be found at " + this.logfile); // do not use logger
×
1156
      }
1157
      throw t;
2✔
1158
    } finally {
1159
      step.close();
2✔
1160
      assert (this.currentStep == null);
4!
1161
      step.logSummary(supressStepSuccess);
3✔
1162
    }
1163
  }
1164

1165
  /**
1166
   * Ensure the logging system is initialized.
1167
   */
1168
  private void activateLogging(Commandlet cmd) {
1169

1170
    configureJavaUtilLogging(cmd);
3✔
1171
    this.startContext.activateLogging();
3✔
1172
  }
1✔
1173

1174
  /**
1175
   * Configures the logging system (JUL).
1176
   *
1177
   * @param cmd the {@link Commandlet} to be called. May be {@code null}.
1178
   */
1179
  public void configureJavaUtilLogging(Commandlet cmd) {
1180

1181
    if (this.julConfigured) {
3✔
1182
      return;
1✔
1183
    }
1184
    boolean writeLogfile = isWriteLogfile(cmd);
4✔
1185
    this.startContext.setWriteLogfile(writeLogfile);
4✔
1186
    Properties properties = createJavaUtilLoggingProperties(writeLogfile, cmd);
5✔
1187
    try {
1188
      ByteArrayOutputStream out = new ByteArrayOutputStream(512);
5✔
1189
      properties.store(out, null);
4✔
1190
      out.flush();
2✔
1191
      ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
6✔
1192
      LogManager.getLogManager().readConfiguration(in);
3✔
1193
      this.julConfigured = true;
3✔
1194
      this.startContext.activateLogging();
3✔
1195
    } catch (IOException e) {
×
1196
      LOG.error("Failed to configure logging: {}", e.toString(), e);
×
1197
    }
1✔
1198
  }
1✔
1199

1200
  protected boolean isWriteLogfile(Commandlet cmd) {
1201
    if ((cmd == null) || !cmd.isWriteLogFile()) {
×
1202
      return false;
×
1203
    }
1204
    Boolean writeLogfile = IdeVariables.IDE_WRITE_LOGFILE.get(this);
×
1205
    return Boolean.TRUE.equals(writeLogfile);
×
1206
  }
1207

1208
  private Properties createJavaUtilLoggingProperties(boolean writeLogfile, Commandlet cmd) {
1209

1210
    Path idePath = getIdePath();
3✔
1211
    if (writeLogfile && (idePath == null)) {
2!
1212
      writeLogfile = false;
×
1213
      LOG.error("Cannot enable log-file since IDE_ROOT is undefined.");
×
1214
    }
1215
    Properties properties = new Properties();
4✔
1216
    // prevent 3rd party (e.g. java.lang.ProcessBuilder) logging into our console via JUL
1217
    // see JulLogLevel for the trick we did to workaround JUL flaws
1218
    properties.setProperty(".level", "SEVERE");
5✔
1219
    if (writeLogfile) {
2!
1220
      this.startContext.setLogLevelLogger(IdeLogLevel.TRACE);
×
1221
      String fileHandlerName = FileHandler.class.getName();
×
1222
      properties.setProperty("handlers", JulConsoleHandler.class.getName() + "," + fileHandlerName);
×
1223
      properties.setProperty(fileHandlerName + ".formatter", SimpleFormatter.class.getName());
×
1224
      properties.setProperty(fileHandlerName + ".encoding", "UTF-8");
×
1225
      this.logfile = createLogfilePath(idePath, cmd);
×
1226
      getFileAccess().mkdirs(this.logfile.getParent());
×
1227
      properties.setProperty(fileHandlerName + ".pattern", this.logfile.toString());
×
1228
    } else {
×
1229
      properties.setProperty("handlers", JulConsoleHandler.class.getName());
6✔
1230
    }
1231
    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✔
1232
    return properties;
2✔
1233
  }
1234

1235
  private Path createLogfilePath(Path idePath, Commandlet cmd) {
1236
    LocalDateTime now = LocalDateTime.now();
×
1237
    Path logsPath = idePath.resolve(FOLDER_LOGS).resolve(DateTimeUtil.formatDate(now, true));
×
1238
    StringBuilder sb = new StringBuilder(32);
×
1239
    if (this.ideHome == null || ((cmd != null) && !cmd.isIdeHomeRequired())) {
×
1240
      sb.append("_ide-");
×
1241
    } else {
1242
      sb.append(this.ideHome.getFileName().toString());
×
1243
      sb.append('-');
×
1244
    }
1245
    sb.append("ide-");
×
1246
    if (cmd != null) {
×
1247
      sb.append(cmd.getName());
×
1248
      sb.append('-');
×
1249
    }
1250
    sb.append(DateTimeUtil.formatTime(now));
×
1251
    sb.append(".log");
×
1252
    return logsPath.resolve(sb.toString());
×
1253
  }
1254

1255
  @Override
1256
  public void runWithoutLogging(Runnable lambda, IdeLogLevel threshold) {
1257

1258
    this.startContext.deactivateLogging(threshold);
4✔
1259
    lambda.run();
2✔
1260
    this.startContext.activateLogging();
3✔
1261
  }
1✔
1262

1263
  /**
1264
   * @param cmd the potential {@link Commandlet} to {@link #apply(CliArguments, Commandlet) apply} and {@link Commandlet#run() run}.
1265
   * @return {@code true} if the given {@link Commandlet} matched and did {@link Commandlet#run() run} successfully, {@code false} otherwise (the
1266
   *     {@link Commandlet} did not match and we have to try a different candidate).
1267
   */
1268
  private ValidationResult applyAndRun(CliArguments arguments, Commandlet cmd) {
1269

1270
    IdeLogLevel previousLogLevel = null;
2✔
1271
    cmd.reset();
2✔
1272
    ValidationResult result = apply(arguments, cmd);
5✔
1273
    if (result.isValid()) {
3!
1274
      result = cmd.validate();
3✔
1275
    }
1276
    if (result.isValid()) {
3!
1277
      LOG.debug("Running commandlet {}", cmd);
4✔
1278
      if (cmd.isIdeHomeRequired() && (this.ideHome == null)) {
6!
1279
        throw new CliException(getMessageNotInsideIdeProject(), ProcessResult.NO_IDE_HOME);
×
1280
      } else if (cmd.isIdeRootRequired() && (this.ideRoot == null)) {
6!
1281
        throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
7✔
1282
      }
1283
      try {
1284
        if (cmd.isProcessableOutput()) {
3!
1285
          if (!LOG.isDebugEnabled()) {
×
1286
            // unless --debug or --trace was supplied, processable output commandlets will disable all log-levels except INFO to prevent other logs interfere
1287
            previousLogLevel = this.startContext.setLogLevelConsole(IdeLogLevel.PROCESSABLE);
×
1288
          }
1289
        } else {
1290
          if (cmd.isIdeHomeRequired()) {
3!
1291
            LOG.debug(getMessageIdeHomeFound());
4✔
1292
          }
1293
          Path settingsRepository = getSettingsGitRepository();
3✔
1294
          if (settingsRepository != null) {
2!
1295
            if (getGitContext().isRepositoryUpdateAvailable(settingsRepository, getSettingsCommitIdPath()) || (
×
1296
                getGitContext().fetchIfNeeded(settingsRepository) && getGitContext().isRepositoryUpdateAvailable(
×
1297
                    settingsRepository, getSettingsCommitIdPath()))) {
×
1298
              String msg;
1299
              if (isSettingsRepositorySymlinkOrJunction()) {
×
1300
                msg = "Updates are available for the settings repository. Please pull the latest changes by yourself or by calling \"ide -f update\" to apply them.";
×
1301
              } else {
1302
                msg = "Updates are available for the settings repository. If you want to apply the latest changes, call \"ide update\"";
×
1303
              }
1304
              IdeLogLevel.INTERACTION.log(LOG, msg);
×
1305
            }
1306
          }
1307
        }
1308
        boolean success = ensureLicenseAgreement(cmd);
4✔
1309
        if (!success) {
2!
1310
          return ValidationResultValid.get();
×
1311
        }
1312
        cmd.run();
2✔
1313
      } finally {
1314
        if (previousLogLevel != null) {
2!
1315
          this.startContext.setLogLevelConsole(previousLogLevel);
×
1316
        }
1317
      }
1✔
1318
    } else {
1319
      LOG.trace("Commandlet did not match");
×
1320
    }
1321
    return result;
2✔
1322
  }
1323

1324
  private boolean ensureLicenseAgreement(Commandlet cmd) {
1325

1326
    if (isTest()) {
3!
1327
      return true; // ignore for tests
2✔
1328
    }
1329
    getFileAccess().mkdirs(this.userHomeIde);
×
1330
    Path licenseAgreement = this.userHomeIde.resolve(FILE_LICENSE_AGREEMENT);
×
1331
    if (Files.isRegularFile(licenseAgreement)) {
×
1332
      return true; // success, license already accepted
×
1333
    }
1334
    if (cmd instanceof EnvironmentCommandlet) {
×
1335
      // 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
1336
      // 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
1337
      // printing anything anymore in such case.
1338
      return false;
×
1339
    }
1340
    activateLogging(cmd);
×
1341
    IdeLogLevel oldLogLevel = this.startContext.getLogLevelConsole();
×
1342
    IdeLogLevel newLogLevel = oldLogLevel;
×
1343
    if (oldLogLevel.ordinal() > IdeLogLevel.INFO.ordinal()) {
×
1344
      newLogLevel = IdeLogLevel.INFO;
×
1345
      this.startContext.setLogLevelConsole(newLogLevel);
×
1346
    }
1347
    StringBuilder sb = new StringBuilder(1180);
×
1348
    sb.append(LOGO).append("""
×
1349
        Welcome to IDEasy!
1350
        This product (with its included 3rd party components) is open-source software and can be used free (also commercially).
1351
        It supports automatic download and installation of arbitrary 3rd party tools.
1352
        By default only open-source 3rd party tools are used (downloaded, installed, executed).
1353
        But if explicitly configured, also commercial software that requires an additional license may be used.
1354
        This happens e.g. if you configure "ultimate" edition of IntelliJ or "docker" edition of Docker (Docker Desktop).
1355
        You are solely responsible for all risks implied by using this software.
1356
        Before using IDEasy you need to read and accept the license agreement with all involved licenses.
1357
        You will be able to find it online under the following URL:
1358
        """).append(LICENSE_URL);
×
1359
    if (this.ideRoot != null) {
×
1360
      sb.append("\n\nAlso it is included in the documentation that you can find here:\n").
×
1361
          append(getIdePath().resolve("IDEasy.pdf").toString()).append("\n");
×
1362
    }
1363
    LOG.info(sb.toString());
×
1364
    askToContinue("Do you accept these terms of use and all license agreements?");
×
1365

1366
    sb.setLength(0);
×
1367
    LocalDateTime now = LocalDateTime.now();
×
1368
    sb.append("On ").append(DateTimeUtil.formatDate(now, false)).append(" at ").append(DateTimeUtil.formatTime(now))
×
1369
        .append(" you accepted the IDEasy license.\n").append(LICENSE_URL);
×
1370
    try {
1371
      Files.writeString(licenseAgreement, sb);
×
1372
    } catch (Exception e) {
×
1373
      throw new RuntimeException("Failed to save license agreement!", e);
×
1374
    }
×
1375
    if (oldLogLevel != newLogLevel) {
×
1376
      this.startContext.setLogLevelConsole(oldLogLevel);
×
1377
    }
1378
    return true;
×
1379
  }
1380

1381
  @Override
1382
  public void verifyIdeMinVersion(boolean throwException) {
1383
    VersionIdentifier minVersion = IDE_MIN_VERSION.get(this);
5✔
1384
    if (minVersion == null) {
2✔
1385
      return;
1✔
1386
    }
1387
    VersionIdentifier versionIdentifier = IdeVersion.getVersionIdentifier();
2✔
1388
    if (versionIdentifier.compareVersion(minVersion).isLess() && !IdeVersion.isUndefined()) {
7!
1389
      String message = String.format("Your version of IDEasy is currently %s\n"
13✔
1390
          + "However, this is too old as your project requires at latest version %s\n"
1391
          + "Please run the following command to update to the latest version of IDEasy and fix the problem:\n"
1392
          + "ide upgrade", versionIdentifier, minVersion);
1393
      if (throwException) {
2✔
1394
        throw new CliException(message);
5✔
1395
      } else {
1396
        LOG.warn(message);
3✔
1397
      }
1398
    }
1399
  }
1✔
1400

1401
  /**
1402
   * @param arguments the {@link CliArguments#ofCompletion(String...) completion arguments}.
1403
   * @param includeContextOptions to include the options of {@link ContextCommandlet}.
1404
   * @return the {@link List} of {@link CompletionCandidate}s to suggest.
1405
   */
1406
  public List<CompletionCandidate> complete(CliArguments arguments, boolean includeContextOptions) {
1407

1408
    CompletionCandidateCollector collector = new CompletionCandidateCollectorDefault(this);
5✔
1409
    if (arguments.current().isStart()) {
4✔
1410
      arguments.next();
3✔
1411
    }
1412
    if (includeContextOptions) {
2✔
1413
      ContextCommandlet cc = new ContextCommandlet();
4✔
1414
      for (Property<?> property : cc.getProperties()) {
11✔
1415
        assert (property.isOption());
4!
1416
        property.apply(arguments, this, cc, collector);
7✔
1417
      }
1✔
1418
    }
1419
    Iterator<Commandlet> commandletIterator = this.commandletManager.findCommandlet(arguments, collector);
6✔
1420
    CliArgument current = arguments.current();
3✔
1421
    if (current.isCompletion() && current.isCombinedShortOption()) {
6✔
1422
      collector.add(current.get(), null, null, null);
7✔
1423
    }
1424
    arguments.next();
3✔
1425
    while (commandletIterator.hasNext()) {
3✔
1426
      Commandlet cmd = commandletIterator.next();
4✔
1427
      if (!arguments.current().isEnd()) {
4✔
1428
        completeCommandlet(arguments.copy(), cmd, collector);
6✔
1429
      }
1430
    }
1✔
1431
    return collector.getSortedCandidates();
3✔
1432
  }
1433

1434
  private void completeCommandlet(CliArguments arguments, Commandlet cmd, CompletionCandidateCollector collector) {
1435

1436
    LOG.trace("Trying to match arguments for auto-completion for commandlet {}", cmd.getName());
5✔
1437
    Iterator<Property<?>> valueIterator = cmd.getValues().iterator();
4✔
1438
    valueIterator.next(); // skip first property since this is the keyword property that already matched to find the commandlet
3✔
1439
    List<Property<?>> properties = cmd.getProperties();
3✔
1440
    // we are creating our own list of options and remove them when matched to avoid duplicate suggestions
1441
    List<Property<?>> optionProperties = new ArrayList<>(properties.size());
6✔
1442
    for (Property<?> property : properties) {
10✔
1443
      if (property.isOption()) {
3✔
1444
        optionProperties.add(property);
4✔
1445
      }
1446
    }
1✔
1447
    CliArgument currentArgument = arguments.current();
3✔
1448
    while (!currentArgument.isEnd()) {
3✔
1449
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1450
      if (currentArgument.isOption() && !arguments.isEndOptions()) {
6!
1451
        if (currentArgument.isCompletion()) {
3✔
1452
          Iterator<Property<?>> optionIterator = optionProperties.iterator();
3✔
1453
          while (optionIterator.hasNext()) {
3✔
1454
            Property<?> option = optionIterator.next();
4✔
1455
            boolean success = option.apply(arguments, this, cmd, collector);
7✔
1456
            if (success) {
2✔
1457
              optionIterator.remove();
2✔
1458
              arguments.next();
3✔
1459
            }
1460
          }
1✔
1461
        } else {
1✔
1462
          Property<?> option = cmd.getOption(currentArgument.get());
5✔
1463
          if (option != null) {
2✔
1464
            arguments.next();
3✔
1465
            boolean removed = optionProperties.remove(option);
4✔
1466
            if (!removed) {
2!
1467
              option = null;
×
1468
            }
1469
          }
1470
          if (option == null) {
2✔
1471
            LOG.trace("No such option was found.");
3✔
1472
            return;
1✔
1473
          }
1474
        }
1✔
1475
      } else {
1476
        if (valueIterator.hasNext()) {
3✔
1477
          Property<?> valueProperty = valueIterator.next();
4✔
1478
          boolean success = valueProperty.apply(arguments, this, cmd, collector);
7✔
1479
          if (!success) {
2✔
1480
            LOG.trace("Completion cannot match any further.");
3✔
1481
            return;
1✔
1482
          }
1483
        } else {
1✔
1484
          LOG.trace("No value left for completion.");
3✔
1485
          return;
1✔
1486
        }
1487
      }
1488
      currentArgument = arguments.current();
4✔
1489
    }
1490
  }
1✔
1491

1492
  /**
1493
   * @param arguments the {@link CliArguments} to apply. Will be {@link CliArguments#next() consumed} as they are matched. Consider passing a
1494
   *     {@link CliArguments#copy() copy} as needed.
1495
   * @param cmd the potential {@link Commandlet} to match.
1496
   * @return the {@link ValidationResult} telling if the {@link CliArguments} can be applied successfully or if validation errors ocurred.
1497
   */
1498
  public ValidationResult apply(CliArguments arguments, Commandlet cmd) {
1499

1500
    LOG.trace("Trying to match arguments to commandlet {}", cmd.getName());
5✔
1501
    CliArgument currentArgument = arguments.current();
3✔
1502
    Iterator<Property<?>> propertyIterator = cmd.getValues().iterator();
4✔
1503
    Property<?> property = null;
2✔
1504
    if (propertyIterator.hasNext()) {
3!
1505
      property = propertyIterator.next();
4✔
1506
    }
1507
    while (!currentArgument.isEnd()) {
3✔
1508
      LOG.trace("Trying to match argument '{}'", currentArgument);
4✔
1509
      Property<?> currentProperty = property;
2✔
1510
      if (!arguments.isEndOptions()) {
3!
1511
        Property<?> option = cmd.getOption(currentArgument.getKey());
5✔
1512
        if (option != null) {
2!
1513
          currentProperty = option;
×
1514
        }
1515
      }
1516
      if (currentProperty == null) {
2!
1517
        LOG.trace("No option or next value found");
×
1518
        ValidationState state = new ValidationState(null);
×
1519
        state.addErrorMessage("No matching property found");
×
1520
        return state;
×
1521
      }
1522
      LOG.trace("Next property candidate to match argument is {}", currentProperty);
4✔
1523
      if (currentProperty == property) {
3!
1524
        if (!property.isMultiValued()) {
3✔
1525
          if (propertyIterator.hasNext()) {
3✔
1526
            property = propertyIterator.next();
5✔
1527
          } else {
1528
            property = null;
2✔
1529
          }
1530
        }
1531
        if ((property != null) && property.isValue() && property.isMultiValued()) {
8!
1532
          arguments.stopSplitShortOptions();
2✔
1533
        }
1534
      }
1535
      boolean matches = currentProperty.apply(arguments, this, cmd, null);
7✔
1536
      if (!matches) {
2!
1537
        ValidationState state = new ValidationState(null);
×
1538
        state.addErrorMessage("No matching property found");
×
1539
        return state;
×
1540
      }
1541
      currentArgument = arguments.current();
3✔
1542
    }
1✔
1543
    return ValidationResultValid.get();
2✔
1544
  }
1545

1546
  @Override
1547
  public Path findBash() {
1548
    if (this.bash != null) {
3✔
1549
      return this.bash;
3✔
1550
    }
1551
    Path bashPath = findBashOnBashPath();
3✔
1552
    if (bashPath == null) {
2✔
1553
      bashPath = findBashInPath();
3✔
1554
      if (bashPath == null && (getSystemInfo().isWindows() || SystemInfoImpl.INSTANCE.isWindows())) {
6!
1555
        bashPath = findBashOnWindowsDefaultGitPath();
3✔
1556
        if (bashPath == null) {
2!
1557
          bashPath = findBashInWindowsRegistry();
3✔
1558
        }
1559
      }
1560
    }
1561
    if (bashPath == null) {
2✔
1562
      LOG.error("No bash executable could be found on your system.");
4✔
1563
    } else {
1564
      this.bash = bashPath;
3✔
1565
    }
1566
    return bashPath;
2✔
1567
  }
1568

1569
  private Path findBashOnBashPath() {
1570
    LOG.trace("Trying to find BASH_PATH environment variable.");
3✔
1571
    Path bash;
1572
    String bashPathVariableName = IdeVariables.BASH_PATH.getName();
3✔
1573
    String bashVariable = getVariables().get(bashPathVariableName);
5✔
1574
    if (bashVariable != null) {
2✔
1575
      bash = Path.of(bashVariable);
5✔
1576
      if (Files.exists(bash)) {
5✔
1577
        LOG.debug("{} environment variable was found and points to: {}", bashPathVariableName, bash);
5✔
1578
        return bash;
2✔
1579
      } else {
1580
        LOG.error("The environment variable {} points to a non existing file: {}", bashPathVariableName, bash);
5✔
1581
        return null;
2✔
1582
      }
1583
    } else {
1584
      LOG.debug("{} environment variable was not found", bashPathVariableName);
4✔
1585
      return null;
2✔
1586
    }
1587
  }
1588

1589
  /**
1590
   * @param path the path to check.
1591
   * @param toIgnore the String sequence which needs to be checked and ignored.
1592
   * @return {@code true} if the sequence to ignore was not found, {@code false} if the path contained the sequence to ignore.
1593
   */
1594
  private boolean checkPathToIgnoreLowercase(Path path, String toIgnore) {
1595
    String s = path.toAbsolutePath().toString().toLowerCase(Locale.ROOT);
6✔
1596
    return !s.contains(toIgnore);
7!
1597
  }
1598

1599
  /**
1600
   * Tries to find the bash.exe within the PATH environment variable.
1601
   *
1602
   * @return Path to bash.exe if found in PATH environment variable, {@code null} if bash.exe was not found.
1603
   */
1604
  private Path findBashInPath() {
1605
    LOG.trace("Trying to find bash in PATH environment variable.");
3✔
1606
    Path bash;
1607
    String pathVariableName = IdeVariables.PATH.getName();
3✔
1608
    if (pathVariableName != null) {
2!
1609
      Path plainBash = Path.of(BASH);
5✔
1610
      Predicate<Path> pathsToIgnore = p -> checkPathToIgnoreLowercase(p, "\\appdata\\local\\microsoft\\windowsapps") && checkPathToIgnoreLowercase(p,
16!
1611
          "\\windows\\system32");
1612
      Path bashPath = getPath().findBinary(plainBash, pathsToIgnore);
6✔
1613
      bash = bashPath.toAbsolutePath();
3✔
1614
      if (bashPath.equals(plainBash)) {
4✔
1615
        LOG.warn("No usable bash executable was found in your PATH environment variable!");
3✔
1616
        bash = null;
3✔
1617
      } else {
1618
        if (Files.exists(bashPath)) {
5!
1619
          LOG.debug("A proper bash executable was found in your PATH environment variable at: {}", bash);
5✔
1620
        } else {
1621
          bash = null;
×
1622
          LOG.error("A path to a bash executable was found in your PATH environment variable at: {} but the file is not existing.", bash);
×
1623
        }
1624
      }
1625
    } else {
1✔
1626
      bash = null;
×
1627
      // this should never happen...
1628
      LOG.error("PATH environment variable was not found");
×
1629
    }
1630
    return bash;
2✔
1631
  }
1632

1633
  /**
1634
   * Tries to find the bash.exe within the Windows registry.
1635
   *
1636
   * @return Path to bash.exe if found in registry, {@code null} if bash.exe was found.
1637
   */
1638
  protected Path findBashInWindowsRegistry() {
1639
    LOG.trace("Trying to find bash in Windows registry");
×
1640
    // If not found in the default location, try the registry query
1641
    String[] bashVariants = { "GitForWindows", "Cygwin\\setup" };
×
1642
    String[] registryKeys = { "HKEY_LOCAL_MACHINE", "HKEY_CURRENT_USER" };
×
1643
    for (String bashVariant : bashVariants) {
×
1644
      LOG.trace("Trying to find bash variant: {}", bashVariant);
×
1645
      for (String registryKey : registryKeys) {
×
1646
        LOG.trace("Trying to find bash from registry key: {}", registryKey);
×
1647
        String toolValueName = ("GitForWindows".equals(bashVariant)) ? "InstallPath" : "rootdir";
×
1648
        String registryPath = registryKey + "\\Software\\" + bashVariant;
×
1649

1650
        String path = getWindowsHelper().getRegistryValue(registryPath, toolValueName);
×
1651
        if (path != null) {
×
1652
          Path bashPath = Path.of(path + "\\bin\\bash.exe");
×
1653
          if (Files.exists(bashPath)) {
×
1654
            LOG.debug("Found bash at: {}", bashPath);
×
1655
            return bashPath;
×
1656
          } else {
1657
            LOG.error("Found bash at: {} but it is not pointing to an existing file", bashPath);
×
1658
            return null;
×
1659
          }
1660
        } else {
1661
          LOG.info("No bash executable could be found in the Windows registry.");
×
1662
        }
1663
      }
1664
    }
1665
    // no bash found
1666
    return null;
×
1667
  }
1668

1669
  private Path findBashOnWindowsDefaultGitPath() {
1670
    // Check if Git Bash exists in the default location
1671
    LOG.trace("Trying to find bash on the Windows default git path.");
3✔
1672
    Path defaultPath = Path.of(getDefaultWindowsGitPath());
6✔
1673
    if (!defaultPath.toString().isEmpty() && Files.exists(defaultPath)) {
4!
1674
      LOG.trace("Found default path to git bash on Windows at: {}", getDefaultWindowsGitPath());
×
1675
      return defaultPath;
×
1676
    }
1677
    LOG.debug("No bash was found on the Windows default git path.");
3✔
1678
    return null;
2✔
1679
  }
1680

1681
  @Override
1682
  public WindowsPathSyntax getPathSyntax() {
1683

1684
    return this.pathSyntax;
3✔
1685
  }
1686

1687
  /**
1688
   * @param pathSyntax new value of {@link #getPathSyntax()}.
1689
   */
1690
  public void setPathSyntax(WindowsPathSyntax pathSyntax) {
1691

1692
    this.pathSyntax = pathSyntax;
3✔
1693
  }
1✔
1694

1695
  /**
1696
   * @return the {@link IdeStartContextImpl}.
1697
   */
1698
  public IdeStartContextImpl getStartContext() {
1699

1700
    return startContext;
3✔
1701
  }
1702

1703
  /**
1704
   * @return the {@link WindowsHelper}.
1705
   */
1706
  public final WindowsHelper getWindowsHelper() {
1707

1708
    if (this.windowsHelper == null) {
3✔
1709
      this.windowsHelper = createWindowsHelper();
4✔
1710
    }
1711
    return this.windowsHelper;
3✔
1712
  }
1713

1714
  /**
1715
   * @return the new {@link WindowsHelper} instance.
1716
   */
1717
  protected WindowsHelper createWindowsHelper() {
1718

1719
    return new WindowsHelperImpl(this);
×
1720
  }
1721

1722
  /**
1723
   * Reloads this context and re-initializes the {@link #getVariables() variables}.
1724
   */
1725
  public void reload() {
1726

1727
    this.variables = null;
3✔
1728
    this.customToolRepository = null;
3✔
1729
  }
1✔
1730

1731
  @Override
1732
  public void writeVersionFile(VersionIdentifier version, Path installationPath) {
1733

1734
    assert (Files.isDirectory(installationPath));
6!
1735
    Path versionFile = installationPath.resolve(FILE_SOFTWARE_VERSION);
4✔
1736
    getFileAccess().writeFileContent(version.toString(), versionFile);
6✔
1737
  }
1✔
1738

1739
  /*
1740
   * @param home the IDE_HOME directory.
1741
   * @param workspace the name of the active workspace folder.
1742
   */
1743
  protected static record IdeHomeAndWorkspace(Path home, String workspace) {
9✔
1744

1745
  }
1746

1747
  /**
1748
   * Returns the default git path on Windows. Required to be overwritten in tests.
1749
   *
1750
   * @return default path to git on Windows.
1751
   */
1752
  public String getDefaultWindowsGitPath() {
1753
    return DEFAULT_WINDOWS_GIT_PATH;
×
1754
  }
1755

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