From d5d28c8fcf23d97e1014448d9ac129cef776a611 Mon Sep 17 00:00:00 2001 From: singularperturbation Date: Mon, 1 Sep 2014 16:52:30 -0500 Subject: [PATCH] '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