1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41 public class UpgradeInvoker extends LookupInvoker<UpgradeContext> {
42
43 public static final int OK = 0;
44 public static final int ERROR = 1;
45 public static final int BAD_OPERATION = 2;
46 public static final int CANCELED = 3;
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 }