commit a0b746402156fb637269ebe32ec9d2a64ac56df5 Author: Jonathan Bernard Date: Sun Feb 16 00:50:07 2020 -0600 Initial commit. diff --git a/update_nim_package_version.nim b/update_nim_package_version.nim new file mode 100644 index 0000000..663b569 --- /dev/null +++ b/update_nim_package_version.nim @@ -0,0 +1,67 @@ +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 +""" + 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() + diff --git a/update_nim_package_version.nimble b/update_nim_package_version.nimble new file mode 100644 index 0000000..8ee02f0 --- /dev/null +++ b/update_nim_package_version.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "Jonathan Bernard" +description = "Small util to update version consistently for nim packages I write." +license = "MIT" +bin = @["update_nim_package_version"] + + + +# Dependencies + +requires "nim >= 1.0.4"