Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Add squirrel app builder / hook manager
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Dec 26, 2023
1 parent b021ac5 commit ce35a29
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Rust/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn apply<'a>(restart: bool, wait_for_parent: bool, package: Option<&PathBuf>
}

if restart {
shared::start_package(&app, &root_path, exe_args)?;
shared::start_package(&app, &root_path, exe_args, Some("CLOWD_SQUIRREL_RESTART"))?;
}

Ok(())
Expand Down Expand Up @@ -79,7 +79,7 @@ fn apply_package<'a>(package: Option<&PathBuf>, app: &Manifest, root_path: &Path
info!("Applying package to current: {}", found_version);

#[cfg(target_os = "windows")]
crate::windows::run_hook(&app, &root_path, "--squirrel-obsoleted", 15);
crate::windows::run_hook(&app, &root_path, "--squirrel-obsolete", 15);

let current_dir = app.get_current_path(&root_path);
shared::replace_dir_with_rollback(current_dir.clone(), || {
Expand Down
4 changes: 2 additions & 2 deletions src/Rust/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub fn start(wait_for_parent: bool, exe_name: Option<&String>, exe_args: Option<
info!("About to launch: '{}' in dir '{}'", exe_to_execute.to_string_lossy(), current);

if let Some(args) = exe_args {
crate::shared::run_process(exe_to_execute, args, current)?;
crate::windows::run_process(exe_to_execute, args, current)?;
} else if let Some(args) = legacy_args {
crate::windows::run_process_raw_args(exe_to_execute, args, current)?;
} else {
crate::shared::run_process(exe_to_execute, vec![], current)?;
crate::windows::run_process(exe_to_execute, vec![], current)?;
};

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/Rust/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ fn install_app(pkg: &bundle::BundleInfo, root_path: &PathBuf, tx: &std::sync::mp
app.write_uninstall_entry(root_path)?;

if !dialogs::get_silent() {
info!("Starting app: \"{}\" --squirrel-firstrun", main_exe_path);
let args = vec!["--squirrel-firstrun"];
let _ = shared::run_process(&main_exe_path, args, &current_path);
info!("Starting app...");
shared::start_package(&app, &root_path, None, Some("CLOWD_SQUIRREL_FIRSTRUN"))?;
}

Ok(())
Expand Down
11 changes: 0 additions & 11 deletions src/Rust/src/shared/util_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use anyhow::{anyhow, bail, Result};
use rand::distributions::{Alphanumeric, DistString};
use regex::Regex;
use std::{
ffi::OsStr,
fs,
io::{self},
path::{Path, PathBuf},
process::Command as Process,
thread,
time::Duration,
};
Expand Down Expand Up @@ -62,15 +60,6 @@ where
}
}

pub fn run_process<S, P>(exe: S, args: Vec<&str>, work_dir: P) -> Result<()>
where
S: AsRef<OsStr>,
P: AsRef<Path>,
{
Process::new(exe).args(args).current_dir(work_dir).spawn()?;
Ok(())
}

pub fn retry_io<F, T>(op: F) -> io::Result<T>
where
F: Fn() -> io::Result<T>,
Expand Down
13 changes: 8 additions & 5 deletions src/Rust/src/shared/util_osx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,24 @@ pub fn force_stop_package<P: AsRef<Path>>(root_dir: P) -> Result<()> {
Ok(())
}

pub fn start_package<P: AsRef<Path>>(_app: &Manifest, root_dir: P, exe_args: Option<Vec<&str>>) -> Result<()> {
pub fn start_package<P: AsRef<Path>>(_app: &Manifest, root_dir: P, exe_args: Option<Vec<&str>>, set_env: Option<&str>) -> Result<()> {
let root_dir = root_dir.as_ref().to_string_lossy().to_string();
let mut args = vec!["-n", &root_dir];
if let Some(a) = exe_args {
args.push("--args");
args.extend(a);
}
Process::new("/usr/bin/open").args(args).spawn().map_err(|z| anyhow!("Failed to start application ({}).", z))?;
let mut psi = Process::new("/usr/bin/open").args(args);
if let Some(env) = set_env {
psi.env(env, "true");
}
psi.spawn().map_err(|z| anyhow!("Failed to start application ({}).", z))?;
Ok(())
}

#[test]
#[ignore]
fn test_start_and_stop_package()
{
fn test_start_and_stop_package() {
let mani = Manifest::default();
let root_dir = "/Applications/Calcbot.app";
let _ = force_stop_package(root_dir);
Expand All @@ -59,4 +62,4 @@ fn test_start_and_stop_package()
force_stop_package(root_dir).unwrap();
std::thread::sleep(Duration::from_secs(1));
assert!(!is_running());
}
}
19 changes: 14 additions & 5 deletions src/Rust/src/shared/util_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, bail, Result};
use std::{
collections::HashMap,
path::{Path, PathBuf},
process::Command as Process,
};
use winsafe::{self as w, co, prelude::*};

Expand Down Expand Up @@ -118,7 +119,7 @@ pub fn force_stop_package<P: AsRef<Path>>(root_dir: P) -> Result<()> {
Ok(())
}

pub fn start_package<P: AsRef<Path>>(app: &Manifest, root_dir: P, exe_args: Option<Vec<&str>>) -> Result<()> {
pub fn start_package<P: AsRef<Path>>(app: &Manifest, root_dir: P, exe_args: Option<Vec<&str>>, set_env: Option<&str>) -> Result<()> {
let root_dir = root_dir.as_ref().to_path_buf();
let current = app.get_current_path(&root_dir);
let exe = app.get_main_exe_path(&root_dir);
Expand All @@ -130,11 +131,19 @@ pub fn start_package<P: AsRef<Path>>(app: &Manifest, root_dir: P, exe_args: Opti

crate::windows::assert_can_run_binary_authenticode(&exe_to_execute)?;

let mut psi = Process::new(&exe_to_execute);
psi.current_dir(&current);
if let Some(args) = exe_args {
super::run_process(exe_to_execute, args, current)?;
} else {
crate::shared::run_process(exe_to_execute, vec![], current)?;
};
psi.args(args);
}
if let Some(env) = set_env {
debug!("Setting environment variable: {}={}", env, "true");
psi.env(env, "true");
}

info!("About to launch: '{}' in dir '{}'", exe_to_execute.to_string_lossy(), current);
info!("Args: {:?}", psi.get_args());
psi.spawn().map_err(|z| anyhow!("Failed to start application ({}).", z))?;

Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions src/Rust/src/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ where
}
}

pub fn run_process<S, P>(exe: S, args: Vec<&str>, work_dir: P) -> Result<()>
where
S: AsRef<OsStr>,
P: AsRef<Path>,
{
Process::new(exe).args(args).current_dir(work_dir).spawn()?;
Ok(())
}

pub fn run_process_no_console<S, P>(exe: S, args: Vec<&str>, work_dir: P) -> Result<()>
where
S: AsRef<OsStr>,
Expand Down
2 changes: 1 addition & 1 deletion src/Squirrel/Locators/ISquirrelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface ISquirrelLocator
/// <summary>
/// Finds latest .nupkg file in the PackagesDir or null if not found.
/// </summary>
public ReleaseEntry GetLatestLocalPackage();
public ReleaseEntry GetLatestLocalFullPackage();

/// <summary>
/// Unique identifier for this user which is used to calculate whether this user is eligible for
Expand Down
13 changes: 10 additions & 3 deletions src/Squirrel/Locators/SquirrelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NuGet.Versioning;

namespace Squirrel.Locators
Expand All @@ -21,14 +22,16 @@ public abstract class SquirrelLocator : ISquirrelLocator
/// </summary>
public static SquirrelLocator GetDefault(ILogger logger)
{
var log = logger ?? NullLogger.Instance;

if (_current != null)
return _current;

if (SquirrelRuntimeInfo.IsWindows)
return _current ??= new WindowsSquirrelLocator(logger);
return _current ??= new WindowsSquirrelLocator(log);

if (SquirrelRuntimeInfo.IsOSX)
return _current ??= new OsxSquirrelLocator(logger);
return _current ??= new OsxSquirrelLocator(log);

throw new NotSupportedException($"OS platform '{SquirrelRuntimeInfo.SystemOs.GetOsLongName()}' is not supported.");
}
Expand Down Expand Up @@ -78,17 +81,21 @@ protected SquirrelLocator(ILogger logger)
/// <inheritdoc/>
public virtual List<ReleaseEntry> GetLocalPackages()
{
if (CurrentlyInstalledVersion == null)
return new List<ReleaseEntry>(0);

return Directory.EnumerateFiles(PackagesDir, "*.nupkg")
.Select(x => ReleaseEntry.GenerateFromFile(x))
.Where(x => x?.Version != null)
.ToList();
}

/// <inheritdoc/>
public ReleaseEntry GetLatestLocalPackage()
public ReleaseEntry GetLatestLocalFullPackage()
{
return GetLocalPackages()
.OrderByDescending(x => x.Version)
.Where(x => !x.IsDelta)
.FirstOrDefault();
}

Expand Down
Loading

0 comments on commit ce35a29

Please sign in to comment.