123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import options, parseutils, strutils
- from os import lastPathPart, getHomeDir, `/`
- const
- BoldPurple = "\e[1;35m"
- BoldGreen = "\e[1;32m"
- BoldYellow = "\e[1;33m"
- BoldRed = "\e[1;31m"
- Green = "\e[0;32m"
- BgPurple = "\e[45m"
- ColorReset = "\e[m"
- func colorMessage(msg, color: string): string =
- "$1$2$3" % [color, msg, ColorReset]
- func errorMessage(msg: string): string =
- colorMessage("[ERROR]: ", BoldRed) & msg
- proc echoError(msg: string) =
- echo(errorMessage(msg))
- proc promptMessage(message: string, default = ""): string =
- echo("$1$2$3" % [BoldGreen, message, ColorReset])
- result = readLineFromStdin()
- if result == "":
- result = default
- proc banner =
- echo(colorMessage(" ", BgPurple))
- echo(colorMessage(" MATERIALIZE KARAX JESTER ", BgPurple & BoldRed))
- echo(colorMessage(" ", BgPurple))
- type
- NimbleConfig = object
- version: string
- author: string
- description: string
- license: string
- binary: string
- dependencies: seq[string]
- rest: string
- proc newNimbleConfig: NimbleConfig =
- NimbleConfig(
- version: "",
- author: "",
- description: "",
- license: "",
- binary: "",
- dependencies: newSeq[string](),
- rest: ""
- )
- proc toString(c: NimbleConfig): string =
- result = """# Package
- version = "$1"
- author = "$2"
- description = "$3"
- license = "$4"
- srcDir = "src"
- bin = @["$5"]
- # Dependencies
- """ % [c.version, c.author, c.description, c.license, c.binary]
- for dep in c.dependencies:
- result.add("requires \"$1\"\n" % dep)
- result.add("\n" & c.rest)
- proc readConfig(cfgFile: string): Option[NimbleConfig] =
- var
- packageSeq: seq[string]
- depsSeq: seq[string]
- if not cfgFile.endsWith(".nimble"):
- echoError(cfgFile & " is not a valid nimble config file.")
- return none(NimbleConfig)
- if fileExists(cfgFile):
- echo("Reading config file: ", colorMessage(cfgFile, BoldYellow))
- let contents = readFile(cfgFile)
- var
- lines = contents.splitlines()
- linesRead = 0
- for lineNum, line in lines:
- var value = captureBetween(line, '"')
- if line.len() == 0 or line[0] == '#':
- continue
- elif line.contains("=") and not line.contains("requires"):
- packageSeq.add(value)
- elif line.contains("requires"):
- depsSeq.add(value)
- else:
- linesRead = lineNum
- break
- let restConfig = lines[linesRead..lines.len() - 4].join("\n")
- result = some(NimbleConfig(
- version: packageSeq[0],
- author: packageSeq[1],
- description: packageSeq[2],
- license: packageSeq[3],
- binary: packageSeq[5],
- dependencies: depsSeq,
- rest: restConfig
- ))
- else:
- echoError("Config file " & cfgFile & " does not exist.")
- result = none(NimbleConfig)
- proc writeConfig(conf: NimbleConfig) =
- writeFile(conf.binary & ".nimble", conf.toString())
- proc promptDependencies: seq[string] =
- echo(colorMessage("Enter nim dependencies, press <enter> to stop: ", Green))
- echo("""Examples:
- nim-pkg >= 0.3.5
- nim-pkg#fa929b6
- nim-pkg
- https://github.com/nim-pkg-dev/nim-pkg >= 0.1.0
- """)
- echo(colorMessage("""
- For more info use: `nimble list`
- or `nimble search` in your terminal.""", Green))
- var dep = "dummy"
- result = newSeq[string]()
- while dep.len() > 0:
- echo(colorMessage("> ", BoldGreen))
- dep = readLineFromStdin()
- if dep != "":
- result.add(dep)
- when isMainModule:
- banner()
- let
- possibleBinary = getCurrentDir().lastPathPart()
- possibleUser = getHomeDir().lastPathPart()
- oldConfigFile = "materialize_karax_jester.nimble"
- if possibleBinary == "materialize_karax_jester":
- echoError("You should give your project dir a new name.")
- else:
- let
- oldConfig = readConfig(oldConfigFile).get()
- sourceDir = getCurrentDir() / "src"
- mainExe = sourceDir / "materialize_karax_jester.nim"
- var newConfig = newNimbleConfig()
- echo(colorMessage("Enter your new configuration: ", BoldPurple))
- echo("(Leave it blank to set it to default)")
- newConfig.binary = promptMessage("Project binary [default $1]: " % [possibleBinary], possibleBinary)
- newConfig.version = promptMessage("Project version [default 0.1.0]: ", "0.1.0")
- newConfig.description = promptMessage("Project description [default \"\"]: ")
- newConfig.license = promptMessage("Project license [default $1]: " % oldConfig.license, oldConfig.license)
- newConfig.author = promptMessage("Project author [default $1]: " % possibleUser, possibleUser)
- newConfig.dependencies = oldConfig.dependencies & promptDependencies()
- newConfig.rest = oldConfig.rest
- cpFile(mainExe, mainExe & ".bak")
- mvFile(mainExe, sourceDir / (newConfig.binary & ".nim"))
- mvFile(oldConfigFile, oldConfigFile & ".backup")
- newConfig.writeConfig()
|