From f972236ed9dd1ffddf9f0c7539716f494e63e122 Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 22 Dec 2016 12:41:20 +0100 Subject: [PATCH] outline of how the support for native package managers should look like --- src/nimblepkg/nimscriptapi.nim | 57 +++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/nimblepkg/nimscriptapi.nim b/src/nimblepkg/nimscriptapi.nim index 1d7c4bd..d9b36cc 100644 --- a/src/nimblepkg/nimscriptapi.nim +++ b/src/nimblepkg/nimscriptapi.nim @@ -41,4 +41,59 @@ template builtin = discard proc getPkgDir*(): string = ## Returns the package directory containing the .nimble file currently ## being evaluated. - builtin \ No newline at end of file + builtin + +from strutils import contains + +type + Distribution* {.pure.} = enum ## an enum so that the poor programmer + ## cannot introduce typos + Windows, ## some version of Windows + Posix, ## some Posix system + MacOSX, ## some version of OSX + Linux, ## some version of Linux + Ubuntu, + Gentoo, + Fedora, + RedHat, + BSD, + FreeBSD, + OpenBSD + +proc detectOsImpl(d: Distribution): bool = + case d + of Distribution.Windows: ## some version of Windows + result = defined(windows) + of Distribution.Posix: result = defined(posix) + of Distribution.MacOSX: result = defined(macosx) + of Distribution.Linux: result = defined(linux) + of Distribution.Ubuntu, Distribution.Gentoo, Distribution.FreeBSD, + Distribution.OpenBSD, Distribution.Fedora: + result = $d in gorge"uname" + of Distribution.RedHat: + result = "Red Hat" in gorge"uname" + of Distribution.BSD: result = defined(bsd) + +template detectOs*(d: untyped): bool = + detectOsImpl(Distribution.d) + +var foreignDeps: seq[string] = @[] + +proc foreignCmd*(cmd: string; requiresSudo=false) = + foreignDeps.add((if requiresSudo: "sudo " else: "") & cmd) + +proc foreignDep*(foreignPackageName: string) = + let p = foreignPackageName + when defined(windows): + foreignCmd "Chocolatey install " & p + elif defined(bsd): + foreignCmd "ports install " & p, true + elif defined(linux): + if detectOs(Ubuntu): + foreignCmd "apt-get install " & p, true + elif detectOs(Gentoo): + foreignCmd "emerge install " & p, true + elif detectOs(Fedora): + foreignCmd "yum install " & p, true + else: + discard