/*
 * Copyright 2012 the original author or authors.
 *
 * 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.gradle.api.publish.maven.tasks;

import org.gradle.api.DefaultTask;
import org.gradle.api.Incubating;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.AbstractFileCollection;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.publish.maven.MavenPom;
import org.gradle.api.publish.maven.internal.MavenPomGenerator;
import org.gradle.api.publish.maven.internal.MavenPomInternal;
import org.gradle.api.publish.maven.internal.MavenProjectIdentity;
import org.gradle.api.specs.Specs;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskDependency;

import javax.inject.Inject;
import java.io.File;
import java.util.Collections;
import java.util.Set;

/**
 * Generates an Ivy XML Module Descriptor file.
 *
 * @since 1.4
 */
@Incubating
public class GenerateMavenPom extends DefaultTask {

    private final FileCollection pomFile;
    private final FileResolver fileResolver;
    private MavenPom pom;
    private Object destination;

    @Inject
    public GenerateMavenPom(FileResolver fileResolver) {
        this.fileResolver = fileResolver;

        // Never up to date; we don't understand the data structures.
        getOutputs().upToDateWhen(Specs.satisfyNone());

        this.pomFile = new PomFileCollection();
    }

    /**
     * The Maven pom.
     *
     * @return The Maven pom.
     */
    public MavenPom getPom() {
        return pom;
    }

    public void setPom(MavenPom pom) {
        this.pom = pom;
    }

    /**
     * The file the pom will be written to.
     *
     * @return The file the pom will be written to
     */
    @OutputFile
    public File getDestination() {
        return destination == null ? null : fileResolver.resolve(destination);
    }

    /**
     * Sets the destination the descriptor will be written to.
     *
     * The value is resolved with {@link org.gradle.api.Project#file(Object)}
     *
     * @param destination The file the descriptor will be written to.
     */
    public void setDestination(Object destination) {
        this.destination = destination;
    }

    public FileCollection getPomFile() {
        return pomFile;
    }

    @TaskAction
    public void doGenerate() {
        MavenPomInternal pomInternal = (MavenPomInternal) getPom();

        MavenPomGenerator pomGenerator = new MavenPomGenerator();
        copyIdentity(pomInternal.getProjectIdentity(), pomGenerator);
        copyDependencies(pomInternal.getRuntimeDependencies(), pomGenerator);
        pomGenerator.withXml(pomInternal.getXmlAction());

        pomGenerator.writeTo(getDestination());
    }

    private void copyIdentity(MavenProjectIdentity projectIdentity, MavenPomGenerator pom) {
        pom.setArtifactId(projectIdentity.getArtifactId());
        pom.setGroupId(projectIdentity.getGroupId());
        pom.setVersion(projectIdentity.getVersion());
        pom.setPackaging(projectIdentity.getPackaging());
    }

    private void copyDependencies(Set<Dependency> runtimeDependencies, MavenPomGenerator pom) {
        for (Dependency runtimeDependency : runtimeDependencies) {
            pom.addRuntimeDependency(runtimeDependency);
        }
    }

    private class PomFileCollection extends AbstractFileCollection {
        private final DefaultTaskDependency dependency;

        public PomFileCollection() {
            this.dependency = new DefaultTaskDependency();
            this.dependency.add(GenerateMavenPom.this);
        }

        @Override
        public String getDisplayName() {
            return "pom-file";
        }

        @Override
        public TaskDependency getBuildDependencies() {
            return dependency;
        }

        public Set<File> getFiles() {
            return Collections.singleton(getDestination());
        }
    }
}

@@ -794,197 +794,34 @@
 api.
-file.FileCollection;%0Aimport org.gradle.api.internal.file.AbstractFileCollection;%0Aimport org.gradle.api.internal.file.FileResolver;%0Aimport org.gradle.api.internal.tasks.DefaultTaskDependency
+internal.file.FileResolver
 ;%0Aim
@@ -909,24 +909,30 @@
 nternal.
+tasks.
 MavenPom
 Generato
@@ -915,32 +915,36 @@
 l.tasks.MavenPom
+File
 Generator;%0Aimpor
@@ -1186,52 +1186,8 @@
 ion;
-%0Aimport org.gradle.api.tasks.TaskDependency;
 %0A%0Aim
@@ -1237,38 +1237,8 @@
 le;%0A
-import java.util.Collections;%0A
 impo
@@ -1398,50 +1398,8 @@
  %7B%0A%0A
-    private final FileCollection pomFile;%0A
     
@@ -1739,57 +1739,8 @@
 ());
-%0A%0A        this.pomFile = new PomFileCollection();
 %0A   
@@ -2546,80 +2546,8 @@
  %7D%0A%0A
-    public FileCollection getPomFile() %7B%0A        return pomFile;%0A    %7D%0A%0A
     
@@ -2666,32 +2666,36 @@
         MavenPom
+File
 Generator pomGen
@@ -2707,32 +2707,36 @@
 r = new MavenPom
+File
 Generator();%0A   
@@ -3062,32 +3062,36 @@
 entity, MavenPom
+File
 Generator pom) %7B
@@ -3403,16 +3403,20 @@
 MavenPom
+File
 Generato
@@ -3567,627 +3567,7 @@
    %7D
-%0A%0A    private class PomFileCollection extends AbstractFileCollection %7B%0A        private final DefaultTaskDependency dependency;%0A%0A        public PomFileCollection() %7B%0A            this.dependency = new DefaultTaskDependency();%0A            this.dependency.add(GenerateMavenPom.this);%0A        %7D%0A%0A        @Override%0A        public String getDisplayName() %7B%0A            return %22pom-file%22;%0A        %7D%0A%0A        @Override%0A        public TaskDependency getBuildDependencies() %7B%0A            return dependency;%0A        %7D%0A%0A        public Set%3CFile%3E getFiles() %7B%0A            return Collections.singleton(getDestination());%0A        %7D%0A    %7D
 %0A%7D%0A

Use task.getOutputs().getFiles() instead of a custom file collection