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 """ 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()