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

Add apply function #3

Merged
merged 1 commit into from
Oct 14, 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
Binary file added .CondaPkg/meta
Binary file not shown.
2 changes: 2 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
copier = ""
5 changes: 5 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ uuid = "68831218-7a4b-5845-b8cc-89c14679f12c"
authors = ["Tangi Migot <[email protected]>"]
version = "0.1.0"

[deps]
BestieTemplate = "e5d671a7-4186-4f1a-bf88-c19b630da94f"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
julia = "1.10"
26 changes: 26 additions & 0 deletions docs/src/1-howto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# JSOBestie Template

## Creating a new package

We rely on [BestieTemplate.jl](https://github.com/JuliaBesties/BestieTemplate.jl) for generating packages, so please have a look at their documentation.

```julia
julia> using BestieTemplate
julia> BestieTemplate.generate("path/to/YourNewPackage.jl")
julia> # or BestieTemplate.apply("path/to/YourExistingPackage.jl")
```

Please answer all the questions, and once the new package is ready, adds an additional layer on the template with the following.

```julia
julia> using JSOBestieTemplate
julia> JSOBestieTemplate.apply("path/to/YourExistingPackage.jl")
```

## Updating your package

```julia
julia> using BestieTemplate
julia> BestieTemplate.update("path/to/YourExistingPackage.jl")
julia> JSOBestieTemplate.apply("path/to/YourExistingPackage.jl")
```
10 changes: 3 additions & 7 deletions src/JSOBestieTemplate.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module JSOBestieTemplate

"""
hi = hello_world()
A simple function to return "Hello, World!"
"""
function hello_world()
return "Hello, World!"
end
using BestieTemplate, OrderedCollections, YAML

include("apply.jl")

end
78 changes: 78 additions & 0 deletions src/apply.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
apply(dst_path[, data]; kwargs...)
apply(src_path, dst_path[, data]; true, kwargs...)

Applies the template to an existing project at path ``dst_path``.
If the `dst_path` does not exist, this will throw an error.
For new packages, use `BestieTemplate.generate` instead.

## Keyword arguments

- `warn_existing_pkg::Bool = true`: Whether to check if you actually meant `update`. If you run `apply` 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.
- `quiet::Bool = false`: Whether to print greetings, info, and other messages. This keyword is also used by copier.

The other keyword arguments are passed directly to the internal [`Copier.copy`](@ref).
"""
function apply(
src_path,
dst_path;
add_benchmark = true,
warn_existing_pkg = true,
kwargs...,
)
quiet = get(kwargs, :quiet, false)

if !isdir(dst_path)
error("$dst_path does not exist. For new packages, use `BestieTemplate` first.")
end
if !isdir(joinpath(dst_path, ".git"))
error("""No folder $dst_path/.git found. Are you using git on the project?
To apply to existing packages, git is required to avoid data loss.""")
end
if isdir(joinpath(dst_path, "benchmark"))
add_benchmark = false
end

if warn_existing_pkg && isfile(joinpath(dst_path, ".copier-answers.jso.yml"))
@warn """There already exists a `.copier-answers.jso.yml` file in the destination path.
You might have meant to use `(JSO)BestieTemplate.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

# generate_jso_copier_answers(dst_path)
data = YAML.load_file(joinpath(dst_path, ".copier-answers.yml"))
jso_data = OrderedDict{String,Any}()
names_copier = ["PackageName", "PackageOwner", "PackageUUID", "_src_path", "_commit"]
for entry in names_copier
jso_data[entry] = data[entry]
end
jso_data["AddBreakage"] = true
jso_data["AddBenchmark"] = !isdir(joinpath(dst_path, "benchmark")) | add_benchmark
jso_data["AddBenchmarkCI"] =
isdir(joinpath(dst_path, "benchmark")) | jso_data["AddBenchmark"]
jso_data["AddCirrusCI"] = true
YAML.write_file(joinpath(dst_path, ".copier-answers.jso.yml"), jso_data)

BestieTemplate.Copier.copy(
"template",
"../JSOSuite.jl",
answers_file = ".copier-answers.jso.yml",
)

package_name = data["PackageName"]
quiet || println("""JSOBestieTemplate was applied to $package_name.jl! 🎉 """)

return nothing
end

function apply(dst_path; kwargs...)
apply(
"https://github.com/tmigot/JSOBestieTemplate.jl/template",
dst_path,
data;
kwargs...,
)
end
Loading