liftinstall/src/installer.rs

90 lines
2.5 KiB
Rust
Raw Normal View History

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-27 04:27:41 +01:00
use config::Config;
/// 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
}
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())
}
/// Sends a request for something to be installed.
pub fn install(&self, items: Vec<String>) {
2018-01-29 13:28:14 +01:00
// TODO: Error handling
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
for package in to_install.iter() {
println!("Installing {}", package.name);
let results = package.source.get_current_releases().unwrap();
let filtered_regex = package.source.match_regex.replace("#PLATFORM#", OS);
let regex = Regex::new(&filtered_regex).unwrap();
// Find the latest release in here
let latest_result = results
.into_iter()
.filter(|f| f.files.iter().filter(|x| regex.is_match(&x.name)).count() > 0)
2018-01-29 13:37:30 +01:00
.max_by_key(|f| f.version.clone())
.unwrap();
// Find the matching file in here
2018-01-29 13:37:30 +01:00
let latest_file = latest_result
.files
.into_iter()
.filter(|x| regex.is_match(&x.name))
2018-01-29 13:37:30 +01:00
.next()
.unwrap();
println!("{:?}", latest_file);
2018-01-29 13:28:14 +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
}
}