Merge branch 'pr_polite_install' of git://github.com/gradha/babel into gradha-pr_polite_install

This commit is contained in:
Dominik Picheta 2013-11-30 14:08:20 +00:00
commit 3b619f1071
2 changed files with 52 additions and 11 deletions

6
.gitignore vendored
View file

@ -1 +1,7 @@
# Any path
*.swp
*~ *~
nimcache/
# Absolute paths
/babel

View file

@ -21,6 +21,11 @@ type
of ActionSearch: of ActionSearch:
search: seq[string] # Search string. search: seq[string] # Search string.
TForcePrompt = enum
DontForcePrompt, ForcePromptYes, ForcePromptNo
var forcePrompts = DontForcePrompt
const const
help = """ help = """
Usage: babel COMMAND [opts] Usage: babel COMMAND [opts]
@ -34,8 +39,10 @@ Commands:
path [pkgname, ...] Shows absolute path to the installed packages. path [pkgname, ...] Shows absolute path to the installed packages.
Options: Options:
-h Print this help message. -h, -help Print this help message.
-v Print version information. -v, -version Print version information.
-y, -accept Accept all interactive prompts.
-n, -reject Reject all interactive prompts.
""" """
babelVersion = "0.1.0" babelVersion = "0.1.0"
defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json" defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json"
@ -88,11 +95,25 @@ proc parseCmdLine(): TAction =
case key case key
of "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": writeVersion() of "version", "v": writeVersion()
of "accept", "y": forcePrompts = ForcePromptYes
of "reject", "n": forcePrompts = ForcePromptNo
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
if result.typ == ActionNil: if result.typ == ActionNil:
writeHelp() writeHelp()
proc prompt(question: string): bool = proc prompt(question: string): bool =
## Asks an interactive question and returns the result.
##
## The proc will return immediately without asking the user if the global
## forcePrompts has a value different than DontForcePrompt.
case forcePrompts
of ForcePromptYes:
echo(question & " -> [forced yes]")
return true
of ForcePromptNo:
echo(question & " -> [forced no]")
return false
of DontForcePrompt:
echo(question & " [y/N]") echo(question & " [y/N]")
let yn = stdin.readLine() let yn = stdin.readLine()
case yn.normalize case yn.normalize
@ -107,10 +128,16 @@ let babelDir = getHomeDir() / ".babel"
let pkgsDir = babelDir / "pkgs" let pkgsDir = babelDir / "pkgs"
let binDir = babelDir / "bin" let binDir = babelDir / "bin"
let nimVer = getNimrodVersion() let nimVer = getNimrodVersion()
var didUpdatePackages = false
proc update(url: string = defaultPackageURL) = proc update(url: string = defaultPackageURL) =
## Downloads the package list from the specified URL.
##
## If the download is successful, the global didUpdatePackages is set to
## true. Otherwise an exception is raised on error.
echo("Downloading package list from " & url) echo("Downloading package list from " & url)
downloadFile(url, babelDir / "packages.json") downloadFile(url, babelDir / "packages.json")
didUpdatePackages = true
echo("Done.") echo("Done.")
proc checkInstallFile(pkgInfo: TPackageInfo, proc checkInstallFile(pkgInfo: TPackageInfo,
@ -296,12 +323,20 @@ proc install(packages: seq[String], verRange: PVersionRange): string =
result = installFromDir(getCurrentDir(), false) result = installFromDir(getCurrentDir(), false)
else: else:
if not existsFile(babelDir / "packages.json"): if not existsFile(babelDir / "packages.json"):
if prompt("Local packages.json not found, download it from internet?"):
update()
install(packages, verRange)
else:
quit("Please run babel update.", QuitFailure) quit("Please run babel update.", QuitFailure)
for p in packages: for p in packages:
var pkg: TPackage var pkg: TPackage
if getPackage(p, babelDir / "packages.json", pkg): if getPackage(p, babelDir / "packages.json", pkg):
let downloadDir = downloadPkg(pkg, verRange) let downloadDir = downloadPkg(pkg, verRange)
result = installFromDir(downloadDir, false) result = installFromDir(downloadDir, false)
else:
if didUpdatePackages == false and prompt(p & " not found in local packages.json, check internet for updated packages?"):
update()
install(@[p], verRange)
else: else:
raise newException(EBabel, "Package not found.") raise newException(EBabel, "Package not found.")