// Path: vscode-car-plugin/src/main/java/org/wso2/maven/Model/ArchiveException.java
// public class ArchiveException extends Exception {
//     public ArchiveException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: vscode-car-plugin/src/main/java/org/wso2/maven/Model/ArtifactDependency.java
// public class ArtifactDependency {
//     private String artifact;
//     private String version;
//     private String serverRole;
//     private Boolean include;
// 
//     public ArtifactDependency(String artifact, String version, String serverRole, Boolean include) {
//         this.artifact = artifact;
//         this.version = version;
//         this.serverRole = serverRole;
//         this.include = include;
//     }
// 
//     /**
//      * @return the artifact
//      */
//     public String getArtifact() {
//         return artifact;
//     }
// 
//     /**
//      * @param artifact of the dependency
//      */
//     public void setArtifact(String artifact) {
//         this.artifact = artifact;
//     }
// 
//     /**
//      * @return the verison
//      */
//     public String getVersion() {
//         return version;
//     }
// 
//     /**
//      * @param version of the dependency
//      */
//     public void setVersion(String version) {
//         this.version = version;
//     }
// 
//     /**
//      * @return the serverRole
//      */
//     public String getServerRole() {
//         return serverRole;
//     }
// 
//     /**
//      * @param serverRole of the dependency
//      */
//     public void setServerRole(String serverRole) {
//         this.serverRole = serverRole;
//     }
// 
//     /**
//      * @return include
//      */
//     public Boolean getInclude() {
//         return include;
//     }
// 
//     /**
//      * @param include of the dependency
//      */
//     public void setInclude(Boolean include) {
//         this.include = include;
//     }
// }


import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
import org.wso2.maven.Model.ArchiveException;
import org.wso2.maven.Model.ArtifactDependency;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.stream.XMLStreamException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
/*
 * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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.wso2.maven;




/**
 * Goal which touches a timestamp file.
 *
 * @goal car
 * @phase package
 */
@Mojo(name = "WSO2ESBDeployableArchive")
public class CARMojo extends AbstractMojo {

    /**
     * Location of the default build folder.
     *
     * @parameter expression="${project.build.directory}"
     */
    private File projectBuildDir;

    /**
     * Location of the archive folder.
     *
     * @parameter expression="${project.build.directory}"
     */
    private File archiveLocation;

    /**
     * archiveName is used if the user wants to override the default name of the generated archive with .car extension.
     *
     * @parameter expression="${project.build.archiveName}"
     */
    private String archiveName;

    /**
     * CarbonApp is used if the user wants to override the default name of the carbon application.
     *
     * @parameter
     */
    private String cAppName;

    /**
     * @parameter default-value="${project}"
     */
    private MavenProject project;

    /**
     * A classifier for the build final name.
     *
     * @parameter
     */
    private String classifier;

