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::home_dir;
|
|
|
|
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-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;
|
|
|
|
use tasks::DependencyTree;
|
2018-01-31 04:36:29 +01:00
|
|
|
|
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-03 12:45:42 +02:00
|
|
|
pub config: Config,
|
|
|
|
pub database: Vec<LocalInstallation>,
|
|
|
|
pub install_path: Option<PathBuf>,
|
|
|
|
pub preexisting_install: bool,
|
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-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.
|
|
|
|
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-05-03 05:30:58 +02:00
|
|
|
/// items: Array of named packages to be installed
|
|
|
|
/// 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-03 12:45:42 +02:00
|
|
|
println!(
|
|
|
|
"Framework: Installing {:?} to {:?}",
|
|
|
|
items,
|
|
|
|
self.install_path
|
|
|
|
.clone()
|
|
|
|
.expect("Install directory not initialised")
|
|
|
|
);
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-03 12:45:42 +02:00
|
|
|
let task = Box::new(InstallTask {
|
|
|
|
items,
|
|
|
|
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-03 12:45:42 +02:00
|
|
|
println!("Dependency tree:\n{}", tree);
|
2018-01-30 05:53:28 +01:00
|
|
|
|
2018-08-03 12:45:42 +02:00
|
|
|
tree.execute(self, &|msg: &str, progress: f32| match messages
|
|
|
|
.send(InstallMessage::Status(msg.to_string(), progress as _))
|
2018-05-03 06:51:44 +02:00
|
|
|
{
|
2018-08-03 12:45:42 +02:00
|
|
|
Err(v) => eprintln!("Failed to submit queue message: {:?}", v),
|
2018-01-30 10:11:34 +01:00
|
|
|
_ => {}
|
2018-08-03 12:45:42 +02:00
|
|
|
}).map(|_x| ())
|
2018-01-29 12:08:28 +01:00
|
|
|
}
|
|
|
|
|
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-05-03 13:50:44 +02:00
|
|
|
None => return Err(format!("No install directory for installer")),
|
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-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-01-27 12:58:56 +01:00
|
|
|
pub fn new(config: Config) -> Self {
|
2018-05-03 05:30:58 +02:00
|
|
|
InstallerFramework {
|
|
|
|
config,
|
2018-05-03 13:50:44 +02:00
|
|
|
database: Vec::new(),
|
|
|
|
install_path: None,
|
|
|
|
preexisting_install: false,
|
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-05-03 13:50:44 +02:00
|
|
|
pub fn new_with_db(config: Config, 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 {
|
|
|
|
config,
|
|
|
|
database,
|
2018-05-03 13:50:44 +02:00
|
|
|
install_path: Some(path),
|
|
|
|
preexisting_install: true,
|
2018-05-03 05:30:58 +02:00
|
|
|
})
|
2018-01-27 04:27:41 +01:00
|
|
|
}
|
|
|
|
}
|