From 9391fbc56d9e4d3080c10583268f243b8b78edd5 Mon Sep 17 00:00:00 2001 From: inv2004 Date: Sat, 18 Jan 2020 01:15:34 +0300 Subject: [PATCH] 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 --- src/nimble.nim | 18 +++++++++-------- src/nimblepkg/options.nim | 41 +++++++++++++++++++++++++-------------- tests/tester.nim | 26 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 412c987..6d233f6 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -221,7 +221,7 @@ proc buildFromDir( options: Options ) = ## Builds a package as specified by ``pkgInfo``. - let binToBuild = options.getCompilationBinary() + let binToBuild = options.getCompilationBinary(pkgInfo) # Handle pre-`build` hook. let realDir = pkgInfo.getRealDir() cd realDir: # Make sure `execHook` executes the correct .nimble file. @@ -535,9 +535,9 @@ proc build(options: Options) = var args = options.getCompilationFlags() buildFromDir(pkgInfo, paths, args, options) -proc execBackend(options: Options) = +proc execBackend(pkgInfo: PackageInfo, options: Options) = let - bin = options.getCompilationBinary().get() + bin = options.getCompilationBinary(pkgInfo).get() binDotNim = bin.addFileExt("nim") if bin == "": raise newException(NimbleError, "You need to specify a file.") @@ -1070,11 +1070,11 @@ proc test(options: Options) = if options.continueTestsOnFailure: inc tests try: - execBackend(optsCopy) + execBackend(pkgInfo, optsCopy) except NimbleError: inc failures else: - execBackend(optsCopy) + execBackend(pkgInfo, optsCopy) let existsAfter = existsFile(binFileName) @@ -1113,11 +1113,12 @@ proc check(options: Options) = proc run(options: Options) = # Verify parameters. - let binary = options.getCompilationBinary().get("") + var pkgInfo = getPkgInfo(getCurrentDir(), options) + + let binary = options.getCompilationBinary(pkgInfo).get("") if binary.len == 0: raiseNimbleError("Please specify a binary to run") - var pkgInfo = getPkgInfo(getCurrentDir(), options) if binary notin pkgInfo.bin: raiseNimbleError( "Binary '$#' is not defined in '$#' package." % [binary, pkgInfo.name] @@ -1176,7 +1177,8 @@ proc doAction(options: var Options) = of actionRun: run(options) of actionCompile, actionDoc: - execBackend(options) + var pkgInfo = getPkgInfo(getCurrentDir(), options) + execBackend(pkgInfo, options) of actionInit: init(options) of actionPublish: diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index a338978..14d1c31 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -57,7 +57,7 @@ type backend*: string compileOptions: seq[string] of actionRun: - runFile: string + runFile: Option[string] compileFlags: seq[string] runFlags*: seq[string] of actionCustom: @@ -89,11 +89,11 @@ Commands: uninstall [pkgname, ...] Uninstalls a list of packages. [-i, --inclDeps] Uninstall package and dependent package(s). build [opts, ...] [bin] Builds a package. - run [opts, ...] bin Builds and runs a package. - A binary name needs - to be specified after any compilation options, - any flags after the binary name are passed to - the binary when it is run. + run [opts, ...] [bin] Builds and runs a package. + Binary needs to be specified after any + compilation options if there are several + binaries defined, any flags after the binary + 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 to the Nim compiler. test Compiles and executes tests @@ -266,6 +266,12 @@ proc parseCommand*(key: string, result: var Options) = result.action = Action(typ: parseActionType(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) = case result.action.typ of actionNil: @@ -297,10 +303,7 @@ proc parseArgument*(key: string, result: var Options) = of actionBuild: result.action.file = key of actionRun: - if result.action.runFile.len == 0: - result.action.runFile = key - else: - result.action.runFlags.add(key) + result.setRunOptions(key, key, true) of actionCustom: result.action.arguments.add(key) else: @@ -371,7 +374,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) = result.action.compileOptions.add(getFlagString(kind, flag, val)) of actionRun: result.showHelp = false - result.action.runFlags.add(getFlagString(kind, flag, val)) + result.setRunOptions(flag, getFlagString(kind, flag, val), false) of actionCustom: if result.action.command.normalize == "test": if f == "continue" or f == "c": @@ -441,7 +444,7 @@ proc parseCmdLine*(): Options = else: parseArgument(key, result) of cmdLongOption, cmdShortOption: - parseFlag(key, val, result, kind) + parseFlag(key, val, result, kind) of cmdEnd: assert(false) # cannot happen handleUnknownFlags(result) @@ -526,15 +529,23 @@ proc getCompilationFlags*(options: Options): seq[string] = var opt = options return opt.getCompilationFlags() -proc getCompilationBinary*(options: Options): Option[string] = +proc getCompilationBinary*(options: Options, pkgInfo: PackageInfo): Option[string] = case options.action.typ of actionBuild, actionDoc, actionCompile: let file = options.action.file.changeFileExt("") if file.len > 0: return some(file) 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: - return some(runFile) + return some(runFile.changeFileExt(ExeExt)) else: discard diff --git a/tests/tester.nim b/tests/tester.nim index f969f66..c5d9925 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -950,6 +950,32 @@ suite "nimble run": [$DirSep, "run".changeFileExt(ExeExt)]) 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": cd "nimbleVersionDefine": var (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim")