mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-13 08:15:33 +00:00
_
This commit is contained in:
@@ -39,7 +39,6 @@ proc isGitRepo*(dir: string): bool =
|
||||
# remove trailing whitespaces from the result.
|
||||
result = status == 0 and output.strip() == ""
|
||||
|
||||
|
||||
proc diffStrings*(a, b: string): string =
|
||||
runnableExamples:
|
||||
let a = "ok1\nok2\nok3"
|
||||
@@ -48,10 +47,10 @@ proc diffStrings*(a, b: string): string =
|
||||
echo c
|
||||
|
||||
template tmpFileImpl(prefix, str): auto =
|
||||
# pending
|
||||
let (fd, path) = createTempFile(prefix, "")
|
||||
# pending https://github.com/nim-lang/Nim/pull/17889
|
||||
# let (fd, path) = createTempFile(prefix, "")
|
||||
let path = genTempPath(prefix, "")
|
||||
defer:
|
||||
close fd
|
||||
removeFile(path)
|
||||
writeFile(path, str)
|
||||
(fd, path)
|
||||
|
||||
@@ -90,13 +90,21 @@ template randomPathName(length: Natural): string =
|
||||
res[i] = state.sample(letters)
|
||||
res
|
||||
|
||||
proc genTempPath*(prefix, suffix: string, dir = ""): string =
|
||||
## Generates a path name in `dir`.
|
||||
##
|
||||
## If `dir` is empty, (`getTempDir <os.html#getTempDir>`_) will be used.
|
||||
## The path begins with `prefix` and ends with `suffix`.
|
||||
var dir = dir
|
||||
if dir.len == 0:
|
||||
dir = getTempDir()
|
||||
result = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
|
||||
|
||||
proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: string] =
|
||||
## `createTempFile` creates a new temporary file in the directory `dir`.
|
||||
## Creates a new temporary file in the directory `dir`.
|
||||
##
|
||||
## If `dir` is the empty string, the default directory for temporary files
|
||||
## (`getTempDir <os.html#getTempDir>`_) will be used.
|
||||
## The temporary file name begins with `prefix` and ends with `suffix`.
|
||||
## `createTempFile` returns a file handle to an open file and the path of that file.
|
||||
## This generates a path name using `genTempPath(prefix, suffix, dir)` and
|
||||
## returns a file handle to an open file and the path of that file.
|
||||
##
|
||||
## If failing to create a temporary file, `IOError` will be raised.
|
||||
##
|
||||
@@ -108,7 +116,7 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
|
||||
createDir(dir)
|
||||
|
||||
for i in 0 ..< maxRetry:
|
||||
result.path = dir / (prefix & randomPathName(nimTempPathLength) & suffix)
|
||||
result.path = createTempPath(prefix, suffix, dir)
|
||||
try:
|
||||
result.fd = safeOpen(result.path)
|
||||
except OSError:
|
||||
|
||||
Reference in New Issue
Block a user