Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

169
LINES

< > BotCompany Repo | #1034723 // G22AutoStarter - runs auto-start scripts on project open

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (63226L) is out of date.

srecord noeq G22AutoStarter(G22Utils g22utils) > MetaWithChangeListeners is AutoCloseable {
  settableWithVar volatile bool enabled = true;
  settableWithVar volatile int initialDelay = 1;
  settableWithVar volatile int nScriptsRun;
  settableWithVar volatile AutoStartScript currentScript;
  settableWithVar volatile bool cancelOnCtrl;

  event fireDone;
  
  new Flag started;
  new Flag waited;
  new Flag ctrlPressed;
  L<AutoStartScript> scripts = syncL();
  AutoCloseable ctrlListener;
  ReliableSingleThread rst = rst(l0 _run);
  
  JComponent topStackComponent;
  JProgressBar progressBar;

  record noeq AutoStartScript(G22LeftArrowScript script) > RunResultWithTimestamps {
    run {
      LASCompileResult cr = script.compileForAutoRun();
      if (cr == null) cr = script.compileSaved();
      reMutable cr;
      run(-> {
        G22AutoStarter.this.change();
        if (cr == null) fail("Script is not saved: " + script);
        var parsed = cr.parsedScriptMandatory();
        ret parsed!;
      });
    }
    
    toString { ret str(script); }
  }
  
  void init() {
    if (ctrlListener != null) ret;
    
    scripts.clear();
    
    var scripts = conceptsWhere(g22utils.concepts, G22LeftArrowScript, runOnProjectOpen := true);
    var sorted = sortByCalculatedFieldAlphaNumIC(scripts, s -> s.runOrder);
    for (script : sorted)
      this.scripts.add(new AutoStartScript(script));
    change();
    
    ctrlListener = tempAddGlobalCtrlKeyListener(b -> {
      if (cancelOnCtrl && b) {
        ctrlPressed.raise();
        cancel();
      }
    });
  }
  
  void start {
    print("G22AutoStarter: Start");
    if (enabled) {
      var topStack = g22utils.projectActions().topStack();
      //topStackComponent = jCenteredLabel("Autostart in progress...");
      progressBar = jProgressBarWithText(l(scripts));
      topStackComponent = withTopMargin(jCenteredSection("Booting project",
        centerAndEastWithMargin(
          progressBar,
          jThreadedButton("Cancel", -> {
            disableButton(heldButton());
            cancel();
          }))));
      topStack.addComponent(topStackComponent);
      
      rst!;
    }
  }
  
  void _run {
    if (!enabled) ret;
    
    temp g22utils.backgroundProcessesUI.tempAdd("Auto-Start Scripts");
    if (started.raise()) change();
    
    if (!waited!) {
      print("G22AutoStarter: Sleeping");
      while (initialDelay > 0) {
        sleepSeconds(1);
        initialDelay(initialDelay-1);
      }
      waited.raise();
      change();
      print("G22AutoStarter: Slept");
    }
    
    while (enabled && !done()) {
      var script = scripts.get(nScriptsRun);
      currentScript(script);
      setProgressBarValue(progressBar, nScriptsRun);
      setProgressBarText(progressBar, str(script));
      change();
      script.run();
      nScriptsRun(nScriptsRun+1);
      currentScript(null);
      change();
    }
    
    var topStack = g22utils.projectActions().topStack();
    topStack.removeComponent(topStackComponent);
    topStackComponent = null;
    progressBar = null;
    
    fireDone();
    change();
    close();
  }
  
  bool done() { ret nScriptsRun >= l(scripts); }
  bool waitedAndDone() { ret waited! && done(); }
  
  void cancel { setEnabled(false); }
  
  bool canResume() { ret !done() && !enabled; }
  
  void resume {
    if (done())
      ret with infoBox("Nothing to resume - auto-start is done");
    if (enabled)
      ret with infoBox("Already running");
    infoBox("Resuming auto-start");
    setEnabled(true);
    rst!;
  }
  
  S status() {
    ret !enabled ?
      ctrlPressed!
        ? "Cancelled due to ctrl key"
        : "Cancelled"
      : done() ? "Done"
      : waited! ? "Started" 
      : started! ? "Pre-start wait (" + formatDouble1(initialDelay) + "s)"
      : "Not started";
  }
  
  S stats() {
    ret status() + ". " + scriptsRunStats();
  }
  
  S scriptsRunStats() {
    ret "Scripts run: " + nScriptsRun() + "/" + l(scripts);
  }
  
  S currentScriptStats() {
    var s = currentScript;
    if (s == null) ret "";
    ret "Running: " + s.script;
  }
  
  close {
    if (ctrlListener != null) {
      dispose ctrlListener;
      change();
    }
  }
  
  void waitUntilDone {
    waitForVarPredicate(varNScriptsRun(), -> done());
  }
  
  bool ctrlEnabled() { ret ctrlListener != null; }
  
  bool cancelled() { ret !enabled; }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034723
Snippet name: G22AutoStarter - runs auto-start scripts on project open
Eternal ID of this version: #1034723/54
Text MD5: 352738b9423bd33a5afdf2f522cbaeca
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-10-26 17:05:20
Source code size: 4540 bytes / 169 lines
Pitched / IR pitched: No / No
Views / Downloads: 310 / 645
Version history: 53 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)