Nimscript: added support for 'patchFile'

This commit is contained in:
Andreas Rumpf 2016-07-09 14:26:00 +02:00
commit 5e82ffc8d5
7 changed files with 52 additions and 3 deletions

View file

@ -151,6 +151,7 @@ const
var var
gConfigVars* = newStringTable(modeStyleInsensitive) gConfigVars* = newStringTable(modeStyleInsensitive)
gDllOverrides = newStringTable(modeCaseInsensitive) gDllOverrides = newStringTable(modeCaseInsensitive)
gModuleOverrides* = newStringTable(modeStyleInsensitive)
gPrefixDir* = "" # Overrides the default prefix dir in getPrefixDir proc. gPrefixDir* = "" # Overrides the default prefix dir in getPrefixDir proc.
libpath* = "" libpath* = ""
gProjectName* = "" # holds a name like 'nim' gProjectName* = "" # holds a name like 'nim'
@ -374,6 +375,13 @@ proc rawFindFile2(f: string): string =
it = PStrEntry(it.next) it = PStrEntry(it.next)
result = "" 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.} = proc findFile*(f: string): string {.procvar.} =
if f.isAbsolute: if f.isAbsolute:
result = if f.existsFile: f else: "" result = if f.existsFile: f else: ""
@ -385,6 +393,7 @@ proc findFile*(f: string): string {.procvar.} =
result = f.rawFindFile2 result = f.rawFindFile2
if result.len == 0: if result.len == 0:
result = f.toLower.rawFindFile2 result = f.toLower.rawFindFile2
patchModule()
proc findModule*(modulename, currentModule: string): string = proc findModule*(modulename, currentModule: string): string =
# returns path to module # returns path to module
@ -403,6 +412,7 @@ proc findModule*(modulename, currentModule: string): string =
result = currentPath / m result = currentPath / m
if not existsFile(result): if not existsFile(result):
result = findFile(m) result = findFile(m)
patchModule()
proc libCandidates*(s: string, dest: var seq[string]) = proc libCandidates*(s: string, dest: var seq[string]) =
var le = strutils.find(s, '(') var le = strutils.find(s, '(')

View file

@ -13,7 +13,7 @@
import import
ast, modules, passes, passaux, condsyms, ast, modules, passes, passaux, condsyms,
options, nimconf, lists, sem, semdata, llstream, vm, vmdef, commands, msgs, 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: # we support 'cmpIgnoreStyle' natively for efficiency:
from strutils import cmpIgnoreStyle from strutils import cmpIgnoreStyle
@ -122,6 +122,12 @@ proc setupVM*(module: PSym; scriptName: string): PEvalContext =
cbconf warningImpl: cbconf warningImpl:
processSpecificNote(a.getString 0, wWarning, passPP, unknownLineInfo(), processSpecificNote(a.getString 0, wWarning, passPP, unknownLineInfo(),
a.getString 1) 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) = proc runNimScript*(scriptName: string; freshDefines=true) =
passes.gIncludeFile = includeModule passes.gIncludeFile = includeModule

View file

@ -65,6 +65,21 @@ proc hint*(name: string; val: bool) =
let v = if val: "on" else: "off" let v = if val: "on" else: "off"
hintImpl(name & "]:" & v, "hint[" & name & "]:" & v) 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 = proc getCommand*(): string =
## Gets the Nim command that the compiler has been invoked with, for example ## Gets the Nim command that the compiler has been invoked with, for example
## "c", "js", "build", "help". ## "c", "js", "build", "help".

View file

@ -0,0 +1,4 @@
proc ln*(x: float): float =
return 0.5

View file

@ -1,10 +1,12 @@
discard """ discard """
cmd: "nim default $file" cmd: "nim default $file"
output: '''hello world!''' output: '''hello world! 0.5'''
msg: '''[NimScript] exec: gcc -v''' msg: '''[NimScript] exec: gcc -v'''
""" """
when not defined(definedefine): when not defined(definedefine):
{.fatal: "wrong nim script configuration".} {.fatal: "wrong nim script configuration".}
echo "hello world!" import math
echo "hello world! ", ln 2.0

View file

@ -11,6 +11,8 @@ import ospaths
warning("uninit", off) warning("uninit", off)
hint("processing", off) hint("processing", off)
patchFile("stdlib", "math", "mymath")
task listDirs, "lists every subdirectory": task listDirs, "lists every subdirectory":
for x in listDirs("."): for x in listDirs("."):
echo "DIR ", x echo "DIR ", x

View file

@ -49,6 +49,16 @@ Compiler Additions
- The ``-d/--define`` flag can now optionally take a value to be used - The ``-d/--define`` flag can now optionally take a value to be used
by code at compile time. 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 Language Additions
------------------ ------------------