rclean-cli 0.2.0

Find and safely clean rebuildable developer artifacts across local workspaces
use std::path::Path;

use super::ide_caches::is_dynamic_candidate_name as is_ide_dynamic_candidate_name;
use super::macos_system::is_dynamic_candidate_name as is_macos_system_dynamic_candidate_name;
use super::markers::{
    has_marker, has_prefixed_marker, is_dotnet_project, is_python_project, is_ruby_project,
    package_mentions,
};
use super::node_global::is_pnpm_store_version_name;
use super::user_tool_caches::is_dynamic_candidate_name as is_user_tool_dynamic_candidate_name;

pub fn is_candidate_name(name: &str) -> bool {
    matches!(
        name,
        "node_modules"
            | ".next"
            | ".turbo"
            | ".vite"
            | ".parcel-cache"
            | ".venv"
            | "venv"
            | "__pycache__"
            | ".pytest_cache"
            | ".mypy_cache"
            | ".ruff_cache"
            | ".tox"
            | "target"
            | "mod"
            | "vendor"
            | "Pods"
            | "coverage"
            | ".gradle"
            | ".dart_tool"
            | "bin"
            | "obj"
            | ".bundle"
            | "build"
            | "dist"
            | "out"
            | "DerivedData"
            | "cache"
            | "download"
            | "db"
            | "downloads"
            | ".downloadIntermediates"
            | "build-cache"
            | "hosted"
            | "git"
            | "go-build"
            | "store"
            | "_cacache"
            | "_npx"
            | "_logs"
            | "_prebuilds"
            | "Yarn"
            | "pip"
            | "hub"
            | "torch_compile_cache"
            | "whisper"
            | "llama.cpp"
            | "models"
            | "puppeteer"
            | "pre-commit"
            | "deno"
            | "uv"
            | "pypoetry"
            | "pipx"
            | "caches"
            | "repository"
            | "CoreSimulator"
            | "ms-playwright"
            | "Chrome"
            | "GoogleUpdater"
            | "LarkInternational"
            | "com.google.Chrome.code_sign_clone"
            | "videos"
            | "OptGuideOnDeviceModel"
            | "update"
            | "MapTiles"
            | "MediaCache"
            | "com.apple.mediaanalysisd"
            | "com.apple.idleassetsd"
            | "compact_index"
            | "logs"
            | "log"
            | "Cache"
            | "CachedData"
            | "Code Cache"
            | "GPUCache"
    ) || is_pnpm_store_version_name(name)
        || is_shipit_candidate_name(name)
        || is_ide_dynamic_candidate_name(name)
        || is_macos_system_dynamic_candidate_name(name)
        || is_user_tool_dynamic_candidate_name(name)
}

fn is_shipit_candidate_name(name: &str) -> bool {
    name.ends_with(".ShipIt") && name.len() > ".ShipIt".len()
}

/// Returns true for rule ids whose classifier intentionally targets
/// paths inside the user runtime/system tree (e.g. `~/Library/...`).
/// `apply_path_safety` skips the generic runtime-path block for these.
pub fn is_global_rule(rule_id: &str) -> bool {
    matches!(
        rule_id,
        "xcode.derived_data"
            | "cargo.registry_cache"
            | "cargo.git_db"
            | "homebrew.downloads"
            | "android_sdk.download_intermediates"
            | "android_sdk.legacy_build_cache"
            | "jetbrains.system_caches"
            | "jetbrains.logs"
            | "android_studio.system_caches"
            | "android_studio.logs"
            | "dart.pub_hosted_cache"
            | "dart.pub_git_cache"
            | "node.npm_cacache"
            | "node.yarn_cache"
            | "node.pnpm_store"
            | "ai.huggingface_hub"
            | "ai.torch_hub"
            | "ai.vllm_compile_cache"
            | "ai.whisper_models"
            | "ai.llama_cpp_cache"
            | "ai.ollama_models"
            | "browser.puppeteer"
            | "js.deno_cache"
            | "pip.cache"
            | "python.uv_cache"
            | "python.poetry_cache"
            | "python.pipx_cache"
            | "gradle.caches"
            | "maven.local_repo"
            | "xcode.simulators"
            | "go.module_download_cache"
            | "go.module_cache"
            | "go.build_cache"
            | "bun.cache"
            | "pre_commit.cache"
            | "playwright.browsers"
            | "app.shipit_caches"
            | "chrome.cache"
            | "chrome.google_updater"
            | "app.lark_cache"
            | "macos.chrome_code_sign_clone"
            | "macos.remem_dry_run_tmp"
            | "apple.wallpaper_aerial_videos"
            | "chrome.opt_guide_model"
            | "app.lark_update"
            | "macos.geod_map_tiles"
            | "macos.mediaanalysisd_cache"
            | "macos.mediaanalysisd_tmp"
            | "apple.idleassetsd"
            | "node.npm_transient"
            | "ruby.bundle_compact_index"
            | "cloud.kube_cache"
            | "cloud.gcloud_logs"
            | "editor.vscode_cache"
            | "editor.cursor_cache"
            | "editor.vscode_obsolete_extension"
            | "editor.cursor_obsolete_extension"
            | "claude.old_version"
            | "app.electron_cache"
    )
}

