Skip to content

Commit

Permalink
Improves detection of build output folders per project (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Apr 28, 2024
1 parent 5d2373b commit ddf7d66
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
3 changes: 2 additions & 1 deletion e2e/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ FROM azul/zulu-openjdk:21-latest

RUN apt-get update && apt-get install -y git openssh-client bats
RUN cd $HOME && mkdir -p $HOME/.ssh
RUN mkdir $HOME/IdeaProjects
RUN ssh-keyscan -t rsa github.com >> $HOME/.ssh/known_hosts
RUN git clone https://github.com/dotanuki-labs/android-archives-watchdog.git $HOME/aaw
RUN git clone https://github.com/dotanuki-labs/android-archives-watchdog.git $HOME/IdeaProjects/aaw

COPY e2e/disk.bats /usr/disk.bats
COPY e2e/ram.bats /usr/ram.bats
Expand Down
14 changes: 7 additions & 7 deletions e2e/disk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}

@test "should detect usages of disk" {
run $HOME/aaw/gradlew tasks -q -p $HOME/aaw
run $HOME/IdeaProjects/aaw/gradlew tasks -q -p $HOME/IdeaProjects/aaw
run gradle-wiper disk evaluate

[[ "$output" == *"Total resources (disk space)"* ]]
[ "$status" -eq 0 ]
}

@test "should perform disk shallow wiping" {
run $HOME/aaw/gradlew shadowJar -q -p $HOME/aaw
run $HOME/IdeaProjects/aaw/gradlew shadowJar -q -p $HOME/IdeaProjects/aaw
run gradle-wiper disk shallow

echo "$output"
Expand All @@ -30,14 +30,14 @@
[ ! -d $HOME/.gradle/daemon ]
[ ! -d $HOME/.gradle/.tmp ]
[ ! -d $HOME/.gradle/.m2 ]
[ ! -d $HOME/aaw/build ]
[ ! -d $HOME/IdeaProjects/aaw/build ]

# Do not clean Gradle metadata on shallow wiping
[ -d $HOME/aaw/.gradle ]
[ -d $HOME/IdeaProjects/aaw/.gradle ]
}

@test "should perform disk deep wiping" {
run $HOME/aaw/gradlew shadowJar -q -p $HOME/aaw
run $HOME/IdeaProjects/gradlew shadowJar -q -p $HOME/IdeaProjects/aaw
run gradle-wiper disk deep

[[ "$output" == *"Reclaimed disk space"* ]]
Expand All @@ -52,6 +52,6 @@
[ ! -d $HOME/.gradle/native ]
[ ! -d $HOME/.gradle/build-scan-data ]
[ ! -d $HOME/.m2 ]
[ ! -d $HOME/aaw/build ]
[ ! -d $HOME/aaw/.gradle ]
[ ! -d $HOME/IdeaProjects/aaw/build ]
[ ! -d $HOME/IdeaProjects/aaw/.gradle ]
}
6 changes: 3 additions & 3 deletions e2e/ram.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}

@test "should detect usages of ram" {
run $HOME/aaw/gradlew tasks -q -p $HOME/aaw
run $HOME/IdeaProjects/aaw/gradlew tasks -q -p $HOME/IdeaProjects//aaw
run gradle-wiper ram evaluate

[[ "$output" == *"Total resources (RAM memory)"* ]]
[ "$status" -eq 0 ]
}

@test "should perform ram shallow wiping" {
run $HOME/aaw/gradlew shadowJar -q -p $HOME/aaw
run $HOME/IdeaProjects/aaw/gradlew shadowJar -q -p $HOME/IdeaProjects/aaw
run gradle-wiper ram shallow

[[ "$output" == *"Reclaimed RAM memory"* ]]
Expand All @@ -30,7 +30,7 @@
}

