mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Scripts: Run checks the script again before running it
When the script's report has changed since the Other Script… warning opened, Run runs nothing, says so in the status bar and opens the warning again with the new report.
This commit is contained in:
1 parent
76bb8b74d9
commit
263223d604
3 files changed
+51
-7
No files matched your search
@@ -15,7 +15,9 @@
|
||||
//! and dialogs it needs, features only the reference app has, and its includes (at most
|
||||
//! [`SHOWN_INCLUDES`], then the count of the rest). It says when the script is too large to check,
|
||||
//! compiled or not a script, when the check failed, and when a list was cut short at its limit.
|
||||
//! The listing is information: Run runs the script as before. The growth question
|
||||
//! The listing is information: Run runs the script as before, after checking it again. When the
|
||||
//! report has changed since the warning opened, nothing runs and the warning opens again with the
|
||||
//! new report. The growth question
|
||||
//! (`scriptGrowth`) and the question quitting asks (`scriptClose`) have the `choice` field alone.
|
||||
//!
|
||||
//! A script's dialog (kind `scriptDialog`) has the fields `kind` (alert, confirm or prompt),
|
||||
@@ -522,8 +524,14 @@ pub(crate) fn chosen(app: &mut VectorcraftApp, params: Value) -> Result<Value, S
|
||||
if !app.session.prefs.show_external_script_warning {
|
||||
return app.run("script.run", params);
|
||||
}
|
||||
let name = params.get("name").and_then(Value::as_str).or(path).map_or_else(|| "script".to_string(), vectorcraft_engine::cmd::fileio::file_name);
|
||||
let reaches = reaches(app, ¶ms);
|
||||
warn(app, params, reaches)
|
||||
}
|
||||
|
||||
/// Opens the warning for the script `params` name, listing `reaches`.
|
||||
fn warn(app: &mut VectorcraftApp, params: Value, reaches: Value) -> Result<Value, String> {
|
||||
let path = params.get("path").and_then(Value::as_str);
|
||||
let name = params.get("name").and_then(Value::as_str).or(path).map_or_else(|| "script".to_string(), vectorcraft_engine::cmd::fileio::file_name);
|
||||
// The script's name is the message: it is what the person reads first.
|
||||
let prompt = StandardPrompt::custom(name)
|
||||
.title(tl!("Run Script"))
|
||||
@@ -537,6 +545,10 @@ pub(crate) fn chosen(app: &mut VectorcraftApp, params: Value) -> Result<Value, S
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// The status when Run finds that the script's report changed since the warning opened: the
|
||||
/// warning opens again with the new report, and nothing runs.
|
||||
pub(crate) const CHANGED_SINCE_WARNING: &str = "The script changed after its warning opened: check the warning again";
|
||||
|
||||
/// The most includes the warning lists; a line after them gives the count of the rest.
|
||||
pub(crate) const SHOWN_INCLUDES: usize = 20;
|
||||
|
||||
@@ -690,10 +702,19 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
}
|
||||
if let Some(DialogResult::Ok(mut answer)) = run
|
||||
&& answer.choice == PromptChoice::Proceed
|
||||
&& let Some(RunWarning { params, .. }) = answer.take_tag::<RunWarning>()
|
||||
&& let Some(RunWarning { params, reaches: listed }) = answer.take_tag::<RunWarning>()
|
||||
{
|
||||
let stored = if answer.skip { app.run("prefs.set", json!({"key": "showExternalScriptWarning", "value": false})).map(drop) } else { Ok(()) };
|
||||
if let Err(e) = stored.and_then(|()| app.run("script.run", params).map(drop)) {
|
||||
// A script whose report changed while the warning was open is listed again before it runs.
|
||||
let now = reaches(app, ¶ms);
|
||||
let ran = if now == listed {
|
||||
let stored =
|
||||
if answer.skip { app.run("prefs.set", json!({"key": "showExternalScriptWarning", "value": false})).map(drop) } else { Ok(()) };
|
||||
stored.and_then(|()| app.run("script.run", params).map(drop))
|
||||
} else {
|
||||
app.status(CHANGED_SINCE_WARNING);
|
||||
warn(app, params, now).map(drop)
|
||||
};
|
||||
if let Err(e) = ran {
|
||||
app.status(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,6 +541,28 @@ fn the_warning_lists_what_a_scripts_included_files_reach() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A script file that changes what it reaches while its warning is open does not run on Run: the
|
||||
/// warning opens again with the new report, and the next Run runs it.
|
||||
#[test]
|
||||
fn run_lists_a_script_again_when_it_changed_after_its_warning_opened() {
|
||||
let mut app = app();
|
||||
let dir = folder("changed");
|
||||
let path = dir.join("changed.jsx");
|
||||
std::fs::write(&path, DRAW).unwrap();
|
||||
menus::run_ui_command(&mut app, "ui.runScript", &json!({"path": path.to_string_lossy()})).unwrap().unwrap();
|
||||
assert_eq!(fields(&app, KIND)["reaches"]["outside"], json!([]));
|
||||
std::fs::write(&path, format!("{DRAW}\nvar s = new Socket();\n")).unwrap();
|
||||
assert!(confirm(&mut app, KIND));
|
||||
assert!(!app.session.script_running());
|
||||
assert!(is_open::<RunQuestion>(&app));
|
||||
assert_eq!(app.ui.status, script_run::CHANGED_SINCE_WARNING);
|
||||
assert_eq!(fields(&app, KIND)["reaches"]["outside"][0]["id"], "network");
|
||||
assert!(confirm(&mut app, KIND));
|
||||
assert!(app.session.script_running());
|
||||
finish(&mut app);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A script with more includes than the warning lists gives the count of the rest.
|
||||
#[test]
|
||||
fn the_warning_counts_the_includes_it_does_not_list() {
|
||||
|
||||
+3
-2
@@ -2643,8 +2643,9 @@ files it opens in other apps, messages to other apps, the network, native code,
|
||||
app's preferences and code the checker can't read), the files and dialogs it needs, features only the reference app
|
||||
has, and the files its `#include` lines name (the first 20, then the count of the rest; VectorCraft's own limit). It
|
||||
says when the script is too large to check, compiled or not a script, when the check failed, and when a list was cut
|
||||
short at its limit. The listing does not stop the run. A
|
||||
script from the Scripts folder runs without the warning.
|
||||
short at its limit. The listing does not stop the run. Run checks the script again, and when the report has changed
|
||||
since the warning opened, nothing runs and the warning opens again with the new report. A script from the Scripts
|
||||
folder runs without the warning.
|
||||
Pass exactly one of `path` and `source`. A `path` must end in `.js` or `.jsx`, in any
|
||||
case. A `.jsxbin` file or text that starts with `@JSXBIN@` is compiled and ends with an error, as does text that is
|
||||
not UTF-8. The language is ES5.1, except that identifiers take the characters Unicode's `ID_Start` and
|
||||
|
||||
Reference in new issue
Block a user