Make the version replacement logic more intelligent.

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.
This commit is contained in:
Jonathan Bernard 2022-01-21 14:36:15 -06:00
parent a0b7464021
commit 5a78579fd7
3 changed files with 28 additions and 22 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.*sw?
update_nim_package_version

View File

@ -1,37 +1,37 @@
import os, strutils, unicode import os, strutils, unicode
import nre except toSeq import nre except toSeq
type VersionInfo = tuple[curVersion, nextVersion, assignment: string] 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 = proc getCurVersion(versionFilename: string): VersionInfo =
let verPat = re(r"(.*=\s*)""([\d.]+)""")
for line in versionFilename.lines: for line in versionFilename.lines:
let m = line.match(verPat) let m = line.match(VERSION_ASSIGNMENT_REGEX)
if m.isSome: if m.isSome:
return ( return (
curVersion: m.get.captures[1], curVersion: m.get.captures[2],
nextVersion: m.get.captures[1], nextVersion: m.get.captures[2],
assignment: m.get.captures[0]) varName: m.get.captures[1])
raise newException(Exception, "Could not find the current version in " & versionFilename) raise newException(Exception, "Could not find the current version in " & versionFilename)
proc updateVersionFile(versionFilename: string, versionInfo: VersionInfo): void = proc updateVersionInFile(file: string, versionInfo: VersionInfo): void =
versionFilename.writeFile(versionInfo.assignment & "\"" & versionInfo.nextVersion & "\"")
proc updateNimbleFile(projectName: string, versionInfo: VersionInfo): void =
var newLines: seq[string] = @[] var newLines: seq[string] = @[]
let verPat = re"(\s*version\s*=\s*).*"
let nimbleFilename = projectName & ".nimble"
for line in nimbleFilename.lines: for line in file.lines:
let m = line.match(verPat) let m1 = line.match(NIMBLE_VERSION_REGEX)
if m.isSome: let m2 = line.match(VERSION_ASSIGNMENT_REGEX)
newLines.add(m.get.captures[0] & '"' & versionInfo.nextVersion & '"')
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) else: newLines.add(line)
nimbleFilename.writeFile(newLines.join("\n")) file.writeFile(newLines.join("\n"))
when isMainModule: when isMainModule:
if paramCount() < 2: if paramCount() < 2:
@ -42,9 +42,9 @@ when isMainModule:
let projectName = paramStr(1) let projectName = paramStr(1)
let versionFilepath = paramStr(2) let versionFilepath = paramStr(2)
var versionInfo = getCurVersion(versionFilepath)
try: try:
var versionInfo = getCurVersion(versionFilepath)
var acceptNewVersion = false var acceptNewVersion = false
while not acceptNewVersion: while not acceptNewVersion:
stdout.writeLine "Current version is " & versionInfo.curVersion & "." stdout.writeLine "Current version is " & versionInfo.curVersion & "."
@ -56,12 +56,16 @@ when isMainModule:
acceptNewVersion = "yes".startsWith(isCorrect.toLower) acceptNewVersion = "yes".startsWith(isCorrect.toLower)
echo "Updating version definition in " & versionFilepath echo "Updating version definition in " & versionFilepath
updateVersionFile(versionFilepath, versionInfo) updateVersionInFile(versionFilepath, versionInfo)
echo "Updating version definition in " & projectName & ".nimble" echo "Updating version definition in " & projectName & ".nimble"
updateNimbleFile(projectName, versionInfo) updateVersionInFile(projectName & ".nimble", versionInfo)
except EOFError: except EOFError:
echo "Aborted" echo "Aborted"
quit() quit()
except:
let ex = getCurrentException()
echo getCurrentExceptionMsg()
echo ex.getStackTrace()

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.0" version = "0.2.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Small util to update version consistently for nim packages I write." description = "Small util to update version consistently for nim packages I write."
license = "MIT" license = "MIT"