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 e2a22fc
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ReusableTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
uses: julia-actions/cache@v2
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
TMPDIR: "${{ runner.temp }}"
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
with:
Expand Down
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
79 changes: 66 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# This is only useful for testing offline. It creates a local env to avoid redownloading things.
# Can't use mktempdir on GitHub actions willy nilly (at least on Mac)
TMPDIR = get(ENV, "TMPDIR", mktempdir())

if get(ENV, "CI", "nothing") == "nothing"
# This is only useful for testing offline. It creates a local env to avoid redownloading things.
ENV["JULIA_CONDAPKG_ENV"] = joinpath(@__DIR__, "conda-env")
if isdir(ENV["JULIA_CONDAPKG_ENV"])
ENV["JULIA_CONDAPKG_OFFLINE"] = true
end

# Mac is a complicated beast
if Sys.isapple()
TMPDIR = "/private$TMPDIR"
end
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 +67,76 @@ 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
mktempdir(TMPDIR; 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")
mktempdir(TMPDIR; prefix = "copy_") do tmpdir
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
mktempdir(TMPDIR; 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
mktempdir(TMPDIR; 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(TMPDIR; 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(TMPDIR; 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(TMPDIR; 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(TMPDIR; prefix = "update_") do tmpdir
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 -q -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 e2a22fc

Please sign in to comment.