From 87008ed99c1da842f6a20644684c1ba0a0f56430 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 7 Nov 2013 23:11:23 +0100 Subject: [PATCH 1/5] Adds didUpdatePackages global to detect downloads. --- babel.nim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/babel.nim b/babel.nim index 9defdcf..0d5b844 100644 --- a/babel.nim +++ b/babel.nim @@ -101,10 +101,16 @@ let babelDir = getHomeDir() / ".babel" let pkgsDir = babelDir / "pkgs" let binDir = babelDir / "bin" let nimVer = getNimrodVersion() +var didUpdatePackages = false 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) downloadFile(url, babelDir / "packages.json") + didUpdatePackages = true echo("Done.") proc checkInstallFile(pkgInfo: TPackageInfo, From 58ee3a5d819c9b08b5031c31467ed612528c446a Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 7 Nov 2013 23:12:20 +0100 Subject: [PATCH 2/5] Adds some temporary masks to gitignore. --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index b25c15b..45db0b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ +# Any path +*.swp *~ +nimcache/ + +# Absolute paths +/babel From e08f9101b813373a21ece644f961d23eda27b62c Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 7 Nov 2013 23:27:33 +0100 Subject: [PATCH 3/5] Adds update suggestion to failed install commands. The install proc checks the didUpdatePackages first to avoid infinite recursion. --- babel.nim | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/babel.nim b/babel.nim index 0d5b844..7fb0f43 100644 --- a/babel.nim +++ b/babel.nim @@ -296,14 +296,22 @@ proc install(packages: seq[String], verRange: PVersionRange): string = result = installFromDir(getCurrentDir(), false) else: if not existsFile(babelDir / "packages.json"): - quit("Please run babel update.", QuitFailure) + if didUpdatePackages == false and 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: var pkg: TPackage if getPackage(p, babelDir / "packages.json", pkg): let downloadDir = downloadPkg(pkg, verRange) result = installFromDir(downloadDir, false) 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 = var pkgInfo = getPkgInfo(getCurrentDir()) From d2ff13144db9f50ffd2d468a9db3889cb001b254 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 7 Nov 2013 23:42:27 +0100 Subject: [PATCH 4/5] Adds -y/-n switches for non interactive operation. --- babel.nim | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/babel.nim b/babel.nim index 7fb0f43..9ceea64 100644 --- a/babel.nim +++ b/babel.nim @@ -19,6 +19,11 @@ type of ActionSearch: search: seq[string] # Search string. + TForcePrompt = enum + DontForcePrompt, ForcePromptYes, ForcePromptNo + +var forcePrompts = DontForcePrompt + const help = """ Usage: babel COMMAND [opts] @@ -31,8 +36,10 @@ Commands: list Lists all packages. Options: - -h Print this help message. - -v Print version information. + -h, -help Print this help message. + -v, -version Print version information. + -y, -accept Accept all interactive prompts. + -n, -reject Reject all interactive prompts. """ babelVersion = "0.1.0" defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json" @@ -82,20 +89,34 @@ proc parseCmdLine(): TAction = case key of "help", "h": writeHelp() of "version", "v": writeVersion() + of "accept", "y": forcePrompts = ForcePromptYes + of "reject", "n": forcePrompts = ForcePromptNo of cmdEnd: assert(false) # cannot happen if result.typ == ActionNil: writeHelp() proc prompt(question: string): bool = - echo(question & " [y/N]") - let yn = stdin.readLine() - case yn.normalize - of "y", "yes": + ## 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 "n", "no": - return false - else: + of ForcePromptNo: + echo(question & " -> [forced no]") 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 pkgsDir = babelDir / "pkgs" From cd8969d43931cb37fe49212fb5417cee2e5929c8 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Wed, 13 Nov 2013 18:55:07 +0100 Subject: [PATCH 5/5] Removes didUpdatePackages check without json file. --- babel.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/babel.nim b/babel.nim index 9ceea64..e29c4c9 100644 --- a/babel.nim +++ b/babel.nim @@ -317,7 +317,7 @@ proc install(packages: seq[String], verRange: PVersionRange): string = result = installFromDir(getCurrentDir(), false) else: if not existsFile(babelDir / "packages.json"): - if didUpdatePackages == false and prompt("Local packages.json not found, download it from internet?"): + if prompt("Local packages.json not found, download it from internet?"): update() install(packages, verRange) else: