View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2017 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.maven.plugins.jsondlgcnv;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Enumeration;
26  
27  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
28  import org.apache.commons.compress.archivers.zip.ZipFile;
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
33  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.plugins.annotations.Component;
38  import org.apache.maven.plugins.annotations.Mojo;
39  import org.apache.maven.plugins.annotations.Parameter;
40  import org.apache.maven.plugins.annotations.ResolutionScope;
41  import org.apache.maven.repository.RepositorySystem;
42  
43  /**
44   * Converts dialog definitions in JSON format using the cq-dialog-conversion tool.
45   */
46  @Mojo(name = "convert", requiresProject = true, threadSafe = true,
47      requiresDependencyResolution = ResolutionScope.COMPILE)
48  public class ConversionMojo extends AbstractMojo {
49  
50    /**
51     * Source path containing Sling-Initial-Content JSON files.
52     */
53    @Parameter(defaultValue = "${project.basedir}/src/main/webapp/app-root", property = "convert.source")
54    private File source;
55  
56    /**
57     * Temporary work directory
58     */
59    @Parameter(defaultValue = "${project.build.directory}/json-dialog-conversion-work")
60    private File workDir;
61  
62    /**
63     * Ruleset for transformation
64     */
65    @Parameter(defaultValue = "/libs/cq/dialogconversion/rules/coral2", property = "convert.rules")
66    private String rules;
67  
68    /**
69     * If set to true, the affected JSON files are only reformatted, but not transformed.
70     */
71    @Parameter(defaultValue = "false", property = "convert.formatOnly")
72    private boolean formatOnly;
73  
74    /**
75     * Version of artifact com.adobe.cq:cq-dialog-conversion-content:zip
76     */
77    @Parameter(defaultValue = "2.0.0", property = "convert.toolVersion")
78    private String dialogConversionToolVersion;
79  
80    @Component
81    private RepositorySystem repository;
82    @Parameter(property = "localRepository", required = true, readonly = true)
83    private ArtifactRepository localRepository;
84    @Parameter(property = "project.remoteArtifactRepositories", required = true, readonly = true)
85    private java.util.List<ArtifactRepository> remoteRepositories;
86  
87    @Override
88    public void execute() throws MojoExecutionException, MojoFailureException {
89      try {
90        initialize();
91  
92        File dialogConversionContent = getDialogConversionContentDir();
93  
94        SlingMockWrapper wrapper = new SlingMockWrapper(dialogConversionContent, source);
95        wrapper.execute(context -> {
96          DialogConverter converter = new DialogConverter(context, rules, getLog());
97          if (formatOnly) {
98            converter.format();
99          }
100         else {
101           converter.convert();
102         }
103       });
104     }
105     catch (IOException ex) {
106       throw new MojoExecutionException(ex.getMessage(), ex);
107     }
108   }
109 
110   private void initialize() throws IOException {
111     // cleanup work dir
112     if (workDir.exists()) {
113       FileUtils.deleteDirectory(workDir);
114     }
115     workDir.mkdirs();
116   }
117 
118   private File getDialogConversionContentDir() throws MojoExecutionException, IOException {
119     File targetDir = new File(workDir, "cq-dialog-conversion-content");
120     targetDir.mkdir();
121 
122     // get cq-dialog-conversion-content ZIP from maven artifact
123     File file = getArtifactFile("com.adobe.cq", "cq-dialog-conversion-content", "zip", dialogConversionToolVersion);
124 
125     // unzip file to target dir
126     try (ZipFile zipFile = new ZipFile(file)) {
127       Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
128       while (entries.hasMoreElements()) {
129         ZipArchiveEntry entry = entries.nextElement();
130         File outFile = new File(targetDir, entry.getName());
131         if (entry.isDirectory()) {
132           if (!outFile.exists()) {
133             outFile.mkdirs();
134           }
135           continue;
136         }
137         if (outFile.exists()) {
138           continue;
139         }
140         try (InputStream entryStream = zipFile.getInputStream(entry)) {
141           FileUtils.copyInputStreamToFile(entryStream, outFile);
142         }
143       }
144     }
145 
146     return targetDir;
147   }
148 
149   private File getArtifactFile(String groupId, String artifactId, String packaging, String version) throws MojoExecutionException {
150     Artifact artifactObject = repository.createArtifact(groupId, artifactId, version, packaging);
151 
152     // resolve artifact
153     ArtifactResolutionRequest request = new ArtifactResolutionRequest();
154     request.setArtifact(artifactObject);
155     request.setLocalRepository(localRepository);
156     request.setRemoteRepositories(remoteRepositories);
157     ArtifactResolutionResult result = repository.resolve(request);
158     if (result.isSuccess()) {
159       return artifactObject.getFile();
160     }
161     else {
162       throw new MojoExecutionException("Unable to download artifact: " + artifactObject.toString());
163     }
164   }
165 
166 }