From 5a78579fd7f88014263aed38c60327c85f6f8bcf Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 21 Jan 2022 14:36:15 -0600 Subject: [PATCH] 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. --- .gitignore | 2 ++ update_nim_package_version.nim | 46 +++++++++++++++++-------------- update_nim_package_version.nimble | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5784cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.*sw? +update_nim_package_version diff --git a/update_nim_package_version.nim b/update_nim_package_version.nim index 663b569..57f102c 100644 --- a/update_nim_package_version.nim +++ b/update_nim_package_version.nim @@ -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() diff --git a/update_nim_package_version.nimble b/update_nim_package_version.nimble index 8ee02f0..bbe35d5 100644 --- a/update_nim_package_version.nimble +++ b/update_nim_package_version.nimble @@ -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"