diff --git a/bump.nim b/bump.nim index b5d4d26..7dcbd6f 100644 --- a/bump.nim +++ b/bump.nim @@ -6,6 +6,8 @@ import strformat import nre import logging +from macros import nil + type Version* = tuple @@ -101,16 +103,26 @@ proc isValid*(ver: Version): bool = ## true if the version seems legit result = ver.major > 0 or ver.minor > 0 or ver.patch > 0 -proc parseVersion*(line: string): Option[Version] = - ## parse a version specifier line from the .nimble file - let - verex = line.match re(r"""^version\s*=\s*"(\d+).(\d+).(\d+)"""") - if not verex.isSome: - return - let cap = verex.get.captures.toSeq - result = (major: cap[0].get.parseInt, - minor: cap[1].get.parseInt, - patch: cap[2].get.parseInt).some +proc parseVersion*(nimble: string): Option[Version] = + ## try to parse a version from any line in a .nimble; + ## safe to use at compile-time + for line in nimble.splitLines: + if not line.startsWith("version"): + continue + let + fields = line.split('=') + if fields.len != 2: + continue + let + dotted = fields[1].replace("\"").strip.split('.') + if dotted.len != 3: + continue + try: + result = (major: dotted[0].parseInt, + minor: dotted[1].parseInt, + patch: dotted[2].parseInt).some + except ValueError: + discard proc bumpVersion*(ver: Version; major, minor, patch = false): Option[Version] = ## increment the version by the specified metric @@ -491,6 +503,43 @@ proc bump*(minor = false; major = false; patch = true; release = false; fatal "🐼nimgitsfu fail" return 1 +when defined(NimSupportsGlobAtCompileTime): + proc isNamedLikeDotNimble(dir: string; file: string): bool = + ## true if it the .nimble filename (minus ext) matches the directory + if dir == "" or file == "": + return + if not file.endsWith(dotNimble): + return + result = dir.lastPathPart == file.changeFileExt("") + +proc projectVersion*(hint = ""): Option[Version] {.compileTime.} = + ## try to get the version from the current (compile-time) project + let + path = macros.getProjectPath() + splat = path.splitFile + var + nimble: string + + if hint != "": + nimble = staticRead path / hint & dotNimble + elif fileExists(path / splat.name & dotNimble): + nimble = staticRead path / splat.name & dotNimble + else: + when defined(NimSupportsGlobAtCompileTime): + for file in walkFiles(path / "*" & dotNimble): + if nimble != "" and not path.isNamedLikeDotNimble(file): + macros.error &"{file} is 2nd {dotNimble} in {path}!" + nimble = staticRead path / file + if nimble == "": + # we won't know what to do if we find a second .nimble + # and yet out nimble contents are empty; so error out + macros.error &"{file} is empty; what version is this?!" + else: + macros.error &"provide the name of your project, minus {dotNimble}" + if nimble == "": + macros.error &"missing/empty {dotNimble}; what version is this?!" + result = parseVersion(nimble) + when isMainModule: import cligen @@ -499,7 +548,16 @@ when isMainModule: useStderr = true, fmtStr = "") logger = CuteLogger(forward: console) addHandler(logger) - dispatch bump, cmdName = "bump", + + # find the version of bump itself, whatfer --version reasons + let + version = projectVersion() + if version.isSome: + clCfg.version = $version.get + else: + clCfg.version = "(unknown version)" + + dispatchCf bump, cmdName = "bump", cf = clCfg, doc = "increment the version of a nimble package, " & "tag it, and push it via git", help = { diff --git a/bump.nimble b/bump.nimble index 5d69bc4..444ea98 100644 --- a/bump.nimble +++ b/bump.nimble @@ -1,4 +1,4 @@ -version = "1.8.1" +version = "1.8.2" author = "disruptek" description = "a tiny tool to bump nimble versions" license = "MIT" diff --git a/docs/bump.html b/docs/bump.html index 80edb81..c2a9dbe 100644 --- a/docs/bump.html +++ b/docs/bump.html @@ -847,7 +847,7 @@

bump

  • isValidVersion
  • parseVersionVersion
  • + title="parseVersion(nimble: string): Option[Version]">parseVersionVersion
  • bumpVersionVersion
  • bump title="bump(minor = false; major = false; patch = true; release = false; dry_run = false; folder = ""; nimble = ""; log_level = logLevel; commit = false; v = false; manual = ""; message: seq[string]): int">bump
  • +
  • projectVersionVersion
  • @@ -944,12 +946,10 @@

    Procs

    -
    proc parseVersion(line: string): Option[Version] {...}{.raises: [FieldError, ValueError,
    -    AccessViolationError, RegexInternalError, InvalidUnicodeError, KeyError,
    -    SyntaxError, StudyError, IndexError, UnpackError], tags: [].}
    +
    proc parseVersion(nimble: string): Option[Version] {...}{.raises: [], tags: [].}
    -parse a version specifier line from the .nimble file +try to parse a version from any line in a .nimble; safe to use at compile-time
    @@ -1049,16 +1049,23 @@

    Procs

    proc bump(minor = false; major = false; patch = true; release = false; dry_run = false;
              folder = ""; nimble = ""; log_level = logLevel; commit = false; v = false;
    -         manual = ""; message: seq[string]): int {...}{.raises: [Exception, FieldError,
    -    ValueError, AccessViolationError, RegexInternalError, InvalidUnicodeError,
    -    KeyError, SyntaxError, StudyError, IndexError, UnpackError, OSError, IOError,
    -    Defect, Exception, ValueError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect,
    -                                       ReadIOEffect, WriteDirEffect,
    -                                       WriteIOEffect, ExecIOEffect].}
    + manual = ""; message: seq[string]): int {...}{.raises: [Exception, ValueError, + UnpackError, OSError, IOError, FieldError, AccessViolationError, + RegexInternalError, InvalidUnicodeError, KeyError, SyntaxError, StudyError, + IndexError, Defect, Exception, ValueError], tags: [RootEffect, ReadDirEffect, + ReadEnvEffect, ReadIOEffect, WriteDirEffect, WriteIOEffect, ExecIOEffect].}
    the entry point from the cli +
    + +
    proc projectVersion(hint = ""): Option[Version] {...}{.compileTime, raises: [ValueError],
    +    tags: [ReadDirEffect].}
    +
    + +try to get the version from the current (compile-time) project +
    @@ -1070,7 +1077,7 @@

    Procs