|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.cli; |
| 20 | + |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | +import java.util.ListIterator; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.apache.commons.cli.CommandLine; |
| 28 | +import org.apache.commons.cli.Option; |
| 29 | +import org.apache.commons.cli.Options; |
| 30 | +import org.apache.commons.cli.ParseException; |
| 31 | +import org.apache.maven.cling.invoker.mvn.CommonsCliMavenOptions; |
| 32 | +import org.codehaus.plexus.interpolation.BasicInterpolator; |
| 33 | +import org.codehaus.plexus.interpolation.InterpolationException; |
| 34 | +import org.mvndaemon.mvnd.common.Environment; |
| 35 | + |
| 36 | +import static org.apache.maven.cling.invoker.Utils.createInterpolator; |
| 37 | + |
| 38 | +public class CommonsCliDaemonMavenOptions extends CommonsCliMavenOptions implements DaemonMavenOptions { |
| 39 | + public static CommonsCliDaemonMavenOptions parse(String source, String[] args) throws ParseException { |
| 40 | + CLIManager cliManager = new CLIManager(); |
| 41 | + return new CommonsCliDaemonMavenOptions(source, cliManager, cliManager.parse(args)); |
| 42 | + } |
| 43 | + |
| 44 | + protected CommonsCliDaemonMavenOptions(String source, CLIManager cliManager, CommandLine commandLine) { |
| 45 | + super(source, cliManager, commandLine); |
| 46 | + } |
| 47 | + |
| 48 | + public org.apache.commons.cli.Options getOptions() { |
| 49 | + return this.cliManager.getOptions(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Optional<Boolean> rawStreams() { |
| 54 | + if (commandLine.hasOption(CLIManager.RAW_STREAMS) |
| 55 | + || Environment.MVND_RAW_STREAMS.asOptional().isPresent()) { |
| 56 | + return Optional.of(Boolean.TRUE); |
| 57 | + } |
| 58 | + return Optional.empty(); |
| 59 | + } |
| 60 | + |
| 61 | + private static CommonsCliDaemonMavenOptions interpolate( |
| 62 | + CommonsCliDaemonMavenOptions options, Collection<Map<String, String>> properties) { |
| 63 | + try { |
| 64 | + // now that we have properties, interpolate all arguments |
| 65 | + BasicInterpolator interpolator = createInterpolator(properties); |
| 66 | + CommandLine.Builder commandLineBuilder = new CommandLine.Builder(); |
| 67 | + commandLineBuilder.setDeprecatedHandler(o -> {}); |
| 68 | + for (Option option : options.commandLine.getOptions()) { |
| 69 | + if (!CLIManager.USER_PROPERTY.equals(option.getOpt())) { |
| 70 | + List<String> values = option.getValuesList(); |
| 71 | + for (ListIterator<String> it = values.listIterator(); it.hasNext(); ) { |
| 72 | + it.set(interpolator.interpolate(it.next())); |
| 73 | + } |
| 74 | + } |
| 75 | + commandLineBuilder.addOption(option); |
| 76 | + } |
| 77 | + for (String arg : options.commandLine.getArgList()) { |
| 78 | + commandLineBuilder.addArg(interpolator.interpolate(arg)); |
| 79 | + } |
| 80 | + return new CommonsCliDaemonMavenOptions( |
| 81 | + options.source, (CLIManager) options.cliManager, commandLineBuilder.build()); |
| 82 | + } catch (InterpolationException e) { |
| 83 | + throw new IllegalArgumentException("Could not interpolate CommonsCliOptions", e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public DaemonMavenOptions interpolate(Collection<Map<String, String>> properties) { |
| 89 | + return interpolate(this, properties); |
| 90 | + } |
| 91 | + |
| 92 | + protected static class CLIManager extends CommonsCliMavenOptions.CLIManager { |
| 93 | + public static final String RAW_STREAMS = "ras"; |
| 94 | + |
| 95 | + @Override |
| 96 | + protected void prepareOptions(Options options) { |
| 97 | + super.prepareOptions(options); |
| 98 | + options.addOption(Option.builder(RAW_STREAMS) |
| 99 | + .longOpt("raw-streams") |
| 100 | + .desc("Use raw-streams for daemon communication") |
| 101 | + .build()); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments