mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-11 18:54:55 +00:00
Scripts: a script chosen from File › Scripts reads its #include files and runs $.evalFile
File › Scripts ran an installed script as source text, so the run had no
file: an #include line ended it with "script file read refused", and
$.evalFile was refused too. The menu now checks the script as before and
runs it with script.run {path}, as docs/mcp.md describes, so its
included files and the files $.evalFile runs are read beside it.
This commit is contained in:
@@ -242,13 +242,14 @@ pub fn readable(p: &Value) -> bool {
|
||||
}
|
||||
|
||||
/// `ui.runInstalledScript {path}`: run the listed script at `path`. A script that is no longer in
|
||||
/// the folder is not run, and the folder is read again.
|
||||
/// the folder is not run, and the folder is read again. The script runs from its file, so its
|
||||
/// `#include` lines and `$.evalFile` read beside it.
|
||||
pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
let path = p.get("path").and_then(Value::as_str).ok_or("missing `path`")?;
|
||||
let source = app.session.installed_script(path).map_err(|e| {
|
||||
app.session.installed_script(path).map_err(|e| {
|
||||
let ctx = app.ui_ctx.clone();
|
||||
refresh(app, ctx.as_ref());
|
||||
e.to_string()
|
||||
})?;
|
||||
app.run("script.run", json!({"source": source, "name": vectorcraft_engine::cmd::fileio::file_name(path)}))
|
||||
app.run("script.run", json!({ "path": path }))
|
||||
}
|
||||
|
||||
@@ -142,6 +142,20 @@ fn a_script_runs_its_own_file_whatever_its_name_ends_with() {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_listed_script_reads_its_included_files_and_runs_files_beside_it() {
|
||||
let dir = folder("include");
|
||||
let mut app = app(&dir);
|
||||
write(&dir, "lib/two.jsxinc", DRAW);
|
||||
write(&dir, "lib/one.js", "vc.run('shape.rectangle', {x:40,y:0,width:10,height:10});");
|
||||
let main = write(&dir, "Main.jsx", "#include \"lib/two.jsxinc\"\n$.evalFile('lib/one.js');\n");
|
||||
read(&app);
|
||||
menus::run_ui_command(&mut app, scripts_menu::COMMAND, &json!({ "path": main })).unwrap().unwrap();
|
||||
finish(&mut app);
|
||||
assert_eq!(objects(&app), 3, "{}", app.ui.status);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_and_folder_names_are_shown_as_they_are_in_every_language() {
|
||||
let dir = folder("language");
|
||||
|
||||
Reference in New Issue
Block a user