Skip to content

Commit b0d2311

Browse files
author
jan Anja
authored
nimble install: add --noRebuild option (#966)
* Add --noRebuild option for install * Add --noRebuild test in tmisctests.nim
1 parent 1339046 commit b0d2311

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

src/nimble.nim

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,21 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string],
191191
if bin.extractFilename().changeFileExt("") != binToBuild:
192192
continue
193193

194+
let outputDir = pkgInfo.getOutputDir("")
195+
if dirExists(outputDir):
196+
if fileExists(outputDir / bin):
197+
if not pkgInfo.needsRebuild(outputDir / bin, realDir, options):
198+
display("Skipping", "$1/$2 (up-to-date)" %
199+
[pkginfo.basicInfo.name, bin], priority = HighPriority)
200+
binariesBuilt.inc()
201+
continue
202+
else:
203+
createDir(outputDir)
204+
194205
let outputOpt = "-o:" & pkgInfo.getOutputDir(bin).quoteShell
195206
display("Building", "$1/$2 using $3 backend" %
196207
[pkginfo.basicInfo.name, bin, pkgInfo.backend], priority = HighPriority)
197208

198-
let outputDir = pkgInfo.getOutputDir("")
199-
if not dirExists(outputDir):
200-
createDir(outputDir)
201-
202209
let input = realDir / src.changeFileExt("nim")
203210
# `quoteShell` would be more robust than `\"` (and avoid quoting when
204211
# un-necessary) but would require changing `extractBin`

src/nimblepkg/options.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type
6868
passNimFlags*: seq[string]
6969
devActions*: seq[DevelopAction]
7070
path*: string
71+
noRebuild*: bool
7172
withDependencies*: bool
7273
## Whether to put in develop mode also the dependencies of the packages
7374
## listed in the develop command.
@@ -99,6 +100,7 @@ Commands:
99100
install [pkgname, ...] Installs a list of packages.
100101
[-d, --depsOnly] Install only dependencies.
101102
[-p, --passNim] Forward specified flag to compiler.
103+
[--noRebuild] Don't rebuild binaries if they're up-to-date.
102104
develop [pkgname, ...] Clones a list of packages for development.
103105
Adds them to a develop file if specified or
104106
to `nimble.develop` if not specified and
@@ -499,6 +501,8 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
499501
case f
500502
of "depsonly", "d":
501503
result.depsOnly = true
504+
of "norebuild":
505+
result.action.noRebuild = true
502506
of "passnim", "p":
503507
result.action.passNimFlags.add(val)
504508
else:

src/nimblepkg/packageinfo.nim

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Stdlib imports
55
import system except TResult
6-
import hashes, json, strutils, os, sets, tables, httpclient, strformat
6+
import hashes, json, strutils, os, sets, tables, times, httpclient, strformat
77
from net import SslError
88

99
# Local imports
@@ -494,6 +494,22 @@ proc iterInstallFiles*(realDir: string, pkgInfo: PackageInfo,
494494

495495
action(file)
496496

497+
proc needsRebuild*(pkgInfo: PackageInfo, bin: string, dir: string, options: Options): bool =
498+
if options.action.typ != actionInstall:
499+
return true
500+
if not options.action.noRebuild:
501+
return true
502+
503+
let binTimestamp = getFileInfo(bin).lastWriteTime
504+
var rebuild = false
505+
iterFilesWithExt(dir, pkgInfo,
506+
proc (file: string) =
507+
let srcTimestamp = getFileInfo(file).lastWriteTime
508+
if binTimestamp < srcTimestamp:
509+
rebuild = true
510+
)
511+
return rebuild
512+
497513
proc getCacheDir*(pkgInfo: PackageBasicInfo): string =
498514
&"{pkgInfo.name}-{pkgInfo.version}-{$pkgInfo.checksum}"
499515

@@ -515,7 +531,7 @@ proc getNameAndVersion*(pkgInfo: PackageInfo): string =
515531
&"{pkgInfo.basicInfo.name}@{pkgInfo.basicInfo.version}"
516532

517533
when isMainModule:
518-
import unittest
534+
import unittest
519535

520536
test "toValidPackageName":
521537
check toValidPackageName("foo__bar") == "foo_bar"

tests/tmisctests.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ suite "misc tests":
5050
let (_, exitCode) = execNimble("install", "--passNim:-d:passNimIsWorking")
5151
check exitCode == QuitSuccess
5252

53+
test "install with --noRebuild flag":
54+
cd "run":
55+
check execNimbleYes("build").exitCode == QuitSuccess
56+
57+
let (output, exitCode) = execNimbleYes("install", "--noRebuild")
58+
check exitCode == QuitSuccess
59+
check output.contains("Skipping")
60+
5361
test "NimbleVersion is defined":
5462
cd "nimbleVersionDefine":
5563
let (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim")

0 commit comments

Comments
 (0)