<commit_before>/*
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package se.tla.mavenversionbumper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;

import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.jdom.JDOMException;

import se.tla.mavenversionbumper.vcs.AbstractVersionControl;
import se.tla.mavenversionbumper.vcs.Clearcase;
import se.tla.mavenversionbumper.vcs.Git;
import se.tla.mavenversionbumper.vcs.NoopVersionControl;
import se.tla.mavenversionbumper.vcs.VersionControl;
import bsh.EvalError;
import bsh.Interpreter;

/**
 * Command line interface for the version bumper.
 */
public class Main {

    private static final List<Module> modulesLoadedForUpdate = new LinkedList<Module>();

    private static String baseDirName;
    private static VersionControl versionControl = new NoopVersionControl();
    private static final Map<String, Class<? extends VersionControl>> versionControllers;

    static {
        versionControllers = new HashMap<String, Class<? extends VersionControl>>();
        // Only lower case names.
        versionControllers.put("git", Git.class);
        versionControllers.put("clearcase", Clearcase.class);
    }

    enum Option {
        DRYRUN("Dry run. Don't modify anything, only validate configuration.", "d", "dry-run"),
        REVERT("Revert any uncommited changes.", "r", "revert"),
        PREPARETEST("Prepare module(s) for a test build.", "p", "prepare-test-build"),
        WARNOFSNAPSHOTS("Searches for any SNAPSHOT dependencies and warns about them. Works great with --dry-run.", "w", "warn-snapshots"),
        REVERSEENGINEER("Build a basic scenario file from a set of modules.", "reverse-engineer"),
        HELP("Show help.", "h", "?", "help");

        private final String helpText;
        private final String[] aliases;

        Option(String helpText, String... aliases) {
            this.helpText = helpText;
            this.aliases = aliases;
        }

        public List<String> getAliases() {
            return Arrays.asList(aliases);
        }

        public String getHelpText() {
            return helpText;
        }

        public boolean presentIn(OptionSet o) {
            return o.has(aliases[0]);
        }
    }

    enum TYPE {
        NORMAL, DRYRUN, REVERT, PREPARETEST
    }