pub fn is_project_marker_name(name: &str) -> bool {
    matches!(
        name,
        "package.json"
            | "Cargo.toml"
            | "go.mod"
            | "Podfile"
            | "pyproject.toml"
            | "requirements.txt"
            | "setup.py"
            | "Pipfile"
            | "Gemfile"
            | "pom.xml"
            | "build.gradle"
            | "build.gradle.kts"
            | "pubspec.yaml"
            | "composer.json"
    )
}

pub fn detect_project_kind(dir: &Path) -> (String, Vec<String>) {
    let mut markers = Vec::new();

    for marker in [
        "package.json",
        "Cargo.toml",
        "go.mod",
        "Podfile",
        "pyproject.toml",
        "requirements.txt",
        "setup.py",
        "Pipfile",
        "Gemfile",
        "pom.xml",
        "build.gradle",
        "build.gradle.kts",
        "pubspec.yaml",
        "composer.json",
    ] {
        if has_marker(dir, marker) {
            markers.push(marker.to_string());
        }
    }

    if has_marker(dir, "package.json") {
        if has_prefixed_marker(dir, "next.config.") || package_mentions(dir, "next") {
            return ("Next.js".to_string(), markers);
        }
        if has_prefixed_marker(dir, "vite.config.") || package_mentions(dir, "vite") {
            return ("Vite".to_string(), markers);
        }
        return ("Node.js".to_string(), markers);
    }
    if has_marker(dir, "Cargo.toml") {
        return ("Rust".to_string(), markers);
    }
    if is_python_project(dir) {
        return ("Python".to_string(), markers);
    }
    if has_marker(dir, "go.mod") {
        return ("Go".to_string(), markers);
    }
    if has_marker(dir, "Podfile") {
        return ("iOS".to_string(), markers);
    }
    if is_dotnet_project(dir) {
        return (".NET".to_string(), markers);
    }
    if is_ruby_project(dir) {
        return ("Ruby".to_string(), markers);
    }
    if has_marker(dir, "pom.xml") {
        return ("Java (Maven)".to_string(), markers);
    }
    if has_marker(dir, "build.gradle") || has_marker(dir, "build.gradle.kts") {
        return ("Java (Gradle)".to_string(), markers);
    }
    if has_marker(dir, "pubspec.yaml") {
        return ("Flutter/Dart".to_string(), markers);
    }

    ("Unknown".to_string(), markers)
}

#[cfg(test)]
mod tests {
    use super::is_candidate_name;

    #[test]
    fn candidate_prefilter_includes_global_app_cache_names() {
        for name in [
            "ms-playwright",
            "com.microsoft.VSCode.ShipIt",
            "Chrome",
            "GoogleUpdater",
            "LarkInternational",
            "com.google.Chrome.code_sign_clone",
            "remem-dry-run-123",
            "downloads",
            ".downloadIntermediates",
            "build-cache",
            "hosted",
            "git",
            "videos",
            "OptGuideOnDeviceModel",
            "update",
            "MapTiles",
            "MediaCache",
            "com.apple.mediaanalysisd",
            "mod",
            "compact_index",
            "_npx",
            "_logs",
            "_prebuilds",
            "torch_compile_cache",
            "whisper",
            "llama.cpp",
            "Code Cache",
            "DawnGraphiteCache",
            "publisher.tool-1.2.0",
            "1.2.3",
        ] {
            assert!(is_candidate_name(name), "{name} should pass prefilter");
        }

        assert!(
            !is_candidate_name(".ShipIt"),
            "bare .ShipIt must not pass the dynamic prefilter"
        );
    }
}