View Javadoc
1   /*
2     Copyright (C) 2020 - 2022 Alexander Kapitman
3   
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15  */
16  
17  package ru.akman.maven.plugins;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.stream.Collectors;
22  import org.codehaus.plexus.util.cli.Arg;
23  import org.codehaus.plexus.util.cli.Commandline;
24  
25  /**
26   * CommandLine builder is a wrapper added group of agruments to an option.
27   */
28  public class CommandLineBuilder {
29  
30    /**
31     * Building command line.
32     */
33    private final Commandline cmdLine;
34  
35    /**
36     * List of command line options.
37     */
38    private final List<CommandLineOption> options;
39  
40    /**
41     * Default constructor.
42     */
43    public CommandLineBuilder() {
44      cmdLine = new Commandline();
45      options = new ArrayList<>();
46    }
47  
48    /**
49     * Set executable.
50     *
51     * @param executable the executable path
52     */
53    public void setExecutable(final String executable) {
54      cmdLine.setExecutable(executable);
55    }
56  
57    /**
58     * Create a new option (group of arguments).
59     *
60     * @return created empty option
61     */
62    public CommandLineOption createOpt() {
63      final CommandLineOption opt = new CommandLineOption(cmdLine);
64      options.add(opt);
65      return opt;
66    }
67  
68    /**
69     * Create a new agrument and a new option for it, then
70     * add created argument to this option.
71     *
72     * @return created option with added argument
73     */
74    public Arg createArg() {
75      return createOpt().createArg();
76    }
77  
78    /**
79     * Build command line.
80     *
81     * @return builded command line
82     */
83    public Commandline buildCommandLine() {
84      return cmdLine;
85    }
86  
87    /**
88     * Build list of options.
89     *
90     * @return builded option list
91     */
92    public List<String> buildOptionList() {
93      return options.stream()
94          .map(opt -> opt.toString())
95          .collect(Collectors.toList());
96    }
97  
98  }