From bbb586dbfc0b5c3cd8e7684223b88f150cc3fa3f Mon Sep 17 00:00:00 2001 From: yuchunzhou Date: Tue, 29 Oct 2019 05:28:46 +0800 Subject: [PATCH] add vcs to the new created project (#729) * add git vcs and a default .nim.cfg file to the new created project * remove the gitapi dependency * add vcs support for new nimble project * add vcs support for new nimble project * update pull request #729 * add --git/hg flag instead of --vcs flag for nimble init command --- src/nimble.nim | 19 ++++++++++++++++++- src/nimblepkg/options.nim | 11 +++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/nimble.nim b/src/nimble.nim index e214538..412c987 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -3,12 +3,13 @@ import system except TResult -import os, tables, strtabs, json, algorithm, sets, uri, sugar, sequtils +import os, tables, strtabs, json, algorithm, sets, uri, sugar, sequtils, osproc import std/options as std_opt import strutils except toLower from unicode import toLower from sequtils import toSeq +from strformat import fmt import nimblepkg/packageinfo, nimblepkg/version, nimblepkg/tools, nimblepkg/download, nimblepkg/config, nimblepkg/common, @@ -724,6 +725,11 @@ proc dump(options: Options) = echo "backend: ", p.backend.escape proc init(options: Options) = + # Check whether the vcs is installed. + let vcsBin = options.action.vcsOption + if vcsBin != "" and findExe(vcsBin, true) == "": + raise newException(NimbleError, "Please install git or mercurial first") + # Determine the package name. let pkgName = if options.action.projName != "": @@ -858,6 +864,17 @@ js - Compile using JavaScript backend.""", pkgRoot ) + # Create a git or hg repo in the new nimble project. + if vcsBin != "": + let cmd = fmt"cd {pkgRoot} && {vcsBin} init" + let ret: tuple[output: string, exitCode: int] = execCmdEx(cmd) + if ret.exitCode != 0: quit ret.output + + var ignoreFile = if vcsBin == "git": ".gitignore" else: ".hgignore" + var fd = open(joinPath(pkgRoot, ignoreFile), fmWrite) + fd.write(pkgName & "\n") + fd.close() + display("Success:", "Package $# created successfully" % [pkgName], Success, HighPriority) diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index 9deb357..a338978 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -51,6 +51,7 @@ type search*: seq[string] # Search string. of actionInit, actionDump: projName*: string + vcsOption*: string of actionCompile, actionDoc, actionBuild: file*: string backend*: string @@ -80,6 +81,8 @@ Commands: init [pkgname] Initializes a new Nimble project in the current directory or if a name is provided a new directory of the same name. + --git + --hg Create a git or hg repo in the new nimble project. publish Publishes a package on nim-lang/packages. The current working directory needs to be the toplevel directory of the Nimble package. @@ -201,8 +204,10 @@ proc initAction*(options: var Options, key: string) = else: options.action.backend = keyNorm of actionInit: options.action.projName = "" + options.action.vcsOption = "" of actionDump: options.action.projName = "" + options.action.vcsOption = "" options.forcePrompts = forcePromptYes of actionRefresh: options.action.optionalURL = "" @@ -349,6 +354,12 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) = result.action.passNimFlags.add(val) else: wasFlagHandled = false + of actionInit: + case f + of "git", "hg": + result.action.vcsOption = f + else: + wasFlagHandled = false of actionUninstall: case f of "incldeps", "i":