Skip to content

Commit

Permalink
Merge pull request #81 from lou15b/master
Browse files Browse the repository at this point in the history
Update to nim 0.10.2 coding convention
  • Loading branch information
dom96 committed Jan 4, 2015
2 parents e6bdf43 + ceb1e48 commit 4130771
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 352 deletions.
287 changes: 152 additions & 135 deletions src/nimble.nim

Large diffs are not rendered by default.

29 changes: 0 additions & 29 deletions src/nimblepkg/compat.nim

This file was deleted.

14 changes: 7 additions & 7 deletions src/nimblepkg/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
# BSD License. Look at license.txt for more info.
import parsecfg, streams, strutils, os

import tools, version
import tools, version, nimbletypes

type
TConfig* = object
Config* = object
nimbleDir*: string
chcp*: bool # Whether to change the code page in .cmd files on Win.


proc initConfig(): TConfig =
proc initConfig(): Config =
if getNimrodVersion() > newVersion("0.9.6"):
result.nimbleDir = getHomeDir() / ".nimble"
else:
result.nimbleDir = getHomeDir() / ".babel"

result.chcp = true

proc parseConfig*(): TConfig =
proc parseConfig*(): Config =
result = initConfig()
var confFile = getConfigDir() / "nimble" / "nimble.ini"

Expand All @@ -32,7 +32,7 @@ proc parseConfig*(): TConfig =

