Skip to content

Commit

Permalink
Add copy, recopy and update from copier and change the API
Browse files Browse the repository at this point in the history
Adds copy, recopy, and update, following the copier API.
Change generate to accept a positional argument `data`.

BREAKING CHANGES: generate has a different API now

Closes #136
  • Loading branch information
abelsiqueira committed Jun 3, 2024
1 parent 835dbb2 commit 98be0ef
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 18 deletions.
64 changes: 54 additions & 10 deletions src/COPIERTemplate.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""
# COPIERTemplate.jl
This package defines a copier template for Julia packages and a basic user interface aroud copier
to use it from Julia.
The main functions are: [`generate`](@ref)
"""
module COPIERTemplate

using PythonCall, CondaPkg, UUIDs
Expand All @@ -7,24 +15,60 @@ function __init__()
end

"""
generate(dst_path; kwargs...)
generate(src_path, dst_path; kwargs...)
copy(dst_path[, data]; kwargs...)
copy(src_path, dst_path[, data]; kwargs...)
Runs the `copy` command of [copier](https://github.com/copier-org/copier) with the COPIERTemplate template.
Wrapper around Python's [copier.run_copy](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_copy).
Use [`generate`](@ref) for a more user-friendly function.
"""
function Base.copy(src_path, dst_path, data::Dict = Dict(); kwargs...)
copier = PythonCall.pyimport("copier")
copier.run_copy(src_path, dst_path, data; kwargs...)
end

If `src_path` is not informed, the GitHub URL of COPIERTemplate.jl is used.
function Base.copy(dst_path, data::Dict = Dict(); kwargs...)
copy(joinpath(@__DIR__, ".."), dst_path, data; kwargs...)
end

Even though the template is available offline through this template, this uses the github URL to allow updating.
"""
recopy(dst_path[, data]; kwargs...)
The keyword arguments are passed directly to the `run_copy` function of `copier`.
Wrapper around Python's [copier.run_recopy](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_recopy).
"""
function generate(src_path, dst_path; kwargs...)
function recopy(dst_path, data::Dict = Dict(); kwargs...)
copier = PythonCall.pyimport("copier")
copier.run_copy(src_path, dst_path; kwargs..., vcs_ref = "HEAD")
copier.run_recopy(dst_path, data; kwargs...)
end

"""
update(dst_path[, data]; kwargs...)
Wrapper around Python's [copier.run_update](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_update).
"""
function update(dst_path, data::Dict = Dict(); kwargs...)
copier = PythonCall.pyimport("copier")
copier.run_update(dst_path, data; kwargs...)
end

"""
generate(dst_path[, data]; kwargs...)
generate(src_path, dst_path[, data]; true, kwargs...)
Runs the `copy` command of [copier](https://github.com/copier-org/copier) with the COPIERTemplate template.
If `src_path` is not informed, the GitHub URL of COPIERTemplate.jl is used.
The `data` argument is a dictionary of answers (values) to questions (keys) that can be used to bypass some of the interactive questions.
## Keyword arguments
The keyword arguments are passed directly to [`copy`](@ref).
"""
function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
copy(src_path, dst_path, data; kwargs...)
end

function generate(dst_path, args...; kwargs...)
generate("https://github.com/abelsiqueira/COPIERTemplate.jl", dst_path, args...; kwargs...)
function generate(dst_path, data::Dict = Dict(); kwargs...)
generate("https://github.com/abelsiqueira/COPIERTemplate.jl", dst_path, data; kwargs...)
end

end
64 changes: 56 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ end
using COPIERTemplate
using Test

template_minimum_options = Dict(
"PackageName" => "Tmp",
"PackageUUID" => "1234",
"PackageOwner" => "test",
"AuthorName" => "Test",
"AuthorEmail" => "[email protected]",
)

template_options = Dict(
"PackageName" => "Tmp",
"PackageUUID" => "1234",
Expand Down Expand Up @@ -51,39 +59,79 @@ function test_diff_dir(dir1, dir2)
end
end

min_bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_minimum_options]...)
bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_options]...)
template_path = joinpath(@__DIR__, "..")
template_url = "https://github.com/abelsiqueira/COPIERTemplate.jl"

# This is a hack because Windows managed to dirty the repo.
if get(ENV, "CI", "nothing") == "true" && Sys.iswindows()
run(`git reset --hard HEAD`)
end

@testset "Compare COPIERTemplate.generate vs copier CLI on URL/main" begin
mktempdir(; prefix = "cli_") do dir_copier_cli
run(`copier copy --vcs-ref main --quiet $bash_args $template_url $dir_copier_cli`)

mktempdir(; prefix = "copy_") do tmpdir
COPIERTemplate.generate(tmpdir; data = template_options, quiet = true, vcs_ref = "main")
COPIERTemplate.generate(tmpdir, template_options; quiet = true, vcs_ref = "main")
test_diff_dir(tmpdir, dir_copier_cli)
end
end
end

@testset "Compare COPIERTemplate.generate vs copier CLI on HEAD" begin
# This is a hack because Windows managed to dirty the repo.
if get(ENV, "CI", "nothing") == "true" && Sys.iswindows()
run(`git reset --hard HEAD`)
end

mktempdir(; prefix = "cli_") do dir_copier_cli
run(`copier copy --vcs-ref HEAD --quiet $bash_args $template_path $dir_copier_cli`)

mktempdir(; prefix = "copy_") do tmpdir
COPIERTemplate.generate(
template_path,
tmpdir;
data = template_options,
tmpdir,
template_options;
quiet = true,
vcs_ref = "HEAD",
)
test_diff_dir(tmpdir, dir_copier_cli)
end
end
end

@testset "Testing copy, recopy and rebase" begin
mktempdir(; prefix = "cli_") do dir_copier_cli
run(`copier copy --quiet $bash_args $template_path $dir_copier_cli`)

@testset "Compare copied project vs copier CLI baseline" begin
mktempdir(; prefix = "copy_") do tmpdir
COPIERTemplate.copy(tmpdir, template_options; quiet = true)
test_diff_dir(tmpdir, dir_copier_cli)
end
end

@testset "Compare recopied project vs copier CLI baseline" begin
mktempdir(; prefix = "recopy_") do tmpdir
run(`copier copy --defaults --quiet $min_bash_args $template_path $tmpdir`)
COPIERTemplate.recopy(tmpdir, template_options; quiet = true, overwrite = true)
test_diff_dir(tmpdir, dir_copier_cli)
end
end

@testset "Compare updated project vs copier CLI baseline" begin
mktempdir(; prefix = "update_") do tmpdir
if !startswith("/private")(tmpdir)
tmpdir = "/private" * tmpdir
end
run(`copier copy --defaults --quiet $min_bash_args $template_path $tmpdir`)
cd(tmpdir) do
run(`git init`)
run(`git add .`)
run(`git config user.name "Test"`)
run(`git config user.email "[email protected]"`)
run(`git commit -m "First commit"`)
end
COPIERTemplate.update(tmpdir, template_options; overwrite = true, quiet = true)
test_diff_dir(tmpdir, dir_copier_cli)
end
end
end
end

0 comments on commit 98be0ef

Please sign in to comment.