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  import java.util.stream.Collectors;
30  
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Parameter;
34  
35  /**
36   * Common functionality for mojos that generate configuration ZIP files for Adobe Cloud Manager.
37   */
38  abstract class AbstractCloudManagerMojo extends AbstractMojo {
39  
40    /**
41     * Selected environments to generate.
42     */
43    @Parameter(property = "conga.environments")
44    private String[] environments;
45  
46    /**
47     * Selected nodes to generate.
48     */
49    @Parameter(property = "conga.nodes")
50    private String[] nodes;
51  
52    /**
53     * Path for the generated configuration files.
54     */
55    @Parameter(defaultValue = "${project.build.directory}/configuration")
56    private File configurationDir;
57  
58    /**
59     * Target path for the generated copied files.
60     */
61    @Parameter(defaultValue = "${project.build.directory}")
62    private File target;
63  
64    /**
65     * @return Target directory
66     */
67    protected File getTargetDir() {
68      // create directory if it does not exist already
69      if (!target.exists() && !target.mkdirs()) {
70        throw new IllegalStateException("Unable to create target dir: " + getCanonicalPath(target));
71      }
72      return target;
73    }
74  
75    /**
76     * Get directory of the selected environment. It has to be exactly one matching environment.
77     * @return Environment directory
78     * @throws MojoExecutionException if no or multiple directories found
79     */
80    protected List<File> getEnvironmentDir() throws MojoExecutionException {
81      List<File> directories = null;
82      Set<String> selectedEnvironments = toSet(this.environments);
83      if (configurationDir.exists() && configurationDir.isDirectory()) {
84        File[] files = configurationDir.listFiles();
85        if (files != null) {
86          directories = Arrays.stream(files)
87              .filter(File::isDirectory)
88              .filter(dir -> (selectedEnvironments.isEmpty() || selectedEnvironments.contains(dir.getName())))
89              .collect(Collectors.toList());
90        }
91      }
92      if (directories == null || directories.isEmpty()) {
93        throw new MojoExecutionException("No matching environment directory found in " + getCanonicalPath(configurationDir));
94      }
95      return directories;
96    }
97  
98    /**
99     * Checks if the given environment was configured explicitly in plugin configuration.
100    * @param environment Environment name
101    * @return true if configured explicitly
102    */
103   protected boolean isEnvironmentConfiguredExplicitely(String environment) {
104     Set<String> selectedEnvironments = toSet(this.environments);
105     return selectedEnvironments.contains(environment);
106   }
107 
108   /**
109    * Get matching node directories from environment.
110    * @param environmentDir Environment directory
111    * @return List of directories
112    */
113   protected List<File> getNodeDirs(File environmentDir) {
114     Set<String> selectedNodes = toSet(this.nodes);
115     File[] files = environmentDir.listFiles();
116     if (files != null) {
117       return Arrays.stream(files)
118           .filter(File::isDirectory)
119           .filter(dir -> selectedNodes.isEmpty() || selectedNodes.contains(dir.getName()))
120           .collect(Collectors.toList());
121     }
122     else {
123       return Collections.emptyList();
124     }
125   }
126 
127   private static Set<String> toSet(String[] values) {
128     if (values != null) {
129       return Set.of(values);
130     }
131     else {
132       return Set.of();
133     }
134   }
135 
136 }