Improve modulo app bundle generation. Fix #430

This commit is contained in:
Freddy 2020-09-08 22:00:24 +02:00
parent e94567b37b
commit fbfafe3564
3 changed files with 29 additions and 11 deletions

View File

@ -10,7 +10,7 @@ edition = "2018"
build="build.rs" build="build.rs"
[modulo] [modulo]
version = "0.1.0" version = "0.1.1"
[dependencies] [dependencies]
widestring = "0.4.0" widestring = "0.4.0"

View File

@ -1,10 +1,25 @@
use log::info; use log::info;
use std::path::PathBuf;
use std::os::unix::fs::symlink; use std::os::unix::fs::symlink;
const MODULO_APP_BUNDLE_NAME: &str = "Modulo.app"; const MODULO_APP_BUNDLE_NAME: &str = "Modulo.app";
const MODULO_APP_BUNDLE_PLIST_CONTENT: &'static str = include_str!("../../res/mac/modulo.plist"); const MODULO_APP_BUNDLE_PLIST_CONTENT: &'static str = include_str!("../../res/mac/modulo.plist");
pub fn generate_modulo_app_bundle(modulo_path: &str) -> Result<PathBuf, std::io::Error> { pub fn generate_modulo_app_bundle(modulo_path: &str) -> Result<PathBuf, std::io::Error> {
let modulo_pathbuf = PathBuf::from(modulo_path);
let modulo_path: String = if !modulo_pathbuf.exists() {
// If modulo was taken from the PATH, we need to calculate the absolute path
// To do so, we use the `which` command
let output = std::process::Command::new("which").arg("modulo").output().expect("unable to call 'which' command to determine modulo's full path");
let path = String::from_utf8_lossy(output.stdout.as_slice());
let path = path.trim();
info!("Detected modulo's full path: {:?}", &path);
path.to_string()
}else{
modulo_path.to_owned()
};
let data_dir = crate::context::get_data_dir(); let data_dir = crate::context::get_data_dir();
let modulo_app_dir = data_dir.join(MODULO_APP_BUNDLE_NAME); let modulo_app_dir = data_dir.join(MODULO_APP_BUNDLE_NAME);
@ -24,7 +39,7 @@ pub fn generate_modulo_app_bundle(modulo_path: &str) -> Result<PathBuf, std::io:
std::fs::create_dir(&macos_dir)?; std::fs::create_dir(&macos_dir)?;
// Generate the Plist file // Generate the Plist file
let plist_content = MODULO_APP_BUNDLE_PLIST_CONTENT.replace("{{{modulo_path}}}", modulo_path); let plist_content = MODULO_APP_BUNDLE_PLIST_CONTENT.replace("{{{modulo_path}}}", &modulo_path);
let plist_file = contents_dir.join("Info.plist"); let plist_file = contents_dir.join("Info.plist");
std::fs::write(plist_file, plist_content)?; std::fs::write(plist_file, plist_content)?;

View File

@ -44,16 +44,19 @@ impl ModuloManager {
} }
} }
// TODO: explain why if let Some(ref path) = modulo_path {
#[cfg(target_os = "macos")] info!("Using modulo at {:?}", path);
{
modulo_path = generate_modulo_app_bundle(&modulo_path).expect("unable to generate modulo app stub")
.to_string_lossy().to_string();
}
// MacOS specific remark
if let Some(ref modulo_path) = modulo_path { // In order to give modulo the focus when spawning a form, modulo has to be
info!("Using modulo at {:?}", modulo_path); // wrapped inside an application bundle. Therefore, we generate a bundle
// at startup.
// See issue: https://github.com/federico-terzi/espanso/issues/430
#[cfg(target_os = "macos")]
{
modulo_path = Some(mac::generate_modulo_app_bundle(path).expect("unable to generate modulo app stub")
.to_string_lossy().to_string());
}
} }
Self { modulo_path } Self { modulo_path }