From 5e82ffc8d560dd3b2b7bbda034e5436fb036c20d Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sat, 9 Jul 2016 14:26:00 +0200 Subject: [PATCH] Nimscript: added support for 'patchFile' --- compiler/options.nim | 10 ++++++++++ compiler/scriptconfig.nim | 8 +++++++- lib/system/nimscript.nim | 15 +++++++++++++++ tests/newconfig/mymath.nim | 4 ++++ tests/newconfig/tfoo.nim | 6 ++++-- tests/newconfig/tfoo.nims | 2 ++ web/news/version_0_15_released.rst | 10 ++++++++++ 7 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 tests/newconfig/mymath.nim diff --git a/compiler/options.nim b/compiler/options.nim index 7797a4c824..fca945393f 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -151,6 +151,7 @@ const var gConfigVars* = newStringTable(modeStyleInsensitive) gDllOverrides = newStringTable(modeCaseInsensitive) + gModuleOverrides* = newStringTable(modeStyleInsensitive) gPrefixDir* = "" # Overrides the default prefix dir in getPrefixDir proc. libpath* = "" gProjectName* = "" # holds a name like 'nim' @@ -374,6 +375,13 @@ proc rawFindFile2(f: string): string = it = PStrEntry(it.next) result = "" +template patchModule() {.dirty.} = + if result.len > 0 and gModuleOverrides.len > 0: + let key = getPackageName(result) & "_" & splitFile(result).name + if gModuleOverrides.hasKey(key): + let ov = gModuleOverrides[key] + if ov.len > 0: result = ov + proc findFile*(f: string): string {.procvar.} = if f.isAbsolute: result = if f.existsFile: f else: "" @@ -385,6 +393,7 @@ proc findFile*(f: string): string {.procvar.} = result = f.rawFindFile2 if result.len == 0: result = f.toLower.rawFindFile2 + patchModule() proc findModule*(modulename, currentModule: string): string = # returns path to module @@ -403,6 +412,7 @@ proc findModule*(modulename, currentModule: string): string = result = currentPath / m if not existsFile(result): result = findFile(m) + patchModule() proc libCandidates*(s: string, dest: var seq[string]) = var le = strutils.find(s, '(') diff --git a/compiler/scriptconfig.nim b/compiler/scriptconfig.nim index ca42cc8fa7..dcb92227d1 100644 --- a/compiler/scriptconfig.nim +++ b/compiler/scriptconfig.nim @@ -13,7 +13,7 @@ import ast, modules, passes, passaux, condsyms, options, nimconf, lists, sem, semdata, llstream, vm, vmdef, commands, msgs, - os, times, osproc, wordrecg + os, times, osproc, wordrecg, strtabs # we support 'cmpIgnoreStyle' natively for efficiency: from strutils import cmpIgnoreStyle @@ -122,6 +122,12 @@ proc setupVM*(module: PSym; scriptName: string): PEvalContext = cbconf warningImpl: processSpecificNote(a.getString 0, wWarning, passPP, unknownLineInfo(), a.getString 1) + cbconf patchFile: + let key = a.getString(0) & "_" & a.getString(1) + var val = a.getString(2).addFileExt(NimExt) + if not isAbsolute(val): + val = vthisDir / val + gModuleOverrides[key] = val proc runNimScript*(scriptName: string; freshDefines=true) = passes.gIncludeFile = includeModule diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index db6d72d7bb..cc96bcba88 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -65,6 +65,21 @@ proc hint*(name: string; val: bool) = let v = if val: "on" else: "off" hintImpl(name & "]:" & v, "hint[" & name & "]:" & v) +proc patchFile*(package, filename, replacement: string) = + ## Overrides the location of a given file belonging to the + ## passed package. + ## If the ``replacement`` is not an absolute path, the path + ## is interpreted to be local to the Nimscript file that contains + ## the call to ``patchFile``, Nim's ``--path`` is not used at all + ## to resolve the filename! + ## + ## Example: + ## + ## .. code-block:: nim + ## + ## patchFile("stdlib", "asyncdispatch", "patches/replacement") + discard + proc getCommand*(): string = ## Gets the Nim command that the compiler has been invoked with, for example ## "c", "js", "build", "help". diff --git a/tests/newconfig/mymath.nim b/tests/newconfig/mymath.nim new file mode 100644 index 0000000000..5668b448be --- /dev/null +++ b/tests/newconfig/mymath.nim @@ -0,0 +1,4 @@ + + +proc ln*(x: float): float = + return 0.5 diff --git a/tests/newconfig/tfoo.nim b/tests/newconfig/tfoo.nim index d593d4a756..2e10167b12 100644 --- a/tests/newconfig/tfoo.nim +++ b/tests/newconfig/tfoo.nim @@ -1,10 +1,12 @@ discard """ cmd: "nim default $file" - output: '''hello world!''' + output: '''hello world! 0.5''' msg: '''[NimScript] exec: gcc -v''' """ when not defined(definedefine): {.fatal: "wrong nim script configuration".} -echo "hello world!" +import math + +echo "hello world! ", ln 2.0 diff --git a/tests/newconfig/tfoo.nims b/tests/newconfig/tfoo.nims index 8d1461c78e..f87aba619e 100644 --- a/tests/newconfig/tfoo.nims +++ b/tests/newconfig/tfoo.nims @@ -11,6 +11,8 @@ import ospaths warning("uninit", off) hint("processing", off) +patchFile("stdlib", "math", "mymath") + task listDirs, "lists every subdirectory": for x in listDirs("."): echo "DIR ", x diff --git a/web/news/version_0_15_released.rst b/web/news/version_0_15_released.rst index e940a2da8e..0dfde1ce29 100644 --- a/web/news/version_0_15_released.rst +++ b/web/news/version_0_15_released.rst @@ -49,6 +49,16 @@ Compiler Additions - The ``-d/--define`` flag can now optionally take a value to be used by code at compile time. +Nimscript Additions +------------------- + +- Finally it's possible to dis/enable specific hints and warnings in + Nimscript via the procs ``warning`` and ``hint``. +- Nimscript exports a proc named ``patchFile`` which can be used to + patch modules or include files for different Nimble packages, including + the ``stdlib`` package. + + Language Additions ------------------