Skip to content

Commit

Permalink
feat: search both origin and canonicalized directory of current_exe (
Browse files Browse the repository at this point in the history
…#94)

In recent changes, we force `current_exe` to be canonicalized.
However, for homebrew, `maa` is installed in separate directory,
and linked to `/usr/local/bin/maa` or `/opt/homebrew/bin/maa`.
To search library and resource also installed by homebrew,
we need to search the origin directory of `current_exe`.
So we search both origin and canonicalized directory of `current_exe`.
  • Loading branch information
wangl-cc authored Oct 30, 2023
1 parent e816e01 commit fee2195
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
6 changes: 3 additions & 3 deletions maa-cli/src/installer/maa_cli.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::{
download::{download, Checker},
extract::Archive,
maa_core::current_exe,
};

use crate::dirs::{Dirs, Ensure};

use std::{
env::{consts::EXE_SUFFIX, var_os},
env::{consts::EXE_SUFFIX, current_exe, var_os},
path::Path,
};

use anyhow::{bail, Context, Result};
use dunce::canonicalize;
use semver::Version;
use serde::Deserialize;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn update(dirs: &Dirs) -> Result<()> {
);

let bin_name = name();
let bin_path = current_exe()?;
let bin_path = canonicalize(current_exe()?)?;
let cache_dir = dirs.cache().ensure()?;

asset.download(cache_dir)?.extract(|path| {
Expand Down
38 changes: 25 additions & 13 deletions maa-cli/src/installer/maa_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use std::{
env::{
consts::{DLL_PREFIX, DLL_SUFFIX},
var_os,
current_exe, var_os,
},
path::{Component, Path, PathBuf},
time::Duration,
Expand Down Expand Up @@ -293,8 +293,7 @@ pub fn find_lib_dir(dirs: &Dirs) -> Option<PathBuf> {
return Some(lib_dir.to_path_buf());
}

if let Ok(path) = current_exe() {
let exe_dir = path.parent().unwrap();
current_exe_dir_find(|exe_dir| {
if exe_dir.join(MAA_CORE_NAME).exists() {
return Some(exe_dir.to_path_buf());
}
Expand All @@ -304,9 +303,9 @@ pub fn find_lib_dir(dirs: &Dirs) -> Option<PathBuf> {
return Some(lib_dir);
}
}
}

None
None
})
}

pub fn find_resource(dirs: &Dirs) -> Option<PathBuf> {
Expand All @@ -315,8 +314,7 @@ pub fn find_resource(dirs: &Dirs) -> Option<PathBuf> {
return Some(resource_dir.to_path_buf());
}

if let Ok(path) = current_exe() {
let exe_dir = path.parent().unwrap();
current_exe_dir_find(|exe_dir| {
let resource_dir = exe_dir.join("resource");
if resource_dir.exists() {
return Some(resource_dir);
Expand All @@ -334,12 +332,26 @@ pub fn find_resource(dirs: &Dirs) -> Option<PathBuf> {
return Some(resource_dir);
}
}
}

None
None
})
}

pub fn current_exe() -> Result<PathBuf> {
let path = std::env::current_exe()?;
Ok(canonicalize(path)?)
/// Find path starting from current executable directory
pub fn current_exe_dir_find<F>(finder: F) -> Option<PathBuf>
where
F: Fn(&Path) -> Option<PathBuf>,
{
let exe_path = current_exe().ok()?;
println!("exe_path: {:?}", exe_path);
let exe_dir = exe_path.parent().unwrap();
if let Some(path) = finder(exe_dir) {
return Some(path);
}
let canonicalized = canonicalize(exe_dir).ok()?;
println!("canonicalized: {:?}", canonicalized);
if canonicalized == exe_dir {
None
} else {
finder(&canonicalized)
}
}

0 comments on commit fee2195

Please sign in to comment.