    /**
     * Read artifact.xml files and create .car file.
     */
    public void execute() throws MojoExecutionException {
        appendLogs();
        // Create CApp
        try {
            // Create directory to be compressed.
            String archiveDirectory = getArchiveFile(Constants.EMPTY_STRING).getAbsolutePath();
            boolean createdArchiveDirectory = org.wso2.developerstudio.eclipse.utils.file.FileUtils.createDirectories(
                    archiveDirectory);

            if (createdArchiveDirectory) {
                CAppHandler cAppHandler = new CAppHandler(cAppName);
                List<ArtifactDependency> dependencies = new ArrayList<>();

                cAppHandler.processConfigArtifactXmlFile(projectBuildDir, archiveDirectory, dependencies);
                cAppHandler.processRegistryResourceArtifactXmlFile(projectBuildDir, archiveDirectory, dependencies);
                cAppHandler.createDependencyArtifactsXmlFile(archiveDirectory, dependencies, project);

                File fileToZip = new File(archiveDirectory);
                File carFile = getArchiveFile(".car");
                zipFolder(fileToZip.getPath(), carFile.getPath());

                // Attach carFile to Maven context.
                this.project.getArtifact().setFile(carFile);

                recursiveDelete(fileToZip);

            } else {
                getLog().error("Could not create corresponding archive directory.");
            }
// Path: vscode-car-plugin/src/main/java/org/wso2/maven/Model/ArchiveException.java
// public class ArchiveException extends Exception {
//     public ArchiveException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: vscode-car-plugin/src/main/java/org/wso2/maven/Model/ArtifactDependency.java
// public class ArtifactDependency {
//     private String artifact;
//     private String version;
//     private String serverRole;
//     private Boolean include;
// 
//     public ArtifactDependency(String artifact, String version, String serverRole, Boolean include) {
//         this.artifact = artifact;
//         this.version = version;
//         this.serverRole = serverRole;
//         this.include = include;
//     }
// 
//     /**
//      * @return the artifact
//      */
//     public String getArtifact() {
//         return artifact;
//     }
// 
//     /**
//      * @param artifact of the dependency
//      */
//     public void setArtifact(String artifact) {
//         this.artifact = artifact;
//     }
// 
//     /**
//      * @return the verison
//      */
//     public String getVersion() {
//         return version;
//     }
// 
//     /**
//      * @param version of the dependency
//      */
//     public void setVersion(String version) {
//         this.version = version;
//     }
// 
//     /**
//      * @return the serverRole
//      */
//     public String getServerRole() {
//         return serverRole;
//     }
// 
//     /**
//      * @param serverRole of the dependency
//      */
//     public void setServerRole(String serverRole) {
//         this.serverRole = serverRole;
//     }
// 
//     /**
//      * @return include
//      */
//     public Boolean getInclude() {
//         return include;
//     }
// 
//     /**
//      * @param include of the dependency
//      */
//     public void setInclude(Boolean include) {
//         this.include = include;
//     }
// }


// Path: vscode-car-plugin/src/main/java/org/wso2/maven/CARMojo.java
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
import org.wso2.maven.Model.ArchiveException;
import org.wso2.maven.Model.ArtifactDependency;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.stream.XMLStreamException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/*
 * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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.wso2.maven;




/**
 * Goal which touches a timestamp file.
 *
 * @goal car
 * @phase package
 */
@Mojo(name = "WSO2ESBDeployableArchive")
public class CARMojo extends AbstractMojo {

    /**
     * Location of the default build folder.
     *
     * @parameter expression="${project.build.directory}"
     */
    private File projectBuildDir;

    /**
     * Location of the archive folder.
     *
     * @parameter expression="${project.build.directory}"
     */
    private File archiveLocation;

    /**
     * archiveName is used if the user wants to override the default name of the generated archive with .car extension.
     *
     * @parameter expression="${project.build.archiveName}"
     */
    private String archiveName;

    /**
     * CarbonApp is used if the user wants to override the default name of the carbon application.
     *
     * @parameter
     */
    private String cAppName;

    /**
     * @parameter default-value="${project}"
     */
    private MavenProject project;

    /**
     * A classifier for the build final name.
     *
     * @parameter
     */
    private String classifier;

    /**
     * Read artifact.xml files and create .car file.
     */
    public void execute() throws MojoExecutionException {
        appendLogs();
        // Create CApp
        try {
            // Create directory to be compressed.
            String archiveDirectory = getArchiveFile(Constants.EMPTY_STRING).getAbsolutePath();
            boolean createdArchiveDirectory = org.wso2.developerstudio.eclipse.utils.file.FileUtils.createDirectories(
                    archiveDirectory);

            if (createdArchiveDirectory) {
                CAppHandler cAppHandler = new CAppHandler(cAppName);
                List<ArtifactDependency> dependencies = new ArrayList<>();

                cAppHandler.processConfigArtifactXmlFile(projectBuildDir, archiveDirectory, dependencies);
                cAppHandler.processRegistryResourceArtifactXmlFile(projectBuildDir, archiveDirectory, dependencies);
                cAppHandler.createDependencyArtifactsXmlFile(archiveDirectory, dependencies, project);

                File fileToZip = new File(archiveDirectory);
                File carFile = getArchiveFile(".car");
                zipFolder(fileToZip.getPath(), carFile.getPath());

                // Attach carFile to Maven context.
                this.project.getArtifact().setFile(carFile);

                recursiveDelete(fileToZip);

            } else {
                getLog().error("Could not create corresponding archive directory.");
            }
        } catch (XMLStreamException | IOException | ArchiveException e) {