From 8203019dd88fdbcf4a56edf187fd3241a5332845 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Wed, 25 Dec 2019 22:01:01 +0900 Subject: [PATCH 1/3] :wrench: Add task to generate module template --- faker.nimble | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/faker.nimble b/faker.nimble index 58943a0..b66cbe6 100644 --- a/faker.nimble +++ b/faker.nimble @@ -9,7 +9,7 @@ installExt = @["nim"] bin = @["faker/cli/faker"] binDir = "bin" - +from os import `/`, splitFile # Dependencies @@ -17,3 +17,37 @@ requires "nim >= 1.0.4" task docs, "Generate API documents": exec "nimble doc --index:on --project --out:docs --hints:off src/faker.nim" + +task genMod, "Generate module": + let module = paramStr(paramCount()) + if module == "genMod": + echo "Need 1 args" + quit 1 + + # Copy module dir + let + srcModule = "job" + baseDir = "src" / "faker" / "provider" + srcDir = baseDir / srcModule + dstDir = baseDir / module + rmDir dstDir + cpDir srcDir, dstDir + echo "Generated: " & module + + # Copy module index file + let + srcFile = srcDir & ".nim" + dstFile = dstDir & ".nim" + rmFile dstFile + cpFile srcFile, dstFile + echo "Generated: " & dstFile + + # Rename module name + for path in listFiles(dstDir): + let (dir, name, ext) = splitFile(path) + if name == "interfaces": + continue + let newName = name.replace(srcModule, module) + let newPath = dir / newName & ext + mvFile path, newPath + echo "Generated: " & newPath From 3ac826f0a45c350c731b091c4bc5b112f7a8b659 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Wed, 25 Dec 2019 23:12:33 +0900 Subject: [PATCH 2/3] :wrench: Add task to generate module index file --- faker.nimble | 57 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/faker.nimble b/faker.nimble index b66cbe6..f1c27f0 100644 --- a/faker.nimble +++ b/faker.nimble @@ -10,11 +10,16 @@ bin = @["faker/cli/faker"] binDir = "bin" from os import `/`, splitFile +import strformat, strutils, sequtils # Dependencies requires "nim >= 1.0.4" +let + providerDir = "src" / "faker" / "provider" + locales = ["en_US", "ja_JP"] + task docs, "Generate API documents": exec "nimble doc --index:on --project --out:docs --hints:off src/faker.nim" @@ -27,9 +32,8 @@ task genMod, "Generate module": # Copy module dir let srcModule = "job" - baseDir = "src" / "faker" / "provider" - srcDir = baseDir / srcModule - dstDir = baseDir / module + srcDir = providerDir / srcModule + dstDir = providerDir / module rmDir dstDir cpDir srcDir, dstDir echo "Generated: " & module @@ -51,3 +55,50 @@ task genMod, "Generate module": let newPath = dir / newName & ext mvFile path, newPath echo "Generated: " & newPath + +task genProvs, "Generate provider file": + for dir in listDirs(providerDir): + let dstFile = dir & ".nim" + var lines: seq[string] + lines.add("# ----------------------------------------------- #") + lines.add("# This module was generated by 'nimble genProvs'. #") + lines.add("# See 'faker.nimble'. #") + lines.add("# ----------------------------------------------- #") + lines.add("") + lines.add("import util") + lines.add("import ../base") + + # Get submodule names + let (_, prefix, _) = splitFile(dir) + var modules: seq[string] + for file in listFiles(dir): + let (_, moduleName, _) = splitFile(file) + if moduleName == "interfaces": + continue + modules.add(moduleName) + lines.add(&"""import {prefix}/[{modules.join(", ")}]""") + + lines.add("export base") + lines.add("") + + # Set proc name list and locales + + # 1. proc + lines.add(&"genProc {prefix},") + lines.add(" [") + let procs = readFile(dir / "interfaces.nim") + .split("\n") + .filterIt(it.startsWith("proc") and "*(f: Faker)" in it) + for p in procs: + let procStr = p.split("*")[0].split("proc ")[1].strip() + lines.add(&" {procStr},") + lines.add(" ], [") + + # 2. locale + for locale in locales: + lines.add(&" {locale},") + lines.add(" ]") + + let body = lines.join("\n") + writeFile(dir & ".nim", body) + From bcb12f6765cf145eb5b9c30a3bb46aa85b37f98a Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Wed, 25 Dec 2019 23:14:28 +0900 Subject: [PATCH 3/3] :art: Generated code --- src/faker/provider/address.nim | 18 ++++++++++-------- src/faker/provider/job.nim | 14 ++++++++++---- src/faker/provider/phone_number.nim | 16 +++++++++++----- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/faker/provider/address.nim b/src/faker/provider/address.nim index 13430bd..d2279ae 100644 --- a/src/faker/provider/address.nim +++ b/src/faker/provider/address.nim @@ -1,4 +1,11 @@ -import util, ../base, address/[address_en_US, address_ja_JP] +# ----------------------------------------------- # +# This module was generated by 'nimble genProvs'. # +# See 'faker.nimble'. # +# ----------------------------------------------- # + +import util +import ../base +import address/[address_ja_JP, address_en_US] export base genProc address, @@ -9,26 +16,21 @@ genProc address, cityPrefix, citySuffix, country, - #countryCode, militaryApo, militaryDpo, militaryShip, militaryState, postalcode, - #postalcodeInState, postalcodePlus4, postcode, secondaryAddress, state, - #stateAbbr, streetAddress, streetName, streetSuffix, zipcode, - #zipcodeInState, zipcodePlus4, - ], - [ + ], [ en_US, ja_JP, - ] + ] \ No newline at end of file diff --git a/src/faker/provider/job.nim b/src/faker/provider/job.nim index 049587e..6ee115b 100644 --- a/src/faker/provider/job.nim +++ b/src/faker/provider/job.nim @@ -1,11 +1,17 @@ -import util, ../base, job/[job_en_US, job_ja_JP] +# ----------------------------------------------- # +# This module was generated by 'nimble genProvs'. # +# See 'faker.nimble'. # +# ----------------------------------------------- # + +import util +import ../base +import job/[job_en_US, job_ja_JP] export base genProc job, [ job, - ], - [ + ], [ en_US, ja_JP, - ] + ] \ No newline at end of file diff --git a/src/faker/provider/phone_number.nim b/src/faker/provider/phone_number.nim index 0276cad..01ac2eb 100644 --- a/src/faker/provider/phone_number.nim +++ b/src/faker/provider/phone_number.nim @@ -1,12 +1,18 @@ -import util, ../base, phone_number/[phone_number_en_US, phone_number_ja_JP] +# ----------------------------------------------- # +# This module was generated by 'nimble genProvs'. # +# See 'faker.nimble'. # +# ----------------------------------------------- # + +import util +import ../base +import phone_number/[phone_number_en_US, phone_number_ja_JP] export base -genProc phoneNumber, +genProc phone_number, [ phoneNumber, msisdn, - ], - [ + ], [ en_US, ja_JP, - ] + ] \ No newline at end of file