Skip to content

Commit 9391fbc

Browse files
inv2004dom96
authored andcommitted
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
1 parent e9d45ca commit 9391fbc

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

src/nimble.nim

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ proc buildFromDir(
221221
options: Options
222222
) =
223223
## Builds a package as specified by ``pkgInfo``.
224-
let binToBuild = options.getCompilationBinary()
224+
let binToBuild = options.getCompilationBinary(pkgInfo)
225225
# Handle pre-`build` hook.
226226
let realDir = pkgInfo.getRealDir()
227227
cd realDir: # Make sure `execHook` executes the correct .nimble file.
@@ -535,9 +535,9 @@ proc build(options: Options) =
535535
var args = options.getCompilationFlags()
536536
buildFromDir(pkgInfo, paths, args, options)
537537

538-
proc execBackend(options: Options) =
538+
proc execBackend(pkgInfo: PackageInfo, options: Options) =
539539
let
540-
bin = options.getCompilationBinary().get()
540+
bin = options.getCompilationBinary(pkgInfo).get()
541541
binDotNim = bin.addFileExt("nim")
542542
if bin == "":
543543
raise newException(NimbleError, "You need to specify a file.")
@@ -1070,11 +1070,11 @@ proc test(options: Options) =
10701070
if options.continueTestsOnFailure:
10711071
inc tests
10721072
try:
1073-
execBackend(optsCopy)
1073+
execBackend(pkgInfo, optsCopy)
10741074
except NimbleError:
10751075
inc failures
10761076
else:
1077-
execBackend(optsCopy)
1077+
execBackend(pkgInfo, optsCopy)
10781078

10791079
let
10801080
existsAfter = existsFile(binFileName)
@@ -1113,11 +1113,12 @@ proc check(options: Options) =
11131113

11141114
proc run(options: Options) =
11151115
# Verify parameters.
1116-
let binary = options.getCompilationBinary().get("")
1116+
var pkgInfo = getPkgInfo(getCurrentDir(), options)
1117+
1118+
let binary = options.getCompilationBinary(pkgInfo).get("")
11171119
if binary.len == 0:
11181120
raiseNimbleError("Please specify a binary to run")
11191121

