/*
 * Copyright 2017 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.language.swift.tasks;

import com.google.common.io.Files;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Incubating;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DirectoryVar;
import org.gradle.api.file.RegularFileVar;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.language.swift.internal.SwiftStdlibToolLocator;
import org.gradle.process.ExecSpec;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Creates Apple bundle from compiled Swift code.
 *
 * @since 4.3
 */
@Incubating
public class CreateSwiftBundle extends DefaultTask {
    private final RegularFileVar informationFile;
    private final RegularFileVar executableFile;
    private final DirectoryVar outputDir;
    private final SwiftStdlibToolLocator swiftStdlibToolLocator;

    @Inject
    public CreateSwiftBundle(SwiftStdlibToolLocator swiftStdlibToolLocator) {
        this.informationFile = newInputFile();
        this.executableFile = newInputFile();
        this.outputDir = newOutputDirectory();
        this.swiftStdlibToolLocator = swiftStdlibToolLocator;

    }

    @TaskAction
    void createBundle() throws IOException {
        getProject().copy(new Action<CopySpec>() {
            @Override
            public void execute(CopySpec copySpec) {
                copySpec.from(getExecutableFile(), new Action<CopySpec>() {
                    @Override
                    public void execute(CopySpec copySpec) {
                        copySpec.into("Contents/MacOS");
                    }
                });

                copySpec.into(getOutputDir());
            }
        });

        File inputFile = getInformationFileIfExists();
        File outputFile = getOutputDir().file("Contents/Info.plist").get().getAsFile();
        if (inputFile == null) {
            Files.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
                + "<plist version=\"1.0\">\n"
                + "<dict/>\n"
                + "</plist>", outputFile, Charset.forName("UTF-8"));
        } else {
            Files.copy(inputFile, outputFile);
        }

        getProject().exec(new Action<ExecSpec>() {
            @Override
            public void execute(ExecSpec execSpec) {
                execSpec.executable(swiftStdlibToolLocator.find());
                execSpec.args(
                    "--copy",
                    "--scan-executable", executableFile.getAsFile().get().getAbsolutePath(),
                    "--destination", outputDir.dir("Contents/Frameworks").get().getAsFile().getAbsolutePath(),
                    "--platform", "macosx",
                    "--resource-destination", outputDir.dir("Contents/Resources").get().getAsFile().getAbsolutePath(),
                    "--scan-folder", outputDir.dir("Contents/Frameworks").get().getAsFile().getAbsolutePath()
                );
            }
        }).assertNormalExitValue();
    }

    @OutputDirectory
    public DirectoryVar getOutputDir() {
        return outputDir;
    }

    @InputFile
    public RegularFileVar getExecutableFile() {
        return executableFile;
    }

    @Internal("Covered by informationFileIfExists")
    public RegularFileVar getInformationFile() {
        return informationFile;
    }

    // Workaround for when the task is given an input file that doesn't exist
    @Optional
    @InputFile
    public File getInformationFileIfExists() {
        File inputFile = this.informationFile.getAsFile().getOrNull();
        if (inputFile != null && inputFile.exists()) {
            return inputFile;
        }
        return null;
    }
}

@@ -976,22 +976,24 @@
 tasks.In
-ternal
+putFiles
 ;%0Aimport
@@ -1958,25 +1958,24 @@
 oolLocator;%0A
-%0A
     %7D%0A%0A    @
@@ -2501,63 +2501,8 @@
 );%0A%0A
-        File inputFile = getInformationFileIfExists();%0A
     
@@ -2601,25 +2601,36 @@
 if (
+!
 in
-putFile == null
+formationFile.isPresent()
 ) %7B%0A
@@ -3016,23 +3016,47 @@
 .copy(in
-putFile
+formationFile.get().getAsFile()
 , output
@@ -4086,54 +4086,32 @@
    @
-Internal(%22Covered by informationFileIfExists%22)
+Optional%0A    @InputFiles
 %0A   
@@ -4197,355 +4197,7 @@
    %7D
-%0A%0A    // Workaround for when the task is given an input file that doesn't exist%0A    @Optional%0A    @InputFile%0A    public File getInformationFileIfExists() %7B%0A        File inputFile = this.informationFile.getAsFile().getOrNull();%0A        if (inputFile != null && inputFile.exists()) %7B%0A            return inputFile;%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

Use @InputFiles on information file