|
# 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 |