Newer
Older
IoTP / src / main / java / info / istlab / IoTP / ScriptRunner.java
package info.istlab.IoTP;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;

/**
 * スクリプトファイルの実行
 */
public class ScriptRunner implements Runnable {
    String scriptfile;
    String srcfile;

    Thread thread;
    Process process;
    JButton runB;
    String originalButtonLabel;

    public ScriptRunner(String shfile, JButton rB, String targetfile) {
        scriptfile = shfile;
        srcfile = targetfile;
        runB = rB;
    }

    public void startstop() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
            // originalButtonLabel = runB.getText();
            // runB.setText("Stop");
        } else {
            process.destroyForcibly();
            thread = null;
            // runB.setText(originalButtonLabel);
        }
    }

    @Override
    public void run() {
        // Run script
        // System.out.println(scriptfile);
        ArrayList<String> comlist = new ArrayList<String>();
        if (App.isWindows) {
            SerialWindow.check();
            comlist.add("cmd");
            comlist.add("/c");
            comlist.add("start");
            comlist.add("Upload.bat");
            if (srcfile.length() > 0)
                comlist.add(srcfile);
            if (App.serialName != null)
                comlist.add(App.serialName);
        } else {
            comlist.add("bash");
            comlist.add("Upload.sh");
            if (srcfile.length() > 0)
                comlist.add(srcfile);
        }

        ProcessBuilder processBuilder = new ProcessBuilder(comlist);
        // ProcessBuilder processBuilder = new ProcessBuilder("pwd");
        processBuilder.directory(new File(App.workingDir));
        // System.out.println(App.workingDir);
        // processBuilder.inheritIO();
        JTAConsole con = new JTAConsole("(Exec) " + scriptfile + " " + srcfile);
        // con.setMainSrcByFileName(scriptfile);
        con.Systemoutprintln("=== 実行開始 ===");
        // con.Systemoutprintln(CommandRunner.prompt+"cd ~" + App.nwpsrc+" (注:~ はチルダ記号〜
        // で,ホームディレクトリを表します)");
        // for (String s : comlist) {
        // con.Systemoutprintln(s);
        // }
        // Map<String, String> envs = processBuilder.environment();
        // System.out.println(envs.get("Path"));
        // envs.put("Path", "C:\\Users\\istlab\\bin");
        // processBuilder.redirectErrorStream();

        try {
            process = processBuilder.start();
            con.startBR(process);

            // BufferedReader reader = new BufferedReader(new
            // InputStreamReader(process.getInputStream()));
            // String line;
            // while ((line = reader.readLine()) != null) {
            // jta.append(line);
            // }
            process.waitFor();
        } catch (IOException | InterruptedException e1) {
            e1.printStackTrace();
        }
        con.Systemoutprintln("=== 終了 === (ALT+Wで閉じる)");
        thread = null;
        // runB.setText("Run");
        SerialWindow.invoke();
        if (App.isWindows) {
            con.frame.dispose();
        }
    }

}