if f != nil:
echo("Reading from config file at ", confFile)
var p: TCfgParser
var p: CfgParser
open(p, f, confFile)
while true:
var e = next(p)
Expand All @@ -49,8 +49,8 @@ proc parseConfig*(): TConfig =
of "chcp":
result.chcp = parseBool(e.value)
else:
raise newException(ENimble, "Unable to parse config file:" &
raise newException(NimbleError, "Unable to parse config file:" &
" Unknown key: " & e.key)
of cfgError:
raise newException(ENimble, "Unable to parse config file: " & e.msg)
raise newException(NimbleError, "Unable to parse config file: " & e.msg)
close(p)
100 changes: 51 additions & 49 deletions src/nimblepkg/download.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,48 @@

import parseutils, os, osproc, strutils, tables, pegs

import packageinfo, version, tools
import packageinfo, version, tools, nimbletypes

type
TDownloadMethod* {.pure.} = enum
Git = "git", Hg = "hg"
DownloadMethod* {.pure.} = enum
git = "git", hg = "hg"

proc getSpecificDir(meth: TDownloadMethod): string =
proc getSpecificDir(meth: DownloadMethod): string =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
".git"
of TDownloadMethod.Hg:
of DownloadMethod.hg:
".hg"

proc doCheckout(meth: TDownloadMethod, downloadDir, branch: string) =
proc doCheckout(meth: DownloadMethod, downloadDir, branch: string) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
cd downloadDir:
# Force is used here because local changes may appear straight after a
# clone has happened. Like in the case of git on Windows where it
# messes up the damn line endings.
doCmd("git checkout --force " & branch)
of TDownloadMethod.Hg:
of DownloadMethod.hg:
cd downloadDir:
doCmd("hg checkout " & branch)

proc doPull(meth: TDownloadMethod, downloadDir: string) =
proc doPull(meth: DownloadMethod, downloadDir: string) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
doCheckout(meth, downloadDir, "master")
cd downloadDir:
doCmd("git pull")
if existsFile(".gitmodules"):
doCmd("git submodule update")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
doCheckout(meth, downloadDir, "default")
cd downloadDir:
doCmd("hg pull")

proc doClone(meth: TDownloadMethod, url, downloadDir: string, branch = "", tip = true) =
proc doClone(meth: DownloadMethod, url, downloadDir: string, branch = "",
tip = true) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
let
depthArg = if tip: "--depth 1 " else: ""
branchArg = if branch == "": "-b origin/master" else: "-b " & branch & " "
Expand All @@ -60,28 +61,28 @@ proc doClone(meth: TDownloadMethod, url, downloadDir: string, branch = "", tip =
doCmd("git reset --hard FETCH_HEAD")
doCmd("git checkout --force " & branchArg)
doCmd("git submodule update --init --recursive")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
let
tipArg = if tip: "-r tip " else: ""
branchArg = if branch == "": "" else: "-b " & branch & " "
doCmd("hg clone " & tipArg & branchArg & url & " " & downloadDir)

proc getTagsList(dir: string, meth: TDownloadMethod): seq[string] =
proc getTagsList(dir: string, meth: DownloadMethod): seq[string] =
cd dir:
var output = execProcess("git tag")
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
output = execProcess("git tag")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
output = execProcess("hg tags")
if output.len > 0:
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
result = @[]
for i in output.splitLines():
if i == "": continue
result.add(i)
of TDownloadMethod.Hg:
of DownloadMethod.hg:
result = @[]
for i in output.splitLines():
if i == "": continue
Expand All @@ -92,61 +93,61 @@ proc getTagsList(dir: string, meth: TDownloadMethod): seq[string] =
else:
result = @[]

proc getTagsListRemote*(url: string, meth: TDownloadMethod): seq[string] =
proc getTagsListRemote*(url: string, meth: DownloadMethod): seq[string] =
result = @[]
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
var (output, exitCode) = doCmdEx("git ls-remote --tags " & url)
if exitCode != QuitSuccess:
raise newException(EOS, "Unable to query remote tags for " & url &
raise newException(OSError, "Unable to query remote tags for " & url &
". Git returned: " & output)
for i in output.splitLines():
if i == "": continue
let start = i.find("refs/tags/")+"refs/tags/".len
let tag = i[start .. -1]
if not tag.endswith("^{}"): result.add(tag)

of TDownloadMethod.Hg:
of DownloadMethod.hg:
# http://stackoverflow.com/questions/2039150/show-tags-for-remote-hg-repository
raise newException(EInvalidValue, "Hg doesn't support remote tag querying.")
raise newException(ValueError, "Hg doesn't support remote tag querying.")

proc getVersionList*(tags: seq[string]): TTable[TVersion, string] =
proc getVersionList*(tags: seq[string]): Table[Version, string] =
# Returns: TTable of version -> git tag name
result = initTable[TVersion, string]()
result = initTable[Version, string]()
for tag in tags:
if tag != "":
let i = skipUntil(tag, Digits) # skip any chars before the version
# TODO: Better checking, tags can have any names. Add warnings and such.
result[newVersion(tag[i .. -1])] = tag

proc getDownloadMethod*(meth: string): TDownloadMethod =
proc getDownloadMethod*(meth: string): DownloadMethod =
case meth
of "git": return TDownloadMethod.Git
of "hg", "mercurial": return TDownloadMethod.Hg
of "git": return DownloadMethod.git
of "hg", "mercurial": return DownloadMethod.hg
else:
raise newException(ENimble, "Invalid download method: " & meth)
raise newException(NimbleError, "Invalid download method: " & meth)

proc getHeadName*(meth: TDownloadMethod): string =
proc getHeadName*(meth: DownloadMethod): string =
## Returns the name of the download method specific head. i.e. for git
## it's ``head`` for hg it's ``tip``.
case meth
of TDownloadMethod.Git: "head"
of TDownloadMethod.Hg: "tip"
of DownloadMethod.git: "head"
of DownloadMethod.hg: "tip"

proc checkUrlType*(url: string): TDownloadMethod =
proc checkUrlType*(url: string): DownloadMethod =
## Determines the download method based on the URL.
if doCmdEx("git ls-remote " & url).exitCode == QuitSuccess:
return TDownloadMethod.Git
return DownloadMethod.git
elif doCmdEx("hg identify " & url).exitCode == QuitSuccess:
return TDownloadMethod.Hg
return DownloadMethod.hg
else:
raise newException(ENimble, "Unable to identify url.")
raise newException(NimbleError, "Unable to identify url.")

proc isURL*(name: string): bool =
name.startsWith(peg" @'://' ")

proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
downMethod: TDownloadMethod) =
proc doDownload*(url: string, downloadDir: string, verRange: VersionRange,
downMethod: DownloadMethod) =
template getLatestByTag(meth: stmt): stmt {.dirty, immediate.} =
echo("Found tags...")
# Find latest version that fits our ``verRange``.
Expand All @@ -164,7 +165,7 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
## version range.
let pkginfo = getPkgInfo(downloadDir)
if pkginfo.version.newVersion notin verRange:
raise newException(ENimble,
raise newException(NimbleError,
"Downloaded package's version does not satisfy requested version " &
"range: wanted $1 got $2." %
[$verRange, $pkginfo.version])
Expand All @@ -177,14 +178,14 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
else:
# Mercurial requies a clone and checkout. The git clone operation is
# already fragmented into multiple steps so we just call doClone().
if downMethod == TDownloadMethod.Git:
if downMethod == DownloadMethod.git:
doClone(downMethod, url, downloadDir, $verRange.spe)
else:
doClone(downMethod, url, downloadDir, tip = false)
doCheckout(downMethod, downloadDir, $verRange.spe)
else:
case downMethod
of TDownloadMethod.Git:
of DownloadMethod.git:
# For Git we have to query the repo remotely for its tags. This is
# necessary as cloning with a --depth of 1 removes all tag info.
let versions = getTagsListRemote(url, downMethod).getVersionList()
Expand All @@ -197,7 +198,7 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
doClone(downMethod, url, downloadDir) # Grab HEAD.

verifyClone()
of TDownloadMethod.Hg:
of DownloadMethod.hg:
doClone(downMethod, url, downloadDir)
let versions = getTagsList(downloadDir, downMethod).getVersionList()

Expand All @@ -208,10 +209,10 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,

verifyClone()

proc echoPackageVersions*(pkg: TPackage) =
proc echoPackageVersions*(pkg: Package) =
let downMethod = pkg.downloadMethod.getDownloadMethod()
case downMethod
of TDownloadMethod.Git:
of DownloadMethod.git:
try:
let versions = getTagsListRemote(pkg.url, downMethod).getVersionList()
if versions.len > 0:
Expand All @@ -225,7 +226,8 @@ proc echoPackageVersions*(pkg: TPackage) =
echo(" versions: " & vstr)
else:
echo(" versions: (No versions tagged in the remote repository)")
except EOS:
except OSError:
echo(getCurrentExceptionMsg())
of TDownloadMethod.Hg:
echo(" versions: (Remote tag retrieval not supported by " & pkg.downloadMethod & ")")
of DownloadMethod.hg:
echo(" versions: (Remote tag retrieval not supported by " &
pkg.downloadMethod & ")")
7 changes: 7 additions & 0 deletions src/nimblepkg/nimbletypes.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# BSD License. Look at license.txt for more info.
#
# Various miscellaneous common types reside here, to avoid problems with
# recursive imports

type
NimbleError* = object of Exception
Loading

0 comments on commit 4130771

Please sign in to comment.