configure.nims 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import options, parseutils, strutils
  2. from os import lastPathPart, getHomeDir, `/`
  3. const
  4. BoldPurple = "\e[1;35m"
  5. BoldGreen = "\e[1;32m"
  6. BoldYellow = "\e[1;33m"
  7. BoldRed = "\e[1;31m"
  8. Green = "\e[0;32m"
  9. BgPurple = "\e[45m"
  10. ColorReset = "\e[m"
  11. func colorMessage(msg, color: string): string =
  12. "$1$2$3" % [color, msg, ColorReset]
  13. func errorMessage(msg: string): string =
  14. colorMessage("[ERROR]: ", BoldRed) & msg
  15. proc echoError(msg: string) =
  16. echo(errorMessage(msg))
  17. proc promptMessage(message: string, default = ""): string =
  18. echo("$1$2$3" % [BoldGreen, message, ColorReset])
  19. result = readLineFromStdin()
  20. if result == "":
  21. result = default
  22. proc banner =
  23. echo(colorMessage(" ", BgPurple))
  24. echo(colorMessage(" MATERIALIZE KARAX JESTER ", BgPurple & BoldRed))
  25. echo(colorMessage(" ", BgPurple))
  26. type
  27. NimbleConfig = object
  28. version: string
  29. author: string
  30. description: string
  31. license: string
  32. binary: string
  33. dependencies: seq[string]
  34. rest: string
  35. proc newNimbleConfig: NimbleConfig =
  36. NimbleConfig(
  37. version: "",
  38. author: "",
  39. description: "",
  40. license: "",
  41. binary: "",
  42. dependencies: newSeq[string](),
  43. rest: ""
  44. )
  45. proc toString(c: NimbleConfig): string =
  46. result = """# Package
  47. version = "$1"
  48. author = "$2"
  49. description = "$3"
  50. license = "$4"
  51. srcDir = "src"
  52. bin = @["$5"]
  53. # Dependencies
  54. """ % [c.version, c.author, c.description, c.license, c.binary]
  55. for dep in c.dependencies:
  56. result.add("requires \"$1\"\n" % dep)
  57. result.add("\n" & c.rest)
  58. proc readConfig(cfgFile: string): Option[NimbleConfig] =
  59. var
  60. packageSeq: seq[string]
  61. depsSeq: seq[string]
  62. if not cfgFile.endsWith(".nimble"):
  63. echoError(cfgFile & " is not a valid nimble config file.")
  64. return none(NimbleConfig)
  65. if fileExists(cfgFile):
  66. echo("Reading config file: ", colorMessage(cfgFile, BoldYellow))
  67. let contents = readFile(cfgFile)
  68. var
  69. lines = contents.splitlines()
  70. linesRead = 0
  71. for lineNum, line in lines:
  72. var value = captureBetween(line, '"')
  73. if line.len() == 0 or line[0] == '#':
  74. continue
  75. elif line.contains("=") and not line.contains("requires"):
  76. packageSeq.add(value)
  77. elif line.contains("requires"):
  78. depsSeq.add(value)
  79. else:
  80. linesRead = lineNum
  81. break
  82. let restConfig = lines[linesRead..lines.len() - 4].join("\n")
  83. result = some(NimbleConfig(
  84. version: packageSeq[0],
  85. author: packageSeq[1],
  86. description: packageSeq[2],
  87. license: packageSeq[3],
  88. binary: packageSeq[5],
  89. dependencies: depsSeq,
  90. rest: restConfig
  91. ))
  92. else:
  93. echoError("Config file " & cfgFile & " does not exist.")
  94. result = none(NimbleConfig)
  95. proc writeConfig(conf: NimbleConfig) =
  96. writeFile(conf.binary & ".nimble", conf.toString())
  97. proc promptDependencies: seq[string] =
  98. echo(colorMessage("Enter nim dependencies, press <enter> to stop: ", Green))
  99. echo("""Examples:
  100. nim-pkg >= 0.3.5
  101. nim-pkg#fa929b6
  102. nim-pkg
  103. https://github.com/nim-pkg-dev/nim-pkg >= 0.1.0
  104. """)
  105. echo(colorMessage("""
  106. For more info use: `nimble list`
  107. or `nimble search` in your terminal.""", Green))
  108. var dep = "dummy"
  109. result = newSeq[string]()
  110. while dep.len() > 0:
  111. echo(colorMessage("> ", BoldGreen))
  112. dep = readLineFromStdin()
  113. if dep != "":
  114. result.add(dep)
  115. when isMainModule:
  116. banner()
  117. let
  118. possibleBinary = getCurrentDir().lastPathPart()
  119. possibleUser = getHomeDir().lastPathPart()
  120. oldConfigFile = "materialize_karax_jester.nimble"
  121. if possibleBinary == "materialize_karax_jester":
  122. echoError("You should give your project dir a new name.")
  123. else:
  124. let
  125. oldConfig = readConfig(oldConfigFile).get()
  126. sourceDir = getCurrentDir() / "src"
  127. mainExe = sourceDir / "materialize_karax_jester.nim"
  128. var newConfig = newNimbleConfig()
  129. echo(colorMessage("Enter your new configuration: ", BoldPurple))
  130. echo("(Leave it blank to set it to default)")
  131. newConfig.binary = promptMessage("Project binary [default $1]: " % [possibleBinary], possibleBinary)
  132. newConfig.version = promptMessage("Project version [default 0.1.0]: ", "0.1.0")
  133. newConfig.description = promptMessage("Project description [default \"\"]: ")
  134. newConfig.license = promptMessage("Project license [default $1]: " % oldConfig.license, oldConfig.license)
  135. newConfig.author = promptMessage("Project author [default $1]: " % possibleUser, possibleUser)
  136. newConfig.dependencies = oldConfig.dependencies & promptDependencies()
  137. newConfig.rest = oldConfig.rest
  138. cpFile(mainExe, mainExe & ".bak")
  139. mvFile(mainExe, sourceDir / (newConfig.binary & ".nim"))
  140. mvFile(oldConfigFile, oldConfigFile & ".backup")
  141. newConfig.writeConfig()