Java Web Start版 Groovy Console をちょっとだけ変えてみた その2

最近やたらと Java Web Start 版の Groovy Console を使うことが多くなってきたので もうちょっとだけ変更してみた。

今回の変更点は以下…

  • Grape が使えなかったので ivy-2.2.0-rc1.jar を読み込むようにしてみた。
  • コンソール起動時に前回終了した時のコンソールの内容を初期表示するようにしてみた。
    → コンソールの内容は終了した時に ${user.home}/.groovy/groovyConsole.history に保存される。

Groovy Console 起動用のスクリプトはこんな感じ...

package com.bluepapa32.groovy

import javax.swing.*;
import groovy.ui.*;

class ConsoleLauncher {
    static void main(args) {
        Console.metaClass.constructor = { ClassLoader parent ->
            init(new Console(parent, new Binding()));
        }
        Console.metaClass.run  = run;
        Console.metaClass.exit = exit;
        Console.main(args);
    }

    static def _run = Console.metaClass.getMetaMethod("run");
    static def run = {
        _run.invoke(delegate);
        load(delegate);
    }

    static def _exit = Console.metaClass.getMetaMethod("exit", EventObject.class);
    static def exit = { EventObject e ->
        _exit.invoke(delegate, e);

        if (delegate.consoleControllers.empty) {
            save(delegate);
        }
    }

    static def home  = System.getProperty("user.home");
    static def ghome = "${home}/.groovy";

    static Console init(Console console) {
        def lib  = new File("${ghome}/lib/");
        if (lib.exists()) {
            lib.eachFileMatch ~/.*\.jar/, {
                console.shell.classLoader.addURL(it.toURL());
            }
        }
        return console;
    }

    static void load(Console console) {
        def f  = new File("${ghome}/groovyConsole.history");
        if (f.exists()) {
            SwingUtilities.invokeLater {
                console.inputArea.setText(f.getText());
            }
        }
    }

    static void save(Console console) {
        def f  = new File("${ghome}/groovyConsole.history");
        if (f.parentFile.exists()) {
            f.write(console.inputArea.getText());
        }
    }
}