From 53c539584f9bc423943e23b629aba446dbceabfa Mon Sep 17 00:00:00 2001 From: James Date: Mon, 29 Jan 2018 23:28:14 +1100 Subject: [PATCH] Add basic installer logic --- Cargo.lock | 69 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/config.rs | 25 +++++++++++--- src/installer.rs | 34 +++++++++++++++++++ src/main.rs | 1 + src/sources/github/mod.rs | 6 ++++ src/sources/mod.rs | 14 +++++++- src/sources/types.rs | 2 +- 8 files changed, 146 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d18d8f..3b0a5c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,11 @@ +[[package]] +name = "aho-corasick" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "base64" version = "0.9.0" @@ -263,6 +271,14 @@ name = "matches" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.2.6" @@ -462,6 +478,23 @@ name = "redox_syscall" version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "regex" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "relay" version = "0.1.0" @@ -608,6 +641,15 @@ dependencies = [ "rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread_local" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "time" version = "0.1.39" @@ -721,6 +763,14 @@ name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "url" version = "1.6.0" @@ -731,6 +781,11 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "utf8-ranges" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "vcpkg" version = "0.2.2" @@ -741,6 +796,11 @@ name = "version_check" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "walkdir" version = "0.1.8" @@ -810,6 +870,7 @@ dependencies = [ "mime_guess 1.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "nfd 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", @@ -821,6 +882,7 @@ dependencies = [ ] [metadata] +"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "229d032f1a99302697f10b27167ae6d03d49d032e6a8e2550e8d3fc13356d2b4" "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" @@ -857,6 +919,7 @@ dependencies = [ "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" "checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376" +"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" "checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e2e00e17be181010a91dbfefb01660b17311059dc8c7f48b9017677721e732bd" "checksum mime_guess 1.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dc7e82a15629bb4ecd9e72365bf33d1382be91e030f820edb8e2a21c02430da8" @@ -879,6 +942,8 @@ dependencies = [ "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)" = "512870020642bb8c221bf68baa1b2573da814f6ccfe5c9699b1c303047abe9b1" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "744554e01ccbd98fff8c457c3b092cd67af62a555a43bfe97ae8a0451f7799fa" +"checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e" "checksum relay 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f301bafeb60867c85170031bdb2fcf24c8041f33aee09e7b116a58d4e9f781c5" "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum schannel 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "acece75e0f987c48863a6c792ec8b7d6c4177d4a027f8ccc72f849794f437016" @@ -899,6 +964,7 @@ dependencies = [ "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" +"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" "checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098" "checksum tokio-core 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "52b4e32d8edbf29501aabb3570f027c6ceb00ccef6538f4bddba0200503e74e8" "checksum tokio-io 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "514aae203178929dbf03318ad7c683126672d4d96eccb77b29603d33c9e25743" @@ -911,9 +977,12 @@ dependencies = [ "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa35e768d4daf1d85733418a49fb42e10d7f633e394fccab4ab7aba897053fe2" +"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9e0a7d8bed3178a8fb112199d466eeca9ed09a14ba8ad67718179b4fd5487d0b" "checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c66c0b9792f0a765345452775f3adbd28dde9d33f30d13e5dcc5ae17cf6f3780" "checksum web-view 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "92f76092d2f039a4989ea946e195366dae806bdeb9be01779af2b96c3cffb90d" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/Cargo.toml b/Cargo.toml index e4d7859..8218674 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ toml = "0.4" nfd = "0.0.4" semver = "0.9.0" +regex = "0.2" [build-dependencies] includedir_codegen = "0.2.0" diff --git a/src/config.rs b/src/config.rs index 740429b..e0f855d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,15 +7,20 @@ use toml::de::Error as TomlError; use serde_json::{self, Error as SerdeError}; +use sources::types::Release; +use sources::get_by_name; + /// Description of the source of a package. -#[derive(Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct PackageSource { pub name : String, + #[serde(rename="match")] + pub match_regex : String, pub config : toml::Value } /// Describes a overview of a individual package. -#[derive(Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct PackageDescription { pub name: String, pub description: String, @@ -24,13 +29,13 @@ pub struct PackageDescription { } /// Describes the application itself. -#[derive(Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct GeneralConfig { pub name: String, pub installing_message: String, } -#[derive(Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct Config { pub general: GeneralConfig, pub packages: Vec, @@ -47,3 +52,15 @@ impl Config { toml::from_str(contents) } } + +impl PackageSource { + /// Fetches releases for a given package + pub fn get_current_releases(&self) -> Result, String> { + let package_handler = match get_by_name(&self.name) { + Some(v) => v, + _ => return Err(format!("Handler {} not found", self.name)) + }; + + package_handler.get_current_releases(&self.config) + } +} \ No newline at end of file diff --git a/src/installer.rs b/src/installer.rs index baac945..2c99a16 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -2,8 +2,11 @@ /// /// Contains the main installer structure, as well as high-level means of controlling it. +use regex::Regex; + use std::env::home_dir; use std::env::var; +use std::env::consts::OS; use std::path::PathBuf; @@ -37,7 +40,38 @@ impl InstallerFramework { /// Sends a request for something to be installed. pub fn install(&self, items : Vec) { + // TODO: Error handling println!("Framework: Installing {:?}", items); + + // 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(); + + println!("Got releases"); + + let filtered_regex = package.source.match_regex.replace("#PLATFORM#", OS); + println!("Filtered regex: {}" , filtered_regex); + 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)).count() > 0) + .max_by_key(|f| f.version.clone()); + println!("{:?}", latest_result); + } } /// Creates a new instance of the Installer Framework with a specified Config. diff --git a/src/main.rs b/src/main.rs index 5193970..cc85323 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ extern crate serde_json; extern crate toml; extern crate semver; +extern crate regex; mod assets; mod rest; diff --git a/src/sources/github/mod.rs b/src/sources/github/mod.rs index 6e5340a..817b7e9 100644 --- a/src/sources/github/mod.rs +++ b/src/sources/github/mod.rs @@ -27,6 +27,12 @@ struct GithubConfig { repo : String } +impl GithubReleases { + pub fn new() -> Self { + GithubReleases {} + } +} + impl ReleaseSource for GithubReleases { fn get_current_releases(&self, config: &TomlValue) -> Result, String> { // Reparse our Config as strongly typed diff --git a/src/sources/mod.rs b/src/sources/mod.rs index 21d1fb6..1c41433 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -4,4 +4,16 @@ pub mod types; -pub mod github; \ No newline at end of file +pub mod github; + +use self::types::ReleaseSource; + +/// Returns a ReleaseSource by a name, if possible +pub fn get_by_name(name : &str) -> Option> { + match name { + "github" => { + Some(Box::new(github::GithubReleases::new())) + } + _ => None + } +} diff --git a/src/sources/types.rs b/src/sources/types.rs index b7ca075..aac5cbb 100644 --- a/src/sources/types.rs +++ b/src/sources/types.rs @@ -9,7 +9,7 @@ pub use semver::Version as SemverVersion; pub use toml::value::Value as TomlValue; /// The version of an application. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub enum Version { Semver(SemverVersion), Integer(u64)