View Javadoc
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.cling.invoker.mvnup;
20  
21  import java.io.InterruptedIOException;
22  import java.util.ArrayList;
23  import java.util.function.Consumer;
24  
25  import org.apache.maven.api.annotations.Nullable;
26  import org.apache.maven.api.cli.InvokerRequest;
27  import org.apache.maven.api.cli.mvnup.UpgradeOptions;
28  import org.apache.maven.api.services.Lookup;
29  import org.apache.maven.cling.invoker.LookupContext;
30  import org.apache.maven.cling.invoker.LookupInvoker;
31  import org.apache.maven.cling.utils.CLIReportingUtils;
32  import org.jline.reader.LineReaderBuilder;
33  import org.jline.reader.UserInterruptException;
34  import org.jline.terminal.Terminal;
35  import org.jline.utils.AttributedStyle;
36  import org.jline.utils.Colors;
37  
38  /**
39   * mvnup invoker implementation.
40   */
41  public class UpgradeInvoker extends LookupInvoker<UpgradeContext> {
42  
43      public static final int OK = 0; // OK
44      public static final int ERROR = 1; // "generic" error
45      public static final int BAD_OPERATION = 2; // bad user input or alike
46      public static final int CANCELED = 3; // user canceled
47  
48      public UpgradeInvoker(Lookup protoLookup, @Nullable Consumer<LookupContext> contextConsumer) {
49          super(protoLookup, contextConsumer);
50      }
51  
52      @Override
53      protected UpgradeContext createContext(InvokerRequest invokerRequest) {
54          return new UpgradeContext(
55                  invokerRequest, (UpgradeOptions) invokerRequest.options().orElse(null));
56      }
57  
58      @Override
59      protected void lookup(UpgradeContext context) throws Exception {
60          if (context.goals == null) {
61              super.lookup(context);
62              context.goals = context.lookup.lookupMap(Goal.class);
63          }
64      }
65  
66      @Override
67      protected int execute(UpgradeContext context) throws Exception {
68          try {
69              context.header = new ArrayList<>();
70              context.style = new AttributedStyle();
71              context.addInHeader(
72                      context.style.italic().bold().foreground(Colors.rgbColor("green")),
73                      "Maven Upgrade " + CLIReportingUtils.showVersionMinimal());
74              context.addInHeader("Tool for upgrading Maven projects and dependencies.");
75              context.addInHeader("This tool is part of Apache Maven 4 distribution.");
76              context.addInHeader("");
77  
78              context.terminal.handle(
79                      Terminal.Signal.INT, signal -> Thread.currentThread().interrupt());
80  
81              context.reader =
82                      LineReaderBuilder.builder().terminal(context.terminal).build();
83  
84              if (context.options().goals().isEmpty()) {
85                  return badGoalsErrorMessage("No goals specified.", context);
86              }
87  
88              String goalName = context.options().goals().get().get(0);
89              Goal goal = context.goals.get(goalName);
90              if (goal == null) {
91                  return badGoalsErrorMessage("Unknown goal: " + goalName, context);
92              }
93  
94              return goal.execute(context);
95          } catch (InterruptedException | InterruptedIOException | UserInterruptException e) {
96              context.logger.error("Goal canceled by user.");
97              return CANCELED;
98          } catch (Exception e) {
99              if (context.options().showErrors().orElse(false)) {
100                 context.logger.error(e.getMessage(), e);
101             } else {
102                 context.logger.error(e.getMessage());
103             }
104             return ERROR;
105         }
106     }
107 
108     protected int badGoalsErrorMessage(String message, UpgradeContext context) {
109         context.logger.error(message);
110         context.logger.error("Supported goals are: " + String.join(", ", context.goals.keySet()));
111         context.logger.error("Use -h to display help.");
112         return BAD_OPERATION;
113     }
114 }