use std::path::PathBuf;
use anyhow::Result;
pub const BUNDLE_IDENTIFIER: &str = "dev.waos.app";
#[cfg(target_os = "macos")]
const PANEL_CLOSED: &str = "--panel-closed";
#[cfg(target_os = "macos")]
const PASSED_ON: [&str; 2] = ["RUST_LOG", "WAOS_NATIVE_UNWIND_INFO"];
#[cfg(target_os = "macos")]
pub fn start() -> Result<PathBuf> {
use std::ffi::OsString;
use std::process::Command;
use anyhow::{Context, bail};
let log = log_path();
let mut open = Command::new("open");
open.args(["-b", BUNDLE_IDENTIFIER])
.arg("--stderr")
.arg(&log);
for name in PASSED_ON {
if let Some(value) = std::env::var_os(name) {
let mut variable = OsString::from(format!("{name}="));
variable.push(value);
open.arg("--env").arg(variable);
}
}
open.args(["--args", PANEL_CLOSED]);
let status = open.status().context("running open")?;
if !status.success() {
bail!(
"WaOS.app is not installed: build it with `cargo waft build macos -p waos-app` and open it once, so the system knows where it is"
);
}
Ok(log)
}
#[cfg(target_os = "macos")]
fn log_path() -> PathBuf {
let home = std::env::home_dir().unwrap_or_else(|| PathBuf::from("/"));
home.join("Library/Logs/WaOS.log")
}
#[cfg(not(target_os = "macos"))]
pub fn start() -> Result<PathBuf> {
anyhow::bail!(
"no WaOS answers, and only macOS's WaOS.app ({BUNDLE_IDENTIFIER}) is known how to start: start WaOS yourself"
)
}