findFile speed improvement
This commit is contained in:
parent
dc222c6011
commit
e78fa71ed1
3 changed files with 52 additions and 29 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import macros, osproc, regex, strformat, strutils, tables
|
||||
import macros, osproc, strformat, strutils, tables
|
||||
|
||||
import os except findExe, sleep
|
||||
|
||||
|
|
@ -253,31 +253,50 @@ proc gitPull*(url: string, outdir = "", plist = "", checkout = "") =
|
|||
echo "# Pulling repository"
|
||||
discard execAction(&"cd {outdirQ} && git pull --depth=1 origin master")
|
||||
|
||||
proc findFile*(file: string|Regex, dir: string, recurse = true, first = false): string =
|
||||
proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string =
|
||||
## Find the file in the specified directory
|
||||
##
|
||||
## `file` can be a string or a regex object
|
||||
## `file` is a regular expression if `regex` is true
|
||||
##
|
||||
## Turn off recursive search with `recurse` and stop on first match with
|
||||
## `first`. Without it, the shortest match is returned.
|
||||
when file is Regex:
|
||||
var
|
||||
rm: RegexMatch
|
||||
var
|
||||
cmd =
|
||||
when defined(windows):
|
||||
"nimgrep --filenames --oneline --nocolor $1 $2 $3"
|
||||
else:
|
||||
"find $3 $1 -regextype egrep -regex $2"
|
||||
|
||||
recursive = ""
|
||||
|
||||
if recurse:
|
||||
when defined(windows):
|
||||
recursive = "--recursive"
|
||||
else:
|
||||
when not defined(windows):
|
||||
recursive = "-maxdepth 1"
|
||||
|
||||
if not regex:
|
||||
let
|
||||
dir = dir / file.parentDir()
|
||||
file = file.extractFilename
|
||||
|
||||
for f in walkDirRec(dir, yieldFilter = {pcFile, pcLinkToFile},
|
||||
followFilter = if recurse: {pcDir} else: {}):
|
||||
let
|
||||
fn = f.extractFilename()
|
||||
when file is string:
|
||||
if (result.len == 0 or result.len > f.len) and fn == file:
|
||||
result = f
|
||||
if first: break
|
||||
else:
|
||||
if (result.len == 0 or result.len > f.len) and fn.match(file, rm):
|
||||
cmd = cmd % [recursive, (".*[\\/]" & file & "$").quoteShell, dir.sanitizePath]
|
||||
|
||||
let
|
||||
(files, ret) = gorgeEx(cmd)
|
||||
if ret == 0:
|
||||
for line in files.splitLines():
|
||||
let f =
|
||||
when defined(windows):
|
||||
if ":" in line:
|
||||
line.split(":", maxsplit = 1)[0]
|
||||
else:
|
||||
""
|
||||
else:
|
||||
line
|
||||
|
||||
if (f.len != 0 and result.len == 0 or result.len > f.len):
|
||||
result = f
|
||||
if first: break
|
||||
|
||||
|
|
@ -442,7 +461,7 @@ proc cmake*(path, check, flags: string) =
|
|||
|
||||
doAssert (path / check).fileExists(), "# cmake failed"
|
||||
|
||||
proc make*(path, check: string|Regex, flags = "") =
|
||||
proc make*(path, check: string, flags = "", regex = false) =
|
||||
## Run the `make` command to build all binaries in the specified path
|
||||
##
|
||||
## `check` is a file that will be generated by the `make` command.
|
||||
|
|
@ -451,9 +470,11 @@ proc make*(path, check: string|Regex, flags = "") =
|
|||
##
|
||||
## `flags` are any flags that should be passed to the `make` command.
|
||||
##
|
||||
## `regex` can be set to true if `check` is a regular expression.
|
||||
##
|
||||
## If `make.exe` is missing and `mingw32-make.exe` is available, it will
|
||||
## be copied over to make.exe in the same location.
|
||||
if findFile(check, path).len != 0:
|
||||
if findFile(check, path, regex = regex).len != 0:
|
||||
return
|
||||
|
||||
echo "# Running make " & flags
|
||||
|
|
@ -474,7 +495,7 @@ proc make*(path, check: string|Regex, flags = "") =
|
|||
|
||||
echo execAction(cmd)
|
||||
|
||||
doAssert findFile(check, path).len != 0, "# make failed"
|
||||
doAssert findFile(check, path, regex = regex).len != 0, "# make failed"
|
||||
|
||||
proc getGccPaths*(mode = "c"): seq[string] =
|
||||
var
|
||||
|
|
@ -532,7 +553,7 @@ proc getStdPath(header: string): string =
|
|||
|
||||
proc getStdLibPath(lname: string): string =
|
||||
for lib in getGccLibPaths():
|
||||
result = findFile(re(lname), lib, recurse = false, first = true)
|
||||
result = findFile(lname, lib, recurse = false, first = true, regex = true)
|
||||
if result.len != 0:
|
||||
break
|
||||
|
||||
|
|
@ -596,7 +617,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
|
|||
conDepStr = ""
|
||||
cmakeDeps = false
|
||||
cmakeDepStr = ""
|
||||
lpath = findFile(re(lname), outdir)
|
||||
lpath = findFile(lname, outdir, regex = true)
|
||||
makeFlagsProc = &"-j {getNumProcs()} {makeFlags}"
|
||||
made = false
|
||||
makePath = outdir
|
||||
|
|
@ -643,7 +664,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
|
|||
conDepStr &= "bash executable missing"
|
||||
|
||||
if fileExists(makePath / "Makefile"):
|
||||
make(makePath, re(lname), makeFlagsProc)
|
||||
make(makePath, lname, makeFlagsProc, regex = true)
|
||||
made = true
|
||||
|
||||
var
|
||||
|
|
@ -656,7 +677,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string): strin
|
|||
error = "No build files found in " & outdir
|
||||
doAssert cmakeDeps or conDeps or made, &"\n# Build configuration failed - {error}\n"
|
||||
|
||||
result = findFile(re(lname), outdir)
|
||||
result = findFile(lname, outdir, regex = true)
|
||||
|
||||
proc getDynlibExt(): string =
|
||||
when defined(windows):
|
||||
|
|
|
|||
|
|
@ -380,9 +380,11 @@ proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
|||
if ", cdecl" in result and dy.len != 0:
|
||||
result = result.replace(".}", dy & ".}")
|
||||
|
||||
proc getComments*(nimState: NimState): string =
|
||||
proc getComments*(nimState: NimState, strip = false): string =
|
||||
if not nimState.gState.nocomments and nimState.commentStr.len != 0:
|
||||
result = "\n" & nimState.commentStr
|
||||
if strip:
|
||||
result = result.replace("\n ", "\n")
|
||||
nimState.commentStr = ""
|
||||
|
||||
proc dll*(path: string): string =
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ proc initGrammar(): Grammar =
|
|||
if ndname.nBl and ndname != nname:
|
||||
if isEnum:
|
||||
if nimState.addNewIdentifer(ndname):
|
||||
nimState.enumStr &= &"{nimState.getComments()}\ntype {ndname}* = {dptr}{nname}"
|
||||
nimState.enumStr &= &"{nimState.getComments(true)}\ntype {ndname}* = {dptr}{nname}"
|
||||
else:
|
||||
if nimState.addNewIdentifer(ndname):
|
||||
let
|
||||
|
|
@ -434,7 +434,7 @@ proc initGrammar(): Grammar =
|
|||
nimState.getIdentifier(name, nskType)
|
||||
|
||||
if nname.nBl and nimState.addNewIdentifer(nname):
|
||||
nimState.enumStr &= &"{nimState.getComments()}\ndefineEnum({nname})"
|
||||
nimState.enumStr &= &"{nimState.getComments(true)}\ndefineEnum({nname})"
|
||||
|
||||
var
|
||||
i = fstart
|
||||
|
|
@ -579,9 +579,9 @@ proc initGrammar(): Grammar =
|
|||
pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl")
|
||||
|
||||
if fptr.len != 0 or ftyp != "object":
|
||||
nimState.procStr &= &"{nimState.getComments()}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
|
||||
else:
|
||||
nimState.procStr &= &"{nimState.getComments()}\nproc {fnname}*({pout}){pragma}"
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}){pragma}"
|
||||
))
|
||||
|
||||
# // comment
|
||||
|
|
@ -597,7 +597,7 @@ proc initGrammar(): Grammar =
|
|||
let
|
||||
line = line.multiReplace([("//", ""), ("/*", ""), ("*/", "")])
|
||||
|
||||
nimState.commentStr &= &"\n# {line.strip(leading=false)}"
|
||||
nimState.commentStr &= &"\n # {line.strip(leading=false)}"
|
||||
))
|
||||
|
||||
proc initRegex(ast: ref Ast) =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue