Merge branch 'pr_polite_install' of git://github.com/gradha/babel into gradha-pr_polite_install
This commit is contained in:
commit
3b619f1071
2 changed files with 52 additions and 11 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -1 +1,7 @@
|
||||||
|
# Any path
|
||||||
|
*.swp
|
||||||
*~
|
*~
|
||||||
|
nimcache/
|
||||||
|
|
||||||
|
# Absolute paths
|
||||||
|
/babel
|
||||||
|
|
|
||||||
57
babel.nim
57
babel.nim
|
|
@ -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,29 +95,49 @@ 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 =
|
||||||
echo(question & " [y/N]")
|
## Asks an interactive question and returns the result.
|
||||||
let yn = stdin.readLine()
|
##
|
||||||
case yn.normalize
|
## The proc will return immediately without asking the user if the global
|
||||||
of "y", "yes":
|
## forcePrompts has a value different than DontForcePrompt.
|
||||||
|
case forcePrompts
|
||||||
|
of ForcePromptYes:
|
||||||
|
echo(question & " -> [forced yes]")
|
||||||
return true
|
return true
|
||||||
of "n", "no":
|
of ForcePromptNo:
|
||||||
return false
|
echo(question & " -> [forced no]")
|
||||||
else:
|
|
||||||
return false
|
return false
|
||||||
|
of DontForcePrompt:
|
||||||
|
echo(question & " [y/N]")
|
||||||
|
let yn = stdin.readLine()
|
||||||
|
case yn.normalize
|
||||||
|
of "y", "yes":
|
||||||
|
return true
|
||||||
|
of "n", "no":
|
||||||
|
return false
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
let babelDir = getHomeDir() / ".babel"
|
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,14 +323,22 @@ 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"):
|
||||||
quit("Please run babel update.", QuitFailure)
|
if prompt("Local packages.json not found, download it from internet?"):
|
||||||
|
update()
|
||||||
|
install(packages, verRange)
|
||||||
|
else:
|
||||||
|
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:
|
else:
|
||||||
raise newException(EBabel, "Package not found.")
|
if didUpdatePackages == false and prompt(p & " not found in local packages.json, check internet for updated packages?"):
|
||||||
|
update()
|
||||||
|
install(@[p], verRange)
|
||||||
|
else:
|
||||||
|
raise newException(EBabel, "Package not found.")
|
||||||
|
|
||||||
proc build =
|
proc build =
|
||||||
var pkgInfo = getPkgInfo(getCurrentDir())
|
var pkgInfo = getPkgInfo(getCurrentDir())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue