Skip to content

Commit

Permalink
distros.nim: better documenation
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Dec 31, 2016
1 parent 33b8ade commit f6c2c4a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/pure/distros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
#

## This module implements the basics for Linux distribution ("distro")
## detection and the OS's native package manager. Its primary purpose is in
## producing output for Nimble packages like::
## detection and the OS's native package manager. Its primary purpose is to
## produce output for Nimble packages like::
##
## To complete the installation, run:
##
## sudo apt-get libblas-dev
## sudo apt-get libvoodoo
##
## The above output could be the result of a code snippet like:
##
## .. code-block:: nim
##
## if detectOs(Ubuntu):
## foreignDep "lbiblas-dev"
## foreignDep "libvoodoo"
##

from strutils import contains, toLowerAscii

when not defined(nimscript):
from osproc import execProcess

type
Distribution* {.pure.} = enum ## an enum so that the poor programmer
## cannot introduce typos
Distribution* {.pure.} = enum ## the list of known distributions
Windows ## some version of Windows
Posix ## some Posix system
MacOSX ## some version of OSX
Expand Down Expand Up @@ -161,20 +168,25 @@ proc detectOsImpl(d: Distribution): bool =
result = toLowerAscii($d) in toLowerAscii(uname())

template detectOs*(d: untyped): bool =
## Distro/OS detection. For convenience the
## required ``Distribution.`` qualifier is added to the
## enum value.
detectOsImpl(Distribution.d)

when not defined(nimble):
var foreignDeps: seq[string] = @[]

proc foreignCmd*(cmd: string; requiresSudo=false) =
## Registers a foreign command to the intern list of commands
## that can be queried later.
let c = (if requiresSudo: "sudo " else: "") & cmd
when defined(nimble):
nimscriptapi.foreignDeps.add(c)
else:
foreignDeps.add(c)

proc foreignDepInstallCmd*(foreignPackageName: string): (string, bool) =
## returns the distro's native command line to install 'foreignPackageName'
## Returns the distro's native command line to install 'foreignPackageName'
## and whether it requires root/admin rights.
let p = foreignPackageName
when defined(windows):
Expand Down Expand Up @@ -211,16 +223,17 @@ proc foreignDepInstallCmd*(foreignPackageName: string): (string, bool) =
result = ("brew install " & p, true)

proc foreignDep*(foreignPackageName: string) =
## Registers 'foreignPackageName' to the internal list of foreign deps.
## It is your job to ensure the package name
let (installCmd, sudo) = foreignDepInstallCmd(foreignPackageName)
foreignCmd installCmd, sudo

proc echoForeignDeps*() =
## Writes the list of registered foreign deps to stdout.
echo "To finish the installation, run:"
for d in foreignDeps:

This comment has been minimized.

Copy link
@dom96

dom96 Dec 31, 2016

Contributor

Please make sure that Nimble can capture the value of this variable. Nimble has its own output system and it would be nice to use it.

This comment has been minimized.

Copy link
@Araq

Araq Dec 31, 2016

Author Member

Nimble has its own implementation in the according PR and doesn't use this primitive solution.

   when defined(nimble):
      nimscriptapi.foreignDeps.add(c)
echo d

when isMainModule:
when false:
foreignDep("libblas-dev")
foreignDep "libfoo"
echoForeignDeps()

0 comments on commit f6c2c4a

Please sign in to comment.