2018-01-27 04:27:41 +01:00
|
|
|
/// installer.rs
|
|
|
|
///
|
|
|
|
/// Contains the main installer structure, as well as high-level means of controlling it.
|
|
|
|
|
2018-01-29 13:28:14 +01:00
|
|
|
use regex::Regex;
|
|
|
|
|
2018-01-27 05:14:56 +01:00
|
|
|
use std::env::home_dir;
|
|
|
|
use std::env::var;
|
2018-01-29 13:28:14 +01:00
|
|
|
use std::env::consts::OS;
|
2018-01-27 05:14:56 +01:00
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-01-30 05:53:28 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2018-01-30 04:35:00 +01:00
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
use config::Config;
|
|
|
|
|
2018-01-30 05:53:28 +01:00
|
|
|
use http::stream_file;
|
|
|
|
|
|
|
|
use number_prefix::{decimal_prefix, Standalone, Prefixed};
|
|
|
|
|
2018-01-30 04:35:00 +01:00
|
|
|
/// A message thrown during the installation of packages.
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub enum InstallMessage {
|
|
|
|
Status(String, f64),
|
|
|
|
Error(String),
|
|
|
|
EOF,
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
/// The installer framework contains metadata about packages, what is installable, what isn't,
|
|
|
|
/// etc.
|
|
|
|
pub struct InstallerFramework {
|
2018-01-27 12:58:56 +01:00
|
|
|
config: Config,
|
2018-01-27 04:27:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 05:53:28 +01:00
|
|
|
struct DownloadProgress {
|
|
|
|
downloaded: usize
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
impl InstallerFramework {
|
|
|
|
/// Returns a copy of the configuration.
|
|
|
|
pub fn get_config(&self) -> Config {
|
|
|
|
self.config.clone()
|
|
|
|
}
|
|
|
|
|
2018-01-27 05:14:56 +01:00
|
|
|
/// Returns the default install path.
|
|
|
|
pub fn get_default_path(&self) -> Option<String> {
|
|
|
|
let app_name = &self.config.general.name;
|
|
|
|
|
|
|
|
let base_dir = match var("LOCALAPPDATA") {
|
|
|
|
Ok(path) => PathBuf::from(path),
|
2018-01-27 12:58:56 +01:00
|
|
|
Err(_) => home_dir()?,
|
2018-01-27 05:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let file = base_dir.join(app_name);
|
|
|
|
|
|
|
|
Some(file.to_str()?.to_owned())
|
|
|
|
}
|
|
|
|
|
2018-01-29 12:08:28 +01:00
|
|
|
/// Sends a request for something to be installed.
|
2018-01-30 04:35:00 +01:00
|
|
|
pub fn install(
|
|
|
|
&self,
|
|
|
|
items: Vec<String>,
|
|
|
|
messages: &Sender<InstallMessage>,
|
|
|
|
) -> Result<(), String> {
|
2018-01-29 13:28:14 +01:00
|
|
|
// TODO: Error handling
|
2018-01-29 12:08:28 +01:00
|
|
|
println!("Framework: Installing {:?}", items);
|
2018-01-29 13:28:14 +01:00
|
|
|
|
|
|
|
// Resolve items in config
|
|
|
|
let mut to_install = Vec::new();
|
|
|
|
|
|
|
|
for description in &self.config.packages {
|
|
|
|
if items.contains(&description.name) {
|
|
|
|
to_install.push(description.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Resolved to {:?}", to_install);
|
|
|
|
|
|
|
|
// Install packages
|
2018-01-30 04:35:00 +01:00
|
|
|
let mut count = 0.0 as f64;
|
|
|
|
let max = to_install.len() as f64;
|
|
|
|
|
2018-01-29 13:28:14 +01:00
|
|
|
for package in to_install.iter() {
|
2018-01-30 04:35:00 +01:00
|
|
|
let base_package_percentage = count / max;
|
|
|
|
let base_package_range = ((count + 1.0) / max) - base_package_percentage;
|
|
|
|
|
2018-01-29 13:28:14 +01:00
|
|
|
println!("Installing {}", package.name);
|
|
|
|
|
2018-01-30 04:35:00 +01:00
|
|
|
messages
|
|
|
|
.send(InstallMessage::Status(
|
|
|
|
format!(
|
|
|
|
"Polling {} for latest version of {}",
|
|
|
|
package.source.name, package.name
|
|
|
|
),
|
|
|
|
base_package_percentage + base_package_range * 0.25,
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let results = package.source.get_current_releases()?;
|
|
|
|
|
|
|
|
messages
|
|
|
|
.send(InstallMessage::Status(
|
|
|
|
format!("Resolving dependency for {}", package.name),
|
|
|
|
base_package_percentage + base_package_range * 0.50,
|
|
|
|
))
|
|
|
|
.unwrap();
|
2018-01-29 13:28:14 +01:00
|
|
|
|
|
|
|
let filtered_regex = package.source.match_regex.replace("#PLATFORM#", OS);
|
2018-01-30 04:35:00 +01:00
|
|
|
let regex = match Regex::new(&filtered_regex) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return Err(format!("An error occured while compiling regex: {:?}", v)),
|
|
|
|
};
|
2018-01-29 13:28:14 +01:00
|
|
|
|
|
|
|
// Find the latest release in here
|
2018-01-29 13:37:17 +01:00
|
|
|
let latest_result = results
|
|
|
|
.into_iter()
|
|
|
|
.filter(|f| f.files.iter().filter(|x| regex.is_match(&x.name)).count() > 0)
|
2018-01-30 04:35:00 +01:00
|
|
|
.max_by_key(|f| f.version.clone());
|
|
|
|
|
|
|
|
let latest_result = match latest_result {
|
|
|
|
Some(v) => v,
|
|
|
|
None => return Err(format!("No release with correct file found")),
|
|
|
|
};
|
2018-01-29 13:37:17 +01:00
|
|
|
|
|
|
|
// Find the matching file in here
|
2018-01-29 13:37:30 +01:00
|
|
|
let latest_file = latest_result
|
|
|
|
.files
|
|
|
|
.into_iter()
|
2018-01-29 13:37:17 +01:00
|
|
|
.filter(|x| regex.is_match(&x.name))
|
2018-01-29 13:37:30 +01:00
|
|
|
.next()
|
|
|
|
.unwrap();
|
2018-01-29 13:37:17 +01:00
|
|
|
|
|
|
|
println!("{:?}", latest_file);
|
2018-01-30 04:35:00 +01:00
|
|
|
|
2018-01-30 05:53:28 +01:00
|
|
|
// Download this file
|
|
|
|
let lock = Arc::new(Mutex::new(DownloadProgress {
|
|
|
|
downloaded: 0
|
|
|
|
}));
|
|
|
|
|
|
|
|
stream_file(latest_file.url, |data, size| {
|
|
|
|
let mut reference = lock.lock().unwrap();
|
|
|
|
reference.downloaded += data.len();
|
|
|
|
|
|
|
|
let base_percentage = base_package_percentage + base_package_range * 0.50;
|
|
|
|
let range_percentage = base_package_range / 2.0;
|
|
|
|
|
|
|
|
let global_percentage = if size == 0 {
|
|
|
|
base_percentage
|
|
|
|
} else {
|
|
|
|
let download_percentage = (reference.downloaded as f64) / (size as f64);
|
|
|
|
// Split up the bar for this download in half (for metadata download + parse), then
|
|
|
|
// add on our current percentage
|
|
|
|
base_percentage + range_percentage * download_percentage
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pretty print data volumes
|
|
|
|
let pretty_current = match decimal_prefix(reference.downloaded as f64) {
|
|
|
|
Standalone(bytes) => format!("{} bytes", bytes),
|
|
|
|
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
|
|
|
|
};
|
|
|
|
let pretty_total = match decimal_prefix(size as f64) {
|
|
|
|
Standalone(bytes) => format!("{} bytes", bytes),
|
|
|
|
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
|
|
|
|
};
|
|
|
|
|
|
|
|
messages
|
|
|
|
.send(InstallMessage::Status(
|
|
|
|
format!("Downloading {} ({} of {})", package.name, pretty_current,
|
|
|
|
pretty_total),
|
|
|
|
global_percentage,
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
})?;
|
|
|
|
|
|
|
|
println!("File downloaded successfully");
|
2018-01-30 04:35:00 +01:00
|
|
|
|
|
|
|
count += 1.0;
|
2018-01-29 13:28:14 +01:00
|
|
|
}
|
2018-01-30 04:35:00 +01:00
|
|
|
|
|
|
|
Ok(())
|
2018-01-29 12:08:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
/// Creates a new instance of the Installer Framework with a specified Config.
|
2018-01-27 12:58:56 +01:00
|
|
|
pub fn new(config: Config) -> Self {
|
|
|
|
InstallerFramework { config }
|
2018-01-27 04:27:41 +01:00
|
|
|
}
|
|
|
|
}
|