mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: a chosen script keeps script.run's reply with startup scripts on
With Run Startup Scripts on, ui.runScript and ui.runInstalledScript
return script.run's reply, or its error when the chosen script cannot
start, with the startup run's reply under startup. While a startup
script runs first, the reply is {running: true, queued: true}.
This commit is contained in:
1 parent
2df812df41
commit
f341cb5b0d
5 files changed
+61
-7
No files matched your search
@@ -2022,13 +2022,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.runScript",
|
||||
"Other Script…",
|
||||
"Cmd+F12",
|
||||
"{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker)",
|
||||
"{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker); with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar",
|
||||
),
|
||||
(
|
||||
"ui.runInstalledScript",
|
||||
"Run Script",
|
||||
"",
|
||||
"{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again",
|
||||
"{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again; with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar",
|
||||
),
|
||||
(
|
||||
"ui.checkScript",
|
||||
|
||||
@@ -2023,13 +2023,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.runScript",
|
||||
"Other Script…",
|
||||
"Cmd+F12",
|
||||
"{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker)",
|
||||
"{path? | source?, name?} run a .js or .jsx file (script.run), after the warning unless Preferences › General › Show External Script Warning is off (dialog `scriptRun`: `name`, `script`, `dontShow`); without a file pick one (the web opens the browser's file picker); with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar",
|
||||
),
|
||||
(
|
||||
crate::scripts_menu::COMMAND,
|
||||
"Run Script",
|
||||
"",
|
||||
"{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again",
|
||||
"{path: a script's path as script.listInstalled gives it} run a script of the Scripts folder, as its File › Scripts item does (script.run with installed reads the file once, checks it again and runs that text, so its #include lines and $.evalFile read in the folder that read found), without the warning Other Script… shows; a file that is no longer a .js or .jsx file in the folder or a subfolder at most 4 levels below it, or that is or lies behind a link or is hidden, is not run, and the menu's listing is read again; with Preferences › General › Run Startup Scripts on, the startup scripts run first (script.startup.run) and script.run's reply also holds `startup`, script.startup.run's reply; while a startup script runs before it the reply is {running: true, queued: true}, and a later failure to start shows in the status bar",
|
||||
),
|
||||
(
|
||||
"ui.checkScript",
|
||||
|
||||
@@ -124,12 +124,30 @@ fn ask(app: &mut VectorcraftApp) -> bool {
|
||||
/// the startup scripts when General › Run Startup Scripts is on (`script.startup.run`). A startup
|
||||
/// script that cannot start, a listing of the Startup Scripts folder that was not read in full, and
|
||||
/// a folder that is there and cannot be read show in the status bar ([`reply_notes`]).
|
||||
/// → `script.run`'s reply, with `startup` holding `script.startup.run`'s when the startup scripts
|
||||
/// were run: the chosen script's own reply when it ran to its end at once, `{running: true, name}`
|
||||
/// when it is running, or `{running: true, queued: true}` while a startup script runs before it.
|
||||
/// A chosen script that could not start gives its error, as `script.run` does.
|
||||
pub(crate) fn run_chosen(app: &mut VectorcraftApp, params: Value) -> Result<Value, String> {
|
||||
if app.session.startup_skip().is_some() {
|
||||
return app.run("script.run", params);
|
||||
}
|
||||
let reply = app.run("script.startup.run", json!({"reason": "menu", "then": params}))?;
|
||||
reply_notes(app, &reply);
|
||||
let mut startup = app.run("script.startup.run", json!({"reason": "menu", "then": params}))?;
|
||||
reply_notes(app, &startup);
|
||||
let mut reply = match startup.as_object_mut().and_then(|o| o.remove("then")) {
|
||||
Some(then) => then,
|
||||
// Nothing runs and the chosen script gave no reply: it could not start, and it is the
|
||||
// last of the runs that could not start.
|
||||
None if !app.session.script_running() => {
|
||||
let last = startup["failed"].as_array().and_then(|failed| failed.last());
|
||||
return Err(last.and_then(|failed| failed["error"].as_str()).unwrap_or_default().to_string());
|
||||
}
|
||||
None if app.session.startup_script_running() => json!({"running": true, "queued": true}),
|
||||
None => json!({"running": true, "name": app.session.script_name().unwrap_or_default()}),
|
||||
};
|
||||
if let Some(o) = reply.as_object_mut() {
|
||||
o.insert("startup".to_string(), startup);
|
||||
}
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
|
||||
@@ -525,6 +525,39 @@ fn with_run_startup_scripts_on_other_script_and_the_launch_run_the_startup_scrip
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_run_startup_scripts_on_a_chosen_script_still_replies_as_script_run() {
|
||||
let dir = folder("startup-reply");
|
||||
let mut app = app(&dir);
|
||||
app.session.prefs.show_external_script_warning = false;
|
||||
let off = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap();
|
||||
finish(&mut app);
|
||||
assert_eq!(off, json!({"running": true, "name": "chosen"}));
|
||||
let empty = dir.join("Startup Scripts");
|
||||
std::fs::create_dir_all(&empty).unwrap();
|
||||
app.session.attach_startup_scripts_dir(empty);
|
||||
app.run("prefs.set", json!({"key": "runStartupScripts", "value": true})).unwrap();
|
||||
// An empty folder: the chosen script starts at once, named as its run is, by a #script line too.
|
||||
let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap();
|
||||
finish(&mut app);
|
||||
assert_eq!((&r["running"], &r["name"], &r["startup"]["queued"]), (&json!(true), &json!("chosen"), &json!(1)), "{r}");
|
||||
let named = format!("#script \"Named\"\n{DRAW}");
|
||||
let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": named })).unwrap().unwrap();
|
||||
finish(&mut app);
|
||||
assert_eq!(r["name"], "Named", "{r}");
|
||||
// A chosen script that cannot start gives its error, as script.run does.
|
||||
let alone = app.run("script.run", json!({"source": "vc.log(("})).unwrap_err();
|
||||
let e = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": "vc.log((" })).unwrap().unwrap_err();
|
||||
assert_eq!(e, alone);
|
||||
// A startup script runs first: the chosen script is queued after it.
|
||||
startup_folder(&mut app, &dir);
|
||||
let r = menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap();
|
||||
finish(&mut app);
|
||||
assert_eq!((&r["running"], &r["queued"], &r["startup"]["queued"]), (&json!(true), &json!(true), &json!(2)), "{r}");
|
||||
assert_eq!(started(&app).last().map(String::as_str), Some("chosen"));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stopped_startup_script_says_it_was_stopped_and_the_chosen_script_still_runs() {
|
||||
let dir = folder("startup-stop");
|
||||
|
||||
+4
-1
@@ -3311,7 +3311,10 @@ reply is `{reason, skipped: false, running, queued, failed, disabled, truncated,
|
||||
the runs that could not start before the first one did. Each run then reports as a `script.run` in the app does, the
|
||||
next one starts at the frame after, and a later run that cannot start, the chosen script included, is named in the
|
||||
status bar. Stop (`script.stop`) asked for between two runs stops the run that starts next. Under `--automation-read-root` it reads the folder only when the folder lies inside a
|
||||
read root.
|
||||
read root. With the preference on, `ui.runScript` and `ui.runInstalledScript` still return `script.run`'s reply,
|
||||
or its error when the chosen script cannot start, and add `startup` holding the `script.startup.run` reply. While a
|
||||
startup script runs before the chosen script, that reply is `{running: true, queued: true}`, and a chosen script that
|
||||
then cannot start is named in the status bar.
|
||||
|
||||
```sh
|
||||
vectorcraft-cli run --cmd script.startup.run --params '{"reason": "launch"}'
|
||||
|
||||
Reference in new issue
Block a user