update-nim-package-version/update_nim_package_version.nim
2020-02-16 00:50:07 -06:00

68 lines
2.1 KiB
Nim

import os, strutils, unicode
import nre except toSeq
type VersionInfo = tuple[curVersion, nextVersion, assignment: string]
proc getCurVersion(versionFilename: string): VersionInfo =
let verPat = re(r"(.*=\s*)""([\d.]+)""")
for line in versionFilename.lines:
let m = line.match(verPat)
if m.isSome:
return (
curVersion: m.get.captures[1],
nextVersion: m.get.captures[1],
assignment: m.get.captures[0])
raise newException(Exception, "Could not find the current version in " & versionFilename)
proc updateVersionFile(versionFilename: string, versionInfo: VersionInfo): void =
versionFilename.writeFile(versionInfo.assignment & "\"" & versionInfo.nextVersion & "\"")
proc updateNimbleFile(projectName: string, versionInfo: VersionInfo): void =
var newLines: seq[string] = @[]
let verPat = re"(\s*version\s*=\s*).*"
let nimbleFilename = projectName & ".nimble"
for line in nimbleFilename.lines:
let m = line.match(verPat)
if m.isSome:
newLines.add(m.get.captures[0] & '"' & versionInfo.nextVersion & '"')
else: newLines.add(line)
nimbleFilename.writeFile(newLines.join("\n"))
when isMainModule:
if paramCount() < 2:
echo """Usage:
update_nim_package_version <pkgName> <pathToVersionDefFile>
"""
quit(QuitFailure)
let projectName = paramStr(1)
let versionFilepath = paramStr(2)
var versionInfo = getCurVersion(versionFilepath)
try:
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
updateVersionFile(versionFilepath, versionInfo)
echo "Updating version definition in " & projectName & ".nimble"
updateNimbleFile(projectName, versionInfo)
except EOFError:
echo "Aborted"
quit()