// Path: srcdeps-maven-base/src/main/java/org/srcdeps/mvn/Constants.java
// public interface Constants {
// 
//     /** See the bin/mvn or bin/mvn.cmd script of your maven distro, where maven.multiModuleProjectDirectory is set */
//     String MAVEN_MULTI_MODULE_PROJECT_DIRECTORY_PROPERTY = "maven.multiModuleProjectDirectory";
// 
//     String SRCDEPS_MAVEN_VERSION = "4.0.1-SNAPSHOT"; // @srcdeps.version@
// 
// }


import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.srcdeps.mvn.Constants;
import org.w3c.dom.Document;
/**
 * Copyright 2015-2018 Maven Source Dependencies
 * Plugin contributors as indicated by the @author tags.
 *
 * 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.srcdeps.mvn.plugin;




/**
 * Adds and/or upgrades the srcdeps core extensions in {@code .mvn/extensions.xml} file. If the
 * {@code .mvn/extensions.xml} does not exist, it creates it from scratch. If the {@code .mvn/extensions.xml} exists, it
 * adds missing srcdeps core extensions and ensures they have the version specified by {@link #newVersion} parameter.
 *
 * @author <a href="https://github.com/ppalaga">Peter Palaga</a>
 */
@SuppressWarnings("deprecation")
@Mojo(name = "up", defaultPhase = LifecyclePhase.NONE, threadSafe = false, requiresProject = true, requiresDependencyResolution = ResolutionScope.NONE)
public class SrcdepsUpgradeMojo extends AbstractMojo {
    protected final File multiModuleRootDir = new File(System.getProperty("user.dir"));
    protected final Path mvnDir;
    protected final Path extensionsXmlPath;

    /** The encoding to use when reading and writing {@code srcdeps.yaml} and {@code extensions.xml} files */
    @Parameter(defaultValue = "utf-8", property = "srcdeps.encoding")
    protected String encoding;

    public SrcdepsUpgradeMojo() {
        super();
        this.mvnDir = multiModuleRootDir.toPath().resolve(".mvn");
        this.extensionsXmlPath = mvnDir.resolve("extensions.xml");
    }

    /**
     * The version of srcdeps core extensions to add or upgarde to in {@code extensions.xml} file. Default is the
     * version if the {@code srcdeps-maven-plugin} that runs this mojo.
     */
    @Parameter(property = "srcdeps.newVersion")
    protected String newVersion;

    /** The limit for the word wrap in {@code extensions.xml} file */
    @Parameter(defaultValue = "128", property = "srcdeps.xmlLineLength")
    protected int xmlLineLength;

    /** The number of spaces to use for one level of indentation in {@code extensions.xml} file */
    @Parameter(defaultValue = "2", property = "srcdeps.xmlIndentSize")
    protected int xmlIndentSize;

    /**
     * If {@code true} the {@code extensions.xml} file will be indented; otherwise the {@code extensions.xml} file will
     * not be indented.
     */
    @Parameter(defaultValue = "true", property = "srcdeps.xmlIndent")
    protected boolean xmlIndent;

    /** If {@code true} the execution of this mojo will be skipped altogether; otherwise this mojo will be executed. */
    @Parameter(defaultValue = "false", property = "srcdeps.skip")
    protected boolean skip;

