<commit_before>package org.mockito.release.internal.gradle;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Exec;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.mockito.release.gradle.BintrayPlugin;
import org.mockito.release.gradle.BumpVersionFileTask;
import org.mockito.release.gradle.ContinuousDeliveryPlugin;
import org.mockito.release.internal.gradle.util.CommonSettings;
import org.mockito.release.internal.gradle.util.ExtContainer;
import org.mockito.release.version.VersionFile;

import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import static java.util.Arrays.asList;
import static org.mockito.release.internal.gradle.util.StringUtil.join;

/**
 * Please keep documentation up to date at {@link ContinuousDeliveryPlugin}
 */
public class DefaultContinuousDeliveryPlugin implements ContinuousDeliveryPlugin {

    private static final Logger LOG = Logging.getLogger(DefaultContinuousDeliveryPlugin.class);

    public void apply(final Project project) {
        project.getPlugins().apply("org.mockito.release-notes");
        project.getPlugins().apply("org.mockito.release-tools.versioning");

        final ExtContainer ext = new ExtContainer(project);

        ((BumpVersionFileTask) project.getTasks().getByName("bumpVersionFile"))
                .setUpdateNotableVersions(isNotableRelease(project));

        CommonSettings.execTask(project, "gitAddBumpVersion", new Action<Exec>() {
            public void execute(Exec t) {
                t.setDescription("Performs 'git add' for the version properties file");

                //TODO dependency/assumptions on versioning plugin (move to git plugin this and other tasks?):
                t.mustRunAfter("bumpVersionFile");
                t.commandLine("git", "add", "version.properties");
            }
        });

        configureNotableReleaseNotes(project);

        CommonSettings.execTask(project, "gitAddReleaseNotes", new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Performs 'git add' for the release notes file");
                t.mustRunAfter("updateReleaseNotes", "updateNotableReleaseNotes");
                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                        //doFirst (execution time)
                        // so that we can access user-configured properties
                        t.commandLine("git", "add", ext.getReleaseNotesFile(), ext.getNotableReleaseNotesFile());
                    }
                });
            }
        });

        CommonSettings.execTask(project, "gitCommit", new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Commits staged changes using generic --author");
                t.mustRunAfter("gitAddBumpVersion", "gitAddReleaseNotes");
                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                    //doFirst (execution time) to pick up user-configured setting
                    t.commandLine("git", "commit", "--author",
                        ext.getGitGenericUserNotation(), "-m", commitMessage("Bumped version and updated release notes"));
                    }
                });
            }
        });

        CommonSettings.execTask(project, "gitTag", new Action<Exec>() {
            public void execute(Exec t) {
                String tag = "v" + project.getVersion();
                t.setDescription("Creates new version tag '" + tag + "'");
                t.commandLine("git", "tag", "-a", tag, "-m", commitMessage("Created new tag " + tag));
            }
        });

        boolean mustBeQuiet = true; //so that we don't expose the token
        CommonSettings.execTask(project, "gitPush", mustBeQuiet, new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Pushes changes to remote repo.");
                t.mustRunAfter("gitCommit", "gitTag");

                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                        t.commandLine(ext.getQuietGitPushArgs());

                        //!!!We must capture and hide the output because when git push fails it can expose the token!
                        ByteArrayOutputStream output = new ByteArrayOutputStream();
                        t.setStandardOutput(output);
                        t.setErrorOutput(output);
                    }
                });
            }
        });

        final Task bintrayUploadAll = CommonSettings.task(project, "bintrayUploadAll", new Action<Task>() {
            public void execute(Task t) {
                t.setDescription("Depends on all 'bintrayUpload' tasks from all Gradle projects.");
                //It is safer to run bintray upload after git push
                //This way, when git push fails we don't publish jars to bintray
                t.mustRunAfter("gitPush");
            }
        });

        project.allprojects(new Action<Project>() {
            public void execute(final Project project) {
                project.getPlugins().withType(BintrayPlugin.class, new Action<BintrayPlugin>() {
                    public void execute(BintrayPlugin bintrayPlugin) {
                        Task bintrayUpload = project.getTasks().getByName(BintrayPlugin.BINTRAY_UPLOAD_TASK);
                        bintrayUploadAll.dependsOn(bintrayUpload);
                    }
                });
            }
        });

        CommonSettings.task(project, "performRelease", new Action<Task>() {
            public void execute(final Task t) {
                t.setDescription("Performs release. To test release use './gradlew testRelease'");

                t.dependsOn("bumpVersionFile", "updateReleaseNotes", "updateNotableReleaseNotes");
                t.dependsOn("gitAddBumpVersion", "gitAddReleaseNotes", "gitCommit", "gitTag");
                t.dependsOn("gitPush");<commit_msg>Made git push and bintray upload run very last<commit_after>package org.mockito.release.internal.gradle;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.gradle.api.tasks.Exec;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.mockito.release.gradle.BintrayPlugin;
import org.mockito.release.gradle.BumpVersionFileTask;
import org.mockito.release.gradle.ContinuousDeliveryPlugin;
import org.mockito.release.internal.gradle.util.CommonSettings;
import org.mockito.release.internal.gradle.util.ExtContainer;
import org.mockito.release.version.VersionFile;

import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import static java.util.Arrays.asList;
import static org.mockito.release.internal.gradle.util.StringUtil.join;

/**
 * Please keep documentation up to date at {@link ContinuousDeliveryPlugin}
 */
public class DefaultContinuousDeliveryPlugin implements ContinuousDeliveryPlugin {

    private static final Logger LOG = Logging.getLogger(DefaultContinuousDeliveryPlugin.class);

    public void apply(final Project project) {
        project.getPlugins().apply("org.mockito.release-notes");
        project.getPlugins().apply("org.mockito.release-tools.versioning");

        final ExtContainer ext = new ExtContainer(project);

        ((BumpVersionFileTask) project.getTasks().getByName("bumpVersionFile"))
                .setUpdateNotableVersions(isNotableRelease(project));

        CommonSettings.execTask(project, "gitAddBumpVersion", new Action<Exec>() {
            public void execute(Exec t) {
                t.setDescription("Performs 'git add' for the version properties file");

                //TODO dependency/assumptions on versioning plugin (move to git plugin this and other tasks?):
                t.mustRunAfter("bumpVersionFile");
                t.commandLine("git", "add", "version.properties");
            }
        });

        configureNotableReleaseNotes(project);

        CommonSettings.execTask(project, "gitAddReleaseNotes", new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Performs 'git add' for the release notes file");
                t.mustRunAfter("updateReleaseNotes", "updateNotableReleaseNotes");
                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                        //doFirst (execution time)
                        // so that we can access user-configured properties
                        t.commandLine("git", "add", ext.getReleaseNotesFile(), ext.getNotableReleaseNotesFile());
                    }
                });
            }
        });

        CommonSettings.execTask(project, "gitCommit", new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Commits staged changes using generic --author");
                t.mustRunAfter("gitAddBumpVersion", "gitAddReleaseNotes");
                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                    //doFirst (execution time) to pick up user-configured setting
                    t.commandLine("git", "commit", "--author",
                        ext.getGitGenericUserNotation(), "-m", commitMessage("Bumped version and updated release notes"));
                    }
                });
            }
        });

        CommonSettings.execTask(project, "gitTag", new Action<Exec>() {
            public void execute(Exec t) {
                String tag = "v" + project.getVersion();
                t.setDescription("Creates new version tag '" + tag + "'");
                t.commandLine("git", "tag", "-a", tag, "-m", commitMessage("Created new tag " + tag));
            }
        });

        boolean mustBeQuiet = true; //so that we don't expose the token
        final Task gitPush = CommonSettings.execTask(project, "gitPush", mustBeQuiet, new Action<Exec>() {
            public void execute(final Exec t) {
                t.setDescription("Pushes changes to remote repo.");
                t.mustRunAfter("gitCommit", "gitTag");

                t.doFirst(new Action<Task>() {
                    public void execute(Task task) {
                        t.commandLine(ext.getQuietGitPushArgs());

                        //!!!We must capture and hide the output because when git push fails it can expose the token!
                        ByteArrayOutputStream output = new ByteArrayOutputStream();
                        t.setStandardOutput(output);
                        t.setErrorOutput(output);
                    }
                });

                //Add soft ordering rule to make git push run as late as possible
                //it is an operation that is hard to roll back / recover
                t.shouldRunAfter(project.getTasks().matching(Specs.SATISFIES_ALL));
            }
        });

        final Task bintrayUploadAll = CommonSettings.task(project, "bintrayUploadAll", new Action<Task>() {
            public void execute(Task t) {
                t.setDescription("Depends on all 'bintrayUpload' tasks from all Gradle projects.");
                //It is safer to run bintray upload after git push
                //This way, when git push fails we don't publish jars to bintray
                t.mustRunAfter("gitPush");

                //Add soft ordering rule to make bintray upload run as late as possible
                //it is an operation that is hard to roll back / recover
                t.shouldRunAfter(project.getTasks().matching(Specs.SATISFIES_ALL));
            }
        });

        project.allprojects(new Action<Project>() {
            public void execute(final Project project) {
                project.getPlugins().withType(BintrayPlugin.class, new Action<BintrayPlugin>() {
                    public void execute(BintrayPlugin bintrayPlugin) {
                        Task bintrayUpload = project.getTasks().getByName(BintrayPlugin.BINTRAY_UPLOAD_TASK);
                        bintrayUploadAll.dependsOn(bintrayUpload);

                        //Add soft ordering rule to make git push run as late as possible
                        //it is an operation that is hard to roll back / recover
                        gitPush.shouldRunAfter(bintrayUpload);
                    }
                });
            }
        });

        CommonSettings.task(project, "performRelease", new Action<Task>() {
            public void execute(final Task t) {
                t.setDescription("Performs release. To test release use './gradlew testRelease'");

                t.dependsOn("bumpVersionFile", "updateReleaseNotes", "updateNotableReleaseNotes");
                t.dependsOn("gitAddBumpVersion", "gitAddReleaseNotes", "gitCommit", "gitTag");
                t.dependsOn("gitPush");
{'fraction_non_alphanumeric': 0.10349744821374962, 'fraction_numerical': 0.0, 'mean_word_length': 2.210843373493976, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774419', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3272, 'n_words': 817}