Skip to content

Commit

Permalink
Hardcode version, json{}, code width 80, isScriptResultCached, no bla…
Browse files Browse the repository at this point in the history
…nk paramStr check
  • Loading branch information
genotrance committed Apr 22, 2019
1 parent 64e5489 commit c278bd6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
14 changes: 1 addition & 13 deletions nimble.nimble
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import strutils

const
parentDir = currentSourcePath.rsplit(seps={'/', '\\', ':'}, maxsplit=1)[0]

when fileExists(parentDir & "/src/nimblepkg/common.nim"):
# In the git repository the Nimble sources are in a ``src`` directory.
import src/nimblepkg/common
else:
# When the package is installed, the ``src`` directory disappears.
import nimblepkg/common

# Package

version = nimbleVersion
version = "0.9.0"
author = "Dominik Picheta"
description = "Nim package manager."
license = "BSD"
Expand Down
3 changes: 1 addition & 2 deletions src/nimblepkg/nimscriptapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ proc requires*(deps: varargs[string]) =

proc getParams() =
for i in 5 .. paramCount():
if paramStr(i).len != 0:
commandLineParams.add paramStr(i).normalize
commandLineParams.add paramStr(i).normalize

proc getCommand(): string =
return command
Expand Down
49 changes: 29 additions & 20 deletions src/nimblepkg/nimscriptwrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
## scripting language.

import common, version, options, packageinfo, cli
import hashes, json, os, streams, strutils, strtabs, tables, times, osproc, sets, pegs
import hashes, json, os, streams, strutils, strtabs,
tables, times, osproc, sets, pegs

type
Flags = TableRef[string, seq[string]]
Expand All @@ -21,9 +22,11 @@ const
nimscriptApi = staticRead("nimscriptapi.nim")
nimscriptHash = $nimscriptApi.hash().abs()

proc execNimscript(nimsFile, actionName: string, options: Options, live = true): tuple[output: string, exitCode: int] =
proc execNimscript(nimsFile, actionName: string, options: Options,
live = true): tuple[output: string, exitCode: int] =
var
cmd = ("nim e --hints:off --verbosity:0 " & nimsFile.quoteShell & " " & actionName).strip()
cmd = ("nim e --hints:off --verbosity:0 " &
nimsFile.quoteShell & " " & actionName).strip()

if live:
result.exitCode = execCmd(cmd)
Expand All @@ -35,35 +38,46 @@ proc execNimscript(nimsFile, actionName: string, options: Options, live = true):
else:
result = execCmdEx(cmd, options = {poUsePath})

proc setupNimscript*(scriptName: string, options: Options): tuple[nimsFile, iniFile: string] =
proc setupNimscript*(scriptName: string, options: Options):
tuple[nimsFile, iniFile: string] =
let
cacheDir = getTempDir() / "nimblecache"
shash = $(scriptName & nimscriptHash).hash().abs()
prjCacheDir = cacheDir / scriptName.splitFile().name & "_" & shash
nimsCacheFile = prjCacheDir / scriptName.extractFilename().changeFileExt ".nims"
nimsCacheFile =
prjCacheDir / scriptName.extractFilename().changeFileExt ".nims"

result.nimsFile = scriptName.parentDir() / scriptName.splitFile().name & "_" & shash & ".nims"
result.iniFile = prjCacheDir / scriptName.extractFilename().changeFileExt ".ini"
result.nimsFile = scriptName.parentDir() / scriptName.splitFile().name &
"_" & shash & ".nims"
result.iniFile =
prjCacheDir / scriptName.extractFilename().changeFileExt ".ini"

if not prjCacheDir.dirExists() or not nimsCacheFile.fileExists() or not result.iniFile.fileExists() or
scriptName.getLastModificationTime() > nimsCacheFile.getLastModificationTime():
let isScriptResultCached =
prjCacheDir.dirExists() and nimsCacheFile.fileExists() and
result.iniFile.fileExists() and
scriptName.getLastModificationTime() < nimsCacheFile.getLastModificationTime()

if not isScriptResultCached:
createDir(prjCacheDir)
writeFile(nimsCacheFile, nimscriptApi & scriptName.readFile() & "\nonExit()\n")
writeFile(nimsCacheFile,
nimscriptApi & scriptName.readFile() & "\nonExit()\n")
discard tryRemoveFile(result.iniFile)

if not result.nimsFile.fileExists():
nimsCacheFile.copyFile(result.nimsFile)

if not result.iniFile.fileExists():
let
(output, exitCode) = result.nimsFile.execNimscript("printPkgInfo", options, live=false)
(output, exitCode) =
result.nimsFile.execNimscript("printPkgInfo", options, live=false)

if exitCode == 0 and output.len != 0:
result.iniFile.writeFile(output)
else:
raise newException(NimbleError, output & "\nprintPkgInfo() failed")

proc execScript*(scriptName, actionName: string, options: Options): ExecutionResult[bool] =
proc execScript*(scriptName, actionName: string, options: Options):
ExecutionResult[bool] =
let
(nimsFile, iniFile) = setupNimscript(scriptName, options)

Expand All @@ -83,21 +97,16 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes
parseJson("{}")

result.flags = newTable[string, seq[string]]()
if "success" in j:
result.success = j["success"].getBool()
if "command" in j:
result.command = j["command"].getStr()
else:
result.command = internalCmd
result.success = j{"success"}.getBool()
result.command = j{"command"}.getStr()
if "project" in j:
result.arguments.add j["project"].getStr()
if "flags" in j:
for flag, vals in j["flags"].pairs:
result.flags[flag] = @[]
for val in vals.items():
result.flags[flag].add val.getStr()
if "retVal" in j:
result.retVal = j["retVal"].getBool()
result.retVal = j{"retVal"}.getBool()

proc execTask*(scriptName, taskName: string,
options: Options): ExecutionResult[bool] =
Expand Down

0 comments on commit c278bd6

Please sign in to comment.