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.goals;
20  
21  import java.nio.file.Path;
22  import java.util.Map;
23  import java.util.Optional;
24  
25  import eu.maveniverse.domtrip.Document;
26  import org.apache.maven.api.cli.mvnup.UpgradeOptions;
27  import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
28  
29  /**
30   * Strategy interface for different types of upgrade operations.
31   * Each strategy handles a specific aspect of the Maven upgrade process.
32   *
33   * <p>Strategies work with domtrip Documents for perfect formatting preservation.
34   * Individual strategies can create domtrip Editors from Documents as needed:
35   * <pre>
36   * Editor editor = new Editor(document);
37   * // ... perform domtrip operations ...
38   * // Document is automatically updated
39   * </pre>
40   */
41  public interface UpgradeStrategy {
42  
43      /**
44       * Applies the upgrade strategy to all eligible POMs.
45       *
46       * @param context the upgrade context
47       * @param pomMap map of all POM files in the project (domtrip Documents)
48       * @return the result of the upgrade operation
49       */
50      UpgradeResult apply(UpgradeContext context, Map<Path, Document> pomMap);
51  
52      /**
53       * Checks if this strategy is applicable given the current options.
54       *
55       * @param context the upgrade context
56       * @return true if this strategy should be applied
57       */
58      boolean isApplicable(UpgradeContext context);
59  
60      /**
61       * Helper method to check if a specific option is enabled, considering --all flag and defaults.
62       *
63       * @param options the upgrade options
64       * @param specificOption the specific option to check
65       * @param defaultWhenNoOptionsSpecified whether this option should be enabled by default
66       * @return true if the option should be enabled
67       */
68      default boolean isOptionEnabled(
69              UpgradeOptions options, Optional<Boolean> specificOption, boolean defaultWhenNoOptionsSpecified) {
70          // Handle --all option (overrides individual options)
71          boolean useAll = options.all().orElse(false);
72          if (useAll) {
73              return true;
74          }
75  
76          // Check specific option
77          if (specificOption.isPresent()) {
78              return specificOption.get();
79          }
80  
81          // Apply default behavior when no specific options are provided
82          if (defaultWhenNoOptionsSpecified
83                  && options.infer().isEmpty()
84                  && options.model().isEmpty()
85                  && options.plugins().isEmpty()
86                  && options.model().isEmpty()) {
87              return true;
88          }
89  
90          return false;
91      }
92  
93      /**
94       * Gets a description of what this strategy does.
95       *
96       * @return a human-readable description of the strategy
97       */
98      String getDescription();
99  }