Generate packages for 1.0
Use Pkg to generate Project.toml and the src entrypoint, and add the Test package from the stdlib. Also remove some options to generate that added complexity: force, temp_dir, and ssh. Trying to create a package at an already-existent path is now an error no matter what. The ssh keyword to generate has been moved to Template. TODO: - Update the file templates to current standards. - Clean up the gen_plugin signature and update those docs. - Pkg.add requirements provided to the template. - Update interactive_template's requirements gathering to use a loop. - **Make smaller commits.**
This commit is contained in:
parent
db1830daa9
commit
b0a9844792
@ -37,9 +37,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
|
||||
|
||||
[[Mustache]]
|
||||
deps = ["Pkg", "Test"]
|
||||
git-tree-sha1 = "fb4b57a278d18434eff78bdc2c06238f6ee3c9e7"
|
||||
git-tree-sha1 = "ce850d2d2c4a6f6a438685eb10ba836651ee9135"
|
||||
uuid = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
|
||||
[[Pkg]]
|
||||
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
|
||||
|
@ -10,6 +10,7 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
|
||||
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
|
||||
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
|
||||
Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
|
||||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
|
||||
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
|
||||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
||||
URIParser = "30578b45-9adc-5946-b283-645ec420af67"
|
||||
|
@ -1,4 +1,3 @@
|
||||
__precompile__()
|
||||
module PkgTemplates
|
||||
|
||||
using AutoHashEquals
|
||||
@ -7,6 +6,7 @@ using Distributed
|
||||
using InteractiveUtils
|
||||
using LibGit2
|
||||
using Mustache
|
||||
using Pkg
|
||||
using REPL.TerminalMenus
|
||||
using URIParser
|
||||
|
||||
|
358
src/generate.jl
358
src/generate.jl
@ -1,276 +1,167 @@
|
||||
"""
|
||||
generate(
|
||||
pkg_name::AbstractString,
|
||||
t::Template;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
) -> Nothing
|
||||
generate(pkg_name::AbstractString, t::Template; ssh::Bool=false) -> Nothing
|
||||
|
||||
Generate a package named `pkg_name` from `template`.
|
||||
|
||||
# Keyword Arguments
|
||||
* `force::Bool=false`: Whether or not to overwrite old packages with the same name.
|
||||
* `ssh::Bool=false`: Whether or not to use SSH for the remote.
|
||||
* `backup_dir::AbstractString=""`: Directory in which to store the generated package if
|
||||
`t.dir` is not a valid directory. If left unset, a temporary directory will be created
|
||||
(this keyword is mostly for internal usage).
|
||||
|
||||
# Notes
|
||||
The package is generated entirely in a temporary directory and only moved into
|
||||
`joinpath(t.dir, pkg_name)` at the very end. In the case of an error, the temporary
|
||||
directory will contain leftovers, but the destination directory will remain untouched
|
||||
(this is especially helpful when `force=true`).
|
||||
Generate a package named `pkg_name` from `t`.
|
||||
"""
|
||||
function generate(
|
||||
pkg_name::AbstractString,
|
||||
t::Template;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
)
|
||||
mktempdir() do temp_dir
|
||||
generate(pkg_name, t, temp_dir; force=force, ssh=ssh, backup_dir=backup_dir)
|
||||
end
|
||||
end
|
||||
|
||||
function generate(
|
||||
pkg_name::AbstractString,
|
||||
t::Template,
|
||||
dir::AbstractString;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
)
|
||||
pkg_name = splitjl(pkg_name)
|
||||
function generate(pkg_name::AbstractString, t::Template)
|
||||
pkg_dir = joinpath(t.dir, pkg_name)
|
||||
temp_pkg_dir = joinpath(dir, pkg_name)
|
||||
ispath(pkg_dir) && throw(ArgumentError("$pkg_dir already exists"))
|
||||
|
||||
if !force && ispath(pkg_dir)
|
||||
throw(ArgumentError(
|
||||
"Path '$pkg_dir' already exists, use force=true to overwrite it."
|
||||
))
|
||||
end
|
||||
|
||||
# Initialize the repo and configure it.
|
||||
repo = LibGit2.init(temp_pkg_dir)
|
||||
@info "Initialized git repo at $temp_pkg_dir"
|
||||
LibGit2.with(LibGit2.GitConfig, repo) do cfg
|
||||
for (key, val) in t.gitconfig
|
||||
LibGit2.set!(cfg, key, val)
|
||||
end
|
||||
end
|
||||
!isempty(t.gitconfig) && @info "Applied git configuration"
|
||||
LibGit2.commit(repo, "Initial commit")
|
||||
@info "Made empty initial commit"
|
||||
rmt = if ssh
|
||||
"git@$(t.host):$(t.user)/$pkg_name.jl.git"
|
||||
else
|
||||
"https://$(t.host)/$(t.user)/$pkg_name.jl"
|
||||
end
|
||||
# We need to set the remote in a strange way, see #8.
|
||||
close(LibGit2.GitRemote(repo, "origin", rmt))
|
||||
@info "Set remote origin to $rmt"
|
||||
|
||||
# Create the gh-pages branch if necessary.
|
||||
if haskey(t.plugins, GitHubPages)
|
||||
LibGit2.branch!(repo, "gh-pages")
|
||||
LibGit2.commit(repo, "Empty initial commit")
|
||||
@info "Created empty gh-pages branch"
|
||||
LibGit2.branch!(repo, "master")
|
||||
end
|
||||
|
||||
# Generate the files.
|
||||
files = vcat(
|
||||
gen_entrypoint(dir, pkg_name, t),
|
||||
gen_tests(dir, pkg_name, t),
|
||||
gen_require(dir, pkg_name, t),
|
||||
gen_readme(dir, pkg_name, t),
|
||||
gen_gitignore(dir, pkg_name, t),
|
||||
gen_license(dir, pkg_name, t),
|
||||
vcat(map(p -> gen_plugin(p, t, dir, pkg_name), values(t.plugins))...),
|
||||
)
|
||||
|
||||
LibGit2.add!(repo, files...)
|
||||
@info "Staged $(length(files)) files/directories: $(join(files, ", "))"
|
||||
LibGit2.commit(repo, "Files generated by PkgTemplates")
|
||||
@info "Committed files generated by PkgTemplates"
|
||||
multiple_branches = length(collect(LibGit2.GitBranchIter(repo))) > 1
|
||||
try
|
||||
mkpath(dirname(pkg_dir))
|
||||
mv(temp_pkg_dir, pkg_dir; force=force)
|
||||
@info "Moved temporary package directory into $(t.dir)/"
|
||||
catch # Likely cause is that t.dir can't be created (is a file, etc.).
|
||||
# We're just going to trust that backup_dir is a valid directory.
|
||||
backup_dir = if isempty(backup_dir)
|
||||
mktempdir()
|
||||
else
|
||||
abspath(backup_dir)
|
||||
end
|
||||
mkpath(backup_dir)
|
||||
mv(temp_pkg_dir, joinpath(backup_dir, pkg_name))
|
||||
@warn "$pkg_name couldn't be moved into $pkg_dir, left package in $backup_dir"
|
||||
end
|
||||
pkg_name = splitjl(pkg_name)
|
||||
pkg_dir = joinpath(t.dir, pkg_name)
|
||||
|
||||
@info "Finished"
|
||||
if multiple_branches
|
||||
@info "Remember to push all created branches to your remote: git push --all"
|
||||
# Create the directory with some boilerplate inside.
|
||||
Pkg.generate(pkg_dir)
|
||||
|
||||
# Initialize the repo and configure it.
|
||||
repo = LibGit2.init(pkg_dir)
|
||||
@info "Initialized git repo at $pkg_dir"
|
||||
LibGit2.with(LibGit2.GitConfig, repo) do cfg
|
||||
for (key, val) in t.gitconfig
|
||||
LibGit2.set!(cfg, key, val)
|
||||
end
|
||||
end
|
||||
isempty(t.gitconfig) || @info "Applied git configuration"
|
||||
LibGit2.commit(repo, "Initial commit")
|
||||
@info "Made empty initial commit"
|
||||
rmt = if t.ssh
|
||||
"git@$(t.host):$(t.user)/$pkg_name.jl.git"
|
||||
else
|
||||
"https://$(t.host)/$(t.user)/$pkg_name.jl"
|
||||
end
|
||||
# We need to set the remote in a strange way, see #8.
|
||||
close(LibGit2.GitRemote(repo, "origin", rmt))
|
||||
@info "Set remote origin to $rmt"
|
||||
|
||||
# Create the gh-pages branch if necessary.
|
||||
if haskey(t.plugins, GitHubPages)
|
||||
LibGit2.branch!(repo, "gh-pages")
|
||||
LibGit2.commit(repo, "Empty initial commit")
|
||||
@info "Created empty gh-pages branch"
|
||||
LibGit2.branch!(repo, "master")
|
||||
end
|
||||
|
||||
# Generate the files.
|
||||
files = vcat(
|
||||
"src/", "Project.toml", # Created by Pkg.generate.
|
||||
gen_tests(pkg_dir, t),
|
||||
gen_require(pkg_dir, t),
|
||||
gen_readme(pkg_dir, t),
|
||||
gen_gitignore(pkg_dir, t),
|
||||
gen_license(pkg_dir, t),
|
||||
vcat(map(p -> gen_plugin(p, t, t.dir, pkg_name), values(t.plugins))...),
|
||||
)
|
||||
|
||||
LibGit2.add!(repo, files...)
|
||||
@info "Staged $(length(files)) files/directories: $(join(files, ", "))"
|
||||
LibGit2.commit(repo, "Files generated by PkgTemplates")
|
||||
@info "Committed files generated by PkgTemplates"
|
||||
multiple_branches = length(collect(LibGit2.GitBranchIter(repo))) > 1
|
||||
|
||||
@info "Finished"
|
||||
if multiple_branches
|
||||
@info "Remember to push all created branches to your remote: git push --all"
|
||||
end
|
||||
catch e
|
||||
rm(pkg_dir; recursive=true)
|
||||
rethrow(e)
|
||||
end
|
||||
end
|
||||
|
||||
function generate(
|
||||
t::Template,
|
||||
pkg_name::AbstractString;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
)
|
||||
generate(pkg_name, t; force=force, ssh=ssh, backup_dir=backup_dir)
|
||||
function generate(t::Template, pkg_name::AbstractString)
|
||||
generate(pkg_name, t)
|
||||
end
|
||||
|
||||
"""
|
||||
generate_interactive(
|
||||
pkg_name::AbstractString;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
fast::Bool=false,
|
||||
) -> Nothing
|
||||
generate_interactive(pkg_name::AbstractString; fast::Bool=false) -> Nothing
|
||||
|
||||
Interactively create a template, and then generate a package with it. Arguments and
|
||||
keywords are used in the same way as in [`generate`](@ref) and
|
||||
[`interactive_template`](@ref).
|
||||
"""
|
||||
function generate_interactive(
|
||||
pkg_name::AbstractString;
|
||||
force::Bool=false,
|
||||
ssh::Bool=false,
|
||||
backup_dir::AbstractString="",
|
||||
fast::Bool=false,
|
||||
)
|
||||
generate(
|
||||
pkg_name,
|
||||
interactive_template(; fast=fast);
|
||||
force=force,
|
||||
ssh=ssh,
|
||||
backup_dir=backup_dir,
|
||||
)
|
||||
function generate_interactive(pkg_name::AbstractString; fast::Bool=false)
|
||||
generate(pkg_name, interactive_template(; fast=fast))
|
||||
end
|
||||
|
||||
"""
|
||||
gen_entrypoint(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
gen_tests(pkg_dir::AbstractString, template::Template) -> Vector{String}
|
||||
|
||||
Create the module entrypoint in the temp package directory.
|
||||
Create the test entrypoint in `pkg_dir`.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `template::Template`: The template whose entrypoint we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_entrypoint(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
text = template.precompile ? "__precompile__()\n" : ""
|
||||
text *= """
|
||||
module $pkg_name
|
||||
|
||||
# Package code goes here.
|
||||
|
||||
end
|
||||
"""
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, "src", "$pkg_name.jl"), text)
|
||||
return ["src/"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_tests(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
|
||||
Create the test directory and entrypoint in the temp package directory.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `pkg_dir::AbstractString`: The package directory in which the files will be generated
|
||||
* `template::Template`: The template whose tests we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_tests(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
text = """
|
||||
using $pkg_name
|
||||
using Base.Test
|
||||
function gen_tests(pkg_dir::AbstractString, template::Template)
|
||||
proj = Base.current_project()
|
||||
try
|
||||
Pkg.activate(pkg_dir)
|
||||
Pkg.add("Test")
|
||||
finally
|
||||
# TODO: What should we do if there is no current project?
|
||||
# Activating the generated project is now a side effect.
|
||||
proj !== nothing && Pkg.activate(dirname(proj))
|
||||
end
|
||||
|
||||
@testset "$pkg_name.jl" begin
|
||||
pkg = basename(pkg_dir)
|
||||
text = """
|
||||
using $pkg
|
||||
using Test
|
||||
|
||||
@testset "$pkg.jl" begin
|
||||
# Write your own tests here.
|
||||
@test 1 == 2
|
||||
end
|
||||
"""
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, "test", "runtests.jl"), text)
|
||||
return ["test/"]
|
||||
gen_file(joinpath(pkg_dir, "test", "runtests.jl"), text)
|
||||
# TODO: Should we be checking Manifest.toml into Git?
|
||||
return ["Manifest.toml", "test/"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_require(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
gen_require(pkg_dir::AbstractString, template::Template) -> Vector{String}
|
||||
|
||||
Create the `REQUIRE` file in the temp package directory.
|
||||
Create the `REQUIRE` file in `pkg_dir`.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `pkg_dir::AbstractString`: The directory in which the files will be generated.
|
||||
* `template::Template`: The template whose REQUIRE we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_require(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
function gen_require(pkg_dir::AbstractString, template::Template)
|
||||
text = "julia $(version_floor(template.julia_version))\n"
|
||||
text *= join(template.requirements, "\n")
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, "REQUIRE"), text)
|
||||
gen_file(joinpath(pkg_dir, "REQUIRE"), text)
|
||||
return ["REQUIRE"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_readme(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
gen_readme(pkg_dir::AbstractString, template::Template) -> Vector{String}
|
||||
|
||||
Create a README in the temp package directory with badges for each enabled plugin.
|
||||
Create a README in `pkg_dir` with badges for each enabled plugin.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `pkg_dir::AbstractString`: The directory in which the files will be generated.
|
||||
* `template::Template`: The template whose README we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_readme(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
text = "# $pkg_name\n"
|
||||
function gen_readme(pkg_dir::AbstractString, template::Template)
|
||||
pkg = basename(pkg_dir)
|
||||
text = "# $pkg\n"
|
||||
done = []
|
||||
# Generate the ordered badges first, then add any remaining ones to the right.
|
||||
for plugin_type in BADGE_ORDER
|
||||
if haskey(template.plugins, plugin_type)
|
||||
text *= "\n"
|
||||
text *= join(
|
||||
badges(template.plugins[plugin_type], template.user, pkg_name),
|
||||
badges(template.plugins[plugin_type], template.user, pkg),
|
||||
"\n",
|
||||
)
|
||||
push!(done, plugin_type)
|
||||
@ -279,33 +170,28 @@ function gen_readme(dir::AbstractString, pkg_name::AbstractString, template::Tem
|
||||
for plugin_type in setdiff(keys(template.plugins), done)
|
||||
text *= "\n"
|
||||
text *= join(
|
||||
badges(template.plugins[plugin_type], template.user, pkg_name),
|
||||
badges(template.plugins[plugin_type], template.user, pkg),
|
||||
"\n",
|
||||
)
|
||||
end
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, "README.md"), text)
|
||||
gen_file(joinpath(pkg_dir, "README.md"), text)
|
||||
return ["README.md"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_gitignore(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
gen_gitignore(pkg_dir::AbstractString, template::Template) -> Vector{String}
|
||||
|
||||
Create a `.gitignore` in the temp package directory.
|
||||
Create a `.gitignore` in `pkg_dir`.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `pkg_dir::AbstractString`: The directory in which the files will be generated.
|
||||
* `template::Template`: The template whose .gitignore we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_gitignore(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
function gen_gitignore(pkg_dir::AbstractString, template::Template)
|
||||
pkg = basename(pkg_dir)
|
||||
seen = [".DS_Store"]
|
||||
patterns = vcat(map(p -> p.gitignore, values(template.plugins))...)
|
||||
for pattern in patterns
|
||||
@ -315,28 +201,22 @@ function gen_gitignore(dir::AbstractString, pkg_name::AbstractString, template::
|
||||
end
|
||||
text = join(seen, "\n")
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, ".gitignore"), text)
|
||||
gen_file(joinpath(pkg_dir, ".gitignore"), text)
|
||||
return [".gitignore"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_license(
|
||||
dir::AbstractString,
|
||||
pkg_name::AbstractString,
|
||||
template::Template,
|
||||
) -> Vector{String}
|
||||
gen_license(pkg_dir::AbstractString, template::Template) -> Vector{String}
|
||||
|
||||
Create a license in the temp package directory.
|
||||
Create a license in `pkg_dir`.
|
||||
|
||||
# Arguments
|
||||
* `dir::AbstractString`: The directory in which the files will be generated. Note that
|
||||
this will be joined to `pkg_name`.
|
||||
* `pkg_name::AbstractString`: Name of the package.
|
||||
* `pkg_dir::AbstractString`: The directory in which the files will be generated.
|
||||
* `template::Template`: The template whose LICENSE we are generating.
|
||||
|
||||
Returns an array of generated file/directory names.
|
||||
"""
|
||||
function gen_license(dir::AbstractString, pkg_name::AbstractString, template::Template)
|
||||
function gen_license(pkg_dir::AbstractString, template::Template)
|
||||
if isempty(template.license)
|
||||
return String[]
|
||||
end
|
||||
@ -344,12 +224,12 @@ function gen_license(dir::AbstractString, pkg_name::AbstractString, template::Te
|
||||
text = "Copyright (c) $(template.years) $(template.authors)\n"
|
||||
text *= read_license(template.license)
|
||||
|
||||
gen_file(joinpath(dir, pkg_name, "LICENSE"), text)
|
||||
gen_file(joinpath(pkg_dir, "LICENSE"), text)
|
||||
return ["LICENSE"]
|
||||
end
|
||||
|
||||
"""
|
||||
gen_file(file_path::AbstractString, text::AbstractString) -> Int
|
||||
gen_file(file::AbstractString, text::AbstractString) -> Int
|
||||
|
||||
Create a new file containing some given text. Always ends the file with a newline.
|
||||
|
||||
@ -364,9 +244,7 @@ function gen_file(file::AbstractString, text::AbstractString)
|
||||
if !endswith(text , "\n")
|
||||
text *= "\n"
|
||||
end
|
||||
open(file, "w") do fp
|
||||
return write(fp, text)
|
||||
end
|
||||
return write(file, text)
|
||||
end
|
||||
|
||||
"""
|
||||
@ -381,10 +259,10 @@ Returns "major.minor" for the most recent release version relative to v. For pre
|
||||
with v.minor == v.patch == 0, returns "major.minor-".
|
||||
"""
|
||||
function version_floor(v::VersionNumber=VERSION)
|
||||
if isempty(v.prerelease) || v.patch > 0
|
||||
return "$(v.major).$(v.minor)"
|
||||
return if isempty(v.prerelease) || v.patch > 0
|
||||
"$(v.major).$(v.minor)"
|
||||
else
|
||||
return "$(v.major).$(v.minor)-"
|
||||
"$(v.major).$(v.minor)-"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -33,8 +33,8 @@ create a template, you can use [`interactive_template`](@ref) instead.
|
||||
the license. Can be supplied by a number, or a string such as "2016 - 2017".
|
||||
* `dir::AbstractString=$(dev_dir())`: Directory in which the package will go. Relative
|
||||
paths are converted to absolute ones at template creation time.
|
||||
* `precompile::Bool=true`: Whether or not to enable precompilation in generated packages.
|
||||
* `julia_version::VersionNumber=VERSION`: Minimum allowed Julia version.
|
||||
* `ssh::Bool=false`: Whether or not to use SSH for the remote.
|
||||
* `requirements::Vector{<:AbstractString}=String[]`: Package requirements. If there are
|
||||
duplicate requirements with different versions, i.e. ["PkgTemplates", "PkgTemplates
|
||||
0.1"], an `ArgumentError` is thrown. Each entry in this array will be copied into the
|
||||
@ -49,8 +49,8 @@ create a template, you can use [`interactive_template`](@ref) instead.
|
||||
authors::AbstractString
|
||||
years::AbstractString
|
||||
dir::AbstractString
|
||||
precompile::Bool
|
||||
julia_version::VersionNumber
|
||||
ssh::Bool
|
||||
requirements::Vector{AbstractString}
|
||||
gitconfig::Dict
|
||||
plugins::Dict{DataType, Plugin}
|
||||
@ -62,8 +62,8 @@ create a template, you can use [`interactive_template`](@ref) instead.
|
||||
authors::Union{AbstractString, Vector{<:AbstractString}}="",
|
||||
years::Union{Integer, AbstractString}=Dates.year(Dates.today()),
|
||||
dir::AbstractString=dev_dir(),
|
||||
precompile::Bool=true,
|
||||
julia_version::VersionNumber=VERSION,
|
||||
ssh::Bool=false,
|
||||
requirements::Vector{<:AbstractString}=String[],
|
||||
gitconfig::Dict=Dict(),
|
||||
plugins::Vector{<:Plugin}=Plugin[],
|
||||
@ -112,8 +112,8 @@ create a template, you can use [`interactive_template`](@ref) instead.
|
||||
end
|
||||
|
||||
new(
|
||||
user, host, license, authors, years, dir, precompile,
|
||||
julia_version, requirements_dedup, gitconfig, plugin_dict,
|
||||
user, host, license, authors, years, dir, julia_version, ssh,
|
||||
requirements_dedup, gitconfig, plugin_dict,
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -134,8 +134,8 @@ function Base.show(io::IO, t::Template)
|
||||
end
|
||||
|
||||
println(io, "$spc→ Package directory: $(replace(maybe_none(t.dir), homedir() => "~"))")
|
||||
println(io, "$spc→ Precompilation enabled: $(t.precompile ? "Yes" : "No")")
|
||||
println(io, "$spc→ Minimum Julia version: v$(version_floor(t.julia_version))")
|
||||
println(io, "$spc→ SSH remote: $(t.ssh ? "Yes" : "No")")
|
||||
|
||||
n = length(t.requirements)
|
||||
s = n == 1 ? "" : "s"
|
||||
@ -248,13 +248,6 @@ function interactive_template(; fast::Bool=false)
|
||||
isempty(dir) ? default_dir : dir
|
||||
end
|
||||
|
||||
kwargs[:precompile] = if fast
|
||||
true
|
||||
else
|
||||
print("Enable precompilation? [yes]: ")
|
||||
!in(uppercase(readline()), ["N", "NO", "F", "FALSE"])
|
||||
end
|
||||
|
||||
kwargs[:julia_version] = if fast
|
||||
VERSION
|
||||
else
|
||||
@ -264,6 +257,13 @@ function interactive_template(; fast::Bool=false)
|
||||
isempty(julia_version) ? default_julia_version : VersionNumber(julia_version)
|
||||
end
|
||||
|
||||
kwargs[:ssh] = if fast
|
||||
false
|
||||
else
|
||||
print("Set remote to SSH? [no]: ")
|
||||
in(uppercase(readline()), ["Y", "YES", "T", "TRUE"])
|
||||
end
|
||||
|
||||
kwargs[:requirements] = if fast
|
||||
String[]
|
||||
else
|
||||
|
@ -16,6 +16,7 @@
|
||||
@test t.years == string(Dates.year(Dates.today()))
|
||||
@test t.dir == default_dir
|
||||
@test t.julia_version == VERSION
|
||||
@test !t.ssh
|
||||
@test isempty(t.requirements)
|
||||
@test isempty(t.gitconfig)
|
||||
@test isempty(t.plugins)
|
||||
@ -25,7 +26,7 @@
|
||||
@test_throws ArgumentError t = interactive_template()
|
||||
end
|
||||
|
||||
write(stdin.buffer, "$me\ngitlab.com\n$('\x1b')[B\r$me\n2016\n$test_file\nno\n0.5\nX Y\nkey val val\nkey2 val2\n\n$('\x1b')[B\r$('\x1b')[B\rd\n\n")
|
||||
write(stdin.buffer, "$me\ngitlab.com\n$('\x1b')[B\r$me\n2016\n$test_file\n0.5\nyes\nX Y\nkey val val\nkey2 val2\n\n$('\x1b')[B\r$('\x1b')[B\rd\n\n")
|
||||
t = interactive_template()
|
||||
@test t.user == me
|
||||
@test t.host == "gitlab.com"
|
||||
@ -34,14 +35,15 @@
|
||||
@test t.authors == me
|
||||
@test t.years == "2016"
|
||||
@test t.dir == abspath(test_file)
|
||||
@test !t.precompile
|
||||
@test t.julia_version == v"0.5.0"
|
||||
@test t.ssh
|
||||
@test Set(t.requirements) == Set(["X", "Y"])
|
||||
@test t.gitconfig == Dict("key" => "val val", "key2" => "val2")
|
||||
# Like above, not sure which plugins this will generate.
|
||||
@test length(t.plugins) == 2
|
||||
|
||||
write(stdin.buffer, "$me\n\n\r\n\n\n\nA B\nA B\n\nd")
|
||||
# TODO: What is this supposed to warn about?
|
||||
write(stdin.buffer, "$me\n\n\r\n\nA B\nA B\n\nd")
|
||||
@test_logs (:warn, r".+") match_mode=:any interactive_template()
|
||||
|
||||
write(stdin.buffer, "$me\nd")
|
||||
@ -54,6 +56,7 @@
|
||||
@test t.years == string(Dates.year(Dates.today()))
|
||||
@test t.dir == default_dir
|
||||
@test t.julia_version == VERSION
|
||||
@test !t.ssh
|
||||
@test isempty(t.requirements)
|
||||
@test isempty(t.gitconfig)
|
||||
@test isempty(t.plugins)
|
||||
|
@ -4,9 +4,9 @@ using Dates
|
||||
using LibGit2
|
||||
|
||||
import PkgTemplates: badges, version_floor, substitute, read_license, gen_file, gen_readme,
|
||||
gen_tests, gen_license, gen_require, gen_entrypoint, gen_gitignore, gen_plugin,
|
||||
show_license, LICENSES, LICENSE_DIR, Plugin, GenericPlugin, CustomPlugin, Badge,
|
||||
format, interactive, DEFAULTS_DIR
|
||||
gen_tests, gen_license, gen_require, gen_gitignore, gen_plugin, show_license, LICENSES,
|
||||
LICENSE_DIR, Plugin, GenericPlugin, CustomPlugin, Badge, format, interactive,
|
||||
DEFAULTS_DIR
|
||||
|
||||
mktempdir() do temp_dir
|
||||
mkdir(joinpath(temp_dir, "dev"))
|
||||
|
@ -76,9 +76,6 @@ write(test_file, template_text)
|
||||
@test t.dir == joinpath(homedir(), basename(test_file))
|
||||
end
|
||||
|
||||
t = Template(; user=me, precompile=false)
|
||||
@test !t.precompile
|
||||
|
||||
t = Template(; user=me, julia_version=v"0.1.2")
|
||||
@test t.julia_version == v"0.1.2"
|
||||
|
||||
@ -156,8 +153,8 @@ end
|
||||
→ Host: github.com
|
||||
→ License: MIT ($(gitconfig["user.name"]) $(Dates.year(now())))
|
||||
→ Package directory: $pkg_dir
|
||||
→ Precompilation enabled: Yes
|
||||
→ Minimum Julia version: v$(PkgTemplates.version_floor())
|
||||
→ SSH remote: No
|
||||
→ 0 package requirements
|
||||
→ Git configuration options:
|
||||
• github.user = $(gitconfig["github.user"])
|
||||
@ -170,6 +167,7 @@ end
|
||||
user=me,
|
||||
license="",
|
||||
requirements=["Foo", "Bar"],
|
||||
ssh=true,
|
||||
gitconfig=gitconfig,
|
||||
plugins=[
|
||||
TravisCI(),
|
||||
@ -185,8 +183,8 @@ end
|
||||
→ Host: github.com
|
||||
→ License: None
|
||||
→ Package directory: $pkg_dir
|
||||
→ Precompilation enabled: Yes
|
||||
→ Minimum Julia version: v$(PkgTemplates.version_floor())
|
||||
→ SSH remote: Yes
|
||||
→ 2 package requirements: Bar, Foo
|
||||
→ Git configuration options:
|
||||
• github.user = $(gitconfig["github.user"])
|
||||
@ -224,7 +222,7 @@ end
|
||||
rm(temp_file)
|
||||
|
||||
# Test the README generation.
|
||||
@test gen_readme(temp_dir, test_pkg, t) == ["README.md"]
|
||||
@test gen_readme(pkg_dir, t) == ["README.md"]
|
||||
@test isfile(joinpath(pkg_dir, "README.md"))
|
||||
readme = readchomp(joinpath(pkg_dir, "README.md"))
|
||||
rm(joinpath(pkg_dir, "README.md"))
|
||||
@ -240,7 +238,7 @@ end
|
||||
something(findfirst("coveralls", readme)).start
|
||||
# Plugins with badges but not in BADGE_ORDER should appear at the far right side.
|
||||
t.plugins[Foo] = Foo()
|
||||
gen_readme(temp_dir, test_pkg, t)
|
||||
gen_readme(pkg_dir, t)
|
||||
readme = readchomp(joinpath(pkg_dir, "README.md"))
|
||||
rm(joinpath(pkg_dir, "README.md"))
|
||||
@test <(
|
||||
@ -249,7 +247,7 @@ end
|
||||
)
|
||||
|
||||
# Test the gitignore generation.
|
||||
@test gen_gitignore(temp_dir, test_pkg, t) == [".gitignore"]
|
||||
@test gen_gitignore(pkg_dir, t) == [".gitignore"]
|
||||
@test isfile(joinpath(pkg_dir, ".gitignore"))
|
||||
gitignore = read(joinpath(pkg_dir, ".gitignore"), String)
|
||||
rm(joinpath(pkg_dir, ".gitignore"))
|
||||
@ -261,7 +259,7 @@ end
|
||||
end
|
||||
|
||||
# Test the license generation.
|
||||
@test gen_license(temp_dir, test_pkg, t) == ["LICENSE"]
|
||||
@test gen_license(pkg_dir, t) == ["LICENSE"]
|
||||
@test isfile(joinpath(pkg_dir, "LICENSE"))
|
||||
license = readchomp(joinpath(pkg_dir, "LICENSE"))
|
||||
rm(joinpath(pkg_dir, "LICENSE"))
|
||||
@ -269,36 +267,21 @@ end
|
||||
@test occursin(t.years, license)
|
||||
@test occursin(read_license(t.license), license)
|
||||
|
||||
# Test the source code entrypoint generation.
|
||||
@test gen_entrypoint(temp_dir, test_pkg, t) == ["src/"]
|
||||
@test isdir(joinpath(pkg_dir, "src"))
|
||||
@test isfile(joinpath(pkg_dir, "src", "$test_pkg.jl"))
|
||||
entrypoint = readchomp(joinpath(pkg_dir, "src", "$test_pkg.jl"))
|
||||
rm(joinpath(pkg_dir, "src"); recursive=true)
|
||||
@test occursin("__precompile__()", entrypoint)
|
||||
@test occursin("module $test_pkg", entrypoint)
|
||||
t2 = Template(; user=me, precompile=false)
|
||||
gen_entrypoint(temp_dir, test_pkg, t2)
|
||||
entrypoint = readchomp(joinpath(pkg_dir, "src", "$test_pkg.jl"))
|
||||
@test !occursin("__precompile__()", entrypoint)
|
||||
@test occursin("module $test_pkg", entrypoint)
|
||||
rm(joinpath(pkg_dir, "src"); recursive=true)
|
||||
|
||||
# Test the REQUIRE generation.
|
||||
@test gen_require(temp_dir, test_pkg, t) == ["REQUIRE"]
|
||||
@test gen_require(pkg_dir, t) == ["REQUIRE"]
|
||||
@test isfile(joinpath(pkg_dir, "REQUIRE"))
|
||||
vf = version_floor(t.julia_version)
|
||||
@test readchomp(joinpath(pkg_dir, "REQUIRE")) == "julia $vf\n$test_pkg"
|
||||
rm(joinpath(pkg_dir, "REQUIRE"))
|
||||
|
||||
# Test the test generation.
|
||||
@test gen_tests(temp_dir, test_pkg, t) == ["test/"]
|
||||
@test gen_tests(pkg_dir, t) == ["Manifest.toml", "test/"]
|
||||
@test isdir(joinpath(pkg_dir, "test"))
|
||||
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
|
||||
runtests = readchomp(joinpath(pkg_dir, "test", "runtests.jl"))
|
||||
rm(joinpath(pkg_dir, "test"); recursive=true)
|
||||
@test occursin("using $test_pkg", runtests)
|
||||
@test occursin("using Base.Test", runtests)
|
||||
@test occursin("using Test", runtests)
|
||||
|
||||
rm(temp_dir; recursive=true)
|
||||
end
|
||||
@ -315,8 +298,10 @@ end
|
||||
@test isfile(joinpath(pkg_dir, ".gitignore"))
|
||||
@test isdir(joinpath(pkg_dir, "src"))
|
||||
@test isfile(joinpath(pkg_dir, "src", "$test_pkg.jl"))
|
||||
@test isfile(joinpath(pkg_dir, "Project.toml"))
|
||||
@test isdir(joinpath(pkg_dir, "test"))
|
||||
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
|
||||
@test isfile(joinpath(pkg_dir, "Manifest.toml"))
|
||||
# Check the gitconfig.
|
||||
repo = LibGit2.GitRepo(pkg_dir)
|
||||
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
|
||||
@ -331,8 +316,9 @@ end
|
||||
@test !LibGit2.isdirty(repo)
|
||||
rm(pkg_dir; recursive=true)
|
||||
|
||||
# Check that the remote is an SSH URL.
|
||||
generate(t, test_pkg; ssh=true) # Test the reversed-arguments method.
|
||||
# Check that the remote is an SSH URL when we want it to be.
|
||||
t = Template(; user=me, gitconfig=gitconfig, ssh=true)
|
||||
generate(t, test_pkg) # Test the reversed-arguments method.
|
||||
repo = LibGit2.GitRepo(pkg_dir)
|
||||
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
|
||||
@test LibGit2.url(remote) == "git@github.com:$me/$test_pkg.jl.git"
|
||||
@ -377,26 +363,6 @@ end
|
||||
@test !LibGit2.isdirty(repo)
|
||||
rm(pkg_dir; recursive=true)
|
||||
|
||||
# Check that an existing directory is removed when force is set.
|
||||
mkdir(pkg_dir)
|
||||
@test_throws ArgumentError generate(test_pkg, t)
|
||||
generate(test_pkg, t; force=true)
|
||||
@test isfile(joinpath(pkg_dir, "README.md"))
|
||||
rm(pkg_dir; recursive=true)
|
||||
|
||||
# Check that the backup directory mechanism works.
|
||||
temp_file, io = mktemp()
|
||||
close(io)
|
||||
temp_dir = mktempdir()
|
||||
t = Template(; user=me, dir=temp_file)
|
||||
@test_logs (:warn, r".+") match_mode=:any generate(test_pkg, t; backup_dir=temp_dir)
|
||||
rm(temp_dir; recursive=true)
|
||||
temp_dir = mktempdir()
|
||||
t = Template(; user=me, dir=joinpath(temp_file, "dir"))
|
||||
@test_logs (:warn, r".+") match_mode=:any generate(test_pkg, t; backup_dir=temp_dir)
|
||||
rm(temp_dir; recursive=true)
|
||||
rm(temp_file)
|
||||
|
||||
# Check that the generated docs root is just the copied README.
|
||||
t = Template(; user=me, gitconfig=gitconfig, plugins=[GitHubPages()])
|
||||
generate(test_pkg, t)
|
||||
|
Loading…
Reference in New Issue
Block a user