Update the Project.toml with TOML instead of string manipulation

This commit is contained in:
Chris de Graaf 2019-08-27 12:05:51 +07:00
parent cfed5fc7e6
commit 05255f5555
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
3 changed files with 28 additions and 19 deletions

View File

@ -6,7 +6,7 @@ using Base.Filesystem: contractuser
using Dates: month, today, year
using InteractiveUtils: subtypes
using LibGit2: LibGit2
using Pkg: Pkg, PackageSpec
using Pkg: Pkg, TOML, PackageSpec
using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request
using Mustache: render

View File

@ -1,5 +1,3 @@
const TEST_UUID = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
"""
(::Template)(pkg::AbstractString)
@ -14,17 +12,14 @@ function (t::Template)(pkg::AbstractString)
# Create the directory with some boilerplate inside.
Pkg.generate(pkg_dir)
# Add a [compat] section for Julia. By default, Julia 1.x is supported.
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.
# Replace the authors field with the template's authors, and add a compat entry.
if !isempty(t.authors)
path = joinpath(pkg_dir, "Project.toml")
project = read(path, String)
authors = string("[", join(map(repr strip, split(t.authors, ",")), ", "), "]")
write(path, replace(project, r"authors = .*" => "authors = $authors"))
toml = TOML.parsefile(path)
# TODO: authors should probably be kept as a vector.
toml["authors"] = split(t.authors, ",")
get!(toml, "compat", Dict())["julia"] = compat_version(t.julia_version)
open(io -> TOML.print(io, toml), path, "w")
end
if t.git
@ -59,3 +54,14 @@ function (t::Template)(pkg::AbstractString)
rethrow()
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

View File

@ -1,3 +1,4 @@
const TEST_UUID = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
const LICENSE_DIR = normpath(joinpath(@__DIR__, "..", "..", "licenses"))
const LICENSES = Dict(
"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.
invoke(gen_plugin, Tuple{BasicPlugin, Template, AbstractString}, p, t, pkg_dir)
# Add the Test dependency as a test-only dependency.
# To avoid visual noise from adding/removing the dependency, insert it manually.
# Add Test as a test-only dependency.
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()
try
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.
finally
proj === nothing ? Pkg.activate() : Pkg.activate(proj)