2018-08-03 13:52:31 +02:00
|
|
|
//! installer.rs
|
|
|
|
//!
|
|
|
|
//! Contains the main installer structure, as well as high-level means of controlling it.
|
|
|
|
|
2018-01-31 04:36:29 +01:00
|
|
|
use serde_json;
|
|
|
|
|
2018-01-30 08:29:34 +01:00
|
|
|
use std::fs::File;
|
2018-01-30 07:19:54 +01:00
|
|
|
|
2018-01-27 05:14:56 +01:00
|
|
|
use std::env::var;
|
|
|
|
|
2018-05-03 05:30:58 +02:00
|
|
|
use std::path::Path;
|
2018-01-27 05:14:56 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-05-03 13:50:44 +02:00
|
|
|
use std::sync::mpsc::Sender;
|
2018-01-30 04:35:00 +01:00
|
|
|
|
2018-08-07 07:34:57 +02:00
|
|
|
use config::BaseAttributes;
|
2018-01-27 04:27:41 +01:00
|
|
|
use config::Config;
|
|
|
|
|
2018-01-31 04:36:29 +01:00
|
|
|
use sources::types::Version;
|
2018-08-03 12:45:42 +02:00
|
|
|
|
|
|
|
use tasks::install::InstallTask;
|
2018-08-03 15:44:35 +02:00
|
|
|
use tasks::uninstall::UninstallTask;
|
2018-08-03 12:45:42 +02:00
|
|
|
use tasks::DependencyTree;
|
2018-01-31 04:36:29 +01:00
|
|
|
|
2018-08-04 09:03:32 +02:00
|
|
|
use logging::LoggingErrors;
|
|
|
|
|
2018-08-04 10:35:00 +02:00
|
|
|
use dirs::home_dir;
|
|
|
|
|
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-08-07 07:34:57 +02:00
|
|
|
pub base_attributes: BaseAttributes,
|
|
|
|
pub config: Option<Config>,
|
2018-08-03 12:45:42 +02:00
|
|
|
pub database: Vec<LocalInstallation>,
|
|
|
|
pub install_path: Option<PathBuf>,
|
|
|
|
pub preexisting_install: bool,
|
2018-08-04 15:35:56 +02:00
|
|
|
pub is_launcher: bool,
|
|
|
|
pub launcher_path: Option<String>,
|
2018-01-27 04:27:41 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 12:52:55 +02:00
|
|
|
/// Contains basic properties on the status of the session. Subset of InstallationFramework.
|
2018-05-03 05:39:55 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct InstallationStatus {
|
2018-08-03 12:45:42 +02:00
|
|
|
pub database: Vec<LocalInstallation>,
|
|
|
|
pub install_path: Option<String>,
|
|
|
|
pub preexisting_install: bool,
|
2018-08-04 15:35:56 +02:00
|
|
|
pub is_launcher: bool,
|
|
|
|
pub launcher_path: Option<String>,
|
2018-01-30 05:53:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 04:36:29 +01:00
|
|
|
/// Tracks the state of a local installation
|
2018-05-03 05:39:55 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub struct LocalInstallation {
|
2018-08-03 12:45:42 +02:00
|
|
|
pub name: String,
|
|
|
|
pub version: Version,
|
|
|
|
pub files: Vec<String>,
|
2018-01-31 04:36:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
impl InstallerFramework {
|
|
|
|
/// Returns a copy of the configuration.
|
2018-08-07 07:34:57 +02:00
|
|
|
pub fn get_config(&self) -> Option<Config> {
|
2018-01-27 04:27:41 +01:00
|
|
|
self.config.clone()
|
|
|
|
}
|
|
|
|
|
2018-01-27 05:14:56 +01:00
|
|
|
/// Returns the default install path.
|
|
|
|
pub fn get_default_path(&self) -> Option<String> {
|
2018-08-07 07:34:57 +02:00
|
|
|
let app_name = &self.base_attributes.name;
|
2018-01-27 05:14:56 +01:00
|
|
|
|
|
|
|
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-08-03 15:44:35 +02:00
|
|
|
/// items: Array of named packages to be installed/kept
|
2018-05-03 05:30:58 +02:00
|
|
|
/// messages: Channel used to send progress messages
|
|
|
|
/// fresh_install: If the install directory must be empty
|
2018-01-30 04:35:00 +01:00
|
|
|
pub fn install(
|
2018-05-03 05:30:58 +02:00
|
|
|
&mut self,
|
2018-01-30 04:35:00 +01:00
|
|
|
items: Vec<String>,
|
|
|
|
messages: &Sender<InstallMessage>,
|
2018-05-03 13:50:44 +02:00
|
|
|
fresh_install: bool,
|
2018-01-30 04:35:00 +01:00
|
|
|
) -> Result<(), String> {
|
2018-08-04 08:28:13 +02:00
|
|
|
info!(
|
2018-08-03 12:45:42 +02:00
|
|
|
"Framework: Installing {:?} to {:?}",
|
|
|
|
items,
|
|
|
|
self.install_path
|
|
|
|
.clone()
|
2018-08-04 09:03:32 +02:00
|
|
|
.log_expect("Install directory not initialised")
|
2018-08-03 12:45:42 +02:00
|
|
|
);
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-03 16:38:34 +02:00
|
|
|
// Calculate packages to *uninstall*
|
|
|
|
let mut uninstall_items = Vec::new();
|
|
|
|
if !fresh_install {
|
|
|
|
for package in &self.database {
|
|
|
|
if !items.contains(&package.name) {
|
|
|
|
uninstall_items.push(package.name.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:28:13 +02:00
|
|
|
info!(
|
2018-08-03 16:38:34 +02:00
|
|
|
"Framework: Uninstalling {:?} additionally.",
|
|
|
|
uninstall_items
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-03 12:45:42 +02:00
|
|
|
let task = Box::new(InstallTask {
|
|
|
|
items,
|
2018-08-03 16:38:34 +02:00
|
|
|
uninstall_items,
|
2018-08-03 12:45:42 +02:00
|
|
|
fresh_install,
|
|
|
|
});
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-03 12:45:42 +02:00
|
|
|
let mut tree = DependencyTree::build(task);
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-04 08:28:13 +02:00
|
|
|
info!("Dependency tree:\n{}", tree);
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-04 10:35:00 +02:00
|
|
|
tree.execute(self, &|msg: &str, progress: f64| {
|
|
|
|
if let Err(v) = messages.send(InstallMessage::Status(msg.to_string(), progress as _)) {
|
|
|
|
error!("Failed to submit queue message: {:?}", v);
|
|
|
|
}
|
2018-08-03 12:45:42 +02:00
|
|
|
}).map(|_x| ())
|
2018-01-29 12:08:28 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 15:44:35 +02:00
|
|
|
/// Sends a request for everything to be uninstalled.
|
|
|
|
pub fn uninstall(&mut self, messages: &Sender<InstallMessage>) -> Result<(), String> {
|
|
|
|
// TODO: Cleanup maintenance tool
|
|
|
|
|
|
|
|
let items: Vec<String> = self.database.iter().map(|x| x.name.clone()).collect();
|
|
|
|
|
|
|
|
let task = Box::new(UninstallTask { items });
|
|
|
|
|
|
|
|
let mut tree = DependencyTree::build(task);
|
|
|
|
|
2018-08-04 08:28:13 +02:00
|
|
|
info!("Dependency tree:\n{}", tree);
|
2018-08-03 15:44:35 +02:00
|
|
|
|
2018-08-04 10:35:00 +02:00
|
|
|
tree.execute(self, &|msg: &str, progress: f64| {
|
|
|
|
if let Err(v) = messages.send(InstallMessage::Status(msg.to_string(), progress as _)) {
|
|
|
|
error!("Failed to submit queue message: {:?}", v);
|
|
|
|
}
|
2018-08-03 15:44:35 +02:00
|
|
|
}).map(|_x| ())
|
|
|
|
}
|
|
|
|
|
2018-05-03 05:30:58 +02:00
|
|
|
/// Saves the applications database.
|
|
|
|
pub fn save_database(&self) -> Result<(), String> {
|
|
|
|
// We have to have a install path for us to be able to do anything
|
|
|
|
let path = match self.install_path.clone() {
|
|
|
|
Some(v) => v,
|
2018-08-04 10:35:00 +02:00
|
|
|
None => return Err("No install directory for installer".to_string()),
|
2018-05-03 05:30:58 +02:00
|
|
|
};
|
|
|
|
|
2018-05-03 06:14:44 +02:00
|
|
|
let metadata_path = path.join("metadata.json");
|
2018-05-03 05:30:58 +02:00
|
|
|
let metadata_file = match File::create(metadata_path) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
|
|
|
|
};
|
|
|
|
|
|
|
|
match serde_json::to_writer(metadata_file, &self.database) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return Err(format!("Unable to write to file: {:?}", v)),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures this installer to install to the specified location.
|
|
|
|
/// If there was a currently configured install path, this will be left as-is.
|
2018-05-03 13:50:44 +02:00
|
|
|
pub fn set_install_dir(&mut self, dir: &str) {
|
2018-05-03 06:14:44 +02:00
|
|
|
self.install_path = Some(Path::new(dir).to_owned());
|
2018-05-03 05:30:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 05:39:55 +02:00
|
|
|
/// Returns metadata on the current status of the installation.
|
|
|
|
pub fn get_installation_status(&self) -> InstallationStatus {
|
|
|
|
InstallationStatus {
|
|
|
|
database: self.database.clone(),
|
2018-05-03 06:14:44 +02:00
|
|
|
install_path: match self.install_path.clone() {
|
|
|
|
Some(v) => Some(v.display().to_string()),
|
2018-05-03 13:50:44 +02:00
|
|
|
None => None,
|
2018-05-03 06:14:44 +02:00
|
|
|
},
|
2018-05-03 13:50:44 +02:00
|
|
|
preexisting_install: self.preexisting_install,
|
2018-08-04 15:35:56 +02:00
|
|
|
is_launcher: self.is_launcher,
|
|
|
|
launcher_path: self.launcher_path.clone(),
|
2018-05-03 05:39:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:27:41 +01:00
|
|
|
/// Creates a new instance of the Installer Framework with a specified Config.
|
2018-08-07 07:34:57 +02:00
|
|
|
pub fn new(attrs: BaseAttributes) -> Self {
|
2018-05-03 05:30:58 +02:00
|
|
|
InstallerFramework {
|
2018-08-07 07:34:57 +02:00
|
|
|
base_attributes: attrs,
|
|
|
|
config: None,
|
2018-05-03 13:50:44 +02:00
|
|
|
database: Vec::new(),
|
|
|
|
install_path: None,
|
|
|
|
preexisting_install: false,
|
2018-08-04 15:35:56 +02:00
|
|
|
is_launcher: false,
|
|
|
|
launcher_path: None,
|
2018-05-03 05:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new instance of the Installer Framework with a specified Config, managing
|
|
|
|
/// a pre-existing installation.
|
2018-08-07 07:34:57 +02:00
|
|
|
pub fn new_with_db(attrs: BaseAttributes, install_path: &Path) -> Result<Self, String> {
|
2018-05-03 06:14:44 +02:00
|
|
|
let path = install_path.to_owned();
|
|
|
|
let metadata_path = path.join("metadata.json");
|
2018-05-03 05:30:58 +02:00
|
|
|
let metadata_file = match File::open(metadata_path) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
|
|
|
|
};
|
|
|
|
|
2018-05-03 13:50:44 +02:00
|
|
|
let database: Vec<LocalInstallation> = match serde_json::from_reader(metadata_file) {
|
2018-05-03 05:30:58 +02:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return Err(format!("Unable to read metadata file: {:?}", v)),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(InstallerFramework {
|
2018-08-07 07:34:57 +02:00
|
|
|
base_attributes: attrs,
|
|
|
|
config: None,
|
2018-05-03 05:30:58 +02:00
|
|
|
database,
|
2018-05-03 13:50:44 +02:00
|
|
|
install_path: Some(path),
|
|
|
|
preexisting_install: true,
|
2018-08-04 15:35:56 +02:00
|
|
|
is_launcher: false,
|
|
|
|
launcher_path: None,
|
2018-05-03 05:30:58 +02:00
|
|
|
})
|
2018-01-27 04:27:41 +01:00
|
|
|
}
|
|
|
|
}
|