1 Commits

Author SHA1 Message Date
Jonathan Bernard d3f2b8fc6d Migrate to nitely/nim-regex away from std/nre.
PR Validation / Unit Tests (pull_request) Successful in 22s
2026-03-09 21:42:57 -05:00
3 changed files with 34 additions and 41 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ suite "update_version":
check:
incrementLastVersionPart("1.0.0") == "1.0.1"
incrementLastVersionPart("1.0.0-alpha.1") == "1.0.0-alpha.2"
incrementLastVersionPart("cicd_alphe.1-prerelease") == "cicd_alphe.2-prerelease"
incrementLastVersionPart("cicd_alpha.1-prerelease") == "cicd_alpha.2-prerelease"
incrementLastVersionPart("2024.04.1") == "2024.04.2"
test "incrementSemverPart":
+31 -38
View File
@@ -1,6 +1,5 @@
import std/[dirs, json, paths, sequtils, strutils, syncio, tables]
import std/nre except toSeq
import docopt, zero_functional
import docopt, regex, zero_functional
const USAGE = """Usage:
update_nim_package_version bump [<src-file> ...] [options]
@@ -85,7 +84,7 @@ Details:
Node: ^\s*export\s+const\s+\S*VERSION\S*\s*=\s*"(<old-version>)"\s*;?$
"""
const UV_VERSION = "1.2.1"
const UV_VERSION = "1.2.2"
type
LangType* = enum lNim, lNode
@@ -110,8 +109,8 @@ type PackageVersion* = object
# See also:
# https://regex101.com/r/Ly7O1x/3/
#
let SemverRegex =
re"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
const SemverRegex =
re2"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
let NimConstPattern = "^\\s*const\\s+\\S*VERSION\\S*\\*?\\s*=\\s*\"($#)\"\\s*$"
let NodeConstPattern =
@@ -199,18 +198,17 @@ proc replaceVersionInSource*(
let rgx =
case pkg.lang
of lNim: re(NimConstPattern % [vOld.replace(".", "\\.")])
of lNode: re(NodeConstPattern % [vOld.replace(".", "\\.")])
of lNim: re2(NimConstPattern % [vOld.replace(".", "\\.")])
of lNode: re2(NodeConstPattern % [vOld.replace(".", "\\.")])
var newLines = newSeq[string]()
for l in source.splitLines():
let mOpt = l.match(rgx)
if mOpt.isSome:
let m = mOpt.get
var m = RegexMatch2()
if match(l, rgx, m):
newLines.add(
l[0..<m.captureBounds[0].a] &
l[0..<m.group(0).a] &
pkg.version &
l[m.captureBounds[0].b+1 .. ^1])
l[m.group(0).b+1 .. ^1])
else: newLines.add(l)
return newLines.join("\p")
@@ -232,12 +230,12 @@ proc fmtSemver*(semver: TableRef[SemverParts, string]): string =
proc setLastVersionPart*(version: string, newVersion: string): string =
let versionParts = toSeq(findIter(version, re"([^\d.]+)?\.?(\d+)"))
let lastVersionPartMatch = versionParts[^1]
let versionParts = findAll(version, re2"([^\d.]+)?\.?(\d+)")
let lastVersionPart = versionParts[^1]
return
version[0..<lastVersionPartMatch.captureBounds[1].a] &
version[0..<lastVersionPart.group(1).a] &
newVersion &
version[lastVersionPartMatch.captureBounds[1].b+1 .. ^1]
version[lastVersionPart.group(1).b+1 .. ^1]
proc setSemverPart*(
@@ -246,34 +244,32 @@ proc setSemverPart*(
newVersionPart: string,
defaults = VERSION_DEFAULTS): string =
let matchOpt = match(version, SemverRegex)
if matchOpt.isNone:
var m: RegexMatch2
if not match(version, SemverRegex, m):
raise newException(ValueError,
"Version [$#] is not a valid Semantic Version number" % version)
let m = matchOpt.get
let versionParts = newTable[SemverParts, string]()
for p in SemverParts.items:
if p == part: versionParts[p] = newVersionPart
elif not m.captures.contains($p) or p > part: versionParts[p] = defaults[p]
else: versionParts[p] = m.captures[$p]
elif m.group($p).b < m.group($p).a or p > part: versionParts[p] = defaults[p]
else: versionParts[p] = version[m.group($p)]
result = fmtSemver(versionParts)
if match(result, SemverRegex).isNone:
if not match(result, SemverRegex):
raise newException(ValueError,
"Refusing to update: Version [$#] would not be a valid Semantic Version number" % result)
proc incrementLastVersionPart*(version: string): string =
let versionParts = toSeq(findIter(version, re"([^\d.]+)?\.?(\d+)"))
let lastVersionPartMatch = versionParts[^1]
let lastVersionPartInt = parseInt(lastVersionPartMatch.captures[1])
let versionParts = findAll(version, re2"([^\d.]+)?\.?(\d+)")
let lastVersionPart = versionParts[^1]
let lastVersionPartInt = parseInt(version[lastVersionPart.group(1)])
return
version[0..<lastVersionPartMatch.captureBounds[1].a] &
version[0..<lastVersionPart.group(1).a] &
$(lastVersionPartInt + 1) &
version[lastVersionPartMatch.captureBounds[1].b+1 .. ^1]
version[lastVersionPart.group(1).b+1 .. ^1]
proc incrementSemverPart*(
@@ -281,27 +277,24 @@ proc incrementSemverPart*(
part: SemverParts,
defaults = VERSION_DEFAULTS): string =
let matchOpt = match(version, SemverRegex)
if matchOpt.isNone:
var m: RegexMatch2
if not match(version, SemverRegex, m):
raise newException(ValueError,
"Version [$#] is not a valid Semantic Version number" % version)
let m = matchOpt.get
let versionParts = newTable[SemverParts, string]()
for p in SemverParts.items:
if p == part:
if m.captures.contains($p):
versionParts[p] = incrementLastVersionPart(m.captures[$p])
if not m.group($p).b < m.group($p).a:
versionParts[p] = incrementLastVersionPart(version[m.group($p)])
elif p < prerelease or defaults[p].len > 0:
versionParts[p] = defaults[p]
# if this part specifically has been requested to be incremented but
# the default is empty, we are still going to give it *something*
elif p == prerelease: versionParts[p] = "prerelease.0"
elif p == buildmetadata: versionParts[p] = "build.0"
elif not m.captures.contains($p) or p > part: versionParts[p] = defaults[p]
else: versionParts[p] = m.captures[$p]
elif m.group($p).b < m.group($p).a or p > part: versionParts[p] = defaults[p]
else: versionParts[p] = version[m.group($p)]
return fmtSemver(versionParts)
+2 -2
View File
@@ -1,6 +1,6 @@
# Package
version = "1.2.1"
version = "1.2.2"
author = "Jonathan Bernard"
description = "Small util to update version consistently for nim and node packages."
license = "MIT"
@@ -10,4 +10,4 @@ bin = @["update_version"]
# Dependencies
requires @["nim >= 1.0.4", "docopt >= 0.7.1", "zero_functional"]
requires @["nim >= 1.0.4", "docopt >= 0.7.1", "regex", "zero_functional"]