    public static void main(String args[]) {

        OptionParser parser = new OptionParser() {
            {
                acceptsAll(Option.DRYRUN.getAliases(), Option.DRYRUN.getHelpText());
                acceptsAll(Option.PREPARETEST.getAliases(), Option.PREPARETEST.getHelpText());
                acceptsAll(Option.REVERT.getAliases(), Option.REVERT.getHelpText());
                acceptsAll(Option.WARNOFSNAPSHOTS.getAliases(), Option.WARNOFSNAPSHOTS.getHelpText());
                acceptsAll(Option.REVERSEENGINEER.getAliases(), Option.REVERSEENGINEER.getHelpText());
                acceptsAll(Option.HELP.getAliases(), Option.HELP.getHelpText());
            }
        };

        OptionSet options = null;
        try {
            options = parser.parse(args);
        } catch (OptionException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

        if (Option.HELP.presentIn(options)) {
            try {
                parser.printHelpOn(System.out);
            } catch (IOException e) {
                System.err.println("Error printing help text: " + e.getMessage());
                System.exit(1);
            }
            System.exit(0);
        }

        if (countTrues(Option.DRYRUN.presentIn(options),
                Option.PREPARETEST.presentIn(options),
                Option.REVERT.presentIn(options),
                Option.REVERSEENGINEER.presentIn(options)) > 1) {
            System.err.println("Only one of --dry-run/-d, --prepare-test-build/-p, --revert/-r and --reverse-engineer");
            System.exit(1);
        }

        List<String> arguments = options.nonOptionArguments();

        if (arguments.size() < 2 || arguments.size() > 3) {
            System.err.println("Usage: [-d | --dry-run] [-p | --prepare-test-build] [-r | --revert] [--reverse-engineer] [-w | --warn-snapshot] [-h | --help] <base directory> <scenarioFile> [<VC properties file>]");
            System.exit(1);
        }

        baseDirName = arguments.get(0);
        String scenarioFileName = arguments.get(1);
        Properties versionControlProperties;

        if (arguments.size() == 3) {
            try {
                String versionControlParameter = arguments.get(2);
                versionControlProperties = new Properties();
                versionControlProperties.load(new AutoCloseInputStream(new FileInputStream(versionControlParameter)));

                String versionControlName = versionControlProperties.getProperty(AbstractVersionControl.VERSIONCONTROL, "").toLowerCase();
                Class<? extends VersionControl> versionControlClass = versionControllers.get(versionControlName);
                if (versionControlClass == null) {
                    System.err.println("No such version control: " + versionControlName);
                    System.exit(1);
                }

                versionControl = versionControlClass.getConstructor(Properties.class).newInstance(versionControlProperties);
            } catch (Exception e) {
                System.err.println("Error starting up the version control");
                e.printStackTrace();
                System.exit(1);
            }
        }
        File scenarioFile = new File(scenarioFileName);

        if (Option.REVERSEENGINEER.presentIn(options)) {
            if (scenarioFile.exists()) {
                System.err.println("Scenario file " + scenarioFileName + " mustn't exist in reverse engineering mode.");
                System.exit(1);
            }

            File baseDir = new File(baseDirName);
            if (!baseDir.isDirectory()) {
                System.err.println("Base directory " + baseDirName + " isn't a directory.");
                System.exit(1);
            }

            try {
                List<ReverseEngineeringModule> modules = findModulesForReverseEngineering(baseDir, "");

                reverseEngineerModules(modules, scenarioFile);
            } catch (JDOMException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        if (!(scenarioFile.isFile() && scenarioFile.canRead())) {
            System.err.println("Scenario file " + scenarioFileName + " isn't a readable file.");
            System.exit(1);
        }

        if (Option.REVERT.presentIn(options) && versionControl == null) {
            System.err.println("Version control has to be defined while reverting.");
            System.exit(1);
        }

        TYPE type = TYPE.NORMAL;
        if (Option.DRYRUN.presentIn(options)) {
            type = TYPE.DRYRUN;
        }
        if (Option.REVERT.presentIn(options)) {
            type = TYPE.REVERT;
        }
        if (Option.PREPARETEST.presentIn(options)) {
            type = TYPE.PREPARETEST;
        }
<commit_msg>Fixed parameter check bug.<commit_after>/*
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package se.tla.mavenversionbumper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;

import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.jdom.JDOMException;

import se.tla.mavenversionbumper.vcs.AbstractVersionControl;
import se.tla.mavenversionbumper.vcs.Clearcase;
import se.tla.mavenversionbumper.vcs.Git;
import se.tla.mavenversionbumper.vcs.NoopVersionControl;
import se.tla.mavenversionbumper.vcs.VersionControl;
import bsh.EvalError;
import bsh.Interpreter;

/**
 * Command line interface for the version bumper.
 */
public class Main {

    private static final List<Module> modulesLoadedForUpdate = new LinkedList<Module>();

    private static String baseDirName;
    private static VersionControl versionControl = new NoopVersionControl();
    private static final Map<String, Class<? extends VersionControl>> versionControllers;

    static {
        versionControllers = new HashMap<String, Class<? extends VersionControl>>();
        // Only lower case names.
        versionControllers.put("git", Git.class);
        versionControllers.put("clearcase", Clearcase.class);
    }

    enum Option {
        DRYRUN("Dry run. Don't modify anything, only validate configuration.", "d", "dry-run"),
        REVERT("Revert any uncommited changes.", "r", "revert"),
        PREPARETEST("Prepare module(s) for a test build.", "p", "prepare-test-build"),
        WARNOFSNAPSHOTS("Searches for any SNAPSHOT dependencies and warns about them. Works great with --dry-run.", "w", "warn-snapshots"),
        REVERSEENGINEER("Build a basic scenario file from a set of modules.", "reverse-engineer"),
        HELP("Show help.", "h", "?", "help");

        private final String helpText;
        private final String[] aliases;

        Option(String helpText, String... aliases) {
            this.helpText = helpText;
            this.aliases = aliases;
        }

        public List<String> getAliases() {
            return Arrays.asList(aliases);
        }

        public String getHelpText() {
            return helpText;
        }

        public boolean presentIn(OptionSet o) {
            return o.has(aliases[0]);
        }
    }

    enum TYPE {
        NORMAL, DRYRUN, REVERT, PREPARETEST
    }

    public static void main(String args[]) {

        OptionParser parser = new OptionParser() {
            {
                acceptsAll(Option.DRYRUN.getAliases(), Option.DRYRUN.getHelpText());
                acceptsAll(Option.PREPARETEST.getAliases(), Option.PREPARETEST.getHelpText());
                acceptsAll(Option.REVERT.getAliases(), Option.REVERT.getHelpText());
                acceptsAll(Option.WARNOFSNAPSHOTS.getAliases(), Option.WARNOFSNAPSHOTS.getHelpText());
                acceptsAll(Option.REVERSEENGINEER.getAliases(), Option.REVERSEENGINEER.getHelpText());
                acceptsAll(Option.HELP.getAliases(), Option.HELP.getHelpText());
            }
        };

        OptionSet options = null;
        try {
            options = parser.parse(args);
        } catch (OptionException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

        if (Option.HELP.presentIn(options)) {
            try {
                parser.printHelpOn(System.out);
            } catch (IOException e) {
                System.err.println("Error printing help text: " + e.getMessage());
                System.exit(1);
            }
            System.exit(0);
        }

        if (countTrues(Option.DRYRUN.presentIn(options),
                Option.PREPARETEST.presentIn(options),
                Option.REVERT.presentIn(options),
                Option.REVERSEENGINEER.presentIn(options)) > 1) {
            System.err.println("Only one of --dry-run/-d, --prepare-test-build/-p, --revert/-r and --reverse-engineer");
            System.exit(1);
        }

        List<String> arguments = options.nonOptionArguments();

        if (arguments.size() < 2 || arguments.size() > 3) {
            System.err.println("Usage: [-d | --dry-run] [-p | --prepare-test-build] [-r | --revert] [--reverse-engineer] [-w | --warn-snapshot] [-h | --help] <base directory> <scenarioFile> [<VC properties file>]");
            System.exit(1);
        }

        baseDirName = arguments.get(0);
        String scenarioFileName = arguments.get(1);
        Properties versionControlProperties;

        if (arguments.size() == 3) {
            try {
                String versionControlParameter = arguments.get(2);
                versionControlProperties = new Properties();
                versionControlProperties.load(new AutoCloseInputStream(new FileInputStream(versionControlParameter)));

                String versionControlName = versionControlProperties.getProperty(AbstractVersionControl.VERSIONCONTROL, "").toLowerCase();
                Class<? extends VersionControl> versionControlClass = versionControllers.get(versionControlName);
                if (versionControlClass == null) {
                    System.err.println("No such version control: " + versionControlName);
                    System.exit(1);
                }

                versionControl = versionControlClass.getConstructor(Properties.class).newInstance(versionControlProperties);
            } catch (Exception e) {
                System.err.println("Error starting up the version control");
                e.printStackTrace();
                System.exit(1);
            }
        }
        File scenarioFile = new File(scenarioFileName);

        if (Option.REVERSEENGINEER.presentIn(options)) {
            if (scenarioFile.exists()) {
                System.err.println("Scenario file " + scenarioFileName + " mustn't exist in reverse engineering mode.");
                System.exit(1);
            }

            File baseDir = new File(baseDirName);
            if (!baseDir.isDirectory()) {
                System.err.println("Base directory " + baseDirName + " isn't a directory.");
                System.exit(1);
            }

            try {
                List<ReverseEngineeringModule> modules = findModulesForReverseEngineering(baseDir, "");

                reverseEngineerModules(modules, scenarioFile);
            } catch (JDOMException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        if (!(scenarioFile.isFile() && scenarioFile.canRead())) {
            System.err.println("Scenario file " + scenarioFileName + " isn't a readable file.");
            System.exit(1);
        }

        if (Option.REVERT.presentIn(options) && versionControl instanceof NoopVersionControl) {
            System.err.println("Version control has to be defined while reverting.");
            System.exit(1);
        }

        TYPE type = TYPE.NORMAL;
        if (Option.DRYRUN.presentIn(options)) {
            type = TYPE.DRYRUN;
        }
        if (Option.REVERT.presentIn(options)) {
            type = TYPE.REVERT;
        }
        if (Option.PREPARETEST.presentIn(options)) {
            type = TYPE.PREPARETEST;
        }

{'fraction_non_alphanumeric': 0.10930050093548192, 'fraction_numerical': 0.0027762689359647533, 'mean_word_length': 2.407361710878059, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '1556087', 'n_tokens_mistral': 4581, 'n_tokens_neox': 4392, 'n_words': 1074}