Created
June 10, 2021 17:24
-
-
Save mallibone/cf3fab05314ff5196a7936e005064f8c to your computer and use it in GitHub Desktop.
.NET build output cleanup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#time | |
open System.IO | |
open System.Globalization | |
let EnumerateDirectories path = | |
Directory.EnumerateDirectories(path) |> Seq.toList | |
let isObjOrBinFolder (folderName:string) = | |
folderName.EndsWith("obj", true, CultureInfo.InvariantCulture) || folderName.EndsWith("bin", true, CultureInfo.InvariantCulture) | |
let rec getFoldersToDelete path = | |
match EnumerateDirectories path with | |
| [] -> [] | |
| subfolders -> | |
let targetFolders = subfolders | |
|> List.filter isObjOrBinFolder | |
let targets = subfolders | |
|> List.filter (isObjOrBinFolder >> not) | |
|> List.collect getFoldersToDelete | |
|> List.append targetFolders | |
targets | |
let deleteFoldersAndSubFolders path = | |
getFoldersToDelete path | |
|> List.iter (fun dir -> | |
printfn "Deleting: %s" dir | |
Directory.Delete dir) | |
let ensureDirectoryExists directory = | |
if Directory.Exists directory then | |
Some directory | |
else | |
printfn "Directory %s not found and will be skipped" directory | |
None | |
match fsi.CommandLineArgs |> Array.skip 1 with | |
| [||] -> | |
deleteFoldersAndSubFolders (Directory.GetCurrentDirectory()) | |
| directoryPaths -> | |
directoryPaths | |
|> Array.map ensureDirectoryExists | |
|> Array.choose id | |
|> Array.iter deleteFoldersAndSubFolders | |
// let path = "/Users/mallibone/Downloads/Testground/test" | |
let defaultPath = Directory.GetCurrentDirectory() | |
match fsi.CommandLineArgs |> Array.skip 1 with | |
| [||] -> | |
deleteFoldersAndSubFolders defaultPath | |
| [|"--help"|] -> | |
printfn "Usage of the BuildDirCleanup script" | |
printfn "" | |
printfn "dotnet fsi BuildDirCleanup.fsx [dir1 dir2 .. dirn]" | |
printfn "" | |
printfn "In every directory provided and it's subdirectories the bin and obj folders will be deleted." | |
printfn "If no directory is given the current direcotry (%s)" defaultPath | |
printfn "will be recursivley searched." | |
printfn "" | |
| directoryPaths -> | |
directoryPaths | |
|> Array.map ensureDirectoryExists | |
|> Array.choose id | |
|> Array.iter deleteFoldersAndSubFolders |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment