Jonathan Bernard
5a78579fd7
Previously we did the replacement differently in the .nimble file and the version file. In the .nimble file we replaced the string in place, but in the version file we just rewrote the whole file. This meant that we could not include the version constant in a file with any other code. Now we do an in-place replacement for both files. This allows the version constant to live alongside other code.
72 lines
2.2 KiB
Nim
72 lines
2.2 KiB
Nim
import os, strutils, unicode
|
|
import nre except toSeq
|
|
|
|
type VersionInfo = tuple[curVersion, nextVersion, varName: string]
|
|
|
|
let NIMBLE_VERSION_REGEX = re"(\s*version\s*=\s*).*"
|
|
let VERSION_ASSIGNMENT_REGEX = re(r"(\s*)const (\S*VERSION\S*)\s*=\s*""([^""]+)""")
|
|
|
|
proc getCurVersion(versionFilename: string): VersionInfo =
|
|
|
|
for line in versionFilename.lines:
|
|
let m = line.match(VERSION_ASSIGNMENT_REGEX)
|
|
if m.isSome:
|
|
return (
|
|
curVersion: m.get.captures[2],
|
|
nextVersion: m.get.captures[2],
|
|
varName: m.get.captures[1])
|
|
|
|
raise newException(Exception, "Could not find the current version in " & versionFilename)
|
|
|
|
proc updateVersionInFile(file: string, versionInfo: VersionInfo): void =
|
|
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 & '"')
|
|
else: newLines.add(line)
|
|
|
|
file.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)
|
|
|
|
try:
|
|
var versionInfo = getCurVersion(versionFilepath)
|
|
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)
|
|
|
|
echo "Updating version definition in " & projectName & ".nimble"
|
|
updateVersionInFile(projectName & ".nimble", versionInfo)
|
|
|
|
except EOFError:
|
|
echo "Aborted"
|
|
quit()
|
|
|
|
except:
|
|
let ex = getCurrentException()
|
|
echo getCurrentExceptionMsg()
|
|
echo ex.getStackTrace()
|