From 4dafb33e8850f0f17c71ab6d39fe24892e3b7974 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 08:55:37 -0500 Subject: [PATCH 01/11] Add "example" action stub --- src/nimble.nim | 2 ++ src/nimblepkg/options.nim | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nimble.nim b/src/nimble.nim index 2d66714a6..2f47d4e0e 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -1976,6 +1976,8 @@ proc doAction(options: var Options) = clean(options) of actionRun: run(options) + of actionExample: + echo "TODO" of actionCompile, actionDoc: var pkgInfo = getPkgInfo(getCurrentDir(), options) execBackend(pkgInfo, options) diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index 449a8fc44..a67edf60d 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -55,7 +55,8 @@ type actionInstall, actionSearch, actionList, actionBuild, actionPath, actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks, actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup, - actionClean, actionDeps + actionClean, actionDeps, + actionExample DevelopActionType* = enum datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude @@ -103,6 +104,8 @@ type custRunFlags*: seq[string] of actionDeps: format*: string + of actionExample: + discard const help* = """ @@ -266,6 +269,8 @@ proc parseActionType*(action: string): ActionType = result = actionCompile of "doc", "doc2": result = actionDoc + of "example": + result = actionExample of "init": result = actionInit of "dump": From 0c495d3b9053cefba418ff4b4b9bf873d1a28ef6 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 10:38:05 -0500 Subject: [PATCH 02/11] Add basic infrastructure for --example flag --- src/nimble.nim | 10 ++++++---- src/nimblepkg/nimscriptapi.nim | 2 ++ src/nimblepkg/options.nim | 11 +++++------ src/nimblepkg/packageinfo.nim | 1 + src/nimblepkg/packageinfotypes.nim | 2 ++ src/nimblepkg/packageparser.nim | 1 + 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 2f47d4e0e..ed278a6ef 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -1918,8 +1918,12 @@ proc run(options: Options) = if binary.len == 0: raise nimbleError("Please specify a binary to run") - if binary notin pkgInfo.bin: - raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) + if not options.action.runExample: + if binary notin pkgInfo.bin: + raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) + else: + if binary notin pkgInfo.exampleBin: + raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) if pkgInfo.isLink: # If this is not installed package then build the binary. @@ -1976,8 +1980,6 @@ proc doAction(options: var Options) = clean(options) of actionRun: run(options) - of actionExample: - echo "TODO" of actionCompile, actionDoc: var pkgInfo = getPkgInfo(getCurrentDir(), options) execBackend(pkgInfo, options) diff --git a/src/nimblepkg/nimscriptapi.nim b/src/nimblepkg/nimscriptapi.nim index a8adcb95e..01cb2e400 100644 --- a/src/nimblepkg/nimscriptapi.nim +++ b/src/nimblepkg/nimscriptapi.nim @@ -24,6 +24,7 @@ var srcDir*: string ## The package's source directory. binDir*: string ## The package's binary directory. backend*: string ## The package's backend. + examplesDir*: string ## The package's examples directory. skipDirs*, skipFiles*, skipExt*, installDirs*, installFiles*, installExt*, bin*: seq[string] = @[] ## Nimble metadata. @@ -126,6 +127,7 @@ proc printPkgInfo(): string = printIfLen srcDir printIfLen binDir printIfLen backend + printIfLen examplesDir printSeqIfLen skipDirs printSeqIfLen skipFiles diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index a67edf60d..b7f4e7987 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -55,8 +55,8 @@ type actionInstall, actionSearch, actionList, actionBuild, actionPath, actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks, actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup, - actionClean, actionDeps, - actionExample + actionClean, actionDeps + DevelopActionType* = enum datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude @@ -97,6 +97,7 @@ type runFile: Option[string] compileFlags: seq[string] runFlags*: seq[string] + runExample*: bool of actionCustom: command*: string arguments*: seq[string] @@ -104,8 +105,6 @@ type custRunFlags*: seq[string] of actionDeps: format*: string - of actionExample: - discard const help* = """ @@ -269,8 +268,6 @@ proc parseActionType*(action: string): ActionType = result = actionCompile of "doc", "doc2": result = actionDoc - of "example": - result = actionExample of "init": result = actionInit of "dump": @@ -450,6 +447,8 @@ proc setRunOptions(result: var Options, key, val: string, isArg: bool) = result.action.compileFlags.add(val) else: result.action.runFlags.add(val) + if val == "--example": + result.action.runExample = true proc parseArgument*(key: string, result: var Options) = case result.action.typ diff --git a/src/nimblepkg/packageinfo.nim b/src/nimblepkg/packageinfo.nim index e2a299014..3fd522ae3 100644 --- a/src/nimblepkg/packageinfo.nim +++ b/src/nimblepkg/packageinfo.nim @@ -38,6 +38,7 @@ proc initPackageInfo*(options: Options, filePath: string): PackageInfo = result.basicInfo.name = fileName result.backend = "c" result.lockedDeps = options.lockFile(fileDir).getLockedDependencies() + result.examplesDir = "examples" proc toValidPackageName*(name: string): string = for c in name: diff --git a/src/nimblepkg/packageinfotypes.nim b/src/nimblepkg/packageinfotypes.nim index 482ab1c3a..4190a5f17 100644 --- a/src/nimblepkg/packageinfotypes.nim +++ b/src/nimblepkg/packageinfotypes.nim @@ -56,8 +56,10 @@ type installExt*: seq[string] requires*: seq[PkgTuple] bin*: Table[string, string] + exampleBin*: Table[string, string] binDir*: string srcDir*: string + examplesDir*: string backend*: string foreignDeps*: seq[string] basicInfo*: PackageBasicInfo diff --git a/src/nimblepkg/packageparser.nim b/src/nimblepkg/packageparser.nim index 8b051fe65..d76eb1c5d 100644 --- a/src/nimblepkg/packageparser.nim +++ b/src/nimblepkg/packageparser.nim @@ -238,6 +238,7 @@ proc readPackageInfoFromNimble(path: string; result: var PackageInfo) = of "license": result.license = ev.value of "srcdir": result.srcDir = ev.value of "bindir": result.binDir = ev.value + of "examplesdir": result.examplesDir = ev.value of "skipdirs": result.skipDirs.add(ev.value.multiSplit) of "skipfiles": From cad3532678f20a9fba2b0275dd3e27bedecbd9d0 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 11:35:10 -0500 Subject: [PATCH 03/11] Working "nimble run --example" --- src/nimble.nim | 44 ++++++++++++++++++------------ src/nimblepkg/options.nim | 3 -- src/nimblepkg/packageinfo.nim | 36 ++++++++++++++++++++---- src/nimblepkg/packageinfotypes.nim | 1 - 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index ed278a6ef..eda48e330 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -153,18 +153,28 @@ proc processFreeDependencies(pkgInfo: PackageInfo, options: Options): addRevDep(options.nimbleData, i, pkgInfo) proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], - args: seq[string], options: Options) = + args: seq[string], options: Options, + example = false) = ## Builds a package as specified by ``pkgInfo``. # Handle pre-`build` hook. let - realDir = pkgInfo.getRealDir() pkgDir = pkgInfo.myPath.parentDir() + realDir = + if example: + pkgInfo.getRealExamplesDir() + else: + pkgInfo.getRealDir() + bin = + if example: + pkgInfo.getExampleBin() + else: + pkgInfo.bin cd pkgDir: # Make sure `execHook` executes the correct .nimble file. if not execHook(options, actionBuild, true): raise nimbleError("Pre-hook prevented further execution.") - if pkgInfo.bin.len == 0: + if bin.len == 0: raise nimbleError( "Nothing to build. Did you specify a module to build using the" & " `bin` key in your .nimble file?") @@ -189,13 +199,13 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], options.getCompilationBinary(pkgInfo).get("") else: "" - for bin, src in pkgInfo.bin: + for bin, src in bin: # Check if this is the only binary that we want to build. if binToBuild.len != 0 and binToBuild != bin: if bin.extractFilename().changeFileExt("") != binToBuild: continue - let outputDir = pkgInfo.getOutputDir("") + let outputDir = pkgInfo.getOutputDir("", example) if dirExists(outputDir): if fileExists(outputDir / bin): if not pkgInfo.needsRebuild(outputDir / bin, realDir, options): @@ -206,7 +216,7 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], else: createDir(outputDir) - let outputOpt = "-o:" & pkgInfo.getOutputDir(bin).quoteShell + let outputOpt = "-o:" & pkgInfo.getOutputDir(bin, example).quoteShell display("Building", "$1/$2 using $3 backend" % [pkginfo.basicInfo.name, bin, pkgInfo.backend], priority = HighPriority) @@ -704,17 +714,17 @@ proc getDependenciesPaths(pkgInfo: PackageInfo, options: Options): let deps = pkgInfo.processAllDependencies(options) return deps.map(dep => dep.getRealDir()) -proc build(pkgInfo: PackageInfo, options: Options) = +proc build(pkgInfo: PackageInfo, options: Options, example=false) = ## Builds the package `pkgInfo`. nimScriptHint(pkgInfo) let paths = pkgInfo.getDependenciesPaths(options) var args = options.getCompilationFlags() - buildFromDir(pkgInfo, paths, args, options) + buildFromDir(pkgInfo, paths, args, options, example) -proc build(options: Options) = +proc build(options: Options, example=false) = let dir = getCurrentDir() let pkgInfo = getPkgInfo(dir, options) - pkgInfo.build(options) + pkgInfo.build(options, example) proc clean(options: Options) = let dir = getCurrentDir() @@ -1918,20 +1928,18 @@ proc run(options: Options) = if binary.len == 0: raise nimbleError("Please specify a binary to run") - if not options.action.runExample: - if binary notin pkgInfo.bin: - raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) - else: - if binary notin pkgInfo.exampleBin: - raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) + let example = "--example" in options.action.runFlags + + if not example and binary notin pkgInfo.bin: + raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) if pkgInfo.isLink: # If this is not installed package then build the binary. - pkgInfo.build(options) + pkgInfo.build(options, example) elif options.getCompilationFlags.len > 0: displayWarning(ignoringCompilationFlagsMsg) - let binaryPath = pkgInfo.getOutputDir(binary) + let binaryPath = pkgInfo.getOutputDir(binary, example) let cmd = quoteShellCommand(binaryPath & options.action.runFlags) displayDebug("Executing", cmd) diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index b7f4e7987..8fa56ba22 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -97,7 +97,6 @@ type runFile: Option[string] compileFlags: seq[string] runFlags*: seq[string] - runExample*: bool of actionCustom: command*: string arguments*: seq[string] @@ -447,8 +446,6 @@ proc setRunOptions(result: var Options, key, val: string, isArg: bool) = result.action.compileFlags.add(val) else: result.action.runFlags.add(val) - if val == "--example": - result.action.runExample = true proc parseArgument*(key: string, result: var Options) = case result.action.typ diff --git a/src/nimblepkg/packageinfo.nim b/src/nimblepkg/packageinfo.nim index 3fd522ae3..f3bec549f 100644 --- a/src/nimblepkg/packageinfo.nim +++ b/src/nimblepkg/packageinfo.nim @@ -38,7 +38,6 @@ proc initPackageInfo*(options: Options, filePath: string): PackageInfo = result.basicInfo.name = fileName result.backend = "c" result.lockedDeps = options.lockFile(fileDir).getLockedDependencies() - result.examplesDir = "examples" proc toValidPackageName*(name: string): string = for c in name: @@ -368,15 +367,28 @@ proc getRealDir*(pkgInfo: PackageInfo): string = else: result = pkgInfo.getNimbleFileDir() -proc getOutputDir*(pkgInfo: PackageInfo, bin: string): string = +proc getOutputDir*(pkgInfo: PackageInfo, bin: string, example = false): string = ## Returns a binary output dir for the package. - if pkgInfo.binDir != "": - result = pkgInfo.getNimbleFileDir() / pkgInfo.binDir / bin + if example and pkgInfo.examplesDir != "": + if pkgInfo.binDir != "": + result = pkgInfo.getNimbleFileDir() / pkgInfo.binDir / pkgInfo.examplesDir / bin + else: + result = pkgInfo.mypath.splitFile.dir / pkgInfo.examplesDir / bin else: - result = pkgInfo.mypath.splitFile.dir / bin + if pkgInfo.binDir != "": + result = pkgInfo.getNimbleFileDir() / pkgInfo.binDir / bin + else: + result = pkgInfo.mypath.splitFile.dir / bin if bin.len != 0 and dirExists(result): result &= ".out" +proc getRealExamplesDir*(pkgInfo: PackageInfo): string = + ## Returns the directory containing the example source files. + ## If no example directory was specified in the package info, empty string + ## is returned. + if pkgInfo.examplesDir != "" and (not pkgInfo.isInstalled or pkgInfo.isLink): + result = pkgInfo.getNimbleFileDir() / pkgInfo.examplesDir + proc echoPackage*(pkg: Package) = echo(pkg.name & ":") if pkg.alias.len > 0: @@ -531,6 +543,20 @@ proc hash*(x: PackageInfo): Hash = proc getNameAndVersion*(pkgInfo: PackageInfo): string = &"{pkgInfo.basicInfo.name}@{pkgInfo.basicInfo.version}" +proc getExampleBin*(pkgInfo: PackageInfo): Table[string,string] = + let examplesDir = pkgInfo.getRealExamplesDir() + if examplesDir == "": + raise nimbleError("Cannot find example files", hint="Was 'examplesDir' defined in '$1' ?" % pkgInfo.myPath) + + if not examplesDir.dirExists(): + raise nimbleError("Examples directory not found: $1" % examplesDir) + + for kind, path in walkDir(examplesDir): + if kind in {pcFile, pcLinkToFile}: + let (_, name, ext) = path.splitFile() + if ext == ".nim": + result[name] = name + when isMainModule: import unittest diff --git a/src/nimblepkg/packageinfotypes.nim b/src/nimblepkg/packageinfotypes.nim index 4190a5f17..ed4c99e48 100644 --- a/src/nimblepkg/packageinfotypes.nim +++ b/src/nimblepkg/packageinfotypes.nim @@ -56,7 +56,6 @@ type installExt*: seq[string] requires*: seq[PkgTuple] bin*: Table[string, string] - exampleBin*: Table[string, string] binDir*: string srcDir*: string examplesDir*: string From a7d80c032301e1c9ed1e9100ecd1821c0e99d191 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 12:58:32 -0500 Subject: [PATCH 04/11] Make examples run without nim.cfg --- src/nimble.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/nimble.nim b/src/nimble.nim index eda48e330..cc1448088 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -191,6 +191,8 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], if options.verbosity == SilentPriority: # Hide Nim warnings args.add("--warnings:off") + if example: + args.add("--path:" & pkgInfo.srcDir.quoteShell) let binToBuild = # Only build binaries specified by user if any, but only if top-level package, From 3593205b61c4d31e6a7f7f3aaf2e265ab881da6e Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 12:59:04 -0500 Subject: [PATCH 05/11] Change getRealExamplesDir to return default example directory name --- src/nimblepkg/packageinfo.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nimblepkg/packageinfo.nim b/src/nimblepkg/packageinfo.nim index f3bec549f..0fbc38265 100644 --- a/src/nimblepkg/packageinfo.nim +++ b/src/nimblepkg/packageinfo.nim @@ -384,10 +384,12 @@ proc getOutputDir*(pkgInfo: PackageInfo, bin: string, example = false): string = proc getRealExamplesDir*(pkgInfo: PackageInfo): string = ## Returns the directory containing the example source files. - ## If no example directory was specified in the package info, empty string + ## If no example directory was specified in the package info, "examples" ## is returned. if pkgInfo.examplesDir != "" and (not pkgInfo.isInstalled or pkgInfo.isLink): result = pkgInfo.getNimbleFileDir() / pkgInfo.examplesDir + elif not pkgInfo.isInstalled or pkgInfo.isLink: + result = "examples" proc echoPackage*(pkg: Package) = echo(pkg.name & ":") From 91ba2e76e553425032ff62f68ff2ab710e749542 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 12:59:24 -0500 Subject: [PATCH 06/11] Add example to library and hybrid init procedures --- src/nimblepkg/init.nim | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/nimblepkg/init.nim b/src/nimblepkg/init.nim index a3b5b6538..78dc1054f 100644 --- a/src/nimblepkg/init.nim +++ b/src/nimblepkg/init.nim @@ -1,4 +1,4 @@ -import os, strutils +import os, strutils, std/options import ./cli, ./tools @@ -20,6 +20,36 @@ proc writeExampleIfNonExistent(file: string, content: string) = display("Info:", "File " & file & " already exists, did not write " & "example code", priority = HighPriority) +proc exampleCode(info: PkgInitInfo): Option[string] = + case info.pkgType + of "library": + result = some(""" +# You may create as many example files as you want. Example file names should +# be valid Nim module names. +# +# To run this example, simply execute `nimble run example1 --example`. + +import $1 + +echo "One plus one equals ", $$add(1, 1) + +""" % info.pkgName) + + of "hybrid": + result = some(""" +# You may create as many example files as you want. Example file names should +# be valid Nim module names. +# +# To run this example, simply execute `nimble run example1 --example`. + +import $1pkg/submodule + +echo getWelcomeMessage() + +""" % info.pkgName) + else: + discard + proc createPkgStructure*(info: PkgInitInfo, pkgRoot: string) = # Create source directory createDirD(pkgRoot / info.pkgSrcDir) @@ -154,6 +184,17 @@ test "correct welcome": else: assert false, "Invalid package type specified." + # Create examples directory and dummy example + let pkgExamplesDir = "examples" + let pkgExampleCode = exampleCode(info) + if pkgExampleCode.isSome(): + let pkgExampleCode = pkgExampleCode.get("") + let pkgExamplesPath = pkgRoot / pkgExamplesDir + + createDirD(pkgExamplesPath) + nimbleFileOptions.add("# examplesDir = $1 # Uncomment to change the name of the examples directory\n" % pkgExamplesDir.escape()) + writeExampleIfNonExistent(pkgExamplesPath / "example1".addFileExt(".nim"), pkgExampleCode) + # Write the nimble file let nimbleFile = pkgRoot / info.pkgName.changeFileExt("nimble") writeFile(nimbleFile, """# Package From 23327583b7ed98bf6b5875a8e2007ca576336cc4 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 13:00:08 -0500 Subject: [PATCH 07/11] Version bump to 0.15.0 --- nimble.nimble | 2 +- src/nimblepkg/common.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nimble.nimble b/nimble.nimble index d56edcf57..23bcb7b1a 100644 --- a/nimble.nimble +++ b/nimble.nimble @@ -1,6 +1,6 @@ # Package -version = "0.14.0" +version = "0.15.0" author = "Dominik Picheta" description = "Nim package manager." license = "BSD" diff --git a/src/nimblepkg/common.nim b/src/nimblepkg/common.nim index 4550d61b4..ca26c0373 100644 --- a/src/nimblepkg/common.nim +++ b/src/nimblepkg/common.nim @@ -22,7 +22,7 @@ type ProcessOutput* = tuple[output: string, exitCode: int] const - nimbleVersion* = "0.14.0" + nimbleVersion* = "0.15.0" nimblePackagesDirName* = "pkgs2" nimblePackagesLinksDirName* ="links" nimbleBinariesDirName* = "bin" From 33cb3b9867751050bf6998b18bd451992e08cd0a Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 13:28:46 -0500 Subject: [PATCH 08/11] Move "--example" to options flag --- src/nimble.nim | 29 +++++++++++++---------------- src/nimblepkg/init.nim | 4 ++-- src/nimblepkg/options.nim | 2 ++ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index cc1448088..93f7f461b 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -153,19 +153,18 @@ proc processFreeDependencies(pkgInfo: PackageInfo, options: Options): addRevDep(options.nimbleData, i, pkgInfo) proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], - args: seq[string], options: Options, - example = false) = + args: seq[string], options: Options) = ## Builds a package as specified by ``pkgInfo``. # Handle pre-`build` hook. let pkgDir = pkgInfo.myPath.parentDir() realDir = - if example: + if options.example: pkgInfo.getRealExamplesDir() else: pkgInfo.getRealDir() bin = - if example: + if options.example: pkgInfo.getExampleBin() else: pkgInfo.bin @@ -191,7 +190,7 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], if options.verbosity == SilentPriority: # Hide Nim warnings args.add("--warnings:off") - if example: + if options.example: args.add("--path:" & pkgInfo.srcDir.quoteShell) let binToBuild = @@ -207,7 +206,7 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], if bin.extractFilename().changeFileExt("") != binToBuild: continue - let outputDir = pkgInfo.getOutputDir("", example) + let outputDir = pkgInfo.getOutputDir("", options.example) if dirExists(outputDir): if fileExists(outputDir / bin): if not pkgInfo.needsRebuild(outputDir / bin, realDir, options): @@ -218,7 +217,7 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string], else: createDir(outputDir) - let outputOpt = "-o:" & pkgInfo.getOutputDir(bin, example).quoteShell + let outputOpt = "-o:" & pkgInfo.getOutputDir(bin, options.example).quoteShell display("Building", "$1/$2 using $3 backend" % [pkginfo.basicInfo.name, bin, pkgInfo.backend], priority = HighPriority) @@ -716,17 +715,17 @@ proc getDependenciesPaths(pkgInfo: PackageInfo, options: Options): let deps = pkgInfo.processAllDependencies(options) return deps.map(dep => dep.getRealDir()) -proc build(pkgInfo: PackageInfo, options: Options, example=false) = +proc build(pkgInfo: PackageInfo, options: Options) = ## Builds the package `pkgInfo`. nimScriptHint(pkgInfo) let paths = pkgInfo.getDependenciesPaths(options) var args = options.getCompilationFlags() - buildFromDir(pkgInfo, paths, args, options, example) + buildFromDir(pkgInfo, paths, args, options) -proc build(options: Options, example=false) = +proc build(options: Options) = let dir = getCurrentDir() let pkgInfo = getPkgInfo(dir, options) - pkgInfo.build(options, example) + pkgInfo.build(options) proc clean(options: Options) = let dir = getCurrentDir() @@ -1930,18 +1929,16 @@ proc run(options: Options) = if binary.len == 0: raise nimbleError("Please specify a binary to run") - let example = "--example" in options.action.runFlags - - if not example and binary notin pkgInfo.bin: + if not options.example and binary notin pkgInfo.bin: raise nimbleError(binaryNotDefinedInPkgMsg(binary, pkgInfo.basicInfo.name)) if pkgInfo.isLink: # If this is not installed package then build the binary. - pkgInfo.build(options, example) + pkgInfo.build(options) elif options.getCompilationFlags.len > 0: displayWarning(ignoringCompilationFlagsMsg) - let binaryPath = pkgInfo.getOutputDir(binary, example) + let binaryPath = pkgInfo.getOutputDir(binary, options.example) let cmd = quoteShellCommand(binaryPath & options.action.runFlags) displayDebug("Executing", cmd) diff --git a/src/nimblepkg/init.nim b/src/nimblepkg/init.nim index 78dc1054f..0ea84d63a 100644 --- a/src/nimblepkg/init.nim +++ b/src/nimblepkg/init.nim @@ -27,7 +27,7 @@ proc exampleCode(info: PkgInitInfo): Option[string] = # You may create as many example files as you want. Example file names should # be valid Nim module names. # -# To run this example, simply execute `nimble run example1 --example`. +# To run this example, simply execute `nimble --example run example1`. import $1 @@ -40,7 +40,7 @@ echo "One plus one equals ", $$add(1, 1) # You may create as many example files as you want. Example file names should # be valid Nim module names. # -# To run this example, simply execute `nimble run example1 --example`. +# To run this example, simply execute `nimble --example run example1`. import $1pkg/submodule diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index 8fa56ba22..1222e464c 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -31,6 +31,7 @@ type showVersion*: bool offline*: bool noColor*: bool + example*: bool disableValidation*: bool continueTestsOnFailure*: bool ## Whether packages' repos should always be downloaded with their history. @@ -518,6 +519,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) = of "tarballs", "t": result.enableTarballs = true of "package", "p": result.package = val of "lock-file": result.lockFileName = val + of "example": result.example = true else: isGlobalFlag = false var wasFlagHandled = true From 2fc057b26ed4b285cb052aff5041ec42769ac9b7 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 13:32:18 -0500 Subject: [PATCH 09/11] Tweak example code comments, add --example to help message --- src/nimblepkg/init.nim | 4 ++-- src/nimblepkg/options.nim | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nimblepkg/init.nim b/src/nimblepkg/init.nim index 0ea84d63a..4c6531155 100644 --- a/src/nimblepkg/init.nim +++ b/src/nimblepkg/init.nim @@ -27,7 +27,7 @@ proc exampleCode(info: PkgInitInfo): Option[string] = # You may create as many example files as you want. Example file names should # be valid Nim module names. # -# To run this example, simply execute `nimble --example run example1`. +# To run this example, simply execute `nimble run --example example1`. import $1 @@ -40,7 +40,7 @@ echo "One plus one equals ", $$add(1, 1) # You may create as many example files as you want. Example file names should # be valid Nim module names. # -# To run this example, simply execute `nimble --example run example1`. +# To run this example, simply execute `nimble run --example example1`. import $1pkg/submodule diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index 1222e464c..fdf2385b0 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -225,6 +225,7 @@ Nimble Options: --silent Hide all Nimble and Nim output --verbose Show all non-debug output. --debug Show all output including debug messages. + --example Build/run an example instead of a package. --offline Don't use network. --noColor Don't colorise output. --noSSLCheck Don't check SSL certificates. From 8e49773b960f26b0cbcf8973c4110c259aab31a1 Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 14:26:26 -0500 Subject: [PATCH 10/11] Make execBackend check for example option --- src/nimble.nim | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/nimble.nim b/src/nimble.nim index 93f7f461b..8117bd9fa 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -735,7 +735,11 @@ proc clean(options: Options) = proc execBackend(pkgInfo: PackageInfo, options: Options) = let - bin = options.getCompilationBinary(pkgInfo).get("") + bin = + if options.example: + pkgInfo.getRealExamplesDir() / options.getCompilationBinary(pkgInfo).get("") + else: + options.getCompilationBinary(pkgInfo).get("") binDotNim = bin.addFileExt("nim") if bin == "": @@ -761,6 +765,8 @@ proc execBackend(pkgInfo: PackageInfo, options: Options) = if options.verbosity == SilentPriority: # Hide Nim warnings args.add("--warnings:off") + if options.example: + args.add("--path:" & pkgInfo.srcDir.quoteShell) for option in options.getCompilationFlags(): args.add(option.quoteShell) From 47da3eeea05737b9b1c89f367428ef194a46da8f Mon Sep 17 00:00:00 2001 From: Evan Grove Date: Wed, 21 Sep 2022 14:43:53 -0500 Subject: [PATCH 11/11] Revert "Version bump to 0.15.0" This reverts commit 23327583b7ed98bf6b5875a8e2007ca576336cc4. --- nimble.nimble | 2 +- src/nimblepkg/common.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nimble.nimble b/nimble.nimble index 23bcb7b1a..d56edcf57 100644 --- a/nimble.nimble +++ b/nimble.nimble @@ -1,6 +1,6 @@ # Package -version = "0.15.0" +version = "0.14.0" author = "Dominik Picheta" description = "Nim package manager." license = "BSD" diff --git a/src/nimblepkg/common.nim b/src/nimblepkg/common.nim index ca26c0373..4550d61b4 100644 --- a/src/nimblepkg/common.nim +++ b/src/nimblepkg/common.nim @@ -22,7 +22,7 @@ type ProcessOutput* = tuple[output: string, exitCode: int] const - nimbleVersion* = "0.15.0" + nimbleVersion* = "0.14.0" nimblePackagesDirName* = "pkgs2" nimblePackagesLinksDirName* ="links" nimbleBinariesDirName* = "bin"