Added a debug var to control verbosity of status messages being outputted.

This commit is contained in:
dom96 2011-02-19 18:54:38 +00:00
commit ee0fd7148f
2 changed files with 65 additions and 32 deletions

View file

@ -5,6 +5,12 @@ type
TDepend = tuple[name: String, verRange: PVersionRange] TDepend = tuple[name: String, verRange: PVersionRange]
var debug* = 1 ## 0 = no messages, 1 - 3 = less to more verbose respectively.
proc echoD*(s: string, verbosity: int = 1, nl: bool = True) =
if debug >= verbosity:
stdout.write(s & (if nl: "\n" else: ""))
proc getBabelDir(): string = proc getBabelDir(): string =
when defined(windows): when defined(windows):
result = getHomeDir() / "babel" result = getHomeDir() / "babel"
@ -28,7 +34,7 @@ proc getNimVersion(cmd: string = "nimrod"): String =
proc compile*(file: string, flags: string = "") = proc compile*(file: string, flags: string = "") =
var args: string = flags & "c " & file var args: string = flags & "c " & file
echo("Compiling " & file & "...") echoD("Compiling " & file & "...")
var code = execShellCmd(findExe("nimrod") & " " & args) var code = execShellCmd(findExe("nimrod") & " " & args)
if code != quitSuccess: if code != quitSuccess:
raise newException(EInstall, "Compilation failed: Nimrod returned exit code " & raise newException(EInstall, "Compilation failed: Nimrod returned exit code " &
@ -61,19 +67,8 @@ proc dependExists(name: string, verRange: PVersionRange): Bool =
proc verifyDepends(proj: TProject): seq[TDepend] = proc verifyDepends(proj: TProject): seq[TDepend] =
result = @[] result = @[]
for i in items(proj.depends): for nameStr, verStr in items(proj.depends):
var spl = i.split() echoD(" " & nameStr & " " & verStr & "...", 2, False)
var nameStr = ""
var verStr = ""
if spl.len == 1:
nameStr = spl[0]
elif spl.len > 1:
nameStr = spl[0]
spl.delete(0)
verStr = join(spl, " ")
else:
raise newException(EInstall, "Incorrect dependency got: " & i)
var verRange: PVersionRange var verRange: PVersionRange
if verStr == "": if verStr == "":
new(verRange) new(verRange)
@ -83,27 +78,30 @@ proc verifyDepends(proj: TProject): seq[TDepend] =
if not dependExists(nameStr, verRange): if not dependExists(nameStr, verRange):
result.add((nameStr, verRange)) result.add((nameStr, verRange))
echoD(" FAIL", 2)
else:
echoD(" OK", 2)
proc createDirDebug(dir: string) = proc createDirDebug(dir: string) =
# Have to check, so that it doesn't echo... # Have to check, so that it doesn't echo...
if not existsDir(dir): if not existsDir(dir):
stdout.write("Creating directory " & dir & "...") echoD("Creating directory " & dir & "...", 3, False)
createDir(dir) createDir(dir)
echo(" Done!") echoD(" Done!", 3)
proc createDirs(dirs: seq[string]) = proc createDirs(dirs: seq[string]) =
for i in items(dirs): for i in items(dirs):
createDirDebug(i) createDirDebug(i)
proc copyFileDebug(file, copyTo: string) = proc copyFileDebug(file, copyTo: string) =
stdout.write("Copying " & file & " to " & copyTo & "...") echoD("Copying " & file & " to " & copyTo & "...", 3, False)
copyFile(file, copyTo) copyFile(file, copyTo)
echo(" Done!") echoD(" Done!", 3)
proc moveFileDebug(file, moveTo: string) = proc moveFileDebug(file, moveTo: string) =
stdout.write("Moving " & file & " to " & moveTo & "...") echoD("Moving " & file & " to " & moveTo & "...", 3, False)
moveFile(file, moveTo) moveFile(file, moveTo)
echo(" Done!") echoD(" Done!", 3)
proc copyFiles(proj: TProject) = proc copyFiles(proj: TProject) =
# This will create a $home/.babel and lib/ or bin/. It will also copy all the # This will create a $home/.babel and lib/ or bin/. It will also copy all the
@ -161,7 +159,6 @@ proc upgrade(proj: TProject) =
var ver = "" var ver = ""
# This will have a dot at the end, it doesn't cause trouble though. # This will have a dot at the end, it doesn't cause trouble though.
discard file.parseToken(ver, digits + {'.'}, proj.name.len() + 1) discard file.parseToken(ver, digits + {'.'}, proj.name.len() + 1)
echo(ver)
if ver > latestVersion: if ver > latestVersion:
latestVersion = ver latestVersion = ver
path = confPath path = confPath
@ -169,7 +166,7 @@ proc upgrade(proj: TProject) =
assert(path != "") assert(path != "")
if proj.library: if proj.library:
echo("Reading " & path & "...") echoD("Reading " & path & "...", 3)
var latestConf = parseBabel(path) var latestConf = parseBabel(path)
var newVerDir = babelDir / "lib" / latestConf.name / latestConf.version var newVerDir = babelDir / "lib" / latestConf.name / latestConf.version
createDirDebug(newVerDir) createDirDebug(newVerDir)
@ -194,7 +191,7 @@ proc install*(name: string, filename: string = "") =
else: else:
path = filename / name.addFileExt("babel") path = filename / name.addFileExt("babel")
echo("Reading ", path, "...") echoD("Reading " & path & "...", 3)
babelFile = parseBabel(path) babelFile = parseBabel(path)
var ret = babelFile.verify() var ret = babelFile.verify()
@ -210,24 +207,24 @@ proc install*(name: string, filename: string = "") =
upgrade = True upgrade = True
if babelFile.depends.len == 1: if babelFile.depends.len == 1:
echo("Verifying 1 dependency...") echoD("Verifying 1 dependency...")
else: else:
echo("Verifying ", babelFile.depends.len(), " dependencies...") echoD("Verifying " & $babelFile.depends.len() & " dependencies...")
var dependsNeeded = babelFile.verifyDepends() var dependsNeeded = babelFile.verifyDepends()
if dependsNeeded.len() > 0: if dependsNeeded.len() > 0:
raise newException(EInstall, "TODO: Download & Install dependencies.") raise newException(EInstall, "TODO: Download & Install dependencies.")
else: else:
echo("All dependencies verified!") echoD("All dependencies verified!")
if not upgrade: if not upgrade:
echo("Installing " & babelFile.name & "-" & babelFile.version & "...") echoD("Installing " & babelFile.name & "-" & babelFile.version & "...")
babelFile.copyFiles() babelFile.copyFiles()
else: else:
echo("Upgrading " & babelFile.name & " to version " & echoD("Upgrading " & babelFile.name & " to version " &
babelFile.version & "...") babelFile.version & "...")
babelFile.upgrade() babelFile.upgrade()
echo("Package " & babelFile.name & " successfully installed.") echoD("Package " & babelFile.name & " successfully installed.")
when isMainModule: when isMainModule:
install(paramStr(1)) install(paramStr(1))

View file

@ -11,7 +11,7 @@ type
homepage*: String homepage*: String
library*: bool library*: bool
depends*: seq[string] # Dependencies depends*: seq[tuple[name, vRange: string]] # Dependencies
modules*: seq[string] # ExtraModules modules*: seq[string] # ExtraModules
files*: seq[string] # files files*: seq[string] # files
@ -49,6 +49,38 @@ proc parseList(s: string): seq[string] =
var many = s.split({',', ';'}) var many = s.split({',', ';'})
for i in items(many): for i in items(many):
result.add(i.strip()) result.add(i.strip())
proc skipUntil(s, token: string, o: var string): int =
## Captures each char until `token` is found, sets `o` to the captured string,
## and returns the number of characters captured.
var i = 0
while True:
case s[i]
of '\0':
break
else:
var found = False
for t in 0..len(token)-1:
if s[i+t] == token[t]:
found = True
else:
found = False
break
if found:
break
inc(i)
o = copy(s, 0, i-1)
return i
proc parseDepends(s: string): seq[tuple[name, vRange: string]] =
result = @[]
var many = s.split({',', ';'})
for i in items(many):
var stripped = i.strip()
var name = ""
var index = skipUntil(stripped, " ", name)
var vRange = stripped.copy(index)
result.add((name.strip(), vRange.strip()))
proc parseErr(p: TCfgParser, msg: string) = proc parseErr(p: TCfgParser, msg: string) =
raise newException(EParseErr, "(" & $p.getLine() & ", " & raise newException(EParseErr, "(" & $p.getLine() & ", " &
@ -92,7 +124,7 @@ proc parseBabel*(file: string): TProject =
of "library": of "library":
case normalize(e.key) case normalize(e.key)
of "depends": of "depends":
result.depends = e.value.parseList() result.depends = e.value.parseDepends()
of "files": of "files":
result.files = e.value.parseList() result.files = e.value.parseList()
of "exposedmodules": of "exposedmodules":
@ -102,7 +134,7 @@ proc parseBabel*(file: string): TProject =
of "exe": of "exe":
case normalize(e.key) case normalize(e.key)
of "depends": of "depends":
result.depends = e.value.parseList() result.depends = e.value.parseDepends()
of "files": of "files":
result.files = e.value.parseList() result.files = e.value.parseList()
of "exe": of "exe":
@ -155,3 +187,7 @@ when isMainModule:
echo(i) echo(i)
var project = parseBabel("babel.babel") var project = parseBabel("babel.babel")
echo project.library echo project.library
echo()
var o = ""
echo skipUntil("nimrod >= 0.8.10", " ", o)
echo(o)