    @Parameter(defaultValue = "${session}")
    protected MavenSession session;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        final Log log = getLog();
        if (skip) {
            log.info("srcdeps: ["+ getClass().getSimpleName() + "] skipped per skip parameter");
        } else if (!multiModuleRootDir.equals(session.getCurrentProject().getBasedir())) {
            log.info("srcdeps: ["+ getClass().getSimpleName() + "] skipped because multiModuleRootDir ["+ multiModuleRootDir +"] != current project basedir ["+ session.getCurrentProject().getBasedir() +"]");
        } else {

            if (newVersion == null || newVersion.isEmpty()) {
// Path: srcdeps-maven-base/src/main/java/org/srcdeps/mvn/Constants.java
// public interface Constants {
// 
//     /** See the bin/mvn or bin/mvn.cmd script of your maven distro, where maven.multiModuleProjectDirectory is set */
//     String MAVEN_MULTI_MODULE_PROJECT_DIRECTORY_PROPERTY = "maven.multiModuleProjectDirectory";
// 
//     String SRCDEPS_MAVEN_VERSION = "4.0.1-SNAPSHOT"; // @srcdeps.version@
// 
// }


// Path: srcdeps-maven-plugin/src/main/java/org/srcdeps/mvn/plugin/SrcdepsUpgradeMojo.java
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.srcdeps.mvn.Constants;
import org.w3c.dom.Document;

/**
 * Copyright 2015-2018 Maven Source Dependencies
 * Plugin contributors as indicated by the @author tags.
 *
 * 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.srcdeps.mvn.plugin;




/**
 * Adds and/or upgrades the srcdeps core extensions in {@code .mvn/extensions.xml} file. If the
 * {@code .mvn/extensions.xml} does not exist, it creates it from scratch. If the {@code .mvn/extensions.xml} exists, it
 * adds missing srcdeps core extensions and ensures they have the version specified by {@link #newVersion} parameter.
 *
 * @author <a href="https://github.com/ppalaga">Peter Palaga</a>
 */
@SuppressWarnings("deprecation")
@Mojo(name = "up", defaultPhase = LifecyclePhase.NONE, threadSafe = false, requiresProject = true, requiresDependencyResolution = ResolutionScope.NONE)
public class SrcdepsUpgradeMojo extends AbstractMojo {
    protected final File multiModuleRootDir = new File(System.getProperty("user.dir"));
    protected final Path mvnDir;
    protected final Path extensionsXmlPath;

    /** The encoding to use when reading and writing {@code srcdeps.yaml} and {@code extensions.xml} files */
    @Parameter(defaultValue = "utf-8", property = "srcdeps.encoding")
    protected String encoding;

    public SrcdepsUpgradeMojo() {
        super();
        this.mvnDir = multiModuleRootDir.toPath().resolve(".mvn");
        this.extensionsXmlPath = mvnDir.resolve("extensions.xml");
    }

    /**
     * The version of srcdeps core extensions to add or upgarde to in {@code extensions.xml} file. Default is the
     * version if the {@code srcdeps-maven-plugin} that runs this mojo.
     */
    @Parameter(property = "srcdeps.newVersion")
    protected String newVersion;

    /** The limit for the word wrap in {@code extensions.xml} file */
    @Parameter(defaultValue = "128", property = "srcdeps.xmlLineLength")
    protected int xmlLineLength;

    /** The number of spaces to use for one level of indentation in {@code extensions.xml} file */
    @Parameter(defaultValue = "2", property = "srcdeps.xmlIndentSize")
    protected int xmlIndentSize;

    /**
     * If {@code true} the {@code extensions.xml} file will be indented; otherwise the {@code extensions.xml} file will
     * not be indented.
     */
    @Parameter(defaultValue = "true", property = "srcdeps.xmlIndent")
    protected boolean xmlIndent;

    /** If {@code true} the execution of this mojo will be skipped altogether; otherwise this mojo will be executed. */
    @Parameter(defaultValue = "false", property = "srcdeps.skip")
    protected boolean skip;

    @Parameter(defaultValue = "${session}")
    protected MavenSession session;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        final Log log = getLog();
        if (skip) {
            log.info("srcdeps: ["+ getClass().getSimpleName() + "] skipped per skip parameter");
        } else if (!multiModuleRootDir.equals(session.getCurrentProject().getBasedir())) {
            log.info("srcdeps: ["+ getClass().getSimpleName() + "] skipped because multiModuleRootDir ["+ multiModuleRootDir +"] != current project basedir ["+ session.getCurrentProject().getBasedir() +"]");
        } else {

            if (newVersion == null || newVersion.isEmpty()) {
                newVersion = Constants.SRCDEPS_MAVEN_VERSION;