Skip to content

Commit

Permalink
nimble install: add --noRebuild option (#966)
Browse files Browse the repository at this point in the history
* Add --noRebuild option for install

* Add --noRebuild test in tmisctests.nim
  • Loading branch information
jan Anja authored Dec 14, 2021
1 parent 1339046 commit b0d2311
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,21 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string],
if bin.extractFilename().changeFileExt("") != binToBuild:
continue

let outputDir = pkgInfo.getOutputDir("")
if dirExists(outputDir):
if fileExists(outputDir / bin):
if not pkgInfo.needsRebuild(outputDir / bin, realDir, options):
display("Skipping", "$1/$2 (up-to-date)" %
[pkginfo.basicInfo.name, bin], priority = HighPriority)
binariesBuilt.inc()
continue
else:
createDir(outputDir)

let outputOpt = "-o:" & pkgInfo.getOutputDir(bin).quoteShell
display("Building", "$1/$2 using $3 backend" %
[pkginfo.basicInfo.name, bin, pkgInfo.backend], priority = HighPriority)

let outputDir = pkgInfo.getOutputDir("")
if not dirExists(outputDir):
createDir(outputDir)

let input = realDir / src.changeFileExt("nim")
# `quoteShell` would be more robust than `\"` (and avoid quoting when
# un-necessary) but would require changing `extractBin`
Expand Down
4 changes: 4 additions & 0 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type
passNimFlags*: seq[string]
devActions*: seq[DevelopAction]
path*: string
noRebuild*: bool
withDependencies*: bool
## Whether to put in develop mode also the dependencies of the packages
## listed in the develop command.
Expand Down Expand Up @@ -99,6 +100,7 @@ Commands:
install [pkgname, ...] Installs a list of packages.
[-d, --depsOnly] Install only dependencies.
[-p, --passNim] Forward specified flag to compiler.
[--noRebuild] Don't rebuild binaries if they're up-to-date.
develop [pkgname, ...] Clones a list of packages for development.
Adds them to a develop file if specified or
to `nimble.develop` if not specified and
Expand Down Expand Up @@ -499,6 +501,8 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
case f
of "depsonly", "d":
result.depsOnly = true
of "norebuild":
result.action.noRebuild = true
of "passnim", "p":
result.action.passNimFlags.add(val)
else:
Expand Down
20 changes: 18 additions & 2 deletions src/nimblepkg/packageinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Stdlib imports
import system except TResult
import hashes, json, strutils, os, sets, tables, httpclient, strformat
import hashes, json, strutils, os, sets, tables, times, httpclient, strformat
from net import SslError

# Local imports
Expand Down Expand Up @@ -494,6 +494,22 @@ proc iterInstallFiles*(realDir: string, pkgInfo: PackageInfo,

action(file)

proc needsRebuild*(pkgInfo: PackageInfo, bin: string, dir: string, options: Options): bool =
if options.action.typ != actionInstall:
return true
if not options.action.noRebuild:
return true

let binTimestamp = getFileInfo(bin).lastWriteTime
var rebuild = false
iterFilesWithExt(dir, pkgInfo,
proc (file: string) =
let srcTimestamp = getFileInfo(file).lastWriteTime
if binTimestamp < srcTimestamp:
rebuild = true
)
return rebuild

proc getCacheDir*(pkgInfo: PackageBasicInfo): string =
&"{pkgInfo.name}-{pkgInfo.version}-{$pkgInfo.checksum}"

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

when isMainModule:
import unittest
import unittest

test "toValidPackageName":
check toValidPackageName("foo__bar") == "foo_bar"
Expand Down
8 changes: 8 additions & 0 deletions tests/tmisctests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ suite "misc tests":
let (_, exitCode) = execNimble("install", "--passNim:-d:passNimIsWorking")
check exitCode == QuitSuccess

test "install with --noRebuild flag":
cd "run":
check execNimbleYes("build").exitCode == QuitSuccess

let (output, exitCode) = execNimbleYes("install", "--noRebuild")
check exitCode == QuitSuccess
check output.contains("Skipping")

test "NimbleVersion is defined":
cd "nimbleVersionDefine":
let (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim")
Expand Down

0 comments on commit b0d2311

Please sign in to comment.