View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2020 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.plugins.aem.maven;
21  
22  import static io.wcm.devops.conga.generator.util.FileUtil.getCanonicalPath;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugins.annotations.Parameter;
33  
34  /**
35   * Common functionality for mojos that generate configuration ZIP files for Adobe Cloud Manager.
36   */
37  abstract class AbstractCloudManagerMojo extends AbstractMojo {
38  
39    /**
40     * Selected environments to generate.
41     */
42    @Parameter(property = "conga.environments")
43    private String[] environments;
44  
45    /**
46     * Selected nodes to generate.
47     */
48    @Parameter(property = "conga.nodes")
49    private String[] nodes;
50  
51    /**
52     * Path for the generated configuration files.
53     */
54    @Parameter(defaultValue = "${project.build.directory}/configuration")
55    private File configurationDir;
56  
57    /**
58     * Target path for the generated copied files.
59     */
60    @Parameter(defaultValue = "${project.build.directory}")
61    private File target;
62  
63    /**
64     * @return Target directory
65     */
66    protected File getTargetDir() {
67      // create directory if it does not exist already
68      if (!target.exists() && !target.mkdirs()) {
69        throw new IllegalStateException("Unable to create target dir: " + getCanonicalPath(target));
70      }
71      return target;
72    }
73  
74    /**
75     * Get directory of the selected environment. It has to be exactly one matching environment.
76     * @return Environment directory
77     * @throws MojoExecutionException if no or multiple directories found
78     */
79    protected List<File> getEnvironmentDir() throws MojoExecutionException {
80      List<File> directories = null;
81      Set<String> selectedEnvironments = toSet(this.environments);
82      if (configurationDir.exists() && configurationDir.isDirectory()) {
83        File[] files = configurationDir.listFiles();
84        if (files != null) {
85          directories = Arrays.stream(files)
86              .filter(File::isDirectory)
87              .filter(dir -> (selectedEnvironments.isEmpty() || selectedEnvironments.contains(dir.getName())))
88              .toList();
89        }
90      }
91      if (directories == null || directories.isEmpty()) {
92        throw new MojoExecutionException("No matching environment directory found in " + getCanonicalPath(configurationDir));
93      }
94      return directories;
95    }
96  
97    /**
98     * Checks if the given environment was configured explicitly in plugin configuration.
99     * @param environment Environment name
100    * @return true if configured explicitly
101    */
102   protected boolean isEnvironmentConfiguredExplicitely(String environment) {
103     Set<String> selectedEnvironments = toSet(this.environments);
104     return selectedEnvironments.contains(environment);
105   }
106 
107   /**
108    * Get matching node directories from environment.
109    * @param environmentDir Environment directory
110    * @return List of directories
111    */
112   protected List<File> getNodeDirs(File environmentDir) {
113     Set<String> selectedNodes = toSet(this.nodes);
114     File[] files = environmentDir.listFiles();
115     if (files != null) {
116       return Arrays.stream(files)
117           .filter(File::isDirectory)
118           .filter(dir -> selectedNodes.isEmpty() || selectedNodes.contains(dir.getName()))
119           .toList();
120     }
121     else {
122       return Collections.emptyList();
123     }
124   }
125 
126   private static Set<String> toSet(String[] values) {
127     if (values != null) {
128       return Set.of(values);
129     }
130     else {
131       return Set.of();
132     }
133   }
134 
135 }