From 9ffa546ec391e1450ee4cd1d2153c3fe0d435ad0 Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Tue, 4 Jun 2024 12:50:07 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Read=20from=20dst=5Fpath?= =?UTF-8?q?/Project.toml=20to=20guess=20answers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If dsp_path/Project.toml exists, read from it to guess some of the answers. Passing data to the generate function will override the guessed values. BREAKING CHANGE: 🧨 Applying to existing packages has a different behaviour now. ✅ Closes: #116 --- CHANGELOG.md | 1 + Project.toml | 1 + src/COPIERTemplate.jl | 41 +++++++++++++++++++++++++++++++++++++++++ test/Project.toml | 2 ++ test/runtests.jl | 24 ++++++++++++++++++++++++ 5 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82a29b79..54eb3da8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning]. - New question: SimplifiedPRTest to simplify the testing on Pull Requests (#105) - New question: AddAllcontributors to add a section and config for (#26) - `copy`, `recopy` and `update` from the copier API (#142) +- When applying to existing projects, read Project.toml to infer a few values (#116) ### Changed diff --git a/Project.toml b/Project.toml index 7e8a67fe..4719be7c 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.4.0" [deps] CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab" PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" +TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] diff --git a/src/COPIERTemplate.jl b/src/COPIERTemplate.jl index 4d4a552a..075185b7 100644 --- a/src/COPIERTemplate.jl +++ b/src/COPIERTemplate.jl @@ -10,6 +10,8 @@ module COPIERTemplate include("Copier.jl") +using TOML: TOML + """ generate(dst_path[, data]; kwargs...) generate(src_path, dst_path[, data]; true, kwargs...) @@ -24,6 +26,17 @@ The `data` argument is a dictionary of answers (values) to questions (keys) that The keyword arguments are passed directly to the internal [`Copier.copy`](@ref). """ function generate(src_path, dst_path, data::Dict = Dict(); kwargs...) + # If there are answers in the destionation path, skip guessing the answers + if !isfile(joinpath(dst_path, ".copier-answers")) && isdir(dst_path) + existing_data = _read_data_from_existing_path(dst_path) + for (key, value) in existing_data + @info "Inferred $key=$value from destination path" + if haskey(data, key) + @info " Being overriden by supplied $key=$(data[key]) value" + end + end + data = merge(existing_data, data) + end Copier.copy(src_path, dst_path, data; kwargs...) end @@ -31,4 +44,32 @@ function generate(dst_path, data::Dict = Dict(); kwargs...) generate("https://github.com/abelsiqueira/COPIERTemplate.jl", dst_path, data; kwargs...) end +""" + data = _read_data_from_existing_path(dst_path) + +Reads the destination folder to figure out some answers. +""" +function _read_data_from_existing_path(dst_path) + data = Dict{String, Any}() + if isfile(joinpath(dst_path, "Project.toml")) + toml_data = TOML.parsefile(joinpath(dst_path, "Project.toml")) + for (toml_key, copier_key) in [("name", "PackageName"), ("uuid", "PackageUUID")] + if haskey(toml_data, toml_key) + data[copier_key] = toml_data[toml_key] + end + end + # Author capture is limited and does not handle multiple authors. See #118 for more information. + if haskey(toml_data, "authors") + author_regex = r"^(.*) <(.*)>(?: and contributors)?" + m = match(author_regex, toml_data["authors"][1]) + if !isnothing(m) + data["AuthorName"] = m[1] + data["AuthorEmail"] = m[2] + end + end + end + + return data +end + end diff --git a/test/Project.toml b/test/Project.toml index 0c363327..30e2e2a6 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,2 +1,4 @@ [deps] +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" diff --git a/test/runtests.jl b/test/runtests.jl index a50c6d9d..3d6fffd5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,7 +15,9 @@ if get(ENV, "CI", "nothing") == "nothing" end using COPIERTemplate +using Pkg using Test +using YAML template_minimum_options = Dict( "PackageName" => "Tmp", @@ -140,3 +142,25 @@ end end end end + +@testset "Test generating the template on an existing project" begin + mktempdir(TMPDIR; prefix = "existing_") do dir_existing + cd(dir_existing) do + Pkg.generate("NewPkg") + @info walkdir(".") |> collect + COPIERTemplate.generate( + template_path, + "NewPkg/", + Dict("AuthorName" => "T. Esther", "PackageOwner" => "test"); + defaults = true, + overwrite = true, + quiet = true, + vcs_ref = "HEAD", + ) + answers = YAML.load_file("NewPkg/.copier-answers.yml") + @test answers["PackageName"] == "NewPkg" + @test answers["AuthorName"] == "T. Esther" + @test answers["PackageOwner"] == "test" + end + end +end