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:
parent
a0b7464021
commit
5a78579fd7
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.*sw?
|
||||
update_nim_package_version
|
@ -1,37 +1,37 @@
|
||||
import os, strutils, unicode
|
||||
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 =
|
||||
|
||||
let verPat = re(r"(.*=\s*)""([\d.]+)""")
|
||||
|
||||
for line in versionFilename.lines:
|
||||
let m = line.match(verPat)
|
||||
let m = line.match(VERSION_ASSIGNMENT_REGEX)
|
||||
if m.isSome:
|
||||
return (
|
||||
curVersion: m.get.captures[1],
|
||||
nextVersion: m.get.captures[1],
|
||||
assignment: m.get.captures[0])
|
||||
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 updateVersionFile(versionFilename: string, versionInfo: VersionInfo): void =
|
||||
versionFilename.writeFile(versionInfo.assignment & "\"" & versionInfo.nextVersion & "\"")
|
||||
|
||||
proc updateNimbleFile(projectName: string, versionInfo: VersionInfo): void =
|
||||
proc updateVersionInFile(file: 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 & '"')
|
||||
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)
|
||||
|
||||
nimbleFilename.writeFile(newLines.join("\n"))
|
||||
file.writeFile(newLines.join("\n"))
|
||||
|
||||
when isMainModule:
|
||||
if paramCount() < 2:
|
||||
@ -42,9 +42,9 @@ when isMainModule:
|
||||
|
||||
let projectName = paramStr(1)
|
||||
let versionFilepath = paramStr(2)
|
||||
var versionInfo = getCurVersion(versionFilepath)
|
||||
|
||||
try:
|
||||
var versionInfo = getCurVersion(versionFilepath)
|
||||
var acceptNewVersion = false
|
||||
while not acceptNewVersion:
|
||||
stdout.writeLine "Current version is " & versionInfo.curVersion & "."
|
||||
@ -56,12 +56,16 @@ when isMainModule:
|
||||
acceptNewVersion = "yes".startsWith(isCorrect.toLower)
|
||||
|
||||
echo "Updating version definition in " & versionFilepath
|
||||
updateVersionFile(versionFilepath, versionInfo)
|
||||
updateVersionInFile(versionFilepath, versionInfo)
|
||||
|
||||
echo "Updating version definition in " & projectName & ".nimble"
|
||||
updateNimbleFile(projectName, versionInfo)
|
||||
updateVersionInFile(projectName & ".nimble", versionInfo)
|
||||
|
||||
except EOFError:
|
||||
echo "Aborted"
|
||||
quit()
|
||||
|
||||
except:
|
||||
let ex = getCurrentException()
|
||||
echo getCurrentExceptionMsg()
|
||||
echo ex.getStackTrace()
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Small util to update version consistently for nim packages I write."
|
||||
license = "MIT"
|
||||
|
Loading…
Reference in New Issue
Block a user