About Monkey 2 › Forums › Monkey 2 Development › Monkey2 Build Cleanup Utility
This topic contains 0 replies, has 1 voice, and was last updated by
cocon 2 years, 3 months ago.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
December 26, 2016 at 4:10 am #6024
I needed to create this cleanup utility to tidy up my filesystem, things got messy after the last months and there were multiple builds and products from previous versions.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203#Import "<std>"Using std..Class DirectorySearchPrivateField foundDirectories:List<String>Field directoryStack:Stack<String>PublicProperty FoundDirectories:String[]()Return foundDirectories.ToArray()EndPrivate' Special Directory Search RulesMethod TestDirectoryIgnore:Bool(path:String)Return (path.Contains(".git")) = FalseEndMethod TestDirectoryFilter:Bool(path:String)Local searchBuild := ArgumentManager.GetWillFindBuild()Local searchProduct := ArgumentManager.GetWillFindProduct()Local containsBuild := path.Contains(".buildv")Local containsProduct := path.Contains(".products")If searchBuild And searchProductReturn containsBuild Or containsProductElseif searchBuild And Not searchProductReturn containsBuildElseif Not searchBuild And searchProductReturn containsProductEndReturn FalseEnd' Path SearchMethod IsDirectory:Bool(path:String)Return GetFileType(path) = FileType.DirectoryEndMethod PushSubdirectoriesToStack(path:String)'Print("Checked: " + path)For Local p := Eachin LoadDir(path)Local newpath := path+"/"+pIf IsDirectory(newpath)If TestDirectoryIgnore(newpath)'Print("Added: " + newpath)directoryStack.Push(newpath)EndEndNextEndMethod AddDirectoryToFound(directory:String)If TestDirectoryFilter(directory)foundDirectories.AddLast(directory)'Print("Filtered: " + directory)EndEndPublicMethod Search:String[](baseDirectory:String)directoryStack = New Stack<String>foundDirectories = New List<String>directoryStack.Push(baseDirectory)While directoryStack.Length > 0Local dirBase := directoryStack.Pop()AddDirectoryToFound(dirBase)PushSubdirectoriesToStack(dirBase)EndReturn foundDirectories.ToArray()EndEndClass DirectoryOperationsFunction PrintDirectories(directories:String[])If directories.Length = 0 Then ReturnPrint("~n")For Local d := Eachin directoriesPrint(d)NextPrint("Total Directories: " + directories.Length)Print("Finished~n")EndFunction DeleteDirectories(directories:String[])For Local d := Eachin directoriesIf DeleteDir(d, True)'Print("Delete: " + d)EndNextPrint("Finished~n")EndEndClass ArgumentManagerPrivateGlobal baseDirectory:StringGlobal willPrint:Bool = FalseGlobal willDelete:Bool = FalseGlobal willFindBuild:Bool = FalseGlobal willFindProduct:Bool = FalseGlobal invalidArguments:Bool = FalseFunction CleanPath:String(path:String)Return path.Replace( "\","/" ).Replace("~q", "")EndPublicFunction GetBaseDirectory:String()Return baseDirectoryEndFunction GetWillPrint:Bool()Return willPrintEndFunction GetWillDelete:Bool()Return willDeleteEndFunction GetWillFindBuild:Bool()Return willFindBuildEndFunction GetWillFindProduct:Bool()Return willFindProductEndFunction ArgumentsWereInvalid:Bool()Return invalidArgumentsEndFunction Parse()Local args := AppArgs()' Usage ExampleIf args.Length = 1Print("Usage: ~qLocation of the base directory~q [build] [product] [print] [delete]")Print("~tbuild: It will find any ~q.buildv~q directory (contains the transpiled sources).")Print("~tproduct: It will find any ~q.products~q directory (contains the built targets).")Print("~tprint: It will print the found directories.")Print("~tdelete: It will delete the found directories.")Print("~tdelete: It will delete the found directories.")invalidArguments = TrueReturnEnd' Base PathLocal directory := CleanPath(args[1])If GetFileType(directory) = FileType.DirectorybaseDirectory = directoryElsePrint("First parameter should be the base directory.")invalidArguments = TrueReturnEnd' Other ArgumentsFor Local i := 2 Until args.LengthLocal arg := args[i].ToLower()Select argCase "build"willFindBuild = TrueCase "product"willFindProduct = TrueCase "print"willPrint = TrueCase "delete"willDelete = TrueEndNext' Last Validation ChecksIf Not willFindBuild And Not willFindProductPrint("Please use at least one of these parameters: [build] or [product]")invalidArguments = TrueEndIf Not willPrint And Not willDeletewillPrint = TrueEndEndEndFunction Main()ArgumentManager.Parse()If ArgumentManager.ArgumentsWereInvalid() Then ReturnLocal searcher := New DirectorySearchsearcher.Search(ArgumentManager.GetBaseDirectory())If ArgumentManager.GetWillPrint()DirectoryOperations.PrintDirectories(searcher.FoundDirectories)EndIf ArgumentManager.GetWillDelete()DirectoryOperations.DeleteDirectories(searcher.FoundDirectories)EndEndUsage Example:
TeX12345678910111213141516First argument must be a directory.Second argument might be any other argument available.This will print all of the build folders (argument order does not matter):monkey2buildclean.exe "D:\Programming\monkey2" build printmonkey2buildclean.exe "D:\Programming\monkey2" print buildThis will print the build and the product folders (if neither print/delete are set, print will be implied):monkey2buildclean.exe "D:\Programming\monkey2" build productThis will print and delete the build and product folders.monkey2buildclean.exe "D:\Programming\monkey2" build product print deleteThis will only delete the build and product folders, it won't print anything.monkey2buildclean.exe "D:\Programming\monkey2" build product delete -
AuthorPosts
Viewing 1 post (of 1 total)
You must be logged in to reply to this topic.