|
| 1 | +use cargo_metadata::MetadataCommand; |
1 | 2 | use self_update::self_replace; |
2 | 3 | use self_update::update::Release; |
3 | 4 | use semver::Version; |
4 | 5 | use std::path::Path; |
5 | 6 | use std::{env, fs, io}; |
6 | 7 |
|
7 | | -const REPO_OWNER: &str = "hacknus"; |
8 | | -const REPO_NAME: &str = "serial-monitor-rust"; |
9 | | -const MACOS_APP_NAME: &str = "Serial Monitor.app"; |
10 | | - |
11 | 8 | /// method to copy the complete directory `src` to `dest` but skipping the binary `binary_name` |
12 | 9 | /// since we have to use `self-replace` for that. |
13 | 10 | 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<()> { |
38 | 35 |
|
39 | 36 | /// Function to check for updates and return the latest one, if it is more recent than the current version |
40 | 37 | 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 | + }; |
41 | 46 | 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) |
44 | 49 | .build() |
45 | 50 | { |
46 | 51 | if let Ok(releases) = builder.fetch() { |
@@ -121,9 +126,24 @@ pub fn update(release: Release) -> Result<(), Box<dyn std::error::Error>> { |
121 | 126 | // extracted archive, but we cannot just assume that the parent directory of the |
122 | 127 | // currently running executable is equal to the app name - this is especially not |
123 | 128 | // 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(); |
124 | 144 | tmp_archive_dir |
125 | 145 | .path() |
126 | | - .join(format!("{}/Contents/MacOS/{}", MACOS_APP_NAME, binary)) |
| 146 | + .join(format!("{}.app/Contents/MacOS/{}", mac_os_app_name, binary)) |
127 | 147 | } else if cfg!(target_os = "linux") { |
128 | 148 | let binary = env::current_exe()? |
129 | 149 | .file_name() |
|
0 commit comments