Fix #209 - cache return val, fix nimcache dir

This commit is contained in:
Ganesh Viswanathan 2020-05-10 20:08:07 -05:00
commit f693c3e30a
2 changed files with 22 additions and 10 deletions

View file

@ -91,18 +91,22 @@ proc execAction*(cmd: string, retry = 0, die = true, cache = false,
# Else cache for preserving functionality in nimsuggest and nimcheck
let
hash = (ccmd & cacheKey).hash().abs()
cacheFile = getNimteropCacheDir() / "execCache" / "nimterop_" & $hash & ".txt"
cachePath = getNimteropCacheDir() / "execCache" / "nimterop_" & $hash
cacheFile = cachePath & ".txt"
retFile = cachePath & "_ret.txt"
when defined(nimsuggest) or defined(nimcheck):
# Load results from cache file if generated in previous run
if fileExists(cacheFile):
if fileExists(cacheFile) and fileExists(retFile):
result.output = cacheFile.readFile()
result.ret = retFile.readFile().parseInt()
elif die:
doAssert false, "Results not cached - run nim c/cpp at least once\n" & ccmd
else:
if cache and fileExists(cacheFile) and not compileOption("forceBuild"):
if cache and fileExists(cacheFile) and fileExists(retFile) and not compileOption("forceBuild"):
# Return from cache when requested
result.output = cacheFile.readFile()
result.ret = retFile.readFile().parseInt()
else:
# Execute command and store results in cache
(result.output, result.ret) = gorgeEx(ccmd)
@ -113,6 +117,7 @@ proc execAction*(cmd: string, retry = 0, die = true, cache = false,
let flag = when not defined(Windows): "-p" else: ""
discard execAction(&"mkdir {flag} {dir.sanitizePath}")
cacheFile.writeFile(result.output)
retFile.writeFile($result.ret)
else:
# Used by toast
(result.output, result.ret) = execCmdEx(ccmd)

View file

@ -58,12 +58,24 @@ proc getProjectDir*(): string =
else:
discard
proc stripName(path, projectName: string): string =
# Remove `pname_d|r` tail from path
let
(head, tail) = path.splitPath()
if projectName in tail:
result = head
else:
result = path
proc getNimcacheDir*(projectDir = ""): string =
## Get nimcache directory for current compilation or specified `projectDir`
when nimvm:
when (NimMajor, NimMinor, NimPatch) >= (1, 2, 0):
# Get value at compile time from `std/compilesettings`
result = querySetting(SingleValueSetting.nimcacheDir)
result = stripName(
querySetting(SingleValueSetting.nimcacheDir),
querySetting(SingleValueSetting.projectName)
)
else:
discard
@ -78,12 +90,7 @@ proc getNimcacheDir*(projectDir = ""): string =
dumpJson = getJson(projectDir)
if dumpJson != nil and dumpJson.hasKey("nimcache"):
result = dumpJson["nimcache"].getStr()
let
(head, tail) = result.splitPath()
if "dummy" in tail:
# Remove `dummy_d` subdir when default nimcache
result = head
result = stripName(dumpJson["nimcache"].getStr(), "dummy")
# Set to OS defaults if not detectable
if result.len == 0: