Improves the directory structure that init creates.

Fixes #413. Refs #315.
This commit is contained in:
Dominik Picheta 2018-09-09 18:15:42 +01:00
commit 3ee1e115f4
3 changed files with 189 additions and 89 deletions

View file

@ -14,7 +14,7 @@ import nimblepkg/packageinfo, nimblepkg/version, nimblepkg/tools,
nimblepkg/download, nimblepkg/config, nimblepkg/common,
nimblepkg/publish, nimblepkg/options, nimblepkg/packageparser,
nimblepkg/cli, nimblepkg/packageinstaller, nimblepkg/reversedeps,
nimblepkg/nimscriptexecutor
nimblepkg/nimscriptexecutor, nimblepkg/init
import nimblepkg/nimscriptsupport
@ -809,94 +809,19 @@ Please specify a valid SPDX identifier.""",
$nimDepDef)
validateVersion(pkgNimDep)
let pkgTestDir = "tests"
# Create source directory
os.createDir(pkgRoot / pkgSrcDir)
display("Success:", "Source directory created successfully", Success,
MediumPriority)
# Create initial source file
cd pkgRoot / pkgSrcDir:
let pkgFile = pkgName.changeFileExt("nim")
try:
if pkgType == "bin":
pkgFile.writeFile "# Hello Nim!\necho \"Hello, World!\"\n"
else:
pkgFile.writeFile """# $#
# Copyright $#
# $#
""" % [pkgName, pkgAuthor, pkgDesc]
display("Success:", "Created initial source file successfully", Success,
MediumPriority)
except:
raise newException(NimbleError, "Unable to open file " & pkgFile &
" for writing: " & osErrorMsg(osLastError()))
# Create test directory
os.createDir(pkgRoot / pkgTestDir)
display("Success:", "Test directory created successfully", Success,
MediumPriority)
cd pkgRoot / pkgTestDir:
try:
"test1.nims".writeFile("""switch("path", "$$projectDir/../$#")""" %
[pkgSrcDir])
display("Success:", "Test config file created successfully", Success,
MediumPriority)
except:
raise newException(NimbleError, "Unable to open file " & "test1.nims" &
" for writing: " & osErrorMsg(osLastError()))
try:
"test1.nim".writeFile("doAssert(1 + 1 == 2)\n")
display("Success:", "Test file created successfully", Success,
MediumPriority)
except:
raise newException(NimbleError, "Unable to open file " & "test1.nim" &
" for writing: " & osErrorMsg(osLastError()))
# Write the nimble file
try:
if pkgType == "lib":
nimbleFile.writeFile """# Package
version = $#
author = $#
description = $#
license = $#
srcDir = $#
# Dependencies
requires "nim >= $#"
""" % [pkgVersion.escape(), pkgAuthor.escape(), pkgDesc.escape(),
pkgLicense.escape(), pkgSrcDir.escape(), pkgNimDep]
else:
nimbleFile.writeFile """# Package
version = $#
author = $#
description = $#
license = $#
srcDir = $#
bin = @[$#]
# Dependencies
requires "nim >= $#"
""" % [pkgVersion.escape(), pkgAuthor.escape(), pkgDesc.escape(),
pkgLicense.escape(), pkgSrcDir.escape(), pkgName.escape(), pkgNimDep]
except:
raise newException(
NimbleError,
"Unable to open file " & nimbleFile & " for writing: " &
osErrorMsg(osLastError())
)
display("Success:", "Nimble file created successfully", Success,
MediumPriority)
createPkgStructure(
(
pkgName,
pkgVersion,
pkgAuthor,
pkgDesc,
pkgLicense,
pkgSrcDir,
pkgNimDep,
pkgType
),
pkgRoot
)
display("Success:", "Package $# created successfully" % [pkgName], Success,
HighPriority)

171
src/nimblepkg/init.nim Normal file
View file

@ -0,0 +1,171 @@
import os, strutils
import ./cli, ./tools
type
PkgInitInfo* = tuple
pkgName: string
pkgVersion: string
pkgAuthor: string
pkgDesc: string
pkgLicense: string
pkgSrcDir: string
pkgNimDep: string
pkgType: string
proc createPkgStructure*(info: PkgInitInfo, pkgRoot: string) =
# Create source directory
createDirD(pkgRoot / info.pkgSrcDir)
# Initialise the source code directories and create some example code.
var nimbleFileOptions = ""
case info.pkgType
of "binary":
let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim")
writeFile(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.
when isMainModule:
echo("Hello, World!")
"""
)
nimbleFileOptions.add("bin = @[\"$1\"]\n" % info.pkgName)
of "library":
let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim")
writeFile(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
# but you can remove it if you wish.
proc add*(x, y: int): int =
## Adds two files together.
return x + y
"""
)
createDirD(pkgRoot / info.pkgSrcDir / info.pkgName)
let submodule = pkgRoot / info.pkgSrcDir / info.pkgName /
"submodule".addFileExt("nim")
writeFile(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
# remove this file altogether. You may create additional modules alongside
# this file as required.
type
Submodule* = object
name*: string
proc initSubmodule*(): Submodule =
## Initialises a new ``Submodule`` object.
Submodule(name: "Anonymous")
""" % info.pkgName
)
of "hybrid":
let mainFile = pkgRoot / info.pkgSrcDir / info.pkgName.changeFileExt("nim")
writeFile(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.
import $1pkg/submodule
when isMainModule:
echo(getWelcomeMessage())
""" % info.pkgName
)
let pkgSubDir = pkgRoot / info.pkgSrcDir / info.pkgName & "pkg"
createDirD(pkgSubDir)
let submodule = pkgSubDir / "submodule".addFileExt("nim")
writeFile(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
# remove this file altogether. You may create additional modules alongside
# this file as required.
proc getWelcomeMessage*(): string = "Hello, World!"
""" % info.pkgName
)
nimbleFileOptions.add("installExt = @[\"nim\"]\n")
nimbleFileOptions.add("bin = @[\"$1\"]\n" % info.pkgName)
else:
assert false, "Invalid package type specified."
let pkgTestDir = "tests"
# Create test directory
case info.pkgType
of "binary":
discard
of "hybrid", "library":
let pkgTestPath = pkgRoot / pkgTestDir
createDirD(pkgTestPath)
writeFile(pkgTestPath / "config".addFileExt("nims"),
"switch(\"path\", \"$$projectDir/../$#\")" % info.pkgSrcDir
)
if info.pkgType == "library":
writeFile(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`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.
import unittest
import $1
test "can add":
check add(5, 5) == 10
""" % info.pkgName
)
else:
writeFile(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`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.
import unittest
import $1pkg/submodule
test "correct welcome":
check getWelcomeMessage() == "Hello, World!"
""" % info.pkgName
)
else:
assert false, "Invalid package type specified."
# Write the nimble file
let nimbleFile = pkgRoot / info.pkgName.changeFileExt("nimble")
writeFile(nimbleFile, """# Package
version = $#
author = $#
description = $#
license = $#
srcDir = $#
$#
# Dependencies
requires "nim >= $#"
""" % [
info.pkgVersion.escape(), info.pkgAuthor.escape(), info.pkgDesc.escape(),
info.pkgLicense.escape(), info.pkgSrcDir.escape(), nimbleFileOptions,
info.pkgNimDep
]
)
display("Info:", "Nimble file created successfully", priority=MediumPriority)

View file

@ -106,6 +106,10 @@ proc copyDirD*(fro, to: string): seq[string] =
createDir(changeRoot(fro, to, path.splitFile.dir))
result.add copyFileD(path, changeRoot(fro, to, path))
proc createDirD*(dir: string) =
display("Creating", "directory $#" % dir, priority = LowPriority)
createDir(dir)
proc getDownloadDirName*(uri: string, verRange: VersionRange): string =
## Creates a directory name based on the specified ``uri`` (url)
result = ""