Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@ cd examples
nimassets -d=templatesdir -o=assetsfile.nim
```

Bundle more than one source directory using multiple `-d` flags
```bash
nimassets -d=templatesdir -d=css -d=js -o=assetsfile.nim
```

`-f` or `--fast` flag can help with large assets directories
`-t` or `--type` encoding method, default is base64


### Use Assets
```
```nim
import assetsfile # name from -o=<filename>

# Getting file contents from a single directory table
echo assetsfile.getAsset("index.html")

# Getting file contents from a multi directory table
echo assetsfile.getAsset("templatesdir/index.html")
echo assetsfile.getAsset("js/app.min.js")
echo assetsfile.getAsset("css/app.min.css")
```

### Development
Expand Down
28 changes: 19 additions & 9 deletions src/nimassets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func stringToByteSeq(str: string): seq[byte] {.inline.} =
result = newSeq[byte](length)
copyMem(result[0].unsafeAddr, str[0].unsafeAddr, length)

proc handleFile(path: string): string {.thread.} =
proc handleFile(path: string, isSingleDir: bool): string {.thread.} =
var val, valString: string

stdout.write fmt"{path} ... "
Expand Down Expand Up @@ -160,22 +160,31 @@ proc handleFile(path: string): string {.thread.} =
stdout.write fmt"zstd-level:{compressLevel} ... "
valString = "\"\"\"" & val & "\"\"\""

var pathKey: string
var dirSep = "/"
when defined(windows):
result = &"""assets["{escape(path, prefix="", suffix="")}"] = {valString}""" & "\n\n"
dirSep = "\\"
if isSingleDir:
let pathKeyArr = path.split(dirSep)
pathKey = pathKeyArr[1 .. pathKeyArr.high].join(dirSep)
else: pathKey = path

when defined(windows):
result = &"""assets["{escape(pathKey, prefix="", suffix="")}"] = {valString}""" & "\n\n"
else:
result = &"""assets["{path}"] = {valString}""" & "\n\n"
result = &"""assets["{pathKey}"] = {valString}""" & "\n\n"
stdout.write "ok\n"

proc generateDirAssetsSimple*(dir: string): string =
proc generateDirAssetsSimple*(dir: string, isSingleDir: bool): string =
for path in expandTilde(dir).walkDirRec():
if not isHidden(path):
result &= handleFile(path)
result &= handleFile(path, isSingleDir)

proc generateDirAssetsSpawn*(dir: string): string =
proc generateDirAssetsSpawn*(dir: string, isSingleDir: bool): string =
var results = newSeq[FlowVar[string]]()
for path in expandTilde(dir).walkDirRec():
if not isHidden(path):
results.add(spawn handleFile(path))
results.add(spawn handleFile(path, isSingleDir))

# wait till all of them are done.
for r in results:
Expand All @@ -187,7 +196,7 @@ proc generateDirAssetsSpawn*(dir: string): string =
proc createAssetsFile*(dirs: seq[string], outputfile = "assets.nim",
fast = false, compress = false) =
var
generator: proc(s: string): string
generator: proc(s: string, isSingleDir: bool): string
data =
case dataType
of tBinary: assetsFileHeaderBinary
Expand All @@ -200,8 +209,9 @@ proc createAssetsFile*(dirs: seq[string], outputfile = "assets.nim",
else:
generator = generateDirAssetsSimple

let dirsLen = dirs.len
for d in dirs:
data &= generator(d)
data &= generator(d, isSingleDir = (dirsLen == 1))

writeFile(outputfile, data)

Expand Down