Run can work without additional option if only one bin in .nimble (#761)

* nimble run can work if only one binary in config

* usage changed

* runFile is option now

* usage updated

* small refactoring to reduce condition

* setRunOptions fix

* some code optimisation
This commit is contained in:
inv2004 2020-01-18 01:15:34 +03:00 committed by Dominik Picheta
commit 9391fbc56d
3 changed files with 62 additions and 23 deletions

View file

@ -221,7 +221,7 @@ proc buildFromDir(
options: Options options: Options
) = ) =
## Builds a package as specified by ``pkgInfo``. ## Builds a package as specified by ``pkgInfo``.
let binToBuild = options.getCompilationBinary() let binToBuild = options.getCompilationBinary(pkgInfo)
# Handle pre-`build` hook. # Handle pre-`build` hook.
let realDir = pkgInfo.getRealDir() let realDir = pkgInfo.getRealDir()
cd realDir: # Make sure `execHook` executes the correct .nimble file. cd realDir: # Make sure `execHook` executes the correct .nimble file.
@ -535,9 +535,9 @@ proc build(options: Options) =
var args = options.getCompilationFlags() var args = options.getCompilationFlags()
buildFromDir(pkgInfo, paths, args, options) buildFromDir(pkgInfo, paths, args, options)
proc execBackend(options: Options) = proc execBackend(pkgInfo: PackageInfo, options: Options) =
let let
bin = options.getCompilationBinary().get() bin = options.getCompilationBinary(pkgInfo).get()
binDotNim = bin.addFileExt("nim") binDotNim = bin.addFileExt("nim")
if bin == "": if bin == "":
raise newException(NimbleError, "You need to specify a file.") raise newException(NimbleError, "You need to specify a file.")
@ -1070,11 +1070,11 @@ proc test(options: Options) =
if options.continueTestsOnFailure: if options.continueTestsOnFailure:
inc tests inc tests
try: try:
execBackend(optsCopy) execBackend(pkgInfo, optsCopy)
except NimbleError: except NimbleError:
inc failures inc failures
else: else:
execBackend(optsCopy) execBackend(pkgInfo, optsCopy)
let let
existsAfter = existsFile(binFileName) existsAfter = existsFile(binFileName)
@ -1113,11 +1113,12 @@ proc check(options: Options) =
proc run(options: Options) = proc run(options: Options) =
# Verify parameters. # Verify parameters.
let binary = options.getCompilationBinary().get("") var pkgInfo = getPkgInfo(getCurrentDir(), options)
let binary = options.getCompilationBinary(pkgInfo).get("")
if binary.len == 0: if binary.len == 0:
raiseNimbleError("Please specify a binary to run") raiseNimbleError("Please specify a binary to run")
var pkgInfo = getPkgInfo(getCurrentDir(), options)
if binary notin pkgInfo.bin: if binary notin pkgInfo.bin:
raiseNimbleError( raiseNimbleError(
"Binary '$#' is not defined in '$#' package." % [binary, pkgInfo.name] "Binary '$#' is not defined in '$#' package." % [binary, pkgInfo.name]
@ -1176,7 +1177,8 @@ proc doAction(options: var Options) =
of actionRun: of actionRun:
run(options) run(options)
of actionCompile, actionDoc: of actionCompile, actionDoc:
execBackend(options) var pkgInfo = getPkgInfo(getCurrentDir(), options)
execBackend(pkgInfo, options)
of actionInit: of actionInit:
init(options) init(options)
of actionPublish: of actionPublish:

View file

@ -57,7 +57,7 @@ type
backend*: string backend*: string
compileOptions: seq[string] compileOptions: seq[string]
of actionRun: of actionRun:
runFile: string runFile: Option[string]
compileFlags: seq[string] compileFlags: seq[string]
runFlags*: seq[string] runFlags*: seq[string]
of actionCustom: of actionCustom:
@ -89,11 +89,11 @@ Commands:
uninstall [pkgname, ...] Uninstalls a list of packages. uninstall [pkgname, ...] Uninstalls a list of packages.
[-i, --inclDeps] Uninstall package and dependent package(s). [-i, --inclDeps] Uninstall package and dependent package(s).
build [opts, ...] [bin] Builds a package. build [opts, ...] [bin] Builds a package.
run [opts, ...] bin Builds and runs a package. run [opts, ...] [bin] Builds and runs a package.
A binary name needs Binary needs to be specified after any
to be specified after any compilation options, compilation options if there are several
any flags after the binary name are passed to binaries defined, any flags after the binary
the binary when it is run. or -- arg are passed to the binary when it is run.
c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options
to the Nim compiler. to the Nim compiler.
test Compiles and executes tests test Compiles and executes tests
@ -266,6 +266,12 @@ proc parseCommand*(key: string, result: var Options) =
result.action = Action(typ: parseActionType(key)) result.action = Action(typ: parseActionType(key))
initAction(result, key) initAction(result, key)
proc setRunOptions(result: var Options, key, val: string, isArg: bool) =
if result.action.runFile.isNone() and (isArg or val == "--"):
result.action.runFile = some(key)
else:
result.action.runFlags.add(val)
proc parseArgument*(key: string, result: var Options) = proc parseArgument*(key: string, result: var Options) =
case result.action.typ case result.action.typ
of actionNil: of actionNil:
@ -297,10 +303,7 @@ proc parseArgument*(key: string, result: var Options) =
of actionBuild: of actionBuild:
result.action.file = key result.action.file = key
of actionRun: of actionRun:
if result.action.runFile.len == 0: result.setRunOptions(key, key, true)
result.action.runFile = key
else:
result.action.runFlags.add(key)
of actionCustom: of actionCustom:
result.action.arguments.add(key) result.action.arguments.add(key)
else: else:
@ -371,7 +374,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
result.action.compileOptions.add(getFlagString(kind, flag, val)) result.action.compileOptions.add(getFlagString(kind, flag, val))
of actionRun: of actionRun:
result.showHelp = false result.showHelp = false
result.action.runFlags.add(getFlagString(kind, flag, val)) result.setRunOptions(flag, getFlagString(kind, flag, val), false)
of actionCustom: of actionCustom:
if result.action.command.normalize == "test": if result.action.command.normalize == "test":
if f == "continue" or f == "c": if f == "continue" or f == "c":
@ -441,7 +444,7 @@ proc parseCmdLine*(): Options =
else: else:
parseArgument(key, result) parseArgument(key, result)
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
parseFlag(key, val, result, kind) parseFlag(key, val, result, kind)
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
handleUnknownFlags(result) handleUnknownFlags(result)
@ -526,15 +529,23 @@ proc getCompilationFlags*(options: Options): seq[string] =
var opt = options var opt = options
return opt.getCompilationFlags() return opt.getCompilationFlags()
proc getCompilationBinary*(options: Options): Option[string] = proc getCompilationBinary*(options: Options, pkgInfo: PackageInfo): Option[string] =
case options.action.typ case options.action.typ
of actionBuild, actionDoc, actionCompile: of actionBuild, actionDoc, actionCompile:
let file = options.action.file.changeFileExt("") let file = options.action.file.changeFileExt("")
if file.len > 0: if file.len > 0:
return some(file) return some(file)
of actionRun: of actionRun:
let runFile = options.action.runFile.changeFileExt(ExeExt) let optRunFile = options.action.runFile
let runFile =
if optRunFile.get("").len > 0:
optRunFile.get()
elif pkgInfo.bin.len == 1:
pkgInfo.bin[0]
else:
""
if runFile.len > 0: if runFile.len > 0:
return some(runFile) return some(runFile.changeFileExt(ExeExt))
else: else:
discard discard

View file

@ -950,6 +950,32 @@ suite "nimble run":
[$DirSep, "run".changeFileExt(ExeExt)]) [$DirSep, "run".changeFileExt(ExeExt)])
check output.contains("""Testing `nimble run`: @["--debug", "check"]""") check output.contains("""Testing `nimble run`: @["--debug", "check"]""")
test "Parameters not passed to single executable":
cd "run":
var (output, exitCode) = execNimble(
"--debug", # Flag to enable debug verbosity in Nimble
"run", # Run command invokation
"--debug" # First argument passed to the executed command
)
check exitCode == QuitSuccess
check output.contains("tests$1run$1$2 --debug" %
[$DirSep, "run".changeFileExt(ExeExt)])
check output.contains("""Testing `nimble run`: @["--debug"]""")
test "Parameters passed to single executable":
cd "run":
var (output, exitCode) = execNimble(
"--debug", # Flag to enable debug verbosity in Nimble
"run", # Run command invokation
"--", # Flag to set run file to "" before next argument
"--debug", # First argument passed to the executed command
"check" # Second argument passed to the executed command.
)
check exitCode == QuitSuccess
check output.contains("tests$1run$1$2 --debug check" %
[$DirSep, "run".changeFileExt(ExeExt)])
check output.contains("""Testing `nimble run`: @["--debug", "check"]""")
test "NimbleVersion is defined": test "NimbleVersion is defined":
cd "nimbleVersionDefine": cd "nimbleVersionDefine":
var (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim") var (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim")