src/grab

  Source   Edit

Adds a grab statement for installing and importing Nimble packages directly through code.

Works with NimScript.

import grab

grab "regex"

assert "abc.123".match(re"\w+\.\d+")

grab package("-Y https://github.com/arnetheduck/nim-result@#HEAD",
             name = "result", forceInstall = true):
  import results

func works(): Result[int, string] =
  result.ok(123)

func fails(): Result[int, string] =
  result.err("abc")

assert works().isOk
assert fails().error == "abc"

Types

Package = object
  name*, installCommand*, pathQuery*: string
  forceInstall*: bool
Package information to be used when installing and importing packages.   Source   Edit

Procs

proc package(installCommand, name, pathQuery: string; forceInstall = false): Package {.
    ...raises: [], tags: [].}
Generates package information with arguments to a nimble install command, package name, and optionally a name and version pair for the purpose of querying the module path.   Source   Edit
proc package(installCommand, name: string; forceInstall = false): Package {.
    ...raises: [], tags: [].}
Generates package information with arguments to a nimble install command and a package name (optionally with a version).   Source   Edit
proc package(installCommand: string; forceInstall = false): Package {.
    ...raises: [], tags: [].}

Converts the arguments of a nimble install command into package information.

If the name of the package is different from the one assumed from the install command, then the package cannot be imported. In this case, a name or name and version pair must be given, such as package("fakename", "realname@0.1.0").

  Source   Edit

Macros

macro grab(installCommand: static string; imports: untyped)

Shorthand for grab(package(installCommand), imports).

See module documentation for usage.

  Source   Edit
macro grab(package)
Calls grab(package, imports) with the main module deduced from the package name imported by default.   Source   Edit
macro grab(package: static Package; imports: untyped)

Installs a package with Nimble and immediately imports it.

Can be followed with a list of imports from the package in an indented block. Imports outside this block will not work. By default, only the main module of the package is imported.

This installs the package globally, and can fairly affect compilation time. For this reason it should only be used for scripts and snippets and the like.

If the package is already installed, it will not reinstall it. This can be overriden by adding -Y at the start of the install command.

See module documentation for usage.

  Source   Edit