diff --git a/e2e/Dockerfile b/e2e/Dockerfile index ad30daf..d31a2bb 100644 --- a/e2e/Dockerfile +++ b/e2e/Dockerfile @@ -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 diff --git a/e2e/disk.bats b/e2e/disk.bats index 09dd99f..4326636 100644 --- a/e2e/disk.bats +++ b/e2e/disk.bats @@ -10,7 +10,7 @@ } @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)"* ]] @@ -18,7 +18,7 @@ } @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" @@ -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"* ]] @@ -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 ] } diff --git a/e2e/ram.bats b/e2e/ram.bats index 589ec56..cf3e9b4 100644 --- a/e2e/ram.bats +++ b/e2e/ram.bats @@ -10,7 +10,7 @@ } @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)"* ]] @@ -18,7 +18,7 @@ } @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"* ]] @@ -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"* ]] diff --git a/src/core/disk/locations.rs b/src/core/disk/locations.rs index 70d92db..740bf91 100644 --- a/src/core/disk/locations.rs +++ b/src/core/disk/locations.rs @@ -25,27 +25,26 @@ pub fn find_all_gradle_projects(user_home: PathBuf) -> Vec { 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::>() } -pub fn find_associated_filepaths(user_home: PathBuf, cached: DiskCached) -> Vec { +pub fn find_associated_filepaths(user_home: &Path, cached: DiskCached) -> Vec { 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::>() + 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![], @@ -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 { + projects.iter().map(|path| path.join(target)).collect::>() +} + +fn all_build_output_folders(projects: &[PathBuf]) -> Vec { + projects.iter().flat_map(find_build_output_dirs).collect::>() +} + +fn find_build_output_dirs(project: &PathBuf) -> Vec { + WalkDir::new(project) + .into_iter() + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.path().ends_with("build")) + .map(|entry| entry.into_path()) + .collect::>() +} + fn ensure_gradle_project(entry: &DirEntry) -> bool { let project_root = entry.path(); @@ -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() diff --git a/src/core/wiper.rs b/src/core/wiper.rs index 7475fc5..f2532dd 100644 --- a/src/core/wiper.rs +++ b/src/core/wiper.rs @@ -96,7 +96,7 @@ fn evaluate_disk_space() -> anyhow::Result { 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; @@ -155,7 +155,7 @@ fn wipe_disk(caches_to_remove: Vec) -> anyhow::Result>(); disk::cleanup_resources(&paths_to_remove);