/*
 * Copyright 2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.swarm.plugin;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

@Mojo(name = "prep-doc-source",
        defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class DocPrepMojo extends AbstractExposedComponentsMojo {
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        final List<File> sourceArtifacts;
        try {
            sourceArtifacts = parsedModules().keySet().stream()
                    .flatMap(module -> resolvedComponents().get(module).stream()
                            .filter(d -> d.doc != null)
                            .map(d -> {
                                return resolveArtifact(BomBuilder.SWARM_GROUP, d.doc, parsedModules().get(module), "sources", "jar");
                            }))
                    .collect(Collectors.toList());
        } catch (ArtifactResolutionRuntimeException e){
            throw new MojoFailureException("Failed to resolve sources artifact", e.getCause());
        }

        this.sourceOutputDir.mkdirs();

        for (File each : sourceArtifacts) {
            ShrinkWrap.createFromZipFile(JavaArchive.class, each)
                    .as(ExplodedExporter.class)
                    .exportExploded(this.sourceOutputDir, ".");
        }

    }

    @Parameter
    private File sourceOutputDir;
}


@@ -674,89 +674,8 @@
 ile;
-%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.stream.Collectors;
 %0A%0Aim
@@ -1358,41 +1358,39 @@
     
-final List%3CFile%3E sourceArtifacts;
+this.sourceOutputDir.mkdirs();%0A
 %0A   
@@ -1415,26 +1415,8 @@
     
- sourceArtifacts =
  par
@@ -1436,25 +1436,16 @@
 keySet()
-.stream()
 %0A       
@@ -1463,14 +1463,14 @@
   .f
-latMap
+orEach
 (mod
@@ -1607,133 +1607,389 @@
    .
-map(d -%3E %7B%0A                                return resolveArtifact(BomBuilder.SWARM_GROUP, d.doc, parsedModules().get(module),
+forEach(d -%3E ShrinkWrap.createFromZipFile(JavaArchive.class,%0A                                                                       resolveArtifact(BomBuilder.SWARM_GROUP, d.doc,%0A                                                                                       parsedModules().get(module),%0A                                                                                      
  %22so
@@ -2002,18 +2002,20 @@
 , %22jar%22)
-;%0A
+)%0A  
         
@@ -2038,59 +2038,123 @@
     
-%7D))%0A                    .collect(Collectors.toList(
+      .as(ExplodedExporter.class)%0A                                    .exportExploded(this.sourceOutputDir, module)
 ));%0A
@@ -2319,282 +2319,8 @@
   %7D%0A
-%0A        this.sourceOutputDir.mkdirs();%0A%0A        for (File each : sourceArtifacts) %7B%0A            ShrinkWrap.createFromZipFile(JavaArchive.class, each)%0A                    .as(ExplodedExporter.class)%0A                    .exportExploded(this.sourceOutputDir, %22.%22);%0A        %7D%0A%0A
     

Store sources in per-module dir [SWARM-10]