Skip to content

Commit

Permalink
Refactors cleaning logic (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Apr 18, 2024
1 parent 3235c2b commit 35c09a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ mod operations;
mod resources;

pub use locations::find_all_gradle_projects;
pub use locations::find_associated_filepaths;
pub use locations::find_gradle_home;
pub use locations::find_maven_local_repository;
pub use operations::cleanup;
pub use operations::cleanup_resources;
pub use resources::resources_used_by_gradle_home;
pub use resources::resources_used_by_gradle_projects;
pub use resources::resources_used_by_maven_local_repository;
30 changes: 18 additions & 12 deletions src/disk/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: MIT

use crate::models::DiskCached;
use anyhow::anyhow;
use directories::BaseDirs;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};
Expand All @@ -14,20 +13,20 @@ pub fn find_gradle_home() -> anyhow::Result<PathBuf> {
return Ok(PathBuf::from(custom_gradle_home));
}

let base_dir = BaseDirs::new().ok_or(anyhow!("Cannot access base directories"))?;
let home_dir = base_dir.home_dir();
let base_dirs = BaseDirs::new().expect("Cannot access base directories");
let home_dir = base_dirs.home_dir();
Ok(home_dir.join(".gradle"))
}

pub fn find_maven_local_repository() -> anyhow::Result<PathBuf> {
let base_dir = BaseDirs::new().ok_or(anyhow!("Cannot access base directories"))?;
let home_dir = base_dir.home_dir();
let base_dirs = BaseDirs::new().expect("Cannot access base directories");
let home_dir = base_dirs.home_dir();
Ok(home_dir.join(".m2"))
}

pub fn find_all_gradle_projects() -> anyhow::Result<Vec<PathBuf>> {
let base_dir = BaseDirs::new().ok_or(anyhow!("Cannot access base directories"))?;
let home_dir = base_dir.home_dir();
let base_dirs = BaseDirs::new().expect("Cannot access base directories");
let home_dir = base_dirs.home_dir();

let android_studio_projects_folder = home_dir.join("AndroidStudioProjects");
let android_projects = find_gradle_projects(android_studio_projects_folder.as_path())?;
Expand All @@ -42,10 +41,7 @@ pub fn find_all_gradle_projects() -> anyhow::Result<Vec<PathBuf>> {
Ok(all_projects)
}

pub(crate) fn find_paths(cached: &DiskCached) -> Vec<PathBuf> {
let base_dir = BaseDirs::new().expect("Cannot access base directories");
let home_dir = base_dir.home_dir();

pub fn find_associated_filepaths(cached: DiskCached) -> Vec<PathBuf> {
match cached {
DiskCached::Standalone(_project_level) => {
let gradle_projects = find_all_gradle_projects().unwrap_or_default();
Expand All @@ -71,7 +67,17 @@ pub(crate) fn find_paths(cached: &DiskCached) -> Vec<PathBuf> {
},
DiskCached::Shared(user_level) => match user_level.path_relative_to_user_home() {
None => vec![],
Some(path) => vec![home_dir.join(path)],
Some(path) => {
if let Ok(custom_gradle_home) = std::env::var("GRADLE_USER_HOME") {
if path.to_string_lossy().contains(".gradle") {
return vec![PathBuf::from(custom_gradle_home).join(path)];
}
}

let base_dirs = BaseDirs::new().expect("Cannot access base directories");
let home_dir = base_dirs.home_dir();
vec![home_dir.join(path)]
},
},
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/disk/operations.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// Copyright 2024 Dotanuki Labs
// SPDX-License-Identifier: MIT

use crate::models::DiskCached;
use std::fs;
use std::path::PathBuf;

pub fn cleanup(resources: &[DiskCached]) {
let paths_to_remove = resources
.iter()
.flat_map(crate::disk::locations::find_paths)
.collect::<Vec<_>>();

pub fn cleanup_resources(paths_to_remove: &[PathBuf]) {
let errors = paths_to_remove
.iter()
.map(fs::remove_dir_all)
Expand Down
8 changes: 7 additions & 1 deletion src/wiper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::models::{
AllocatedResource, DiskCached, EvaluationOutcome, ExecutionOutcome, MachineResource, UserLevelDiskCache,
WipeAction, WipingOutcome,
};
use std::path::PathBuf;
use ubyte::{ByteUnit, ToByteUnit};
use MachineResource::{DiskSpace, RamMemory};
use WipeAction::{DeepWipe, Evaluate, ShallowWipe};
Expand Down Expand Up @@ -73,7 +74,12 @@ fn shallow_wipe_disk() -> anyhow::Result<ExecutionOutcome> {
DiskCached::Shared(UserLevelDiskCache::MavenLocalRepository),
];

disk::cleanup(&caches_to_remove);
let paths_to_remove = caches_to_remove
.into_iter()
.flat_map(disk::find_associated_filepaths)
.collect::<Vec<PathBuf>>();

disk::cleanup_resources(&paths_to_remove);

let after_cleaning = match evaluate_disk_space()? {
ExecutionOutcome::Evaluation(outcome) => outcome.total_size,
Expand Down

0 comments on commit 35c09a8

Please sign in to comment.