Skip to content

Commit

Permalink
new: Auto-clean plugins after an install. (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Sep 22, 2023
1 parent 0865b34 commit c50c621
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
22 changes: 7 additions & 15 deletions crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<usize> {
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);
Expand Down Expand Up @@ -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<usize> {
pub async fn clean_plugins(days: u64) -> miette::Result<usize> {
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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::clean::clean_plugins;
use crate::helpers::{create_progress_bar, disable_progress_bars};
use crate::shell;
use clap::Args;
Expand Down Expand Up @@ -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(())
}

Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions crates/pdk-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
65 changes: 51 additions & 14 deletions plugins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c50c621

Please sign in to comment.