Skip to content

Instantly share code, notes, and snippets.

@oashrafouad
Last active July 22, 2024 20:08
Show Gist options
  • Save oashrafouad/4b716d254cb789f7ead767b4b4351699 to your computer and use it in GitHub Desktop.
Save oashrafouad/4b716d254cb789f7ead767b4b4351699 to your computer and use it in GitHub Desktop.
Clean Flutter Projects

Usage

You provide this script with a base folder that contains multiple Flutter projects, and it iterates through each project folder running flutter clean in each one. Run the script and watch it do its (really simple) magic.

Steps

Windows

  1. Download the ZIP file and open PowerShell at the extracted folder.
  2. Execute the .ps1 script with .\clean_flutter_projects.ps1 and provide path of the base folder as argument.
    • Example: .\clean_flutter_projects.ps1 C:\Users\omar\MyProjects

Linux / macOS

  1. Download the ZIP file and open terminal at the extracted folder.
  2. Execute the .sh script with ./clean_flutter_projects.sh or sh clean_flutter_projects.sh and provide the path of the base folder as argument.
    • Example: ./clean_flutter_projects.sh /Users/omar/MyProjects
# Check if no argument is provided
if ($args.Count -eq 0) {
# Prompt the user to provide a directory as an argument
Write-Host "Provide base directory as argument (Example: .\clean_flutter_projects.ps1 C:\path\to\directory)"
exit 1
} elseif ($args.Count -gt 1) {
# Error out if more than one argument is provided
Write-Host "Error: Too many arguments provided. 1 argument is expected."
exit 1
}
# Check if the provided argument is not a directory
if (!(Test-Path $args[0] -PathType Container)) {
Write-Host "Error: '$($args[0])' is not a valid directory."
exit 1
}
Push-Location
# Change directory to the provided argument
Set-Location $args[0] -ErrorAction Stop
# Create an array of directories in the current directory
$directories = Get-ChildItem -Directory
# Count the total number of projects/directories
$totalProjects = $directories.Count
# Store the name of the current directory
$currentDir = $PWD.Path.Split("\")[-1]
# Display the total number of projects found
Write-Host "Found $totalProjects projects in $currentDir"
# Get initial directory size in MB
$initialSize = (Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
# Initialize a counter for cleaned projects
$count = 0
# Loop through each directory
foreach ($folder in $directories) {
# Check if the item is a directory
if (Test-Path $folder.FullName -PathType Container) {
# Increment the cleaned projects counter
$count++
# Change directory to the project folder
Set-Location $folder.FullName -ErrorAction Stop
# Run flutter clean command
flutter clean
# Change back to the parent directory
Set-Location .. -ErrorAction Stop
# Display the cleaned project info
Write-Host "Cleaned project $($folder.Name) ($count/$totalProjects)"
} else {
# Inform if the found item is not a directory
Write-Host "$($folder.Name) is not a directory."
}
}
# Get final directory size in MB
$finalSize = (Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
# Calculate the size difference
$sizeDifference = $initialSize - $finalSize
# Display the size difference
Write-Host "Cleaning finished. You freed up $($sizeDifference.ToString("F2")) MB!"
Pop-Location
#!/bin/bash
# Check if no argument is provided
if [ $# -eq 0 ]; then
# Prompt the user to provide a directory as an argument
echo "Provide base directory as argument (Example: ./clean_flutter_projects.sh /path/to/directory)"
exit 1
elif [ $# -gt 1 ]; then
# Error out if more than one argument is provided
echo "Error: Too many arguments provided. 1 argument is expected."
exit 1
fi
# Check if the provided argument is not a directory
if [ ! -d "$1" ]; then
echo "Error: '$1' is not a valid directory."
exit 1
fi
# Change directory to the provided argument
cd "$1" || exit
# Create an array of directories in the current directory
directories=(*/)
# Count the total number of projects/directories
total_projects=${#directories[@]}
# Store the name of the current directory
current_dir=${PWD##*/}
# Display the total number of projects found
echo "Found $total_projects projects in $current_dir"
# Get initial directory size in MB
initial_size=$(du -sm . | cut -f1)
# Initialize a counter for cleaned projects
count=0
# Loop through each directory
for folder in "${directories[@]}"; do
# Check if the item is a directory
if [ -d "$folder" ]; then
# Increment the cleaned projects counter
count=$((count + 1))
# Change directory to the project folder
cd "$folder" || { echo "Failed to cd into $folder"; }
# Run flutter clean command
flutter clean || { echo "Failed to run 'flutter clean' in $folder"; }
# Change back to the parent directory
cd .. || { echo "Failed to cd back to parent directory from $folder"; }
# Display the cleaned project info
echo "Cleaned project $folder ($count/$total_projects)"
else
# Inform if the found item is not a directory
echo "$folder is not a directory."
fi
done
# Get final directory size in MB
final_size=$(du -sm . | cut -f1)
# Calculate the size difference
size_difference=$((initial_size - final_size))
# Display the size difference
echo "Cleaning finished. You freed up ${size_difference} MB!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment