moved new feature to separate 'distros.nim' module

This commit is contained in:
Araq 2016-12-23 16:01:10 +01:00
commit b3b4c6343f
6 changed files with 99 additions and 66 deletions

View file

@ -939,6 +939,10 @@ proc doAction(options: Options) =
let (_, pkgInfo) = install(options.action.packages, options)
if options.action.packages.len == 0:
nimScriptHint(pkgInfo)
if pkgInfo.foreignDeps.len > 0:
echo("To finish the installation, run: ")
for i in 0..<pkgInfo.foreignDeps.len:
echo(pkgInfo.foreignDeps[i])
of actionUninstall:
uninstall(options)
of actionSearch:

View file

@ -35,6 +35,7 @@ when not defined(nimscript):
binDir*: string
srcDir*: string
backend*: string
foreignDeps*: seq[string]
const
nimbleVersion* = "0.7.11"

59
src/nimblepkg/distros.nim Normal file
View file

@ -0,0 +1,59 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
## This module is implicitly imported in NimScript .nimble files.
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 -a"
of Distribution.RedHat:
result = "Red Hat" in gorge"uname -a"
of Distribution.BSD: result = defined(bsd)
template detectOs*(d: untyped): bool =
detectOsImpl(Distribution.d)
proc foreignCmd*(cmd: string; requiresSudo=false) =
nimscriptapi.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
elif detectOs(RedHat):
foreignCmd "rpm install " & p, true
else:
discard

View file

@ -19,6 +19,9 @@ var
installExt*, bin*: seq[string] = @[] ## Nimble metadata.
requiresData*: seq[string] = @[] ## The package's dependencies.
foreignDeps*: seq[string] = @[] ## The foreign dependencies. Only
## exported for 'distros.nim'.
proc requires*(deps: varargs[string]) =
## Call this to set the list of requirements of your Nimble
## package.
@ -42,58 +45,3 @@ proc getPkgDir*(): string =
## Returns the package directory containing the .nimble file currently
## being evaluated.
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

View file

@ -200,13 +200,21 @@ proc findNimscriptApi(options: Options): string =
proc getNimPrefixDir(): string = splitPath(findExe("nim")).head.parentDir
when declared(ModuleGraph):
var graph: ModuleGraph
proc execScript(scriptName: string, flags: StringTableRef,
options: Options): PSym =
## Executes the specified script. Returns the script's module symbol.
##
## No clean up is performed and must be done manually!
if "nimblepkg/nimscriptapi" notin compiler_options.implicitIncludes:
compiler_options.implicitIncludes.add("nimblepkg/nimscriptapi")
when declared(resetAllModulesHard):
# for compatibility with older Nim versions:
if "nimblepkg/nimscriptapi" notin compiler_options.implicitIncludes:
compiler_options.implicitIncludes.add("nimblepkg/nimscriptapi")
else:
if "nimblepkg/nimscriptapi" notin compiler_options.implicitImports:
compiler_options.implicitImports.add("nimblepkg/nimscriptapi")
# Ensure the compiler can find its standard library #220.
compiler_options.gPrefixDir = getNimPrefixDir()
@ -241,7 +249,7 @@ proc execScript(scriptName: string, flags: StringTableRef,
when declared(resetAllModulesHard):
result = makeModule(scriptName)
else:
let graph = newModuleGraph()
graph = newModuleGraph()
result = graph.makeModule(scriptName)
incl(result.flags, sfMainModule)
@ -301,24 +309,35 @@ proc readPackageInfoFromNims*(scriptName: string, options: Options,
# Execute the nimscript file.
let thisModule = execScript(scriptName, nil, options)
when declared(resetAllModulesHard):
let apiModule = thisModule
else:
var apiModule: PSym
for i in 0..<graph.modules.len:
if graph.modules[i] != nil and
graph.modules[i].name.s == "nimscriptapi":
apiModule = graph.modules[i]
break
doAssert apiModule != nil
# Check whether an error has occurred.
if msgs.gErrorCounter > 0:
raise newException(NimbleError, previousMsg)
# Extract all the necessary fields populated by the nimscript file.
proc getSym(thisModule: PSym, ident: string): PSym =
result = thisModule.tab.strTableGet(getIdent(ident))
proc getSym(apiModule: PSym, ident: string): PSym =
result = apiModule.tab.strTableGet(getIdent(ident))
if result.isNil:
raise newException(NimbleError, "Ident not found: " & ident)
template trivialField(field) =
result.field = getGlobal(getSym(thisModule, astToStr field))
result.field = getGlobal(getSym(apiModule, astToStr field))
template trivialFieldSeq(field) =
result.field.add getGlobalAsSeq(getSym(thisModule, astToStr field))
result.field.add getGlobalAsSeq(getSym(apiModule, astToStr field))
# keep reasonable default:
let name = getGlobal(thisModule.tab.strTableGet(getIdent"packageName"))
let name = getGlobal(apiModule.tab.strTableGet(getIdent"packageName"))
if name.len > 0: result.name = name
trivialField version
@ -333,14 +352,15 @@ proc readPackageInfoFromNims*(scriptName: string, options: Options,
trivialFieldSeq installDirs
trivialFieldSeq installFiles
trivialFieldSeq installExt
trivialFieldSeq foreignDeps
extractRequires(getSym(thisModule, "requiresData"), result.requires)
extractRequires(getSym(apiModule, "requiresData"), result.requires)
let binSeq = getGlobalAsSeq(getSym(thisModule, "bin"))
let binSeq = getGlobalAsSeq(getSym(apiModule, "bin"))
for i in binSeq:
result.bin.add(i.addFileExt(ExeExt))
let backend = getGlobal(getSym(thisModule, "backend"))
let backend = getGlobal(getSym(apiModule, "backend"))
if backend.len == 0:
result.backend = "c"
elif cmpIgnoreStyle(backend, "javascript") == 0:

View file

@ -37,6 +37,7 @@ proc initPackageInfo*(path: string): PackageInfo =
result.installFiles = @[]
result.installExt = @[]
result.requires = @[]
result.foreignDeps = @[]
result.bin = @[]
result.srcDir = ""
result.binDir = ""