Skip to content

Commit

Permalink
1.8.2: add compile-time projectVersion()
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Nov 1, 2019
1 parent 4962e60 commit e1e83a9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 24 deletions.
80 changes: 69 additions & 11 deletions bump.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import strformat
import nre
import logging

from macros import nil


type
Version* = tuple
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion bump.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "1.8.1"
version = "1.8.2"
author = "disruptek"
description = "a tiny tool to bump nimble versions"
license = "MIT"
Expand Down
31 changes: 19 additions & 12 deletions docs/bump.html
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ <h1 class="title">bump</h1>
<li><a class="reference" href="#isValid%2CVersion"
title="isValid(ver: Version): bool"><wbr />is<wbr />Valid<span class="attachedType">Version</span></a></li>
<li><a class="reference" href="#parseVersion%2Cstring"
title="parseVersion(line: string): Option[Version]"><wbr />parse<wbr />Version<span class="attachedType">Version</span></a></li>
title="parseVersion(nimble: string): Option[Version]"><wbr />parse<wbr />Version<span class="attachedType">Version</span></a></li>
<li><a class="reference" href="#bumpVersion%2CVersion"
title="bumpVersion(ver: Version; major, minor, patch = false): Option[Version]"><wbr />bump<wbr />Version<span class="attachedType">Version</span></a></li>
<li><a class="reference" href="#withCrazySpaces%2CVersion%2Cstring"
Expand All @@ -872,6 +872,8 @@ <h1 class="title">bump</h1>
title="bump(minor = false; major = false; patch = true; release = false; dry_run = false; folder = &quot;&quot;;
nimble = &quot;&quot;; log_level = logLevel; commit = false; v = false; manual = &quot;&quot;;
message: seq[string]): int"><wbr />bump<span class="attachedType"></span></a></li>
<li><a class="reference" href="#projectVersion%2Cstring"
title="projectVersion(hint = &quot;&quot;): Option[Version]"><wbr />project<wbr />Version<span class="attachedType">Version</span></a></li>

</ul>
</li>
Expand Down Expand Up @@ -944,12 +946,10 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>

</dd>
<a id="parseVersion,string"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#parseVersion%2Cstring"><span class="Identifier">parseVersion</span></a><span class="Other">(</span><span class="Identifier">line</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Option</span><span class="Other">[</span><a href="bump.html#Version"><span class="Identifier">Version</span></a><span class="Other">]</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">FieldError</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">,</span>
<span class="Identifier">AccessViolationError</span><span class="Other">,</span> <span class="Identifier">RegexInternalError</span><span class="Other">,</span> <span class="Identifier">InvalidUnicodeError</span><span class="Other">,</span> <span class="Identifier">KeyError</span><span class="Other">,</span>
<span class="Identifier">SyntaxError</span><span class="Other">,</span> <span class="Identifier">StudyError</span><span class="Other">,</span> <span class="Identifier">IndexError</span><span class="Other">,</span> <span class="Identifier">UnpackError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#parseVersion%2Cstring"><span class="Identifier">parseVersion</span></a><span class="Other">(</span><span class="Identifier">nimble</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Option</span><span class="Other">[</span><a href="bump.html#Version"><span class="Identifier">Version</span></a><span class="Other">]</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

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

</dd>
<a id="bumpVersion,Version"></a>
Expand Down Expand Up @@ -1049,16 +1049,23 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
<a id="bump,string,string,string,seq[T][string]"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#bump%2Cstring%2Cstring%2Cstring%2Cseq%5BT%5D%5Bstring%5D"><span class="Identifier">bump</span></a><span class="Other">(</span><span class="Identifier">minor</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">major</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">patch</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">;</span> <span class="Identifier">release</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">dry_run</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span>
<span class="Identifier">folder</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">;</span> <span class="Identifier">nimble</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">;</span> <span class="Identifier">log_level</span> <span class="Other">=</span> <span class="Identifier">logLevel</span><span class="Other">;</span> <span class="Identifier">commit</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">v</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span>
<span class="Identifier">manual</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">;</span> <span class="Identifier">message</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">FieldError</span><span class="Other">,</span>
<span class="Identifier">ValueError</span><span class="Other">,</span> <span class="Identifier">AccessViolationError</span><span class="Other">,</span> <span class="Identifier">RegexInternalError</span><span class="Other">,</span> <span class="Identifier">InvalidUnicodeError</span><span class="Other">,</span>
<span class="Identifier">KeyError</span><span class="Other">,</span> <span class="Identifier">SyntaxError</span><span class="Other">,</span> <span class="Identifier">StudyError</span><span class="Other">,</span> <span class="Identifier">IndexError</span><span class="Other">,</span> <span class="Identifier">UnpackError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">IOError</span><span class="Other">,</span>
<span class="Identifier">Defect</span><span class="Other">,</span> <span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">,</span> <span class="Identifier">ReadDirEffect</span><span class="Other">,</span> <span class="Identifier">ReadEnvEffect</span><span class="Other">,</span>
<span class="Identifier">ReadIOEffect</span><span class="Other">,</span> <span class="Identifier">WriteDirEffect</span><span class="Other">,</span>
<span class="Identifier">WriteIOEffect</span><span class="Other">,</span> <span class="Identifier">ExecIOEffect</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<span class="Identifier">manual</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">;</span> <span class="Identifier">message</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">,</span>
<span class="Identifier">UnpackError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">IOError</span><span class="Other">,</span> <span class="Identifier">FieldError</span><span class="Other">,</span> <span class="Identifier">AccessViolationError</span><span class="Other">,</span>
<span class="Identifier">RegexInternalError</span><span class="Other">,</span> <span class="Identifier">InvalidUnicodeError</span><span class="Other">,</span> <span class="Identifier">KeyError</span><span class="Other">,</span> <span class="Identifier">SyntaxError</span><span class="Other">,</span> <span class="Identifier">StudyError</span><span class="Other">,</span>
<span class="Identifier">IndexError</span><span class="Other">,</span> <span class="Identifier">Defect</span><span class="Other">,</span> <span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">,</span> <span class="Identifier">ReadDirEffect</span><span class="Other">,</span>
<span class="Identifier">ReadEnvEffect</span><span class="Other">,</span> <span class="Identifier">ReadIOEffect</span><span class="Other">,</span> <span class="Identifier">WriteDirEffect</span><span class="Other">,</span> <span class="Identifier">WriteIOEffect</span><span class="Other">,</span> <span class="Identifier">ExecIOEffect</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

the entry point from the cli

</dd>
<a id="projectVersion,string"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#projectVersion%2Cstring"><span class="Identifier">projectVersion</span></a><span class="Other">(</span><span class="Identifier">hint</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Option</span><span class="Other">[</span><a href="bump.html#Version"><span class="Identifier">Version</span></a><span class="Other">]</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">compileTime</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">ReadDirEffect</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

try to get the version from the current (compile-time) project

</dd>

</dl></div>
Expand All @@ -1070,7 +1077,7 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small>Made with Nim. Generated: 2019-10-28 20:30:51 UTC</small>
<small>Made with Nim. Generated: 2019-11-01 22:30:00 UTC</small>
</div>
</div>
</div>
Expand Down

0 comments on commit e1e83a9

Please sign in to comment.