Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Determine package name from the dst_path #210

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning].
- New question: AddAllcontributors to add a section and config for <https://allcontributors.org> (#26)
- `copy`, `recopy` and `update` from the copier API (#142)
- When applying to existing projects, read Project.toml to infer a few values (#116)
- Automatically determines the `PackageName` from the destination folder (#151)

### Changed

Expand Down
20 changes: 20 additions & 0 deletions src/COPIERTemplate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
end
data = merge(existing_data, data)
end
# If the PackageName was not given or guessed from the Project.toml, use the sanitized path
if !haskey(data, "PackageName")
package_name = _sanitize_package_name(dst_path)
if package_name != ""
@info "Using sanitized path $package_name as package name"
data["PackageName"] = package_name
end
end
Copier.copy(src_path, dst_path, data; kwargs...)
end

Expand Down Expand Up @@ -72,4 +80,16 @@ function _read_data_from_existing_path(dst_path)
return data
end

"""
package_name = _sanitize_package_name(path)

Sanitize the `path` to guess the package_name by looking at the
base name and removing an extension. If the result is not a valid
identifier, returns "".
"""
function _sanitize_package_name(dst_path)
package_name = dst_path |> basename |> splitext |> first
return Base.isidentifier(package_name) ? package_name : ""
end

end
28 changes: 27 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ end
mktempdir(TMPDIR; prefix = "existing_") do dir_existing
cd(dir_existing) do
Pkg.generate("NewPkg")
@info walkdir(".") |> collect
COPIERTemplate.generate(
template_path,
"NewPkg/",
Expand All @@ -163,4 +162,31 @@ end
@test answers["PackageOwner"] == "test"
end
end

@testset "Test automatic guessing the package name from the path" begin
mktempdir(TMPDIR; prefix = "path_is_dir_") do dir_path_is_dir
cd(dir_path_is_dir) do
data = Dict(key => value for (key, value) in template_options if key != "PackageName")
mkdir("some_folder")
COPIERTemplate.generate(
template_path,
"some_folder/SomePackage1.jl",
data;
quiet = true,
vcs_ref = "HEAD",
)
answers = YAML.load_file("some_folder/SomePackage1.jl/.copier-answers.yml")
@test answers["PackageName"] == "SomePackage1"
COPIERTemplate.generate(
template_path,
"some_folder/SomePackage2.jl",
merge(data, Dict("PackageName" => "OtherName"));
quiet = true,
vcs_ref = "HEAD",
)
answers = YAML.load_file("some_folder/SomePackage2.jl/.copier-answers.yml")
@test answers["PackageName"] == "OtherName"
end
end
end
end
Loading