Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 26e0239

Browse files
committed
added binary for seed generation on compile time, closes #12, closes #11
1 parent 2668c14 commit 26e0239

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

tempfile.nim

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ proc getTempDir(): string =
4343

4444
proc mktempUnsafe*(prefix = "tmp", suffix = "", dir = "", len = 8): string =
4545
## Returns a unique temporary file name. The file is not created.
46-
when nimvm:
47-
when defined(windows):
48-
var seed = initRand(31157)
49-
else:
50-
var seed = initRand(staticExec("date +'%N'").parseint)
51-
else:
52-
discard
5346
var name = newString(len)
5447
for x in 0..MAX_RETRIES:
5548
for i in 0..len-1:
5649
when nimvm:
50+
var seed: Rand
51+
try:
52+
seed = initRand(staticExec("tempfile_seeder").parseint)
53+
except:
54+
raise newException(IOError, "Cannot find seeder for compile time, make sure you have nimble's bin dir to your $PATH")
5755
name[i] = CHARSET[rand(seed, CHARSET.len-1)]
5856
else:
5957
name[i] = CHARSET[rand(CHARSET.len-1)]

tempfile.nimble

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
[Package]
2-
name = "tempfile"
3-
version = "0.1.6"
1+
# Package
2+
3+
version = "0.1.7"
44
author = "Huy Doan"
55
description = "Temporary files and folders"
66
license = "MIT"
77

8-
[Deps]
9-
Requires: "nim >= 0.19.0"
8+
bin = @["tempfile_seeder"]
9+
10+
# Dependencies
11+
12+
requires "nim >= 0.19.0"

tempfile_seeder.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
when isMainModule:
3+
import random
4+
5+
randomize()
6+
echo rand(high(int))
7+
8+
9+

tests/nim.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--path:".."

tests/test1.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import tempfile
2+
3+
4+
proc ct(): string {.compileTime.} =
5+
result = mktempUnsafe()
6+
echo "Compile time: " & result
7+
8+
const a = ct()
9+
10+
11+
echo "Run time: " & a

0 commit comments

Comments
 (0)