2828fi
2929
3030# --- Global Variables ---
31- SCRIPT_VERSION=" 1.1 .0"
31+ SCRIPT_VERSION=" 1.2 .0"
3232GITHUB_REPO=" https://github.com/jemishavasoya/dev-cleaner"
3333
34+ # Check if FLUTTER_SEARCH_DIR is already set as environment variable
35+ if [ -z " ${FLUTTER_SEARCH_DIR} " ]; then
36+ FLUTTER_SEARCH_DIR=" ." # Default search directory for Flutter cleanup
37+ FLUTTER_DIR_SOURCE=" default"
38+ else
39+ # Expand ~ to home directory if present in environment variable
40+ FLUTTER_SEARCH_DIR=" ${FLUTTER_SEARCH_DIR/# \~ / $HOME } "
41+ FLUTTER_DIR_SOURCE=" environment"
42+
43+ # Validate environment variable directory
44+ if [ ! -d " $FLUTTER_SEARCH_DIR " ]; then
45+ echo -e " ${YELLOW} Warning: FLUTTER_SEARCH_DIR environment variable points to non-existent directory: ${FLUTTER_SEARCH_DIR}${NC} "
46+ echo -e " ${YELLOW} Falling back to current directory.${NC} "
47+ FLUTTER_SEARCH_DIR=" ."
48+ FLUTTER_DIR_SOURCE=" default"
49+ fi
50+ fi
51+
3452# Logo
3553print_logo () {
3654 echo -e " ${CYAN}${BOLD} "
@@ -129,6 +147,12 @@ cleanup_flutter() {
129147 if command -v flutter & > /dev/null; then
130148 print_item " ✓" " ${GREEN} " " Cleaning Flutter projects recursively from: $search_dir "
131149
150+ # Validate that the directory exists
151+ if [ ! -d " $search_dir " ]; then
152+ print_item " ✕" " ${RED} " " Directory not found: $search_dir "
153+ return 1
154+ fi
155+
132156 # Find all pubspec.yaml files recursively and clean each project
133157 local cleaned_count=0
134158 while IFS= read -r -d ' ' pubspec; do
@@ -179,7 +203,7 @@ cleanup_flutter() {
179203 if [ $cleaned_count -gt 0 ]; then
180204 print_item " ✓" " ${GREEN} " " Cleaned $cleaned_count Flutter project(s)"
181205 else
182- print_item " ℹ️" " ${YELLOW} " " No Flutter projects found to clean"
206+ print_item " ℹ️" " ${YELLOW} " " No Flutter projects found to clean in: $search_dir "
183207 fi
184208
185209 print_item " ✓" " ${GREEN} " " Cleaning Flutter global cache..."
@@ -397,7 +421,7 @@ display_menu() {
397421 echo -e " ${GREEN} 1.${NC} Clear All Caches"
398422 echo -e " ${GREEN} 2.${NC} Clear Xcode Caches & DerivedData"
399423 echo -e " ${GREEN} 3.${NC} Clear Android/Gradle Caches"
400- echo -e " ${GREEN} 4.${NC} Clear Flutter Caches"
424+ echo -e " ${GREEN} 4.${NC} Clear Flutter Caches ${FAINT} (with custom directory option) ${NC} "
401425 echo -e " ${GREEN} 5.${NC} Clear npm/Yarn/pnpm Caches"
402426 echo -e " ${GREEN} 6.${NC} Clean Homebrew Caches"
403427 echo -e " ${GREEN} 7.${NC} Clear CocoaPods Caches"
@@ -412,6 +436,33 @@ display_menu() {
412436 echo -e " → Please enter your choice (0-14): ${NC} \c"
413437}
414438
439+ # --- Help function ---
440+ show_help () {
441+ cat << EOF
442+ Dev Cleanup Utility v${SCRIPT_VERSION}
443+ A powerful cleanup utility for development environments on macOS
444+
445+ Usage: $0 [OPTIONS]
446+
447+ Options:
448+ -h, --help Show this help message
449+ -v, --version Show version information
450+ --flutter-dir PATH Set custom directory for Flutter cleanup (default: current directory)
451+ Example: $0 --flutter-dir ~/Projects
452+
453+ Command-line Flutter cleanup:
454+ You can specify a custom directory for Flutter cleanup using the --flutter-dir option.
455+ This directory will be used when running the interactive menu or the "Clear All" option.
456+
457+ Examples:
458+ $0 # Run interactive menu (searches current directory for Flutter projects)
459+ $0 --flutter-dir ~/Development # Run with custom Flutter search directory
460+ $0 --flutter-dir ~/Projects/Flutter # Search only in specific Flutter projects folder
461+
462+ Repository: ${GITHUB_REPO}
463+ EOF
464+ }
465+
415466# --- Main Logic ---
416467main_loop () {
417468 # Request sudo at the start to cover all options that need it
@@ -439,7 +490,7 @@ main_loop() {
439490 cleanup_xcode
440491 cleanup_android
441492 cleanup_android_sdk
442- cleanup_flutter
493+ cleanup_flutter " $FLUTTER_SEARCH_DIR "
443494 cleanup_platformIO
444495 cleanup_npm_yarn
445496 cleanup_homebrew
@@ -460,7 +511,48 @@ main_loop() {
460511 ;;
461512 4)
462513 print_section_header " Performing Flutter Cleanup"
463- cleanup_flutter
514+ echo -e " ${CYAN} Current Flutter search directory: ${FLUTTER_SEARCH_DIR}${NC} "
515+ case " $FLUTTER_DIR_SOURCE " in
516+ " environment" )
517+ echo -e " ${FAINT} (from FLUTTER_SEARCH_DIR environment variable)${NC} "
518+ ;;
519+ " command-line" )
520+ echo -e " ${FAINT} (from --flutter-dir command-line argument)${NC} "
521+ ;;
522+ " default" )
523+ echo -e " ${FAINT} (default: current directory)${NC} "
524+ ;;
525+ esac
526+ echo " "
527+ echo -e " ${YELLOW} Enter a custom directory path, or press Enter to use current setting:${NC} "
528+ read -r custom_flutter_dir
529+
530+ # Use custom directory if provided, otherwise use global setting
531+ if [ -n " $custom_flutter_dir " ]; then
532+ # Expand ~ to home directory
533+ custom_flutter_dir=" ${custom_flutter_dir/# \~ / $HOME } "
534+
535+ if [ -d " $custom_flutter_dir " ]; then
536+ echo -e " ${CYAN} Using interactive override: ${custom_flutter_dir}${NC} "
537+ cleanup_flutter " $custom_flutter_dir "
538+ else
539+ print_item " ✕" " ${RED} " " Directory does not exist: $custom_flutter_dir "
540+ case " $FLUTTER_DIR_SOURCE " in
541+ " environment" )
542+ echo -e " ${YELLOW} Falling back to environment variable setting: ${FLUTTER_SEARCH_DIR}${NC} "
543+ ;;
544+ " command-line" )
545+ echo -e " ${YELLOW} Falling back to command-line argument: ${FLUTTER_SEARCH_DIR}${NC} "
546+ ;;
547+ " default" )
548+ echo -e " ${YELLOW} Falling back to default directory: ${FLUTTER_SEARCH_DIR}${NC} "
549+ ;;
550+ esac
551+ cleanup_flutter " $FLUTTER_SEARCH_DIR "
552+ fi
553+ else
554+ cleanup_flutter " $FLUTTER_SEARCH_DIR "
555+ fi
464556 ;;
465557 5)
466558 print_section_header " Performing npm/Yarn/pnpm Cleanup"
@@ -523,11 +615,43 @@ main_loop() {
523615}
524616
525617# --- Handle command line arguments ---
526- if [[ " $1 " == " --version" || " $1 " == " -v" ]]; then
527- echo " Dev Cleaner v${SCRIPT_VERSION} "
528- echo " A powerful cleanup utility for development environments"
529- echo " Repository: ${GITHUB_REPO} "
530- exit 0
618+ while [[ $# -gt 0 ]]; do
619+ case $1 in
620+ -h|--help)
621+ show_help
622+ exit 0
623+ ;;
624+ -v|--version)
625+ echo " Dev Cleaner v${SCRIPT_VERSION} "
626+ echo " A powerful cleanup utility for development environments"
627+ echo " Repository: ${GITHUB_REPO} "
628+ exit 0
629+ ;;
630+ --flutter-dir)
631+ if [ -n " $2 " ] && [ " ${2: 0: 1} " != " -" ]; then
632+ # Expand ~ to home directory
633+ FLUTTER_SEARCH_DIR=" ${2/# \~ / $HOME } "
634+ FLUTTER_DIR_SOURCE=" command-line"
635+ shift 2
636+ else
637+ echo -e " ${RED} Error: --flutter-dir requires a directory path${NC} "
638+ echo " Use -h or --help for usage information"
639+ exit 1
640+ fi
641+ ;;
642+ * )
643+ echo -e " ${RED} Error: Unknown option: $1 ${NC} "
644+ echo " Use -h or --help for usage information"
645+ exit 1
646+ ;;
647+ esac
648+ done
649+
650+ # Validate Flutter search directory if custom one was provided
651+ if [ " $FLUTTER_SEARCH_DIR " != " ." ] && [ ! -d " $FLUTTER_SEARCH_DIR " ]; then
652+ echo -e " ${RED} Error: Flutter search directory does not exist: ${FLUTTER_SEARCH_DIR}${NC} "
653+ echo " Please provide a valid directory path."
654+ exit 1
531655fi
532656
533657# --- Initial check for user confirmation before starting the interactive menu ---
@@ -536,6 +660,24 @@ echo -e "${RED}--- 🚀 Dev Cleanup Utility ---${NC}"
536660echo " This script will permanently delete cache files from your system."
537661echo " Review the options carefully before proceeding."
538662echo " "
663+
664+ # Report Flutter search directory and its source
665+ if [ " $FLUTTER_SEARCH_DIR " != " ." ]; then
666+ echo -e " ${CYAN} Flutter search directory: ${FLUTTER_SEARCH_DIR}${NC} "
667+ case " $FLUTTER_DIR_SOURCE " in
668+ " environment" )
669+ echo -e " ${FAINT} (set via FLUTTER_SEARCH_DIR environment variable)${NC} "
670+ ;;
671+ " command-line" )
672+ echo -e " ${FAINT} (set via --flutter-dir command-line argument)${NC} "
673+ ;;
674+ esac
675+ echo " "
676+ else
677+ echo -e " ${FAINT} Flutter search directory: current directory (default)${NC} "
678+ echo " "
679+ fi
680+
539681echo -e " ${YELLOW} ⚠️ This action is IRREVERSIBLE for deleted files. ⚠️${NC} "
540682echo -e " ${YELLOW} Please CLOSE all development applications (Xcode, Android Studio, VSCode, etc.) before running.${NC} "
541683echo " "
0 commit comments