@test "should perform ram deep wiping" {
run $HOME/aaw/gradlew tasks -q -p $HOME/aaw
run $HOME/IdeaProjects/aaw/gradlew tasks -q -p $HOME/IdeaProjects/aaw
run gradle-wiper ram deep

[[ "$output" == *"Reclaimed RAM memory"* ]]
Expand Down
48 changes: 37 additions & 11 deletions src/core/disk/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,26 @@ pub fn find_all_gradle_projects(user_home: PathBuf) -> Vec<PathBuf> {
WalkDir::new(user_home)
.into_iter()
.filter_map(|entry| entry.ok())
.filter(standard_project_locations)
.filter(ensure_gradle_project)
.map(|entry| entry.into_path())
.collect::<Vec<_>>()
}

pub fn find_associated_filepaths(user_home: PathBuf, cached: DiskCached) -> Vec<PathBuf> {
pub fn find_associated_filepaths(user_home: &Path, cached: DiskCached) -> Vec<PathBuf> {
match cached {
DiskCached::Standalone(project_level) => {
let gradle_projects = find_all_gradle_projects(user_home);
let gradle_projects = find_all_gradle_projects(user_home.to_path_buf());

if gradle_projects.is_empty() {
return gradle_projects;
};

let target = match project_level {
ProjectLevelDiskCache::BuildOutput => "build",
ProjectLevelDiskCache::GradleMetadata => ".gradle",
ProjectLevelDiskCache::IdeaMetadata => ".idea",
};

gradle_projects.iter().map(|path| path.join(target)).collect::<Vec<_>>()
match project_level {
ProjectLevelDiskCache::BuildOutput => all_build_output_folders(&gradle_projects),
ProjectLevelDiskCache::GradleMetadata => all_metadata_files(&gradle_projects, ".gradle"),
ProjectLevelDiskCache::IdeaMetadata => all_metadata_files(&gradle_projects, ".idea"),
}
},
DiskCached::Shared(user_level) => match user_level.path_relative_to_user_home() {
None => vec![],
Expand All @@ -57,6 +56,33 @@ pub fn find_associated_filepaths(user_home: PathBuf, cached: DiskCached) -> Vec<
}
}

fn standard_project_locations(entry: &DirEntry) -> bool {
let entry_path = entry.path();
let entry_path_raw = entry_path.to_str().expect("Not a valid path");

entry_path_raw.contains("AndroidStudioProjects")
|| entry_path_raw.contains("IdeaProjects")
|| entry_path_raw.contains("Projects")
|| entry_path_raw.contains("Dev")
}

fn all_metadata_files(projects: &[PathBuf], target: &str) -> Vec<PathBuf> {
projects.iter().map(|path| path.join(target)).collect::<Vec<_>>()
}

fn all_build_output_folders(projects: &[PathBuf]) -> Vec<PathBuf> {
projects.iter().flat_map(find_build_output_dirs).collect::<Vec<_>>()
}

fn find_build_output_dirs(project: &PathBuf) -> Vec<PathBuf> {
WalkDir::new(project)
.into_iter()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().ends_with("build"))
.map(|entry| entry.into_path())
.collect::<Vec<_>>()
}

fn ensure_gradle_project(entry: &DirEntry) -> bool {
let project_root = entry.path();

Expand Down Expand Up @@ -133,8 +159,8 @@ mod tests {
fs::write(&temp_dir.path().join(file), "foo").expect("Cant create fixture file");
}

let fake_user_home = temp_dir.path().to_path_buf();
let projects = find_all_gradle_projects(fake_user_home.clone());
let fake_user_home = temp_dir.path();
let projects = find_all_gradle_projects(fake_user_home.to_path_buf());

let expected = ["IdeaProjects/jvm-app", "AndroidStudioProjects/android-app"]
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/core/wiper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn evaluate_disk_space() -> anyhow::Result<ExecutionOutcome> {
debug!("Storage taken by Konan : {}", total_size_for_konan_caches);
}

let gradle_projects = disk::find_all_gradle_projects(user_home);
let gradle_projects = disk::find_all_gradle_projects(user_home.to_path_buf());
let gradle_projects_resources = disk::resources_used_by_gradle_projects(&gradle_projects)?;
let total_size_for_gradle_projects = gradle_projects_resources.amount;

Expand Down Expand Up @@ -155,7 +155,7 @@ fn wipe_disk(caches_to_remove: Vec<DiskCached>) -> anyhow::Result<ExecutionOutco

let paths_to_remove = caches_to_remove
.into_iter()
.flat_map(|item| disk::find_associated_filepaths(disk::user_home_locator(), item))
.flat_map(|item| disk::find_associated_filepaths(disk::user_home_locator().as_path(), item))
.collect::<Vec<PathBuf>>();

disk::cleanup_resources(&paths_to_remove);
Expand Down

0 comments on commit ddf7d66

Please sign in to comment.