Skip to content

Commit

Permalink
build: adding -dev support to chalk version (#195)
Browse files Browse the repository at this point in the history
required some adjustment to version extraction as well as basic
support for suffixes in semver.nim
  • Loading branch information
miki725 authored Feb 8, 2024
1 parent d61f652 commit cf9ebf8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion chalk.nimble
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/[strformat, strutils]
from src/config_version import getChalkVersion

version = getChalkVersion()
version = getChalkVersion(withSuffix = false)
author = "John Viega"
description = "Software artifact metadata to make it easy to tie " &
"deployments to source code and collect metadata."
Expand Down
10 changes: 7 additions & 3 deletions src/config_version.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
## So we have this separate file for now.
import std/[os, strscans, strutils]

proc getChalkVersion*(): string =
proc getChalkVersion*(withSuffix = true): string =
## Returns the value of `chalk_version` in `base_keyspecs.c4m`.
result = ""
const path = currentSourcePath().parentDir() / "configs" / "base_keyspecs.c4m"
for line in path.staticRead().splitLines():
const pattern = """chalk_version$s:=$s"$i.$i.$i$*"$."""
let (isMatch, major, minor, patch, suffix) = line.scanTuple(pattern)
if isMatch and major == 0 and minor in 0..100 and patch in 0..100 and suffix == "":
return $major & '.' & $minor & '.' & $patch
if isMatch and major >= 0 and minor >= 0 and patch >= 0:
let version = $major & '.' & $minor & '.' & $patch
if withSuffix:
return version & suffix
else:
return version
raise newException(ValueError, "Couldn't get `chalk_version` value from " & path)
2 changes: 1 addition & 1 deletion src/configs/base_keyspecs.c4m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
## the more basic per-op stuff such as "_CHALKS" and "ACTION_ID" I did not.

## CHALK SCHEMA
chalk_version := "0.3.3"
chalk_version := "0.3.3-dev"
ascii_magic := "dadfedabbadabbed"

# Field starting with an underscore (_) are "system" metadata fields, that
Expand Down
65 changes: 43 additions & 22 deletions src/semver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,53 @@ import std/strutils
# it only handles basic dot-separated version format

type Version* = ref object
major: int
minor: int
patch: int
name: string
major: int
minor: int
patch: int
suffix: string
name: string

proc parseVersion*(version: string): Version =
var
major = 0
minor = 0
patch = 0
major = 0
minor = 0
patch = 0
suffix = ""
let
name = version.strip(chars={'v', ',', '.'})
parts = name.split('.')
name = version.strip(chars={'V', 'v'}, trailing=false).strip(chars={',', '.', '-', '+'})
sections = name.split({'-', '+'}, maxsplit=1)
parts = sections[0].split('.')
case len(parts):
of 1:
major = parseInt(parts[0])
major = parseInt(parts[0])
of 2:
major = parseInt(parts[0])
minor = parseInt(parts[1])
major = parseInt(parts[0])
minor = parseInt(parts[1])
of 3:
major = parseInt(parts[0])
minor = parseInt(parts[1])
patch = parseInt(parts[2])
major = parseInt(parts[0])
minor = parseInt(parts[1])
patch = parseInt(parts[2])
else:
raise newException(ValueError, "Invalid or unsupported version format")
new result
result.name = name
result.major = major
result.minor = minor
result.patch = patch
if len(sections) == 2:
suffix = sections[1]
return Version(name: name,
major: major,
minor: minor,
patch: patch,
suffix: suffix)

# version parts tuple used for comparison
proc parts(self: Version): (int, int, int) =
return (self.major, self.minor, self.patch)
proc parts(self: Version): (int, int, int, string) =
# TODO how to compare suffix?
# for now treating any suffix as less than no suffix
# assumping any suffix is for pre-releases which is not ideal
# correct but it is fine for chalk versions
# this handles things like 1-dev < 1.0
# no suffix is normalized to highest ascii char code \u7f
# hence it is always greater then any legitimate ascii string
let suffix = if self.suffix == "": "\u7f" else: self.suffix
return (self.major, self.minor, self.patch, suffix)

proc `==`*(self: Version, other: Version): bool =
return self.parts() == other.parts()
Expand All @@ -69,33 +82,41 @@ proc `$`*(self: Version): string =

when isMainModule:
assert($(parseVersion("0.1")) == "0.1")
assert($(parseVersion("0.1-dev")) == "0.1-dev")
assert($(parseVersion("0.1.0")) == "0.1.0")
assert($(parseVersion("0.1.0-dev")) == "0.1.0-dev")

assert(parseVersion("0.1") == parseVersion("0.1.0"))
assert(parseVersion("0.1.0") == parseVersion("0.1.0"))
assert(not(parseVersion("0.1") == parseVersion("0.1.5")))
assert(not(parseVersion("0.1") == parseVersion("0.1-dev")))
assert(not(parseVersion("0.1.0") == parseVersion("0.1.5")))

assert(parseVersion("0.1") != parseVersion("0.1.5"))
assert(parseVersion("0.1") != parseVersion("0.1-dev"))
assert(parseVersion("0.1.0") != parseVersion("0.1.5"))
assert(not(parseVersion("0.1") != parseVersion("0.1")))
assert(not(parseVersion("0.1.0") != parseVersion("0.1")))

assert(parseVersion("0.1-dev") < parseVersion("0.1"))
assert(parseVersion("0.1") < parseVersion("0.1.5"))
assert(parseVersion("0.1.0") < parseVersion("0.1.5"))
assert(not(parseVersion("0.1") < parseVersion("0.1")))
assert(not(parseVersion("0.1") < parseVersion("0.1.0")))

assert(parseVersion("0.1-dev") <= parseVersion("0.1"))
assert(parseVersion("0.1") <= parseVersion("0.1.5"))
assert(parseVersion("0.1.0") <= parseVersion("0.1.5"))
assert(parseVersion("0.1") <= parseVersion("0.1"))
assert(parseVersion("0.1") <= parseVersion("0.1.0"))

assert(parseVersion("0.1") > parseVersion("0.1-dev"))
assert(parseVersion("0.1.5") > parseVersion("0.1"))
assert(parseVersion("0.1.5") > parseVersion("0.1.0"))
assert(not(parseVersion("0.1") > parseVersion("0.1")))
assert(not(parseVersion("0.1.0") > parseVersion("0.1")))

assert(parseVersion("0.1") >= parseVersion("0.1-dev"))
assert(parseVersion("0.1.5") >= parseVersion("0.1"))
assert(parseVersion("0.1.5") >= parseVersion("0.1.0"))
assert(parseVersion("0.1") >= parseVersion("0.1"))
Expand Down

0 comments on commit cf9ebf8

Please sign in to comment.