Skip to content

Commit

Permalink
Add switch support
Browse files Browse the repository at this point in the history
  • Loading branch information
genotrance committed Apr 16, 2019
1 parent deecd90 commit b05d948
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
29 changes: 27 additions & 2 deletions src/nimblepkg/nimscriptapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

## This module is implicitly imported in NimScript .nimble files.

import system except getCommand, setCommand
import strformat, strutils
import system except getCommand, setCommand, switch, `--`
import strformat, strutils, tables

var
packageName* = "" ## Set this to the package name. It
Expand All @@ -28,6 +28,7 @@ var
beforeHooks: seq[string] = @[]
afterHooks: seq[string] = @[]
commandLineParams: seq[string] = @[]
flags: TableRef[string, seq[string]]

command = "e"
project = ""
Expand All @@ -50,6 +51,21 @@ proc setCommand(cmd: string, prj = "") =
if prj.len != 0:
project = prj

proc switch(key: string, value="") =
if flags.isNil:
flags = newTable[string, seq[string]]()

if flags.hasKey(key):
flags[key].add(value)
else:
flags[key] = @[value]

template `--`(key, val: untyped) =
switch(astToStr(key), strip astToStr(val))

template `--`(key: untyped) =
switch(astToStr(key), "")

template printIfLen(varName) =
if varName.len != 0:
iniOut &= astToStr(varName) & ": \"" & varName & "\"\n"
Expand Down Expand Up @@ -98,6 +114,15 @@ proc onExit() =
output &= "\"command\": \"" & command & "\", "
if project.len != 0:
output &= "\"project\": \"" & project & "\", "
if not flags.isNil and flags.len != 0:
output &= "\"flags\": {"
for key, val in flags.pairs:
output &= "\"" & key & "\": ["
for v in val:
output &= "\"" & v & "\", "
output = output[0 .. ^3] & "], "
output = output[0 .. ^3] & "}, "
output &= "\"retVal\": " & $retVal
echo "{" & output & "}"
Expand Down
12 changes: 9 additions & 3 deletions src/nimblepkg/nimscriptwrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes

(output, exitCode) = nimsFile.execNimscript(actionName, options)

defer:
nimsFile.removeFile()

if exitCode != 0:
raise newException(NimbleError, output)

Expand All @@ -73,21 +76,24 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes
parseJson("{}")

result.success = true
result.flags = newTable[string, seq[string]]()
if "command" in j:
result.command = j["command"].getStr()
else:
result.command = internalCmd
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.flags = newTable[string, seq[string]]()

if lines.len > 1:
stdout.writeLine lines[0 .. ^2].join("\n")

nimsFile.removeFile()

proc execTask*(scriptName, taskName: string,
options: Options): ExecutionResult[bool] =
## Executes the specified task in the specified script.
Expand Down
5 changes: 3 additions & 2 deletions src/nimblepkg/packageparser.nim
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,12 @@ proc readPackageInfoFromNims(scriptName: string, options: Options,
let
(nimsFile, iniFile) = setupNimscript(scriptName, options)

defer:
nimsFile.removeFile()

if iniFile.fileExists():
readPackageInfoFromNimble(iniFile, result)

nimsFile.removeFile()

proc inferInstallRules(pkgInfo: var PackageInfo, options: Options) =
# Binary packages shouldn't install .nim files by default.
# (As long as the package info doesn't explicitly specify what should be
Expand Down

0 comments on commit b05d948

Please sign in to comment.