src/version

Author:Kaushal Modi
License:MIT

Introduction

This module provides a Nim library as well as a standalone CLI utility to fetch and parse the version info for almost any CLI app.

Source

Repo link

Types

VersionSegment = enum
  vMajor, vMinor, vPatch
Version = tuple[major: int, minor: int, patch: int]
VersionTup = tuple[tup: Version, str: string]

Consts

versionVersion = "fatal: No names found, cannot describe anything."
versionUnset: Version = (0, 0, 0)
maxVersion = 99

Procs

proc inc(v: var Version; seg = vPatch; maxVersionMinor = maxVersion;
         maxVersionPatch = maxVersion) {....raises: [], tags: [].}

Increment version.

Patch version:

  • The patch version can increment up to maxVersionPatch.
  • Once the max patch version is reached, the minor version is incremented.

Minor version:

  • The minor version can increment up to maxVersionMinor.
  • Once the max minor version is reached, the major version is incremented.
  • The patch version is reset to 0 each time the minor version is incremented.

Major version:

  • The patch and minor versions are reset to 0 each time the major version is incremented.

Example:

var
  v: Version = (1, 9, 9)
v.inc(vPatch, maxVersionMinor = 9, maxVersionPatch = 9)
doAssert v == (2, 0, 0)
v.inc(vPatch)
doAssert v == (2, 0, 1)
v.inc(vMinor)
doAssert v == (2, 1, 0)
v.inc(vMajor)
doAssert v == (3, 0, 0)
proc dec(v: var Version; seg = vPatch; maxVersionMinor = maxVersion;
         maxVersionPatch = maxVersion) {....raises: [], tags: [].}

Decrement version.

Patch version:

  • Once the patch version reaches 0, the minor version is decremented, and the patch version is reset to maxVersionPatch.

Minor version:

  • Once the minor version reaches 0, the major version is decremented, and the minor version is reset to maxVersionMinor.
  • The patch version is reset to 0 each time the minor version is decremented.

Major version:

  • The patch and minor versions are reset to 0 each time the major version is decremented.
  • Once the major patch version reaches 0, it remains 0.

Example:

var
  v: Version = (4, 0, 0)
v.dec(vPatch, maxVersionMinor = 5, maxVersionPatch = 9)
doAssert v == (3, 5, 9)
v.dec(vPatch)
doAssert v == (3, 5, 8)
v.dec(vMinor)
doAssert v == (3, 4, 0)
v.dec(vMajor)
doAssert v == (2, 0, 0)
proc getVersion(versionOutLines: openArray[string]; app = "";
                maxVersionMinor = maxVersion; maxVersionPatch = maxVersion): Version {.
    ...raises: [ValueError], tags: [].}
Return the version parsed from an array or sequence of strings.

Example:

doAssert ["Nim Compiler Version 1.3.5 [Linux: amd64]"
          ].getVersion() == (1, 3, 5)
doAssert ["gcc (GCC) 9.1.0",
          "Copyright (C) 2019 Free Software Foundation, Inc."
          ].getVersion() == (9, 1, 0)
doAssert @["GNU Emacs 27.0.91",
           "Copyright (C) 2020 Free Software Foundation, Inc."
           ].getVersion() == (27, 0, 91)
proc getVersion(app: string; maxVersionMinor = maxVersion;
                maxVersionPatch = maxVersion): Version {.
    ...raises: [ValueError, OSError, Exception, IOError], tags: [ReadDirEffect,
    ReadEnvEffect, ReadIOEffect, ExecIOEffect, RootEffect].}
Return the version parsed from the app's version string.

Example:

doAssert "nim".getVersion() == (NimMajor, NimMinor, NimPatch)
doAssert "version".getVersion() == [versionVersion].getVersion()
proc getVersionCT(app: string; maxVersionMinor = maxVersion;
                  maxVersionPatch = maxVersion): Version {....raises: [ValueError],
    tags: [].}
Return the version parsed from the app's version string. This is a compile time proc.
static:
  doAssert "nim".getVersion() == (NimMajor, NimMinor, NimPatch)
  doAssert "version".getVersion() == [versionVersion].getVersion()
proc `$`(v: Version): string {....raises: [ValueError], tags: [].}
Return the string representation of the version.

Example:

let
  v: Version = (1, 2, 3)
doAssert $v == "1.2.3"