Fixes #503 by creating directory if current and name differ

This commit is contained in:
Christopher Pridgen 2018-09-04 21:19:11 -04:00
commit 260eb854fd
2 changed files with 18 additions and 11 deletions

View file

@ -698,25 +698,31 @@ proc dump(options: Options) =
echo "backend: ", p.backend.escape echo "backend: ", p.backend.escape
proc init(options: Options) = proc init(options: Options) =
var nimbleFile: string = ""
if options.forcePrompts != forcePromptYes: if options.forcePrompts != forcePromptYes:
display("Info:", display("Info:",
"In order to initialise a new Nimble package, I will need to ask you\n" & "In order to initialise a new Nimble package, I will need to ask you\n" &
"some questions. Default values are shown in square brackets, press\n" & "some questions. Default values are shown in square brackets, press\n" &
"enter to use them.", priority = HighPriority) "enter to use them.", priority = HighPriority)
# Ask for package name. # Determine the package name.
let pkgName = let pkgName =
if options.action.projName != "": if options.action.projName != "":
options.action.projName options.action.projName
else: else:
os.getCurrentDir().splitPath.tail.toValidPackageName() os.getCurrentDir().splitPath.tail.toValidPackageName()
nimbleFile = pkgName.changeFileExt("nimble") # Validate the package name.
validatePackageName(nimbleFile.changeFileExt("")) validatePackageName(pkgName)
let nimbleFilePath = os.getCurrentDir() / nimbleFile # Determine the package root.
let pkgRoot = if pkgName == os.getCurrentDir().splitPath.tail:
os.getCurrentDir()
else:
os.getCurrentDir() / pkgName
let nimbleFile = (pkgRoot / pkgName).changeFileExt("nimble")
let nimbleFilePath = pkgRoot / nimbleFile
if existsFile(nimbleFilePath): if existsFile(nimbleFilePath):
let errMsg = "Nimble file already exists: $#" % nimbleFilePath let errMsg = "Nimble file already exists: $#" % nimbleFilePath
raise newException(NimbleError, errMsg) raise newException(NimbleError, errMsg)
@ -781,13 +787,13 @@ Binary packages produce executables.""", [
let pkgTestDir = "tests" let pkgTestDir = "tests"
# Create source directory # Create source directory
os.createDir(pkgSrcDir) os.createDir(pkgRoot / pkgSrcDir)
display("Success:", "Source directory created successfully", Success, display("Success:", "Source directory created successfully", Success,
MediumPriority) MediumPriority)
# Create initial source file # Create initial source file
cd pkgSrcDir: cd pkgRoot / pkgSrcDir:
let pkgFile = pkgName.changeFileExt("nim") let pkgFile = pkgName.changeFileExt("nim")
try: try:
if pkgType == "bin": if pkgType == "bin":
@ -804,12 +810,12 @@ Binary packages produce executables.""", [
" for writing: " & osErrorMsg(osLastError())) " for writing: " & osErrorMsg(osLastError()))
# Create test directory # Create test directory
os.createDir(pkgTestDir) os.createDir(pkgRoot / pkgTestDir)
display("Success:", "Test directory created successfully", Success, display("Success:", "Test directory created successfully", Success,
MediumPriority) MediumPriority)
cd pkgTestDir: cd pkgRoot / pkgTestDir:
try: try:
"test1.nims".writeFile("""switch("path", "$$projectDir/../$#")""" % "test1.nims".writeFile("""switch("path", "$$projectDir/../$#")""" %
[pkgSrcDir]) [pkgSrcDir])

View file

@ -65,7 +65,8 @@ Commands:
check Verifies the validity of a package in the check Verifies the validity of a package in the
current working directory. current working directory.
init [pkgname] Initializes a new Nimble project in the init [pkgname] Initializes a new Nimble project in the
current directory. current directory or if a name is provided a
new directory of the same name.
publish Publishes a package on nim-lang/packages. publish Publishes a package on nim-lang/packages.
The current working directory needs to be the The current working directory needs to be the
toplevel directory of the Nimble package. toplevel directory of the Nimble package.