1120-
var pkgInfo = getPkgInfo(getCurrentDir(), options)
11211122
if binary notin pkgInfo.bin:
11221123
raiseNimbleError(
11231124
"Binary '$#' is not defined in '$#' package." % [binary, pkgInfo.name]
@@ -1176,7 +1177,8 @@ proc doAction(options: var Options) =
11761177
of actionRun:
11771178
run(options)
11781179
of actionCompile, actionDoc:
1179-
execBackend(options)
1180+
var pkgInfo = getPkgInfo(getCurrentDir(), options)
1181+
execBackend(pkgInfo, options)
11801182
of actionInit:
11811183
init(options)
11821184
of actionPublish:

src/nimblepkg/options.nim

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type
5757
backend*: string
5858
compileOptions: seq[string]
5959
of actionRun:
60-
runFile: string
60+
runFile: Option[string]
6161
compileFlags: seq[string]
6262
runFlags*: seq[string]
6363
of actionCustom:
@@ -89,11 +89,11 @@ Commands:
8989
uninstall [pkgname, ...] Uninstalls a list of packages.
9090
[-i, --inclDeps] Uninstall package and dependent package(s).
9191
build [opts, ...] [bin] Builds a package.
92-
run [opts, ...] bin Builds and runs a package.
93-
A binary name needs
94-
to be specified after any compilation options,
95-
any flags after the binary name are passed to
96-
the binary when it is run.
92+
run [opts, ...] [bin] Builds and runs a package.
93+
Binary needs to be specified after any
94+
compilation options if there are several
95+
binaries defined, any flags after the binary
96+
or -- arg are passed to the binary when it is run.
9797
c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options
9898
to the Nim compiler.
9999
test Compiles and executes tests
@@ -266,6 +266,12 @@ proc parseCommand*(key: string, result: var Options) =
266266
result.action = Action(typ: parseActionType(key))
267267
initAction(result, key)
268268

269+
proc setRunOptions(result: var Options, key, val: string, isArg: bool) =
270+
if result.action.runFile.isNone() and (isArg or val == "--"):
271+
result.action.runFile = some(key)
272+
else:
273+
result.action.runFlags.add(val)
274+
269275
proc parseArgument*(key: string, result: var Options) =
270276
case result.action.typ
271277
of actionNil:
@@ -297,10 +303,7 @@ proc parseArgument*(key: string, result: var Options) =
297303
of actionBuild:
298304
result.action.file = key
299305
of actionRun:
300-
if result.action.runFile.len == 0:
301-
result.action.runFile = key
302-
else:
303-
result.action.runFlags.add(key)
306+
result.setRunOptions(key, key, true)
304307
of actionCustom:
305308
result.action.arguments.add(key)
306309
else:
@@ -371,7 +374,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
371374
result.action.compileOptions.add(getFlagString(kind, flag, val))
372375
of actionRun:
373376
result.showHelp = false
374-
result.action.runFlags.add(getFlagString(kind, flag, val))
377+
result.setRunOptions(flag, getFlagString(kind, flag, val), false)
375378
of actionCustom:
376379
if result.action.command.normalize == "test":
377380
if f == "continue" or f == "c":
@@ -441,7 +444,7 @@ proc parseCmdLine*(): Options =
441444
else:
442445
parseArgument(key, result)
443446
of cmdLongOption, cmdShortOption:
444-
parseFlag(key, val, result, kind)
447+
parseFlag(key, val, result, kind)
445448
of cmdEnd: assert(false) # cannot happen
446449

447450
handleUnknownFlags(result)
@@ -526,15 +529,23 @@ proc getCompilationFlags*(options: Options): seq[string] =
526529
var opt = options
527530
return opt.getCompilationFlags()
528531

529-
proc getCompilationBinary*(options: Options): Option[string] =
532+
proc getCompilationBinary*(options: Options, pkgInfo: PackageInfo): Option[string] =
530533
case options.action.typ
531534
of actionBuild, actionDoc, actionCompile:
532535
let file = options.action.file.changeFileExt("")
533536
if file.len > 0:
534537
return some(file)
535538
of actionRun:
536-
let runFile = options.action.runFile.changeFileExt(ExeExt)
539+
let optRunFile = options.action.runFile
540+
let runFile =
541+
if optRunFile.get("").len > 0:
542+
optRunFile.get()
543+
elif pkgInfo.bin.len == 1:
544+
pkgInfo.bin[0]
545+
else:
546+
""
547+
537548
if runFile.len > 0:
538-
return some(runFile)
549+
return some(runFile.changeFileExt(ExeExt))
539550
else:
540551
discard

tests/tester.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,32 @@ suite "nimble run":
950950
[$DirSep, "run".changeFileExt(ExeExt)])
951951
check output.contains("""Testing `nimble run`: @["--debug", "check"]""")
952952

953+
test "Parameters not passed to single executable":
954+
cd "run":
955+
var (output, exitCode) = execNimble(
956+
"--debug", # Flag to enable debug verbosity in Nimble
957+
"run", # Run command invokation
958+
"--debug" # First argument passed to the executed command
959+
)
960+
check exitCode == QuitSuccess
961+
check output.contains("tests$1run$1$2 --debug" %
962+
[$DirSep, "run".changeFileExt(ExeExt)])
963+
check output.contains("""Testing `nimble run`: @["--debug"]""")
964+
965+
test "Parameters passed to single executable":
966+
cd "run":
967+
var (output, exitCode) = execNimble(
968+
"--debug", # Flag to enable debug verbosity in Nimble
969+
"run", # Run command invokation
970+
"--", # Flag to set run file to "" before next argument
971+
"--debug", # First argument passed to the executed command
972+
"check" # Second argument passed to the executed command.
973+
)
974+
check exitCode == QuitSuccess
975+
check output.contains("tests$1run$1$2 --debug check" %
976+
[$DirSep, "run".changeFileExt(ExeExt)])
977+
check output.contains("""Testing `nimble run`: @["--debug", "check"]""")
978+
953979
test "NimbleVersion is defined":
954980
cd "nimbleVersionDefine":
955981
var (output, exitCode) = execNimble("c", "-r", "src/nimbleVersionDefine.nim")

0 commit comments

Comments
 (0)