Update the Project.toml with TOML instead of string manipulation
This commit is contained in:
parent
cfed5fc7e6
commit
05255f5555
@ -6,7 +6,7 @@ using Base.Filesystem: contractuser
|
|||||||
using Dates: month, today, year
|
using Dates: month, today, year
|
||||||
using InteractiveUtils: subtypes
|
using InteractiveUtils: subtypes
|
||||||
using LibGit2: LibGit2
|
using LibGit2: LibGit2
|
||||||
using Pkg: Pkg, PackageSpec
|
using Pkg: Pkg, TOML, PackageSpec
|
||||||
using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request
|
using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request
|
||||||
|
|
||||||
using Mustache: render
|
using Mustache: render
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
const TEST_UUID = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
(::Template)(pkg::AbstractString)
|
(::Template)(pkg::AbstractString)
|
||||||
|
|
||||||
@ -14,17 +12,14 @@ function (t::Template)(pkg::AbstractString)
|
|||||||
# Create the directory with some boilerplate inside.
|
# Create the directory with some boilerplate inside.
|
||||||
Pkg.generate(pkg_dir)
|
Pkg.generate(pkg_dir)
|
||||||
|
|
||||||
# Add a [compat] section for Julia. By default, Julia 1.x is supported.
|
# Replace the authors field with the template's authors, and add a compat entry.
|
||||||
open(joinpath(pkg_dir, "Project.toml"), "a") do io
|
|
||||||
println(io, "\n[compat]\njulia = \"1\"")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Replace the authors field with the template's authors.
|
|
||||||
if !isempty(t.authors)
|
if !isempty(t.authors)
|
||||||
path = joinpath(pkg_dir, "Project.toml")
|
path = joinpath(pkg_dir, "Project.toml")
|
||||||
project = read(path, String)
|
toml = TOML.parsefile(path)
|
||||||
authors = string("[", join(map(repr ∘ strip, split(t.authors, ",")), ", "), "]")
|
# TODO: authors should probably be kept as a vector.
|
||||||
write(path, replace(project, r"authors = .*" => "authors = $authors"))
|
toml["authors"] = split(t.authors, ",")
|
||||||
|
get!(toml, "compat", Dict())["julia"] = compat_version(t.julia_version)
|
||||||
|
open(io -> TOML.print(io, toml), path, "w")
|
||||||
end
|
end
|
||||||
|
|
||||||
if t.git
|
if t.git
|
||||||
@ -59,3 +54,14 @@ function (t::Template)(pkg::AbstractString)
|
|||||||
rethrow()
|
rethrow()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Format the version to be included in Project.toml's [compat] section.
|
||||||
|
function compat_version(v::VersionNumber)
|
||||||
|
return if v.patch == 0 && v.minor == 0
|
||||||
|
string(v.major)
|
||||||
|
elseif v.patch == 0
|
||||||
|
"$(v.major).$(v.minor)"
|
||||||
|
else
|
||||||
|
"$(v.major).$(v.minor).$(v.patch)"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
const TEST_UUID = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
||||||
const LICENSE_DIR = normpath(joinpath(@__DIR__, "..", "..", "licenses"))
|
const LICENSE_DIR = normpath(joinpath(@__DIR__, "..", "..", "licenses"))
|
||||||
const LICENSES = Dict(
|
const LICENSES = Dict(
|
||||||
"MIT" => "MIT \"Expat\" License",
|
"MIT" => "MIT \"Expat\" License",
|
||||||
@ -98,16 +99,18 @@ function gen_plugin(p::Tests, t::Template, pkg_dir::AbstractString)
|
|||||||
# Do the normal BasicPlugin behaviour to create the test script.
|
# Do the normal BasicPlugin behaviour to create the test script.
|
||||||
invoke(gen_plugin, Tuple{BasicPlugin, Template, AbstractString}, p, t, pkg_dir)
|
invoke(gen_plugin, Tuple{BasicPlugin, Template, AbstractString}, p, t, pkg_dir)
|
||||||
|
|
||||||
# Add the Test dependency as a test-only dependency.
|
# Add Test as a test-only dependency.
|
||||||
# To avoid visual noise from adding/removing the dependency, insert it manually.
|
path = joinpath(pkg_dir, "Project.toml")
|
||||||
|
toml = TOML.parsefile(path)
|
||||||
|
get!(toml, "extras", Dict())["Test"] = TEST_UUID
|
||||||
|
get!(toml, "targets", Dict())["test"] = ["Test"]
|
||||||
|
open(io -> TOML.print(io, toml), path, "w")
|
||||||
|
|
||||||
|
# Generate the manifest.
|
||||||
|
touch(joinpath(pkg_dir, "Manifest.toml")) # File must exist to be modified by Pkg.
|
||||||
proj = current_project()
|
proj = current_project()
|
||||||
try
|
try
|
||||||
Pkg.activate(pkg_dir)
|
Pkg.activate(pkg_dir)
|
||||||
lines = readlines(joinpath(pkg_dir, "Project.toml"))
|
|
||||||
dep = "Test = $(repr(TEST_UUID))"
|
|
||||||
push!(lines, "[extras]", dep, "", "[targets]", "test = [\"Test\"]")
|
|
||||||
gen_file(joinpath(pkg_dir, "Project.toml"), join(lines, "\n"))
|
|
||||||
touch(joinpath(pkg_dir, "Manifest.toml")) # File must exist to be modified by Pkg.
|
|
||||||
Pkg.update() # Clean up both Manifest.toml and Project.toml.
|
Pkg.update() # Clean up both Manifest.toml and Project.toml.
|
||||||
finally
|
finally
|
||||||
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
|
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
|
||||||
|
Loading…
Reference in New Issue
Block a user