2020-02-16 06:50:07 +00:00
|
|
|
import os, strutils, unicode
|
|
|
|
import nre except toSeq
|
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
type VersionInfo = tuple[curVersion, nextVersion, varName: string]
|
2020-02-16 06:50:07 +00:00
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
let NIMBLE_VERSION_REGEX = re"(\s*version\s*=\s*).*"
|
|
|
|
let VERSION_ASSIGNMENT_REGEX = re(r"(\s*)const (\S*VERSION\S*)\s*=\s*""([^""]+)""")
|
2020-02-16 06:50:07 +00:00
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
proc getCurVersion(versionFilename: string): VersionInfo =
|
2020-02-16 06:50:07 +00:00
|
|
|
|
|
|
|
for line in versionFilename.lines:
|
2022-01-21 20:36:15 +00:00
|
|
|
let m = line.match(VERSION_ASSIGNMENT_REGEX)
|
2020-02-16 06:50:07 +00:00
|
|
|
if m.isSome:
|
|
|
|
return (
|
2022-01-21 20:36:15 +00:00
|
|
|
curVersion: m.get.captures[2],
|
|
|
|
nextVersion: m.get.captures[2],
|
|
|
|
varName: m.get.captures[1])
|
2020-02-16 06:50:07 +00:00
|
|
|
|
|
|
|
raise newException(Exception, "Could not find the current version in " & versionFilename)
|
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
proc updateVersionInFile(file: string, versionInfo: VersionInfo): void =
|
2020-02-16 06:50:07 +00:00
|
|
|
var newLines: seq[string] = @[]
|
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
for line in file.lines:
|
|
|
|
let m1 = line.match(NIMBLE_VERSION_REGEX)
|
|
|
|
let m2 = line.match(VERSION_ASSIGNMENT_REGEX)
|
|
|
|
|
|
|
|
if m1.isSome:
|
|
|
|
newLines.add(m1.get.captures[0] & '"' & versionInfo.nextVersion & '"')
|
|
|
|
elif m2.isSome:
|
|
|
|
newLines.add(m2.get.captures[0] & "const " & m2.get.captures[1] & " = \"" & versionInfo.nextVersion & '"')
|
2020-02-16 06:50:07 +00:00
|
|
|
else: newLines.add(line)
|
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
file.writeFile(newLines.join("\n"))
|
2020-02-16 06:50:07 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
if paramCount() < 2:
|
|
|
|
echo """Usage:
|
|
|
|
update_nim_package_version <pkgName> <pathToVersionDefFile>
|
|
|
|
"""
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
let projectName = paramStr(1)
|
|
|
|
let versionFilepath = paramStr(2)
|
|
|
|
|
|
|
|
try:
|
2022-01-21 20:36:15 +00:00
|
|
|
var versionInfo = getCurVersion(versionFilepath)
|
2020-02-16 06:50:07 +00:00
|
|
|
var acceptNewVersion = false
|
|
|
|
while not acceptNewVersion:
|
|
|
|
stdout.writeLine "Current version is " & versionInfo.curVersion & "."
|
|
|
|
stdout.write "New version? "
|
|
|
|
versionInfo.nextVersion = stdin.readLine
|
|
|
|
|
|
|
|
stdout.write "New version will be set to " & versionInfo.nextVersion & ". Is this correct (yes/no)? "
|
|
|
|
let isCorrect = stdin.readLine
|
|
|
|
acceptNewVersion = "yes".startsWith(isCorrect.toLower)
|
|
|
|
|
|
|
|
echo "Updating version definition in " & versionFilepath
|
2022-01-21 20:36:15 +00:00
|
|
|
updateVersionInFile(versionFilepath, versionInfo)
|
2020-02-16 06:50:07 +00:00
|
|
|
|
|
|
|
echo "Updating version definition in " & projectName & ".nimble"
|
2022-01-21 20:36:15 +00:00
|
|
|
updateVersionInFile(projectName & ".nimble", versionInfo)
|
2020-02-16 06:50:07 +00:00
|
|
|
|
|
|
|
except EOFError:
|
|
|
|
echo "Aborted"
|
|
|
|
quit()
|
|
|
|
|
2022-01-21 20:36:15 +00:00
|
|
|
except:
|
|
|
|
let ex = getCurrentException()
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
echo ex.getStackTrace()
|