From 55239ba7c095acc2b4930a2e0dbe0c154bab49c9 Mon Sep 17 00:00:00 2001 From: bog Date: Fri, 8 Mar 2024 12:21:56 +0100 Subject: [PATCH] :sparkles: build environment (create build_dir/checksum.txt). --- Cargo.lock | 238 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/app.rs | 4 + src/core/build_env.rs | 131 +++++++++++++++++++++++ src/core/mod.rs | 1 + src/main.rs | 1 - 6 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 src/core/build_env.rs diff --git a/Cargo.lock b/Cargo.lock index 2071b76..66a9062 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "1.1.2" @@ -16,14 +31,167 @@ name = "alchimake" version = "0.1.0" dependencies = [ "regex", + "sha256", ] +[[package]] +name = "async-trait" +version = "0.1.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + +[[package]] +name = "cc" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + [[package]] name = "memchr" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "miniz_oxide" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +dependencies = [ + "adler", +] + +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + [[package]] name = "regex" version = "1.10.3" @@ -52,3 +220,73 @@ name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha256" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" +dependencies = [ + "async-trait", + "bytes", + "hex", + "sha2", + "tokio", +] + +[[package]] +name = "syn" +version = "2.0.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +dependencies = [ + "backtrace", + "bytes", + "pin-project-lite", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml index df1c848..dc0732b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] regex = "1.10.3" +sha256 = "1.5.0" diff --git a/src/app.rs b/src/app.rs index e562e8e..4ed337a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use crate::cli::parse; use crate::core::conf::Conf; +use crate::core::build_env::BuildEnv; pub struct App { conf: Conf @@ -50,10 +51,13 @@ impl App { })); args.parse(&sys_args.iter().map(|v| v.as_str()).collect())?; + Ok(()) } pub fn start(&mut self) -> Result<(), Box> { + let mut benv = BuildEnv::new(&self.conf); + benv.init_build_dir()?; Ok(()) } } diff --git a/src/core/build_env.rs b/src/core/build_env.rs new file mode 100644 index 0000000..c469f65 --- /dev/null +++ b/src/core/build_env.rs @@ -0,0 +1,131 @@ +use std::error::Error; +use std::io::Read; +use std::path::{Path, PathBuf}; +use crate::core::conf::Conf; + +#[derive(Debug)] +struct FileInfo { + pub path: PathBuf, + pub sha: String +} + +pub struct BuildEnv { + conf: Conf, + files: Vec, + checksum_file_path: PathBuf +} + +impl BuildEnv { + pub fn new(conf: &Conf) -> Self { + Self { + conf: conf.clone(), + files: vec![], + checksum_file_path: conf.build_dir.join("checksum.txt") + } + } + + pub fn init_build_dir(&mut self) -> Result<(), Box>{ + if !self.conf.build_dir.as_path().exists() { + std::fs::create_dir(&self.conf.build_dir)?; + } + + if !self.checksum_file_path.as_path().exists() { + std::fs::write(&self.checksum_file_path, b"")?; + } + + self.load_checksum()?; + + Ok(()) + } + + pub fn is_up_to_date(&mut self, file_path: &Path) -> Result> { + let mut f = std::fs::File::open(file_path)?; + let mut content = String::from(""); + f.read_to_string(&mut content)?; + let sha: String = sha256::digest(content); + + if let Some(idx) = self.files.iter().position(|x| { + x.path.canonicalize().unwrap() + == file_path.canonicalize().unwrap() + }) { + return Ok(self.files.get(idx).unwrap().sha == sha); + } + + Ok(false) + } + + pub fn update(&mut self, file_path: &Path) -> Result <(), Box>{ + let mut f = std::fs::File::open(file_path)?; + let mut content = String::from(""); + f.read_to_string(&mut content)?; + let sha = sha256::digest(content); + + if let Some(idx) = self.files.iter().position(|x| { + x.path.canonicalize().unwrap() + == file_path.canonicalize().unwrap() + }) { + self.files.get_mut(idx).unwrap().sha = sha; + } else { + self.files.push(FileInfo { + path: PathBuf::from(file_path), + sha + }); + } + + self.save_checksum()?; + + Ok(()) + } + + pub fn load_checksum(&mut self) -> Result<(), Box>{ + let mut file = std::fs::File::open( + self.checksum_file_path.as_path() + )?; + + let mut content = String::from(""); + file.read_to_string(&mut content)?; + + self.files.clear(); + + for line in content.split('\n') { + if line.len() == 0 { + continue; + } + + let info: Vec = line.split(' ').map( + |s| s.to_string() + ).collect(); + + let f = FileInfo { + path: PathBuf::from(info.get(0).unwrap()), + sha: info.get(1).unwrap().to_owned() + }; + + self.files.push(f); + } + + Ok(()) + } + + pub fn save_checksum(&self) -> Result<(), Box> { + let mut content = String::from(""); + + for file in self.files.iter() { + content += format!("{} {}", + file.path.canonicalize()?.to_str().unwrap(), + file.sha + ).as_str(); + } + + std::fs::write( + self.checksum_file_path.as_path(), + content.as_bytes() + )?; + + Ok(()) + } +} + +#[cfg(test)] +mod test { +} diff --git a/src/core/mod.rs b/src/core/mod.rs index 1644e51..7668147 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -6,3 +6,4 @@ pub mod sym; pub mod cmd; pub mod exec; pub mod conf; +pub mod build_env; diff --git a/src/main.rs b/src/main.rs index f753c55..d684ae8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ mod cli; mod core; mod app; -use crate::cli::parse; use crate::app::App; fn main() -> Result<(), Box> {