Skip to content

Commit

Permalink
Add nimble clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberTailor committed Dec 22, 2021
1 parent 26c9102 commit 07f8b8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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: "."
Expand Down Expand Up @@ -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("")
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 07f8b8d

Please sign in to comment.