Skip to content

Commit

Permalink
Merge pull request #36 from jiro4989/chore/add-code-generator
Browse files Browse the repository at this point in the history
faker.nimble: Add code generator
  • Loading branch information
jiro4989 authored Dec 25, 2019
2 parents f5ea32f + bcb12f6 commit dd5a53d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 18 deletions.
87 changes: 86 additions & 1 deletion faker.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,96 @@ installExt = @["nim"]
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"

task genMod, "Generate module":
let module = paramStr(paramCount())
if module == "genMod":
echo "Need 1 args"
quit 1

# Copy module dir
let
srcModule = "job"
srcDir = providerDir / srcModule
dstDir = providerDir / 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

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)

18 changes: 10 additions & 8 deletions src/faker/provider/address.nim
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
]
]
14 changes: 10 additions & 4 deletions src/faker/provider/job.nim
Original file line number Diff line number Diff line change
@@ -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,
]
]
16 changes: 11 additions & 5 deletions src/faker/provider/phone_number.nim
Original file line number Diff line number Diff line change
@@ -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,
]
]

0 comments on commit dd5a53d

Please sign in to comment.