Small improvements for the distros module (#17309)

This commit is contained in:
konsumlamm 2021-03-09 18:41:07 +01:00 • committed by GitHub
commit 083eeda085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,7 @@
## This module implements the basics for Linux distribution ("distro") ## This module implements the basics for Linux distribution ("distro")
## detection and the OS's native package manager. Its primary purpose is to ## detection and the OS's native package manager. Its primary purpose is to
## produce output for Nimble packages like:: ## produce output for Nimble packages, like::
## ##
## To complete the installation, run: ## To complete the installation, run:
## ##
@ -27,16 +27,16 @@
## ##
## See `packaging <packaging.html>`_ for hints on distributing Nim using OS packages. ## See `packaging <packaging.html>`_ for hints on distributing Nim using OS packages.
from strutils import contains, toLowerAscii from std/strutils import contains, toLowerAscii
when not defined(nimscript): when not defined(nimscript):
from osproc import execProcess from std/osproc import execProcess
from os import existsEnv from std/os import existsEnv
type type
Distribution* {.pure.} = enum ## the list of known distributions Distribution* {.pure.} = enum ## the list of known distributions
Windows ## some version of Windows Windows ## some version of Windows
Posix ## some Posix system Posix ## some POSIX system
MacOSX ## some version of OSX MacOSX ## some version of OSX
Linux ## some version of Linux Linux ## some version of Linux
Ubuntu Ubuntu
@ -133,8 +133,7 @@ type
const const
LacksDevPackages* = {Distribution.Gentoo, Distribution.Slackware, LacksDevPackages* = {Distribution.Gentoo, Distribution.Slackware, Distribution.ArchLinux}
Distribution.ArchLinux}
# we cache the result of the 'cmdRelease' # we cache the result of the 'cmdRelease'
# execution for faster platform detections. # execution for faster platform detections.
@ -157,8 +156,7 @@ proc detectOsWithAllCmd(d: Distribution): bool =
proc detectOsImpl(d: Distribution): bool = proc detectOsImpl(d: Distribution): bool =
case d case d
of Distribution.Windows: ## some version of Windows of Distribution.Windows: result = defined(windows)
result = defined(windows)
of Distribution.Posix: result = defined(posix) of Distribution.Posix: result = defined(posix)
of Distribution.MacOSX: result = defined(macosx) of Distribution.MacOSX: result = defined(macosx)
of Distribution.Linux: result = defined(linux) of Distribution.Linux: result = defined(linux)
@ -183,8 +181,8 @@ proc detectOsImpl(d: Distribution): bool =
of Distribution.ArchLinux: of Distribution.ArchLinux:
result = "arch" in osReleaseID() result = "arch" in osReleaseID()
of Distribution.NixOS: of Distribution.NixOS:
result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE")
# Check if this is a Nix build or NixOS environment # Check if this is a Nix build or NixOS environment
result = existsEnv("NIX_BUILD_TOP") or existsEnv("__NIXOS_SET_ENVIRONMENT_DONE")
of Distribution.OpenSUSE: of Distribution.OpenSUSE:
result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release()) result = "suse" in toLowerAscii(uname()) or "suse" in toLowerAscii(release())
of Distribution.GoboLinux: of Distribution.GoboLinux:
@ -200,7 +198,7 @@ proc detectOsImpl(d: Distribution): bool =
result = false result = false
template detectOs*(d: untyped): bool = template detectOs*(d: untyped): bool =
## Distro/OS detection. For convenience the ## Distro/OS detection. For convenience, the
## required `Distribution.` qualifier is added to the ## required `Distribution.` qualifier is added to the
## enum value. ## enum value.
detectOsImpl(Distribution.d) detectOsImpl(Distribution.d)
@ -209,7 +207,7 @@ when not defined(nimble):
var foreignDeps: seq[string] = @[] var foreignDeps: seq[string] = @[]
proc foreignCmd*(cmd: string; requiresSudo = false) = proc foreignCmd*(cmd: string; requiresSudo = false) =
## Registers a foreign command to the intern list of commands ## Registers a foreign command to the internal list of commands
## that can be queried later. ## that can be queried later.
let c = (if requiresSudo: "sudo " else: "") & cmd let c = (if requiresSudo: "sudo " else: "") & cmd
when defined(nimble): when defined(nimble):
@ -218,11 +216,11 @@ proc foreignCmd*(cmd: string; requiresSudo = false) =
foreignDeps.add(c) foreignDeps.add(c)
proc foreignDepInstallCmd*(foreignPackageName: string): (string, bool) = proc foreignDepInstallCmd*(foreignPackageName: string): (string, bool) =
## Returns the distro's native command line to install 'foreignPackageName' ## Returns the distro's native command to install `foreignPackageName`
## and whether it requires root/admin rights. ## and whether it requires root/admin rights.
let p = foreignPackageName let p = foreignPackageName
when defined(windows): when defined(windows):
result = ("Chocolatey install " & p, false) result = ("choco install " & p, false)
elif defined(bsd): elif defined(bsd):
result = ("ports install " & p, true) result = ("ports install " & p, true)
elif defined(linux): elif defined(linux):
@ -261,17 +259,12 @@ proc foreignDepInstallCmd*(foreignPackageName: string): (string, bool) =
result = ("brew install " & p, false) result = ("brew install " & p, false)
proc foreignDep*(foreignPackageName: string) = proc foreignDep*(foreignPackageName: string) =
## Registers 'foreignPackageName' to the internal list of foreign deps. ## Registers `foreignPackageName` to the internal list of foreign deps.
## It is your job to ensure the package name ## It is your job to ensure that the package name is correct.
let (installCmd, sudo) = foreignDepInstallCmd(foreignPackageName) let (installCmd, sudo) = foreignDepInstallCmd(foreignPackageName)
foreignCmd installCmd, sudo foreignCmd(installCmd, sudo)
proc echoForeignDeps*() = proc echoForeignDeps*() =
## Writes the list of registered foreign deps to stdout. ## Writes the list of registered foreign deps to stdout.
for d in foreignDeps: for d in foreignDeps:
echo d echo d
when false:
foreignDep("libblas-dev")
foreignDep "libfoo"
echoForeignDeps()