From d5d28c8fcf23d97e1014448d9ac129cef776a611 Mon Sep 17 00:00:00 2001 From: singularperturbation Date: Mon, 1 Sep 2014 16:52:30 -0500 Subject: [PATCH 1/2] 'Init' feature initial implementation Added the init() proc to babel.nim as well as a new TActionType and supporting code in doAction. The idea is that if you have a project already created in a directory, this will create a .babel file with sane defaults. I also updated the help text describe the 'init' feature as with the other babel commands. --- src/babel.nim | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/babel.nim b/src/babel.nim index 18ae079..9e21819 100644 --- a/src/babel.nim +++ b/src/babel.nim @@ -21,8 +21,8 @@ type babelData: PJsonNode ## Babeldata.json TActionType = enum - ActionNil, ActionUpdate, ActionInstall, ActionSearch, ActionList, - ActionBuild, ActionPath, ActionUninstall + ActionNil, ActionUpdate, ActionInit, ActionInstall, ActionSearch, + ActionList, ActionBuild, ActionPath, ActionUninstall TAction = object case typ: TActionType @@ -35,6 +35,8 @@ type packages: seq[TPkgTuple] # Optional only for ActionInstall. of ActionSearch: search: seq[string] # Search string. + of ActionInit: + projName: string TForcePrompt = enum DontForcePrompt, ForcePromptYes, ForcePromptNo @@ -45,6 +47,7 @@ Usage: babel COMMAND [opts] Commands: install [pkgname, ...] Installs a list of packages. + init [pkgname, ...] Initializes a new Babel project uninstall [pkgname, ...] Uninstalls a list of packages. build Builds a package. update [url] Updates package list. A package list URL can @@ -100,6 +103,9 @@ proc parseCmdLine(): TOptions = result.action.packages = @[] of "build": result.action.typ = ActionBuild + of "init": + result.action.typ = ActionInit + result.action.projName = "" of "update": result.action.typ = ActionUpdate result.action.optionalURL = "" @@ -128,6 +134,10 @@ proc parseCmdLine(): TOptions = result.action.optionalURL = key of ActionSearch: result.action.search.add(key) + of ActionInit: + if result.action.projName != "": + quit("ERROR: MORE THAN ONE ARGUMENT FOR PACKAGE NAME.") + result.action.projName = key of ActionList, ActionBuild: writeHelp() of cmdLongOption, cmdShortOption: @@ -656,6 +666,44 @@ proc listPaths(options: TOptions) = if errors > 0: raise newException(EBabel, "At least one of the specified packages was not found") +proc init(options: TOptions) = + echo("Initializing new Babel project!") + var + pkgName, fName: string = "" + outFile: TFile + + if (options.action.projName != ""): + pkgName = options.action.projName + fName = pkgName & ".babel" + if ( existsFile( os.getCurrentDir() / fName ) ): + quit("Already have a babel file.") + + else: + echo("Enter a project name for this (blank to use working directory), Ctrl-C to abort:") + pkgName = readline(stdin) + if (pkgName == ""): + pkgName = os.getCurrentDir().splitPath.tail + if (pkgName == ""): + raise newException(EBabel, "Could not get default file path.") + fName = pkgName & ".babel" + + # Now need to write out .babel file with projName and other details + + if (not existsFile( os.getCurrentDir() / fName) and open(f=outFile, filename = fName, mode = fmWrite) ): + outFile.write("[Package]\n") + outFile.write("name = \"" & pkgName & "\"\n") + outFile.write("version = \"0.01\"\n") + outFile.write("author = \"Anonymous\"\n") + outFile.write("description = \"New Babel project for Nimrod\"\n") + outFile.write("license = \"BSD\"\n") + outFile.write("\n") + outFile.write("[Deps]\n") + outFile.write("Requires: \"nimrod >= 0.9.4\"\n") + close(outFile) + + else: + quit("Unable to open file " & fName & " for writing. Check if file exists?") + proc uninstall(options: TOptions) = var pkgsToDelete: seq[TPackageInfo] = @[] # Do some verification. @@ -730,6 +778,8 @@ proc doAction(options: TOptions) = listPaths(options) of ActionBuild: build(options) + of ActionInit: + init(options) of ActionNil: assert false From 9a67b0866463308984b78572324954436c0ec6ca Mon Sep 17 00:00:00 2001 From: singularperturbation Date: Tue, 2 Sep 2014 17:59:45 -0500 Subject: [PATCH 2/2] Style changes, fix some bugs, raise exceptions Did some cleanup to better match style conventions, switched out 'quit' statements in favor of raising 'EBabel' exceptions, used writeln, and other cleanup. See https://github.com/nimrod-code/babel/pull/58 --- src/babel.nim | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/babel.nim b/src/babel.nim index 9e21819..10db107 100644 --- a/src/babel.nim +++ b/src/babel.nim @@ -47,7 +47,7 @@ Usage: babel COMMAND [opts] Commands: install [pkgname, ...] Installs a list of packages. - init [pkgname, ...] Initializes a new Babel project + init [pkgname] Initializes a new Babel project. uninstall [pkgname, ...] Uninstalls a list of packages. build Builds a package. update [url] Updates package list. A package list URL can @@ -136,7 +136,7 @@ proc parseCmdLine(): TOptions = result.action.search.add(key) of ActionInit: if result.action.projName != "": - quit("ERROR: MORE THAN ONE ARGUMENT FOR PACKAGE NAME.") + raise newException(EBabel, "Can only initialize one package at a time.") result.action.projName = key of ActionList, ActionBuild: writeHelp() @@ -668,7 +668,7 @@ proc listPaths(options: TOptions) = proc init(options: TOptions) = echo("Initializing new Babel project!") - var + var pkgName, fName: string = "" outFile: TFile @@ -676,8 +676,7 @@ proc init(options: TOptions) = pkgName = options.action.projName fName = pkgName & ".babel" if ( existsFile( os.getCurrentDir() / fName ) ): - quit("Already have a babel file.") - + raise newException(EBabel, "Already have a babel file.") else: echo("Enter a project name for this (blank to use working directory), Ctrl-C to abort:") pkgName = readline(stdin) @@ -690,19 +689,19 @@ proc init(options: TOptions) = # Now need to write out .babel file with projName and other details if (not existsFile( os.getCurrentDir() / fName) and open(f=outFile, filename = fName, mode = fmWrite) ): - outFile.write("[Package]\n") - outFile.write("name = \"" & pkgName & "\"\n") - outFile.write("version = \"0.01\"\n") - outFile.write("author = \"Anonymous\"\n") - outFile.write("description = \"New Babel project for Nimrod\"\n") - outFile.write("license = \"BSD\"\n") - outFile.write("\n") - outFile.write("[Deps]\n") - outFile.write("Requires: \"nimrod >= 0.9.4\"\n") + outFile.writeln("[Package]") + outFile.writeln("name = \"" & pkgName & "\"") + outFile.writeln("version = \"0.01\"") + outFile.writeln("author = \"Anonymous\"") + outFile.writeln("description = \"New Babel project for Nimrod\"") + outFile.writeln("license = \"BSD\"") + outFile.writeln("") + outFile.writeln("[Deps]") + outFile.writeln("Requires: \"nimrod >= 0.9.4\"") close(outFile) else: - quit("Unable to open file " & fName & " for writing. Check if file exists?") + raise newException(EBabel, "Unable to open file " & fName & " for writing: " & osErrorMsg()) proc uninstall(options: TOptions) = var pkgsToDelete: seq[TPackageInfo] = @[]