Newer
Older
IoTP / src / main / java / info / istlab / IoTP / ScriptRunner.java
@motoki miura motoki miura on 6 Apr 2023 4 KB v0.36
package info.istlab.IoTP;

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

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

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

    public ScriptRunner(String shfile, String targetfile) {
        scriptfile_base = shfile;
        scriptfile = (App.isWindows) ? scriptfile_base + ".bat" : scriptfile_base + ".sh";
        srcfile = targetfile;

        prepareComList();
    }

    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);
        }
    }

    ArrayList<String> comlist;

    public void prepareComList() {
        comlist = new ArrayList<String>();
        if (App.isWindows) {
            SerialWindow.check(false);
            comlist.add("cmd");
            comlist.add("/c");
            comlist.add("start");
            comlist.add(scriptfile);
            if (srcfile != null && srcfile.length() > 0)
                comlist.add(srcfile);
            if (App.serialName != null)
                comlist.add(App.serialName);
        } else { //MacOS, Linux
            comlist.add("bash");
            // comlist.add("-l");
            comlist.add(scriptfile);
            if (srcfile != null && srcfile.length() > 0)
                comlist.add(srcfile);
            if (App.serialName != null)
                comlist.add(App.serialName);
        }

        // 現在選択しているシリアルコンソールを閉じる
        for (String key : SerialWindow.hash.keySet()) {
            if (!key.equals(App.serialName))
                continue; // 選択していないものだったら閉じない。
            SerialWindow swin = SerialWindow.hash.get(key);
            if (swin != null) {
                swin.closeSerialPort(null);
            }
        }
        // 既存のコンパイルコンソールをすべて閉じる
        while (!JTAConsole.winStack.isEmpty()) {
            JTAConsole jcon = JTAConsole.winStack.pop();
            jcon.closeWin();
        }
    }

    @Override
    public void run() {

        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("=== 実行開始 ===");
        if (App.isWindows) {
            con.Systemoutprintln("=== バッチファイルを実行します ===");
            con.Systemoutprintln(String.join(" ", comlist.toArray(new String[] {})));
        }
        // 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");
        if (scriptfile_base.startsWith("Upload")) {
            SerialWindow.disposeByName(App.serialName);
            SerialWindow.invoke(false); // no serial connectionsのとき、メッセージ表示しない
        }
        if (App.isWindows) {
            con.windowClosing(null);
            con.frame.dispose();
        }
    }

}