Skip to content

Commit a90bd69

Browse files
committed
get metadata from cargo.toml
1 parent d266600 commit a90bd69

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

Cargo.lock

+16-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ self_update = { git = "https://github.com/hacknus/self_update", features = ["arc
2727
tempfile = "3.15"
2828
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "http2"] }
2929
semver = "1.0.24"
30+
cargo_metadata = "0.19"
3031

3132
[build-dependencies]
3233
regex = "1.11"

src/update.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
use cargo_metadata::MetadataCommand;
12
use self_update::self_replace;
23
use self_update::update::Release;
34
use semver::Version;
45
use std::path::Path;
56
use std::{env, fs, io};
67

7-
const REPO_OWNER: &str = "hacknus";
8-
const REPO_NAME: &str = "serial-monitor-rust";
9-
const MACOS_APP_NAME: &str = "Serial Monitor.app";
10-
118
/// method to copy the complete directory `src` to `dest` but skipping the binary `binary_name`
129
/// since we have to use `self-replace` for that.
1310
fn copy_dir(src: &Path, dest: &Path, binary_name: &str) -> io::Result<()> {
@@ -38,9 +35,17 @@ fn copy_dir(src: &Path, dest: &Path, binary_name: &str) -> io::Result<()> {
3835

3936
/// Function to check for updates and return the latest one, if it is more recent than the current version
4037
pub fn check_update() -> Option<Release> {
38+
let metadata = MetadataCommand::new().exec().ok()?;
39+
let url = metadata.root_package()?.clone().homepage?;
40+
let parts: Vec<&str> = url.split('/').collect();
41+
let (repo_owner, repo_name) = if parts.len() >= 5 && parts[2] == "github.com" {
42+
(parts[3].to_string(), parts[4].to_string())
43+
} else {
44+
return None;
45+
};
4146
if let Ok(builder) = self_update::backends::github::ReleaseList::configure()
42-
.repo_owner(REPO_OWNER)
43-
.repo_name(REPO_NAME)
47+
.repo_owner(&repo_owner)
48+
.repo_name(&repo_name)
4449
.build()
4550
{
4651
if let Ok(releases) = builder.fetch() {
@@ -121,9 +126,24 @@ pub fn update(release: Release) -> Result<(), Box<dyn std::error::Error>> {
121126
// extracted archive, but we cannot just assume that the parent directory of the
122127
// currently running executable is equal to the app name - this is especially not
123128
// the case if we run the code with `cargo run`.
129+
130+
// Fetch the metadata of the current Cargo project
131+
let metadata = MetadataCommand::new().exec()?;
132+
133+
// Access the package metadata
134+
let mac_os_app_name = metadata
135+
.root_package()
136+
.ok_or("Unable to get cargo metadata!")?
137+
.metadata
138+
.get("bundle")
139+
.ok_or("Unable to get cargo bundle metadata!")?
140+
.get("name")
141+
.ok_or("Unable to get cargo bundle metadata name!")?
142+
.as_str()
143+
.unwrap();
124144
tmp_archive_dir
125145
.path()
126-
.join(format!("{}/Contents/MacOS/{}", MACOS_APP_NAME, binary))
146+
.join(format!("{}.app/Contents/MacOS/{}", mac_os_app_name, binary))
127147
} else if cfg!(target_os = "linux") {
128148
let binary = env::current_exe()?
129149
.file_name()

0 commit comments

Comments
 (0)