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.

1  
srecord noeq G22AutoStarter(G22Utils g22utils) > MetaWithChangeListeners is AutoCloseable {
2  
  settableWithVar volatile bool enabled = true;
3  
  settableWithVar volatile int initialDelay = 1;
4  
  settableWithVar volatile int nScriptsRun;
5  
  settableWithVar volatile AutoStartScript currentScript;
6  
  settableWithVar volatile bool cancelOnCtrl;
7  
8  
  event fireDone;
9  
  
10  
  new Flag started;
11  
  new Flag waited;
12  
  new Flag ctrlPressed;
13  
  L<AutoStartScript> scripts = syncL();
14  
  AutoCloseable ctrlListener;
15  
  ReliableSingleThread rst = rst(l0 _run);
16  
  
17  
  JComponent topStackComponent;
18  
  JProgressBar progressBar;
19  
20  
  record noeq AutoStartScript(G22LeftArrowScript script) > RunResultWithTimestamps {
21  
    run {
22  
      LASCompileResult cr = script.compileForAutoRun();
23  
      if (cr == null) cr = script.compileSaved();
24  
      reMutable cr;
25  
      run(-> {
26  
        G22AutoStarter.this.change();
27  
        if (cr == null) fail("Script is not saved: " + script);
28  
        var parsed = cr.parsedScriptMandatory();
29  
        ret parsed!;
30  
      });
31  
    }
32  
    
33  
    toString { ret str(script); }
34  
  }
35  
  
36  
  void init() {
37  
    if (ctrlListener != null) ret;
38  
    
39  
    scripts.clear();
40  
    
41  
    var scripts = conceptsWhere(g22utils.concepts, G22LeftArrowScript, runOnProjectOpen := true);
42  
    var sorted = sortByCalculatedFieldAlphaNumIC(scripts, s -> s.runOrder);
43  
    for (script : sorted)
44  
      this.scripts.add(new AutoStartScript(script));
45  
    change();
46  
    
47  
    ctrlListener = tempAddGlobalCtrlKeyListener(b -> {
48  
      if (cancelOnCtrl && b) {
49  
        ctrlPressed.raise();
50  
        cancel();
51  
      }
52  
    });
53  
  }
54  
  
55  
  void start {
56  
    print("G22AutoStarter: Start");
57  
    if (enabled) {
58  
      var topStack = g22utils.projectActions().topStack();
59  
      //topStackComponent = jCenteredLabel("Autostart in progress...");
60  
      progressBar = jProgressBarWithText(l(scripts));
61  
      topStackComponent = withTopMargin(jCenteredSection("Booting project",
62  
        centerAndEastWithMargin(
63  
          progressBar,
64  
          jThreadedButton("Cancel", -> {
65  
            disableButton(heldButton());
66  
            cancel();
67  
          }))));
68  
      topStack.addComponent(topStackComponent);
69  
      
70  
      rst!;
71  
    }
72  
  }
73  
  
74  
  void _run {
75  
    if (!enabled) ret;
76  
    
77  
    temp g22utils.backgroundProcessesUI.tempAdd("Auto-Start Scripts");
78  
    if (started.raise()) change();
79  
    
80  
    if (!waited!) {
81  
      print("G22AutoStarter: Sleeping");
82  
      while (initialDelay > 0) {
83  
        sleepSeconds(1);
84  
        initialDelay(initialDelay-1);
85  
      }
86  
      waited.raise();
87  
      change();
88  
      print("G22AutoStarter: Slept");
89  
    }
90  
    
91  
    while (enabled && !done()) {
92  
      var script = scripts.get(nScriptsRun);
93  
      currentScript(script);
94  
      setProgressBarValue(progressBar, nScriptsRun);
95  
      setProgressBarText(progressBar, str(script));
96  
      change();
97  
      script.run();
98  
      nScriptsRun(nScriptsRun+1);
99  
      currentScript(null);
100  
      change();
101  
    }
102  
    
103  
    var topStack = g22utils.projectActions().topStack();
104  
    topStack.removeComponent(topStackComponent);
105  
    topStackComponent = null;
106  
    progressBar = null;
107  
    
108  
    fireDone();
109  
    change();
110  
    close();
111  
  }
112  
  
113  
  bool done() { ret nScriptsRun >= l(scripts); }
114  
  bool waitedAndDone() { ret waited! && done(); }
115  
  
116  
  void cancel { setEnabled(false); }
117  
  
118  
  bool canResume() { ret !done() && !enabled; }
119  
  
120  
  void resume {
121  
    if (done())
122  
      ret with infoBox("Nothing to resume - auto-start is done");
123  
    if (enabled)
124  
      ret with infoBox("Already running");
125  
    infoBox("Resuming auto-start");
126  
    setEnabled(true);
127  
    rst!;
128  
  }
129  
  
130  
  S status() {
131  
    ret !enabled ?
132  
      ctrlPressed!
133  
        ? "Cancelled due to ctrl key"
134  
        : "Cancelled"
135  
      : done() ? "Done"
136  
      : waited! ? "Started" 
137  
      : started! ? "Pre-start wait (" + formatDouble1(initialDelay) + "s)"
138  
      : "Not started";
139  
  }
140  
  
141  
  S stats() {
142  
    ret status() + ". " + scriptsRunStats();
143  
  }
144  
  
145  
  S scriptsRunStats() {
146  
    ret "Scripts run: " + nScriptsRun() + "/" + l(scripts);
147  
  }
148  
  
149  
  S currentScriptStats() {
150  
    var s = currentScript;
151  
    if (s == null) ret "";
152  
    ret "Running: " + s.script;
153  
  }
154  
  
155  
  close {
156  
    if (ctrlListener != null) {
157  
      dispose ctrlListener;
158  
      change();
159  
    }
160  
  }
161  
  
162  
  void waitUntilDone {
163  
    waitForVarPredicate(varNScriptsRun(), -> done());
164  
  }
165  
  
166  
  bool ctrlEnabled() { ret ctrlListener != null; }
167  
  
168  
  bool cancelled() { ret !enabled; }
169  
}

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: 311 / 646
Version history: 53 change(s)
Referenced in: [show references]