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.util.List;
22  import java.util.Map;
23  
24  import org.apache.maven.api.annotations.Nonnull;
25  import org.apache.maven.api.cli.InvokerRequest;
26  import org.apache.maven.api.cli.mvnup.UpgradeOptions;
27  import org.apache.maven.cling.invoker.LookupContext;
28  import org.jline.reader.LineReader;
29  import org.jline.utils.AttributedString;
30  import org.jline.utils.AttributedStringBuilder;
31  import org.jline.utils.AttributedStyle;
32  
33  import static org.apache.maven.cling.invoker.mvnup.goals.UpgradeConstants.Indentation;
34  
35  @SuppressWarnings("VisibilityModifier")
36  public class UpgradeContext extends LookupContext {
37      public UpgradeContext(InvokerRequest invokerRequest, UpgradeOptions upgradeOptions) {
38          super(invokerRequest, true, upgradeOptions);
39      }
40  
41      public Map<String, Goal> goals;
42  
43      public List<AttributedString> header;
44      public AttributedStyle style;
45      public LineReader reader;
46  
47      // Indentation control for nested logging
48      private int indentLevel = 0;
49      private String indentString = Indentation.DEFAULT;
50  
51      public void addInHeader(String text) {
52          addInHeader(AttributedStyle.DEFAULT, text);
53      }
54  
55      public void addInHeader(AttributedStyle style, String text) {
56          AttributedStringBuilder asb = new AttributedStringBuilder();
57          asb.style(style).append(text);
58          header.add(asb.toAttributedString());
59      }
60  
61      /**
62       * Increases the indentation level for nested logging.
63       */
64      public void indent() {
65          indentLevel++;
66      }
67  
68      /**
69       * Decreases the indentation level for nested logging.
70       */
71      public void unindent() {
72          if (indentLevel > 0) {
73              indentLevel--;
74          }
75      }
76  
77      /**
78       * Sets the indentation string to use (e.g., "  ", "    ", "\t").
79       */
80      public void setIndentString(String indentString) {
81          this.indentString = indentString != null ? indentString : Indentation.DEFAULT;
82      }
83  
84      /**
85       * Gets the current indentation prefix based on the current level.
86       */
87      private String getCurrentIndent() {
88          if (indentLevel == 0) {
89              return "";
90          }
91          return indentString.repeat(indentLevel);
92      }
93  
94      /**
95       * Logs an informational message with current indentation.
96       */
97      public void info(String message) {
98          logger.info(getCurrentIndent() + message);
99      }
100 
101     /**
102      * Logs a debug message with current indentation.
103      */
104     public void debug(String message) {
105         logger.debug(getCurrentIndent() + message);
106     }
107 
108     /**
109      * Prints a new line.
110      */
111     public void println() {
112         logger.info("");
113     }
114 
115     // Semantic logging methods with icons for upgrade operations
116 
117     /**
118      * Logs a successful operation with a checkmark icon.
119      */
120     public void success(String message) {
121         logger.info(getCurrentIndent() + "✓ " + message);
122     }
123 
124     /**
125      * Logs an error with an X icon.
126      */
127     public void failure(String message) {
128         logger.error(getCurrentIndent() + "✗ " + message);
129     }
130 
131     /**
132      * Logs a warning with a warning icon.
133      */
134     public void warning(String message) {
135         logger.warn(getCurrentIndent() + "⚠ " + message);
136     }
137 
138     /**
139      * Logs detailed information with a bullet point.
140      */
141     public void detail(String message) {
142         logger.info(getCurrentIndent() + "• " + message);
143     }
144 
145     /**
146      * Logs a performed action with an arrow icon.
147      */
148     public void action(String message) {
149         logger.info(getCurrentIndent() + "→ " + message);
150     }
151 
152     /**
153      * Gets the UpgradeOptions from the invoker request.
154      * This provides convenient access to upgrade-specific options without casting.
155      *
156      * @return the UpgradeOptions
157      */
158     @Nonnull
159     public UpgradeOptions options() {
160         return (UpgradeOptions) super.options();
161     }
162 }