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

devonfw / IDEasy / 9907372175

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

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

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

31.58
cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java
1
package com.devonfw.tools.ide.process;
2

3
import java.nio.file.Path;
4
import java.util.List;
5
import java.util.Objects;
6

7
/**
8
 * Wrapper for {@link ProcessBuilder} to simplify its usage and avoid common mistakes and pitfalls.
9
 */
10
public interface ProcessContext {
11

12
  /**
13
   * @param handling the desired {@link ProcessErrorHandling}.
14
   * @return this {@link ProcessContext} for fluent API calls.
15
   */
16
  ProcessContext errorHandling(ProcessErrorHandling handling);
17

18
  /**
19
   * @param directory the {@link Path} to the working directory from where to execute the command.
20
   * @return this {@link ProcessContext} for fluent API calls.
21
   */
22
  ProcessContext directory(Path directory);
23

24
  /**
25
   * Sets the executable command to be {@link #run()}.
26
   *
27
   * @param executable the {@link Path} to the command to be executed by {@link #run()}. Depending on your operating system and the extension of the executable
28
   * or OS specific conventions. So e.g. a *.cmd or *.bat file will be called via CMD shell on windows while a *.sh file will be called via Bash, etc.
29
   * @return this {@link ProcessContext} for fluent API calls.
30
   */
31
  ProcessContext executable(Path executable);
32

33
  /**
34
   * Sets the executable command to be {@link #run()}.
35
   *
36
   * @param executable the command to be executed by {@link #run()}.
37
   * @return this {@link ProcessContext} for fluent API calls.
38
   * @see #executable(Path)
39
   */
40
  default ProcessContext executable(String executable) {
41

42
    return executable(Path.of(executable));
7✔
43
  }
44

45
  /**
46
   * Adds a single argument for {@link #executable(Path) command}.
47
   *
48
   * @param arg the next argument for {@link #executable(Path) command} to be added.
49
   * @return this {@link ProcessContext} for fluent API calls.
50
   */
51
  ProcessContext addArg(String arg);
52

53
  /**
54
   * Adds a single argument for {@link #executable(Path) command}.
55
   *
56
   * @param arg the next argument for {@link #executable(Path) command} to be added.
57
   * @return this {@link ProcessContext} for fluent API calls.
58
   */
59
  default ProcessContext addArg(Object arg) {
60

61
    Objects.requireNonNull(arg);
×
62
    return addArg(arg.toString());
×
63
  }
64

65
  /**
66
   * Adds the given arguments for {@link #executable(Path) command}. E.g. for {@link #executable(Path) command} "mvn" the arguments "clean" and "install" may be
67
   * added here to run "mvn clean install". If this method would be called again with "-Pmyprofile" and "-X" before {@link #run()} gets called then "mvn clean
68
   * install -Pmyprofile -X" would be run.
69
   *
70
   * @param args the arguments for {@link #executable(Path) command} to be added.
71
   * @return this {@link ProcessContext} for fluent API calls.
72
   */
73
  default ProcessContext addArgs(String... args) {
74

75
    for (String arg : args) {
16✔
76
      addArg(arg);
4✔
77
    }
78
    return this;
2✔
79
  }
80

81
  /**
82
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
83
   * of these arguments to the command.
84
   *
85
   * @param args the arguments for {@link #executable(Path) command} to be added.
86
   * @return this {@link ProcessContext} for fluent API calls.
87
   */
88
  default ProcessContext addArgs(Object... args) {
89

90
    for (Object arg : args) {
×
91
      addArg(arg);
×
92
    }
93
    return this;
×
94
  }
95

96
  /**
97
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
98
   * of these arguments to the command.
99
   *
100
   * @param args the {@link List} of arguments for {@link #executable(Path) command} to be added.
101
   * @return this {@link ProcessContext} for fluent API calls.
102
   */
103
  default ProcessContext addArgs(List<?>... args) {
104

105
    for (Object arg : args) {
×
106
      addArg(arg);
×
107
    }
108
    return this;
×
109
  }
110

111
  /**
112
   * Sets or overrides the specified environment variable only for the planned {@link #run() process execution}. Please note that the environment variables are
113
   * initialized when the {@link ProcessContext} is created. This method explicitly set an additional or overrides an existing environment and will have effect
114
   * for each {@link #run() process execution} invoked from this {@link ProcessContext} instance. Be aware of such side-effects when reusing the same
115
   * {@link ProcessContext} to {@link #run() run} multiple commands.
116
   *
117
   * @param key the name of the environment variable (E.g. "PATH").
118
   * @param value the value of the environment variable.
119
   * @return this {@link ProcessContext} for fluent API calls.
120
   */
121
  ProcessContext withEnvVar(String key, String value);
122

123
  /**
124
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
125
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
126
   *
127
   * @return the exit code. Will be {@link ProcessResult#SUCCESS} on successful completion of the {@link Process}.
128
   */
129
  default int run() {
130

131
    return run(ProcessMode.DEFAULT).getExitCode();
×
132
  }
133

134
  /**
135
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
136
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
137
   *
138
   * @param processMode {@link ProcessMode}
139
   * @return the {@link ProcessResult}.
140
   */
141
  ProcessResult run(ProcessMode processMode);
142

143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc