Embed the res directory in the binary

...and extract to a temporary directory when needed (if
`PARROTD_RESOURCE_DIR` is not defined)
This commit is contained in:
Peter Cai 2022-11-20 22:39:30 -05:00
parent a4c9d30e22
commit 886a2cff75
3 changed files with 88 additions and 1 deletions

71
Cargo.lock generated
View File

@ -144,6 +144,15 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "fastrand"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499"
dependencies = [
"instant",
]
[[package]] [[package]]
name = "field-offset" name = "field-offset"
version = "0.3.4" version = "0.3.4"
@ -420,6 +429,34 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "include_dir"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e"
dependencies = [
"include_dir_macros",
]
[[package]]
name = "include_dir_macros"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "libappindicator" name = "libappindicator"
version = "0.7.1" version = "0.7.1"
@ -544,8 +581,10 @@ dependencies = [
"alphanumeric-sort", "alphanumeric-sort",
"glib", "glib",
"gtk", "gtk",
"include_dir",
"libappindicator", "libappindicator",
"sysinfo", "sysinfo",
"tempfile",
] ]
[[package]] [[package]]
@ -652,6 +691,24 @@ dependencies = [
"num_cpus", "num_cpus",
] ]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
]
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.3.3" version = "0.3.3"
@ -745,6 +802,20 @@ dependencies = [
"version-compare", "version-compare",
] ]
[[package]]
name = "tempfile"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
dependencies = [
"cfg-if",
"fastrand",
"libc",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.37" version = "1.0.37"

View File

@ -11,3 +11,5 @@ gtk = "0.15"
glib = "0.15" glib = "0.15"
sysinfo = "0.26" sysinfo = "0.26"
alphanumeric-sort = "1.4" alphanumeric-sort = "1.4"
include_dir = "0.7"
tempfile = "3"

View File

@ -10,6 +10,9 @@ use pack::*;
use std::cell::RefCell; use std::cell::RefCell;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use sysinfo::*; use sysinfo::*;
use include_dir::{include_dir, Dir};
static EMBEDDED_RES: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/res");
thread_local! { thread_local! {
static SYSTEM: RefCell<System> = RefCell::new(System::new_all()); static SYSTEM: RefCell<System> = RefCell::new(System::new_all());
@ -45,7 +48,14 @@ fn init_indicator(icon_pack: &IconPack) -> AppIndicator {
fn main() { fn main() {
gtk::init().unwrap(); gtk::init().unwrap();
let res_dir = std::env::var("PARROTD_RESOURCE_DIR").unwrap_or("./res".to_owned()); let tmp_dir = tempfile::tempdir();
let _tmp_dir = tmp_dir.as_ref();
let res_dir = std::env::var("PARROTD_RESOURCE_DIR").unwrap_or_else(|_| {
let tmp_dir = _tmp_dir.expect("Cannot create temporary directory");
EMBEDDED_RES.extract(tmp_dir.path()).unwrap();
tmp_dir.path().to_str().unwrap().to_string()
});
let pack_name = std::env::var("PARROTD_ICON_PACK").unwrap_or("parrot".to_owned()); let pack_name = std::env::var("PARROTD_ICON_PACK").unwrap_or("parrot".to_owned());
let variant = PackVariant::parse( let variant = PackVariant::parse(
&std::env::var("PARROTD_VARIANT").unwrap_or("dark".to_owned())).expect("Invalid variant name"); &std::env::var("PARROTD_VARIANT").unwrap_or("dark".to_owned())).expect("Invalid variant name");
@ -75,4 +85,8 @@ fn main() {
}); });
gtk::main(); gtk::main();
if let Ok(tmp_dir) = tmp_dir {
tmp_dir.close().unwrap();
}
} }