From df8317585f5ee99de68cdda38df2a5b1a2d6027f Mon Sep 17 00:00:00 2001 From: liquid600pgm Date: Sat, 22 Jun 2019 00:10:41 +0200 Subject: [PATCH] fixed #581 nimble init does not overwrite existing files with its templates anymore --- src/nimblepkg/init.nim | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/nimblepkg/init.nim b/src/nimblepkg/init.nim index 0617d97..e8c1000 100644 --- a/src/nimblepkg/init.nim +++ b/src/nimblepkg/init.nim @@ -14,6 +14,10 @@ type pkgNimDep: string pkgType: string +proc writeFileIfNonExistent(file: string, content: string) = + if not existsFile(file): + writeFile(file, content) + proc createPkgStructure*(info: PkgInitInfo, pkgRoot: string) = # Create source directory createDirD(pkgRoot / info.pkgSrcDir) @@ -23,7 +27,7 @@ proc createPkgStructure*(info: PkgInitInfo, pkgRoot: string) = case info.pkgType of "binary": let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim") - writeFile(mainFile, + writeFileIfNonExistent(mainFile, """ # This is just an example to get you started. A typical binary package # uses this file as the main entry point of the application. @@ -35,7 +39,7 @@ when isMainModule: nimbleFileOptions.add("bin = @[\"$1\"]\n" % info.pkgName) of "library": let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim") - writeFile(mainFile, + writeFileIfNonExistent(mainFile, """ # This is just an example to get you started. A typical library package # exports the main API in this file. Note that you cannot rename this file @@ -50,7 +54,7 @@ proc add*(x, y: int): int = createDirD(pkgRoot / info.pkgSrcDir / info.pkgName) let submodule = pkgRoot / info.pkgSrcDir / info.pkgName / "submodule".addFileExt("nim") - writeFile(submodule, + writeFileIfNonExistent(submodule, """ # This is just an example to get you started. Users of your library will # import this file by writing ``import $1/submodule``. Feel free to rename or @@ -68,7 +72,7 @@ proc initSubmodule*(): Submodule = ) of "hybrid": let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim") - writeFile(mainFile, + writeFileIfNonExistent(mainFile, """ # This is just an example to get you started. A typical hybrid package # uses this file as the main entry point of the application. @@ -83,7 +87,7 @@ when isMainModule: let pkgSubDir = pkgRoot / info.pkgSrcDir / info.pkgName & "pkg" createDirD(pkgSubDir) let submodule = pkgSubDir / "submodule".addFileExt("nim") - writeFile(submodule, + writeFileIfNonExistent(submodule, """ # This is just an example to get you started. Users of your hybrid library will # import this file by writing ``import $1pkg/submodule``. Feel free to rename or @@ -112,7 +116,7 @@ proc getWelcomeMessage*(): string = "Hello, World!" ) if info.pkgType == "library": - writeFile(pkgTestPath / "test1".addFileExt("nim"), + writeFileIfNonExistent(pkgTestPath / "test1".addFileExt("nim"), """ # This is just an example to get you started. You may wish to put all of your # tests into a single file, or separate them into multiple `test1`, `test2` @@ -129,7 +133,7 @@ test "can add": """ % info.pkgName ) else: - writeFile(pkgTestPath / "test1".addFileExt("nim"), + writeFileIfNonExistent(pkgTestPath / "test1".addFileExt("nim"), """ # This is just an example to get you started. You may wish to put all of your # tests into a single file, or separate them into multiple `test1`, `test2`