Skip to content

Commit

Permalink
feat: 🎸 Create safeguard when running generate in existing pkg
Browse files Browse the repository at this point in the history
If generate is run in a path that has an answer file, a warning message
is shown to remind the user of the update command.

✅ Closes: #247
  • Loading branch information
abelsiqueira committed Jun 8, 2024
1 parent 5d0d6e6 commit 12169b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/COPIERTemplate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ The `data` argument is a dictionary of answers (values) to questions (keys) that
## Keyword arguments
The keyword arguments are passed directly to the internal [`Copier.copy`](@ref).
- `warn_existing_pkg::Boolean = true`: Whether to check if you actually meant `update`. If you run `generate` and the `dst_path` contains a `.copier-answers.yml`, it means that the copy was already made, so you might have means `update` instead. When `true`, a warning is shown and execution is stopped.
The other keyword arguments are passed directly to the internal [`Copier.copy`](@ref).
"""
function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
function generate(src_path, dst_path, data::Dict = Dict(); warn_existing_pkg = true, kwargs...)
if warn_existing_pkg && isfile(joinpath(dst_path, ".copier-answers.yml"))
@warn """There already exists a `.copier-answers.yml` file in the destination path.
You might have meant to use `COPIERTemplate.update` instead, which only fetches the non-applying updates.
If you really meant to use this command, then pass the `warn_existing_pkg = false` flag to this call.
"""

return nothing
end

# 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)
Expand All @@ -46,6 +57,8 @@ function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
end
end
Copier.copy(src_path, dst_path, data; kwargs...)

return nothing
end

function generate(dst_path, data::Dict = Dict(); kwargs...)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ end
end
end

@testset "Test that COPIERTemplate.generate warns and exits for existing copy" begin
mktempdir(TMPDIR; prefix = "cli_") do dir_copier_cli
run(`copier copy --quiet $bash_args $template_url $dir_copier_cli`)

@test_logs (:warn,) COPIERTemplate.generate(dir_copier_cli; quiet = true)
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`)
Expand Down

0 comments on commit 12169b2

Please sign in to comment.