Skip to content

Commit f7486a1

Browse files
CyberTailordom96
authored andcommitted
Add nimble clean command
Closes #555
1 parent 6e4e04e commit f7486a1

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/nimble.nim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string],
231231
cd pkgDir: # Make sure `execHook` executes the correct .nimble file.
232232
discard execHook(options, actionBuild, false)
233233

234+
proc cleanFromDir(pkgInfo: PackageInfo, options: Options) =
235+
## Clean up build files.
236+
# Handle pre-`clean` hook.
237+
let pkgDir = pkgInfo.myPath.parentDir()
238+
239+
cd pkgDir: # Make sure `execHook` executes the correct .nimble file.
240+
if not execHook(options, actionClean, true):
241+
raise nimbleError("Pre-hook prevented further execution.")
242+
243+
if pkgInfo.bin.len == 0:
244+
return
245+
246+
for bin, _ in pkgInfo.bin:
247+
let outputDir = pkgInfo.getOutputDir("")
248+
if dirExists(outputDir):
249+
if fileExists(outputDir / bin):
250+
removeFile(outputDir / bin)
251+
252+
# Handle post-`clean` hook.
253+
cd pkgDir: # Make sure `execHook` executes the correct .nimble file.
254+
discard execHook(options, actionClean, false)
255+
234256
proc promptRemoveEntirePackageDir(pkgDir: string, options: Options) =
235257
let exceptionMsg = getCurrentExceptionMsg()
236258
let warningMsgEnd = if exceptionMsg.len > 0: &": {exceptionMsg}" else: "."
@@ -689,6 +711,12 @@ proc build(options: Options) =
689711
var args = options.getCompilationFlags()
690712
buildFromDir(pkgInfo, paths, args, options)
691713

714+
proc clean(options: Options) =
715+
let dir = getCurrentDir()
716+
let pkgInfo = getPkgInfo(dir, options)
717+
nimScriptHint(pkgInfo)
718+
cleanFromDir(pkgInfo, options)
719+
692720
proc execBackend(pkgInfo: PackageInfo, options: Options) =
693721
let
694722
bin = options.getCompilationBinary(pkgInfo).get("")
@@ -1862,6 +1890,8 @@ proc doAction(options: var Options) =
18621890
listPaths(options)
18631891
of actionBuild:
18641892
build(options)
1893+
of actionClean:
1894+
clean(options)
18651895
of actionRun:
18661896
run(options)
18671897
of actionCompile, actionDoc:

src/nimblepkg/options.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ type
4848
actionNil, actionRefresh, actionInit, actionDump, actionPublish,
4949
actionInstall, actionSearch, actionList, actionBuild, actionPath,
5050
actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks,
51-
actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup
51+
actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup,
52+
actionClean
5253

5354
DevelopActionType* = enum
5455
datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude
@@ -58,7 +59,7 @@ type
5859
Action* = object
5960
case typ*: ActionType
6061
of actionNil, actionList, actionPublish, actionTasks, actionCheck,
61-
actionLock, actionSetup: nil
62+
actionLock, actionSetup, actionClean: nil
6263
of actionSync:
6364
listOnly*: bool
6465
of actionRefresh:
@@ -143,6 +144,7 @@ Commands:
143144
[-i, --inclDeps] Uninstalls package and dependent package(s).
144145
build [opts, ...] [bin] Builds a package. Passes options to the Nim
145146
compiler.
147+
clean Clean build artifacts.
146148
run [opts, ...] [bin] Builds and runs a package.
147149
Binary needs to be specified after any
148150
compilation options if there are several
@@ -231,6 +233,8 @@ proc parseActionType*(action: string): ActionType =
231233
result = actionPath
232234
of "build":
233235
result = actionBuild
236+
of "clean":
237+
result = actionClean
234238
of "run":
235239
result = actionRun
236240
of "c", "compile", "js", "cpp", "cc":

0 commit comments

Comments
 (0)