From c50c62120423312f4c18f0fb493180deb72c6cc1 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 22 Sep 2023 15:03:52 -0700 Subject: [PATCH] new: Auto-clean plugins after an install. (#215) --- CHANGELOG.md | 6 +++ Cargo.lock | 6 +-- Cargo.toml | 2 +- crates/cli/src/commands/clean.rs | 22 ++++------ crates/cli/src/commands/install.rs | 6 +++ crates/core/src/tool.rs | 4 +- crates/pdk-test-utils/src/lib.rs | 21 ++++++---- plugins/Cargo.lock | 65 +++++++++++++++++++++++------- 8 files changed, 90 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 935817202..b51c08460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ - [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md) - [Schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md) +## Unreleased + +#### 🚀 Updates + +- Updated `proto install` to auto-clean stale plugins after a successful installation. + ## 0.18.3 #### 🐞 Fixes diff --git a/Cargo.lock b/Cargo.lock index 45aa28932..c6bb3708b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3246,9 +3246,9 @@ dependencies = [ [[package]] name = "starbase_utils" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "032493983d1aaa0e6107219e8b17e2b315452f902f0f512c8bd5af6df6704b44" +checksum = "9e1665a9066fd0da6e608166a18dba7324734229b5554f6abff7b6a2761b5402" dependencies = [ "dirs 5.0.1", "fs4", @@ -3262,7 +3262,7 @@ dependencies = [ "starbase_styles", "thiserror", "tokio", - "toml 0.7.6", + "toml 0.8.0", "tracing", "wax", ] diff --git a/Cargo.toml b/Cargo.toml index 79f351d2e..69b63a062 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ starbase_archive = { version = "0.2.2", features = [ ] } starbase_sandbox = { version = "0.1.10" } starbase_styles = "0.1.15" -starbase_utils = { version = "0.3.1", default-features = false, features = [ +starbase_utils = { version = "0.3.2", default-features = false, features = [ "json", "toml", ] } diff --git a/crates/cli/src/commands/clean.rs b/crates/cli/src/commands/clean.rs index 3f08f6e6a..127badeb9 100644 --- a/crates/cli/src/commands/clean.rs +++ b/crates/cli/src/commands/clean.rs @@ -9,7 +9,7 @@ use starbase::{system, SystemResult}; use starbase_styles::color; use starbase_utils::fs; use std::collections::HashSet; -use std::time::SystemTime; +use std::time::{Duration, SystemTime}; use tracing::{debug, info}; #[derive(Args, Clone, Debug, Default)] @@ -46,7 +46,7 @@ fn is_older_than_days(now: u128, other: u128, days: u8) -> bool { pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miette::Result { info!("Checking {}", color::shell(tool.get_name())); - if !tool.get_inventory_dir().exists() { + if !tool.is_installed() { debug!("Not being used, skipping"); return Ok(0); @@ -155,31 +155,23 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett Ok(clean_count) } -pub async fn clean_plugins(now: u128, days: u8) -> miette::Result { +pub async fn clean_plugins(days: u64) -> miette::Result { + let duration = Duration::from_secs(86400 * days); let mut clean_count = 0; for file in fs::read_dir(get_plugins_dir()?)? { let path = file.path(); if path.is_file() { - let metadata = fs::metadata(&path)?; - let last_used = metadata - .accessed() - .or_else(|_| metadata.modified()) - .or_else(|_| metadata.created()) - .unwrap_or_else(|_| SystemTime::now()) - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_millis(); + let bytes = fs::remove_file_if_older_than(&path, duration)?; - if is_older_than_days(now, last_used, days) { + if bytes > 0 { debug!( "Plugin {} hasn't been used in over {} days, removing", color::path(&path), days ); - fs::remove_file(path)?; clean_count += 1; } } @@ -281,7 +273,7 @@ pub async fn internal_clean(args: &CleanArgs) -> SystemResult { info!("Finding installed plugins to clean up..."); - clean_count = clean_plugins(now, days).await?; + clean_count = clean_plugins(days as u64).await?; if clean_count > 0 { info!("Successfully cleaned up {} plugins", clean_count); diff --git a/crates/cli/src/commands/install.rs b/crates/cli/src/commands/install.rs index c78f475d4..d989167c4 100644 --- a/crates/cli/src/commands/install.rs +++ b/crates/cli/src/commands/install.rs @@ -1,3 +1,4 @@ +use super::clean::clean_plugins; use crate::helpers::{create_progress_bar, disable_progress_bars}; use crate::shell; use clap::Args; @@ -132,6 +133,11 @@ pub async fn internal_install(args: InstallArgs) -> SystemResult { // Sync shell profile update_shell(tool, args.passthrough.clone())?; + // Clean plugins + debug!("Auto-cleaning old plugins"); + + clean_plugins(7).await?; + Ok(()) } diff --git a/crates/core/src/tool.rs b/crates/core/src/tool.rs index 6f5f522bd..00b5c3d8e 100644 --- a/crates/core/src/tool.rs +++ b/crates/core/src/tool.rs @@ -1182,7 +1182,9 @@ impl Tool { // OPERATIONS impl Tool { - fn is_installed(&self) -> bool { + /// Return true if the tool has been installed. This is less accurate than `is_setup`, + /// as it only checks for the existence of the inventory directory. + pub fn is_installed(&self) -> bool { let dir = self.get_tool_dir(); self.version diff --git a/crates/pdk-test-utils/src/lib.rs b/crates/pdk-test-utils/src/lib.rs index 09d7272dc..073d6d0c2 100644 --- a/crates/pdk-test-utils/src/lib.rs +++ b/crates/pdk-test-utils/src/lib.rs @@ -65,16 +65,21 @@ pub fn create_plugin(id: &str, sandbox: &Path) -> WasmTestWrapper { internal_create_plugin(id, sandbox, HashMap::new()) } -#[cfg(feature = "schema")] +#[allow(unused_variables)] pub fn create_schema_plugin(id: &str, sandbox: &Path, schema: PathBuf) -> WasmTestWrapper { - let schema = fs::read_to_string(schema).unwrap(); - let schema: serde_json::Value = toml::from_str(&schema).unwrap(); - + #[allow(unused_mut)] let mut config = HashMap::new(); - config.insert( - "schema".to_string(), - serde_json::to_string(&schema).unwrap(), - ); + + #[cfg(feature = "schema")] + { + let schema = fs::read_to_string(schema).unwrap(); + let schema: serde_json::Value = toml::from_str(&schema).unwrap(); + + config.insert( + "schema".to_string(), + serde_json::to_string(&schema).unwrap(), + ); + } internal_create_plugin(id, sandbox, config) } diff --git a/plugins/Cargo.lock b/plugins/Cargo.lock index 5a6d42696..d8c4a4424 100644 --- a/plugins/Cargo.lock +++ b/plugins/Cargo.lock @@ -915,9 +915,9 @@ dependencies = [ [[package]] name = "extism" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0fcd6ad10f959f7f35680feda30bdcb7c1f6e0584e8358b09d735ff2c232f58" +checksum = "7a94848d5b49906bd97b83cf5a8bd25082dbc6f8bdfe98f12687910228734552" dependencies = [ "anyhow", "extism-manifest", @@ -965,9 +965,9 @@ dependencies = [ [[package]] name = "extism-runtime" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2be84fc8659ebaa268478a52f98e7066e4352fc7a90b74ec3aa52e7ac03eecd4" +checksum = "a3b0ba8ef6ecbf59c0f6e47fd2feea575ebc3a09e81603d06a41af92fe61cdfa" dependencies = [ "anyhow", "cbindgen", @@ -2044,7 +2044,7 @@ dependencies = [ [[package]] name = "proto_core" -version = "0.18.1" +version = "0.18.4" dependencies = [ "cached", "extism", @@ -2067,12 +2067,13 @@ dependencies = [ "tinytemplate", "tracing", "url", + "version_spec", "warpgate", ] [[package]] name = "proto_pdk" -version = "0.7.2" +version = "0.7.3" dependencies = [ "anyhow", "extism-pdk", @@ -2084,7 +2085,7 @@ dependencies = [ [[package]] name = "proto_pdk_api" -version = "0.7.1" +version = "0.7.2" dependencies = [ "anyhow", "semver", @@ -2096,7 +2097,7 @@ dependencies = [ [[package]] name = "proto_pdk_test_utils" -version = "0.7.0" +version = "0.7.2" dependencies = [ "extism", "proto_core", @@ -2107,7 +2108,7 @@ dependencies = [ [[package]] name = "proto_wasm_plugin" -version = "0.6.5" +version = "0.6.6" dependencies = [ "extism", "proto_pdk_api", @@ -2773,9 +2774,9 @@ dependencies = [ [[package]] name = "starbase_utils" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "032493983d1aaa0e6107219e8b17e2b315452f902f0f512c8bd5af6df6704b44" +checksum = "9e1665a9066fd0da6e608166a18dba7324734229b5554f6abff7b6a2761b5402" dependencies = [ "dirs 5.0.1", "fs4", @@ -2789,7 +2790,7 @@ dependencies = [ "starbase_styles", "thiserror", "tokio", - "toml 0.7.6", + "toml 0.8.0", "tracing", "wax", ] @@ -3037,7 +3038,19 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit", + "toml_edit 0.19.14", +] + +[[package]] +name = "toml" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c226a7bba6d859b63c92c4b4fe69c5b6b72d0cb897dbc8e6012298e6154cb56e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.20.0", ] [[package]] @@ -3062,6 +3075,19 @@ dependencies = [ "winnow", ] +[[package]] +name = "toml_edit" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ff63e60a958cefbb518ae1fd6566af80d9d4be430a33f3723dfc47d1d411d95" +dependencies = [ + "indexmap 2.0.0", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower-service" version = "0.3.2" @@ -3221,6 +3247,17 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "version_spec" +version = "0.1.0" +dependencies = [ + "human-sort", + "once_cell", + "regex", + "semver", + "serde", +] + [[package]] name = "wait-timeout" version = "0.2.0" @@ -3251,7 +3288,7 @@ dependencies = [ [[package]] name = "warpgate" -version = "0.5.6" +version = "0.5.7" dependencies = [ "extism", "miette",