Copy installer executable to target directory, fix version sorting

This commit is contained in:
James 2018-01-30 20:11:34 +11:00
parent 9f0e2352b7
commit 6cd8abebe5
2 changed files with 45 additions and 11 deletions

View File

@ -14,6 +14,7 @@ use std::fs::File;
use std::env::home_dir; use std::env::home_dir;
use std::env::var; use std::env::var;
use std::env::current_exe;
use std::env::consts::OS; use std::env::consts::OS;
use std::path::PathBuf; use std::path::PathBuf;
@ -82,7 +83,7 @@ impl InstallerFramework {
let path = PathBuf::from(path); let path = PathBuf::from(path);
if !path.exists() { if !path.exists() {
match create_dir_all(&path) { match create_dir_all(&path) {
Ok(_) => {}, Ok(_) => {}
Err(v) => return Err(format!("Failed to create install directory: {:?}", v)), Err(v) => return Err(format!("Failed to create install directory: {:?}", v)),
} }
} }
@ -153,7 +154,7 @@ impl InstallerFramework {
let latest_result = results let latest_result = results
.into_iter() .into_iter()
.filter(|f| f.files.iter().filter(|x| regex.is_match(&x.name)).count() > 0) .filter(|f| f.files.iter().filter(|x| regex.is_match(&x.name)).count() > 0)
.min_by_key(|f| f.version.clone()); .max_by_key(|f| f.version.clone());
let latest_result = match latest_result { let latest_result = match latest_result {
Some(v) => v, Some(v) => v,
@ -172,7 +173,7 @@ impl InstallerFramework {
// Download this file // Download this file
let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 })); let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 }));
let data_storage : Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new())); let data_storage: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new()));
// 33-66%: downloading file // 33-66%: downloading file
stream_file(latest_file.url, |data, size| { stream_file(latest_file.url, |data, size| {
@ -237,15 +238,12 @@ impl InstallerFramework {
for i in 0..zip_size { for i in 0..zip_size {
let mut file = zip.by_index(i).unwrap(); let mut file = zip.by_index(i).unwrap();
let percentage = extract_base_percentage + let percentage =
extract_range_percentage / zip_size as f64 * i as f64; extract_base_percentage + extract_range_percentage / zip_size as f64 * i as f64;
messages messages
.send(InstallMessage::Status( .send(InstallMessage::Status(
format!( format!("Extracting {} ({} of {})", file.name(), i + 1, zip_size),
"Extracting {} ({} of {})",
file.name(), i + 1, zip_size
),
percentage, percentage,
)) ))
.unwrap(); .unwrap();
@ -269,7 +267,7 @@ impl InstallerFramework {
Ok(v) => v, Ok(v) => v,
Err(v) => return Err(format!("Unable to open file: {:?}", v)), Err(v) => return Err(format!("Unable to open file: {:?}", v)),
}, },
None => {}, None => {}
} }
let mut target_file = match File::create(target_path) { let mut target_file = match File::create(target_path) {
@ -287,6 +285,42 @@ impl InstallerFramework {
count += 1.0; count += 1.0;
} }
// Copy installer binary to target directory
messages
.send(InstallMessage::Status(
format!("Copying installer binary"),
0.99,
))
.unwrap();
let current_app = match current_exe() {
Ok(v) => v,
Err(v) => return Err(format!("Unable to locate installer binary: {:?}", v)),
};
let mut current_app_file = match File::open(current_app) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open installer binary: {:?}", v)),
};
let platform_extension = if cfg!(windows) {
"maintenancetool.exe"
} else {
"maintenancetool"
};
let new_app = path.join(platform_extension);
let mut new_app_file = match File::create(new_app) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open installer binary: {:?}", v)),
};
match copy(&mut current_app_file, &mut new_app_file) {
Err(v) => return Err(format!("Unable to copy installer binary: {:?}", v)),
_ => {}
};
Ok(()) Ok(())
} }

View File

@ -158,7 +158,7 @@ impl Service for WebService {
.collect::<HashMap<String, String>>(); .collect::<HashMap<String, String>>();
let mut to_install = Vec::new(); let mut to_install = Vec::new();
let mut path : Option<String> = None; let mut path: Option<String> = None;
// Transform results into just an array of stuff to install // Transform results into just an array of stuff to install
for (key, value) in results.iter() { for (key, value) in results.iter() {