mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-26 21:30:19 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd3f8a7138 | ||
|
|
88e6bf0fba | ||
|
|
1cf61ebf48 | ||
|
|
1178a3594e | ||
|
|
bc65aa048c | ||
|
|
fe148ab826 |
+138
-29
@@ -268,14 +268,23 @@ fn initialize_nightly_process_identity() -> Result<(), process_identity::Identit
|
||||
)
|
||||
}
|
||||
|
||||
/// Tell a desktop user why a nightly launch was refused. A headless form (`--version` on a runner)
|
||||
/// never gets here: the version query is answered before this and before any profile work.
|
||||
/// Whether a refusal may also open a native message box. Standard error always carries the reason;
|
||||
/// this only adds a box where one renders without the app's event loop. Never on macOS: rfd's alert
|
||||
/// there needs the AppKit run loop this start has not reached, so a refused nightly launch hung in
|
||||
/// an invisible modal instead of exiting (GridCraft Mac smoke, 2026-11-26).
|
||||
fn refusal_dialog(flavor: build_flavor::BuildFlavor, desktop_session: bool, macos: bool) -> bool {
|
||||
build_flavor::needs_native_error(flavor, desktop_session) && !macos
|
||||
}
|
||||
|
||||
/// Tell a desktop user why a nightly launch was refused, in a native box where one renders without the
|
||||
/// app's event loop (Windows, X11/Wayland), and on standard error always. A headless form (`--version`
|
||||
/// on a runner) never gets here: the version query is answered before this and before any profile work.
|
||||
fn report_launch_profile_error(error: &str) {
|
||||
eprintln!("GridCraft launch profile: {error}");
|
||||
let desktop_session = cfg!(windows)
|
||||
|| cfg!(target_os = "macos")
|
||||
|| (cfg!(target_os = "linux") && ["DISPLAY", "WAYLAND_DISPLAY"].into_iter().any(|key| std::env::var_os(key).is_some_and(|v| !v.is_empty())));
|
||||
if build_flavor::needs_native_error(build_flavor::COMPILED, desktop_session) {
|
||||
if refusal_dialog(build_flavor::COMPILED, desktop_session, cfg!(target_os = "macos")) {
|
||||
if let Err(error) = initialize_nightly_process_identity() {
|
||||
eprintln!("GridCraft launch profile: {error}");
|
||||
return;
|
||||
@@ -330,6 +339,55 @@ fn prepare_launch() -> Option<Vec<std::ffi::OsString>> {
|
||||
Some(early.args)
|
||||
}
|
||||
|
||||
/// The app's own command line, once the launch profile's tokens are gone: `--control`,
|
||||
/// `--control-token`, `--sample` and the files to open. `--` ends the options, and everything after
|
||||
/// it is a file, even a name that reads like one — that is how the Launcher hands a nightly its
|
||||
/// documents (`launch_profile::parse` puts the separator first), so the separator itself is never
|
||||
/// opened as a file.
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
struct DesktopArgs {
|
||||
control_port: Option<u16>,
|
||||
control_token: Option<String>,
|
||||
sample: Option<String>,
|
||||
files: Vec<String>,
|
||||
}
|
||||
|
||||
fn desktop_args(args: &[String], control_port: Option<u16>, control_token: Option<String>) -> DesktopArgs {
|
||||
let mut parsed = DesktopArgs { control_port, control_token, ..DesktopArgs::default() };
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
match args[i].as_str() {
|
||||
"--" => {
|
||||
parsed.files.extend(args[i + 1..].iter().cloned());
|
||||
break;
|
||||
}
|
||||
"--control" => {
|
||||
i += 1;
|
||||
parsed.control_port = args.get(i).and_then(|p| p.parse().ok());
|
||||
}
|
||||
"--control-token" => {
|
||||
i += 1;
|
||||
parsed.control_token = args.get(i).cloned();
|
||||
}
|
||||
"--sample" => {
|
||||
i += 1;
|
||||
parsed.sample = Some(args.get(i).cloned().unwrap_or_else(|| "sales".into()));
|
||||
}
|
||||
a if a.starts_with("-psn_") => {}
|
||||
a => parsed.files.push(a.to_string()),
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
parsed
|
||||
}
|
||||
|
||||
/// A nightly owns no control channel, and only options count: everything after `--` is a file, so a
|
||||
/// document named `--control` is opened rather than read as a flag.
|
||||
fn nightly_control_refused(nightly: bool, args: &[String]) -> bool {
|
||||
let options_end = args.iter().position(|a| a == "--").unwrap_or(args.len());
|
||||
nightly && args[..options_end].iter().any(|a| a == "--control" || a == "--control-token")
|
||||
}
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
// A confined launcher run (`gridcraft_sandbox::launch_command`, which a Python cell starts from
|
||||
// this same executable) comes before everything else: it opens no window, writes no log, and
|
||||
@@ -392,34 +450,14 @@ fn main() -> eframe::Result<()> {
|
||||
}
|
||||
// The control channel is a stable-only service: a nightly window owns no port, writes no
|
||||
// token and never inherits the stable app's `GRIDCRAFT_CONTROL_PORT`/`GRIDCRAFT_CONTROL_TOKEN`.
|
||||
if nightly && args.iter().any(|a| a == "--control" || a == "--control-token") {
|
||||
if nightly_control_refused(nightly, &args) {
|
||||
eprintln!("GridCraft Nightly: the control channel is not part of the nightly channel");
|
||||
std::process::exit(2);
|
||||
}
|
||||
let mut control_port: Option<u16> = (!nightly).then(|| std::env::var("GRIDCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok())).flatten();
|
||||
let mut control_token: Option<String> = (!nightly).then(|| std::env::var("GRIDCRAFT_CONTROL_TOKEN").ok()).flatten();
|
||||
let mut sample: Option<String> = None;
|
||||
let mut files: Vec<String> = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
match args[i].as_str() {
|
||||
"--control" => {
|
||||
i += 1;
|
||||
control_port = args.get(i).and_then(|p| p.parse().ok());
|
||||
}
|
||||
"--control-token" => {
|
||||
i += 1;
|
||||
control_token = args.get(i).cloned();
|
||||
}
|
||||
"--sample" => {
|
||||
i += 1;
|
||||
sample = Some(args.get(i).cloned().unwrap_or_else(|| "sales".into()));
|
||||
}
|
||||
a if a.starts_with("-psn_") => {}
|
||||
a => files.push(a.to_string()),
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
let control_port: Option<u16> = (!nightly).then(|| std::env::var("GRIDCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok())).flatten();
|
||||
let control_token: Option<String> = (!nightly).then(|| std::env::var("GRIDCRAFT_CONTROL_TOKEN").ok()).flatten();
|
||||
let parsed = desktop_args(&args, control_port, control_token);
|
||||
let (control_port, control_token, sample, files) = (parsed.control_port, parsed.control_token, parsed.sample, parsed.files);
|
||||
// The log file lives in the settings folder, next to the preferences; opened after the
|
||||
// arguments, so `--version` leaves no file behind. Records logged until now are written to it
|
||||
// first. Runs without preferences (agents' test runs) log to standard error only, so they
|
||||
@@ -449,7 +487,6 @@ fn main() -> eframe::Result<()> {
|
||||
if !nightly {
|
||||
app.start_update_check();
|
||||
}
|
||||
let control_port = control_port;
|
||||
load_prefs(&mut app);
|
||||
// Stamp the startup workbook's author from the persisted user name.
|
||||
if app.session.documents().is_empty() {
|
||||
@@ -616,3 +653,75 @@ mod packaging_tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod launch_separator_tests {
|
||||
use super::*;
|
||||
use crate::build_flavor::BuildFlavor;
|
||||
|
||||
/// The profile root a Launcher run is started with: absolute for the platform the test runs on.
|
||||
/// `launch_profile::parse` refuses a root that is relative there, and a POSIX path has no drive
|
||||
/// or UNC prefix, so it is not absolute on Windows - where CI runs these tests too.
|
||||
fn launcher_root() -> &'static str {
|
||||
if cfg!(windows) { r"C:\NightlyData\profiles\gridcraft" } else { "/nightly-data/profiles/gridcraft" }
|
||||
}
|
||||
|
||||
/// The argv the Launcher starts a nightly with, extended by `tail`, as the start then reads it.
|
||||
fn launcher_argv(tail: &[&str]) -> Vec<String> {
|
||||
let mut argv: Vec<std::ffi::OsString> =
|
||||
["--launch-profile", "nightly-v1", "--profile-root", launcher_root(), "--"].iter().map(std::ffi::OsString::from).collect();
|
||||
argv.extend(tail.iter().map(std::ffi::OsString::from));
|
||||
launch_profile::parse(argv).unwrap().args.iter().map(|a| a.to_string_lossy().into_owned()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_other_platforms_root_spelling_is_refused_as_relative() {
|
||||
let foreign = if cfg!(windows) { "/nightly-data/profiles/gridcraft" } else { r"C:\NightlyData\profiles\gridcraft" };
|
||||
let argv: Vec<std::ffi::OsString> =
|
||||
["--launch-profile", "nightly-v1", "--profile-root", foreign, "--"].iter().map(std::ffi::OsString::from).collect();
|
||||
assert!(launch_profile::parse(argv).is_err(), "a root that is not absolute on this platform must be refused");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_launchers_separator_is_never_a_file() {
|
||||
let parsed = desktop_args(&launcher_argv(&[]), None, None);
|
||||
assert_eq!(parsed, DesktopArgs::default(), "the separator must not reach the file list");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_after_the_separator_are_opened_in_order() {
|
||||
assert_eq!(desktop_args(&launcher_argv(&["a.file"]), None, None).files, ["a.file"]);
|
||||
let parsed = desktop_args(&launcher_argv(&["a.file", "--version", "--sample"]), None, None);
|
||||
assert_eq!(parsed.files, ["a.file", "--version", "--sample"], "after -- every word is a file");
|
||||
assert_eq!((parsed.sample, parsed.control_port), (None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_control_flag_after_the_separator_is_a_file_not_a_refusal() {
|
||||
let argv = launcher_argv(&["--control"]);
|
||||
assert!(!nightly_control_refused(true, &argv), "a document named --control must open, not refuse");
|
||||
let parsed = desktop_args(&argv, None, None);
|
||||
assert_eq!((parsed.control_port, parsed.files), (None, vec!["--control".to_string()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_control_flag_before_the_separator_still_names_the_port() {
|
||||
let with_file = vec!["--control".to_string(), "7979".to_string(), "--".to_string(), "a.file".to_string()];
|
||||
assert!(nightly_control_refused(true, &with_file));
|
||||
let parsed = desktop_args(&with_file, None, None);
|
||||
assert_eq!((parsed.control_port, parsed.files), (Some(7979), vec!["a.file".to_string()]));
|
||||
let plain = vec!["--control".to_string(), "7979".to_string()];
|
||||
assert!(nightly_control_refused(true, &plain));
|
||||
let parsed = desktop_args(&plain, None, None);
|
||||
assert_eq!((parsed.control_port, parsed.files), (Some(7979), Vec::<String>::new()));
|
||||
assert!(!nightly_control_refused(false, &plain), "the stable app keeps its control channel");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_refusal_never_blocks_on_a_modal_before_the_event_loop_on_macos() {
|
||||
assert!(refusal_dialog(BuildFlavor::NightlyV1, true, false), "a desktop refusal still shows the box where one renders");
|
||||
assert!(!refusal_dialog(BuildFlavor::NightlyV1, true, true), "macOS has no run loop yet: the modal would hang");
|
||||
assert!(!refusal_dialog(BuildFlavor::NightlyV1, false, false), "a headless session has no box");
|
||||
assert!(!refusal_dialog(BuildFlavor::Stable, true, false), "only a nightly refusal explains itself in a box");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user