<commit_before>/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:set ts=4 sw=4 sts=4 et: */
package jp.oist.flint.command;

import jp.oist.flint.component.Component;
import jp.oist.flint.component.Linux;
import jp.oist.flint.component.MacOsX;
import jp.oist.flint.component.Windows;
import jp.oist.flint.headless.Oneshot;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Setup implements Runnable {

    final CommandLineArguments mCommandLineArgs;

    private Runnable mLauncher = null;

    public Setup(final CommandLineArguments commandLineArgs) {
        mCommandLineArgs = commandLineArgs;
    }

    private File setupComponent() {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        // check previous choice
        Preferences prefs = Preferences.userRoot().node("/jp/oist/flint");
        String previous = prefs.get("defaultComponentPath", "");
        File previousFile = new File(previous);
        if (previousFile.isDirectory()) {
            // ask user if available
            int r = JOptionPane.showConfirmDialog(null,
                                                  "Do you use components in " + previous + "?",
                                                  "Default component directory found",
                                                  JOptionPane.YES_NO_OPTION);
            if (r == JOptionPane.YES_OPTION) return previousFile;
        }
        // othewise, user has to specify its path
        fc.setDialogTitle("Specify component directory");
        if (fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
            Logger.getRootLogger().fatal("could not find components");
            System.exit(1); // never return
        }
        File file = fc.getSelectedFile();
        try {
            String path = file.getCanonicalPath();
            prefs.put("defaultComponentPath", path);
            prefs.flush();
        } catch (BackingStoreException bse) {
            Logger.getRootLogger().error(bse.getMessage());
            // print the exception, but just go next
        } catch (IOException ioe) {
            Logger.getRootLogger().fatal(ioe.getMessage());
            System.exit(1); // never return
        }
        return file;
    }

    @Override
    public void run() {
        String osName = System.getProperty("os.name");
        if (osName == null) {
            Logger.getRootLogger().fatal("could not detect OS name");
            System.exit(1); // never return
        }

        if (osName.startsWith("Windows")) {
            Windows windows = new Windows();
            Component.setOs(windows);
        } else if (osName.startsWith("Mac OS X")) {
            MacOsX macOsX = new MacOsX();
            Component.setOs(macOsX);
        } else { // Linux
            Linux linux = new Linux();
            Component.setOs(linux);

            // try to find a command executable in order to check the component directory
            String userDir = System.getProperty("user.dir");
            if (!new File(userDir + File.separator + "bin" + File.separator + "flint-exec").isFile()) {
                File dir = setupComponent();
                Component.setPath(dir);
            }
        }

        if (mCommandLineArgs.isHeadless()) {
            mLauncher = new Oneshot(mCommandLineArgs.getHeadlessOption(), true);
        } else {
            mLauncher = new Launch(mCommandLineArgs);
        }
    }

    public Runnable getLauncher () {
        return mLauncher;<commit_msg>do not ask user if the default component path is available<commit_after>/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:set ts=4 sw=4 sts=4 et: */
package jp.oist.flint.command;

import jp.oist.flint.component.Component;
import jp.oist.flint.component.Linux;
import jp.oist.flint.component.MacOsX;
import jp.oist.flint.component.Windows;
import jp.oist.flint.headless.Oneshot;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Setup implements Runnable {

    private static final String REFERENCE_PROGRAM_NAME = "flint-exec";

    final CommandLineArguments mCommandLineArgs;

    private Runnable mLauncher = null;

    public Setup(final CommandLineArguments commandLineArgs) {
        mCommandLineArgs = commandLineArgs;
    }

    private File setupComponent() {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        // check previous choice
        Preferences prefs = Preferences.userRoot().node("/jp/oist/flint");
        String previous = prefs.get("defaultComponentPath", "");
        File previousFile = new File(previous);
        if ( previousFile.isDirectory() &&
             new File(previousFile, REFERENCE_PROGRAM_NAME).isFile() ) {
            // do not ask user if available
            return previousFile;
        }
        // othewise, user has to specify its path
        fc.setDialogTitle("Specify component directory");
        if (fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
            Logger.getRootLogger().fatal("could not find components");
            System.exit(1); // never return
        }
        File file = fc.getSelectedFile();
        try {
            String path = file.getCanonicalPath();
            prefs.put("defaultComponentPath", path);
            prefs.flush();
        } catch (BackingStoreException bse) {
            Logger.getRootLogger().error(bse.getMessage());
            // print the exception, but just go next
        } catch (IOException ioe) {
            Logger.getRootLogger().fatal(ioe.getMessage());
            System.exit(1); // never return
        }
        return file;
    }

    @Override
    public void run() {
        String osName = System.getProperty("os.name");
        if (osName == null) {
            Logger.getRootLogger().fatal("could not detect OS name");
            System.exit(1); // never return
        }

        if (osName.startsWith("Windows")) {
            Windows windows = new Windows();
            Component.setOs(windows);
        } else if (osName.startsWith("Mac OS X")) {
            MacOsX macOsX = new MacOsX();
            Component.setOs(macOsX);
        } else { // Linux
            Linux linux = new Linux();
            Component.setOs(linux);

            // try to find a command executable in order to check the component directory
            String userDir = System.getProperty("user.dir");
            File binDir = new File(userDir, "bin");
            if ( !binDir.isDirectory() ||
                 !new File(binDir, REFERENCE_PROGRAM_NAME).isFile() ) {
                File dir = setupComponent();
                Component.setPath(dir);
            }
        }

        if (mCommandLineArgs.isHeadless()) {
            mLauncher = new Oneshot(mCommandLineArgs.getHeadlessOption(), true);
        } else {
            mLauncher = new Launch(mCommandLineArgs);
        }
    }

    public Runnable getLauncher () {
        return mLauncher;
{'fraction_non_alphanumeric': 0.10870150862068965, 'fraction_numerical': 0.0024245689655172415, 'mean_word_length': 2.2059585492227978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213781', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2034, 'n_words': 482}