Nimscript: added support for 'patchFile'

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

View File

@@ -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, '(')

View File

@@ -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

View File

@@ -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".

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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
------------------