update-nim-package-version/update_nim_package_version.nim

72 lines
2.2 KiB
Nim
Raw Normal View History

2020-02-16 06:50:07 +00:00
import os, strutils, unicode
import nre except toSeq
type VersionInfo = tuple[curVersion, nextVersion, varName: string]
2020-02-16 06:50:07 +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
proc getCurVersion(versionFilename: string): VersionInfo =
2020-02-16 06:50:07 +00:00
for line in versionFilename.lines:
let m = line.match(VERSION_ASSIGNMENT_REGEX)
2020-02-16 06:50:07 +00:00
if m.isSome:
return (
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)
proc updateVersionInFile(file: string, versionInfo: VersionInfo): void =
2020-02-16 06:50:07 +00:00
var newLines: seq[string] = @[]
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)
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:
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
updateVersionInFile(versionFilepath, versionInfo)
2020-02-16 06:50:07 +00:00
echo "Updating version definition in " & projectName & ".nimble"
updateVersionInFile(projectName & ".nimble", versionInfo)
2020-02-16 06:50:07 +00:00
except EOFError:
echo "Aborted"
quit()
except:
let ex = getCurrentException()
echo getCurrentExceptionMsg()
echo ex.getStackTrace()