Skip to content

Commit

Permalink
Add support for specifying runtime arguments (closes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 committed Aug 6, 2021
1 parent f2fa090 commit ae9a568
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions native/src/components/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl Site {
dirs: &ProjectDirs,
runtime: &Runtime,
url: &Option<Url>,
arguments: &[String],
) -> Result<Child> {
let profile = dirs.userdata.join("profiles").join(&self.profile.to_string());

Expand All @@ -131,6 +132,7 @@ impl Site {
]);
}

args.extend_from_slice(arguments);
runtime.run(args)
}

Expand Down
5 changes: 2 additions & 3 deletions native/src/connector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,11 @@ impl<'a> Connection<'a> {
// Just simulate calling site launch command
cfg_if! {
if #[cfg(target_os = "macos")] {
let command = SiteLaunchCommand { id: *id, url: url.to_owned(), direct_launch: false };
let command = SiteLaunchCommand { id: *id, url: url.to_owned(), arguments: vec![], direct_launch: false };
} else {
let command = SiteLaunchCommand { id: *id, url: url.to_owned() };
let command = SiteLaunchCommand { id: *id, url: url.to_owned(), arguments: vec![] };
}
};

command.run()?;

Ok(ResponseMessage::SiteLaunched)
Expand Down
3 changes: 3 additions & 0 deletions native/src/console/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct SiteLaunchCommand {
/// Identifier of the PWA
pub id: Ulid,

/// Additional arguments for the Firefox runtime
pub arguments: Vec<String>,

/// Optionally launches the PWA with a custom start URL
#[structopt(long)]
pub url: Option<Url>,
Expand Down
8 changes: 5 additions & 3 deletions native/src/console/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ impl Run for SiteLaunchCommand {
let storage = Storage::load(&dirs)?;

let site = storage.sites.get(&self.id).context("Site does not exist")?;
let args = if !&self.arguments.is_empty() { &self.arguments } else { &storage.arguments };

cfg_if! {
if #[cfg(target_os = "macos")] {
use crate::integrations;

if !self.direct_launch {
integrations::launch_site(site, &self.url)?;
integrations::launch_site(site, &self.url, args)?;
return Ok(())
}
}
Expand All @@ -46,11 +47,12 @@ impl Run for SiteLaunchCommand {
runtime.patch(&dirs, site)?;
profile.patch(&dirs)?;

info!("Launching the site");
cfg_if! {
if #[cfg(target_os = "macos")] {
site.launch(&dirs, &runtime, &self.url)?.wait()?;
site.launch(&dirs, &runtime, &self.url, args)?.wait()?;
} else {
site.launch(&dirs, &runtime, &self.url)?;
site.launch(&dirs, &runtime, &self.url, args)?;
}
}

Expand Down
17 changes: 14 additions & 3 deletions native/src/integrations/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ fn verify_app_is_pwa(app_bundle: &Path, appid: &str) -> Result<()> {
Ok(())
}

pub fn launch_site(site: &Site, url: &Option<Url>) -> Result<Child> {
pub fn launch_site(site: &Site, url: &Option<Url>, arguments: &[String]) -> Result<Child> {
let name = site.name().unwrap_or_else(|| site.domain());

let appid = format!("FFPWA-{}", site.ulid.to_string());
Expand All @@ -359,20 +359,31 @@ pub fn launch_site(site: &Site, url: &Option<Url>) -> Result<Child> {
.join(format!("Applications/{}.app", name));

debug!("Verifying that {} is a PWA app bundle", app_path.to_str().unwrap());

if app_path.exists() {
verify_app_is_pwa(&app_path, &appid)?;
}

let mut args = vec![app_path.display().to_string()];

// We need to append `--args` when we provide additional arguments to the PWA
if url.is_some() || !arguments.is_empty() {
args.extend_from_slice(&["--args".into()]);
}

// Support launching PWA with custom URLs
if let Some(url) = url {
#[rustfmt::skip]
args.extend_from_slice(&[
"--args".into(),
"--url".into(), url.to_string(),
]);
}

// Support launching PWA with custom Firefox arguments
if !arguments.is_empty() {
args.extend_from_slice(&["---".into()]);
args.extend_from_slice(arguments);
}

let mut command = Command::new("open");
command.args(&args).spawn().context(LAUNCH_APPLICATION_BUNDLE)
}
Expand Down
5 changes: 3 additions & 2 deletions native/src/integrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ pub fn uninstall(site: &Site, dirs: &ProjectDirs) -> Result<()> {
}

#[cfg(target_os = "macos")]
pub fn launch_site(site: &Site, url: &Option<Url>) -> Result<Child> {
macos::launch_site(site, url)
#[inline]
pub fn launch_site(site: &Site, url: &Option<Url>, arguments: &[String]) -> Result<Child> {
macos::launch_site(site, url, arguments)
}

/// Util: Check if the icon is supported
Expand Down
1 change: 1 addition & 0 deletions native/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Storage {
#[default([(Ulid::nil(), Profile::default())].iter().cloned().collect())]
pub profiles: BTreeMap<Ulid, Profile>,
pub sites: BTreeMap<Ulid, Site>,
pub arguments: Vec<String>,
}

impl Storage {
Expand Down

0 comments on commit ae9a568

Please sign in to comment.