diff --git a/src/nimble.nim b/src/nimble.nim index e1a842cb..16625021 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -231,6 +231,28 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], cd pkgDir: # Make sure `execHook` executes the correct .nimble file. discard execHook(options, actionBuild, false) +proc cleanFromDir(pkgInfo: PackageInfo, options: Options) = + ## Clean up build files. + # Handle pre-`clean` hook. + let pkgDir = pkgInfo.myPath.parentDir() + + cd pkgDir: # Make sure `execHook` executes the correct .nimble file. + if not execHook(options, actionClean, true): + raise nimbleError("Pre-hook prevented further execution.") + + if pkgInfo.bin.len == 0: + return + + for bin, _ in pkgInfo.bin: + let outputDir = pkgInfo.getOutputDir("") + if dirExists(outputDir): + if fileExists(outputDir / bin): + removeFile(outputDir / bin) + + # Handle post-`clean` hook. + cd pkgDir: # Make sure `execHook` executes the correct .nimble file. + discard execHook(options, actionClean, false) + proc promptRemoveEntirePackageDir(pkgDir: string, options: Options) = let exceptionMsg = getCurrentExceptionMsg() let warningMsgEnd = if exceptionMsg.len > 0: &": {exceptionMsg}" else: "." @@ -689,6 +711,12 @@ proc build(options: Options) = var args = options.getCompilationFlags() buildFromDir(pkgInfo, paths, args, options) +proc clean(options: Options) = + let dir = getCurrentDir() + let pkgInfo = getPkgInfo(dir, options) + nimScriptHint(pkgInfo) + cleanFromDir(pkgInfo, options) + proc execBackend(pkgInfo: PackageInfo, options: Options) = let bin = options.getCompilationBinary(pkgInfo).get("") @@ -1862,6 +1890,8 @@ proc doAction(options: var Options) = listPaths(options) of actionBuild: build(options) + of actionClean: + clean(options) of actionRun: run(options) of actionCompile, actionDoc: diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index be042165..5cb56e3c 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -48,7 +48,8 @@ type actionNil, actionRefresh, actionInit, actionDump, actionPublish, actionInstall, actionSearch, actionList, actionBuild, actionPath, actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks, - actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup + actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup, + actionClean DevelopActionType* = enum datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude @@ -58,7 +59,7 @@ type Action* = object case typ*: ActionType of actionNil, actionList, actionPublish, actionTasks, actionCheck, - actionLock, actionSetup: nil + actionLock, actionSetup, actionClean: nil of actionSync: listOnly*: bool of actionRefresh: @@ -143,6 +144,7 @@ Commands: [-i, --inclDeps] Uninstalls package and dependent package(s). build [opts, ...] [bin] Builds a package. Passes options to the Nim compiler. + clean Clean build artifacts. run [opts, ...] [bin] Builds and runs a package. Binary needs to be specified after any compilation options if there are several @@ -231,6 +233,8 @@ proc parseActionType*(action: string): ActionType = result = actionPath of "build": result = actionBuild + of "clean": + result = actionClean of "run": result = actionRun of "c", "compile", "js", "cpp", "cc":