From a31e632ff1a7b8a84db431bb3ace47999c84c926 Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Sat, 23 Sep 2023 00:47:58 +0200 Subject: [PATCH] Using PythonCall to enable running copier from the package --- .github/workflows/Test.yml | 1 - .gitignore | 5 ++-- CHANGELOG.md | 5 ++++ CondaPkg.toml | 2 ++ Project.toml | 7 +++++ README.md | 56 +++++++++++++++++++++++++------------- copier.yml | 2 ++ src/COPIERTemplate.jl | 24 +++++++++++++++- test/runtests.jl | 33 ++++++++++++++++++++-- 9 files changed, 110 insertions(+), 25 deletions(-) create mode 100644 CondaPkg.toml diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 3235a9ee..ee61d612 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -42,7 +42,6 @@ jobs: arch: - x64 allow_failure: [false] - steps: - uses: actions/checkout@v3 - uses: julia-actions/setup-julia@v1 diff --git a/.gitignore b/.gitignore index b19d550c..9a8db360 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ +.CondaPkg *.jl.*.cov *.jl.cov *.jl.mem -Manifest.toml -docs/build/ *.rej +docs/build/ +Manifest.toml diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e43de71..3dfba0a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning]. ## [Unreleased] +### Added + +- Should be installable as a Julia package now +- Use PythonCall to run copier directly from the Julia package + ### Changed - Move template to subdirectory diff --git a/CondaPkg.toml b/CondaPkg.toml new file mode 100644 index 00000000..432fb20b --- /dev/null +++ b/CondaPkg.toml @@ -0,0 +1,2 @@ +[deps] +copier = "" diff --git a/Project.toml b/Project.toml index f7ad5307..c5bf998a 100644 --- a/Project.toml +++ b/Project.toml @@ -3,5 +3,12 @@ uuid = "5022dd56-1d41-4538-9f4c-b20739ff8283" authors = ["Abel Soares Siqueira and contributors"] version = "0.1.7" +[deps] +CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab" +PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" +UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + [compat] julia = "1.6" +CondaPkg = "0.2" +PythonCall = "0.9" diff --git a/README.md b/README.md index 18645f5c..11268faa 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,32 @@ # COPIERTemplate.jl - Copier OPInionated Evolving Reusable Template + +[![Stable Documentation](https://img.shields.io/badge/docs-stable-blue.svg)](https://abelsiqueira.github.io/COPIERTemplate.jl/stable) +[![In development documentation](https://img.shields.io/badge/docs-dev-blue.svg)](https://abelsiqueira.github.io/COPIERTemplate.jl/dev) + [![Lint workflow Status](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Lint.yml/badge.svg?branch=main)](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Lint.yml?query=branch%3Amain) +[![Build Status](https://github.com/abelsiqueira/COPIERTemplate.jl/workflows/Test/badge.svg)](https://github.com/abelsiqueira/COPIERTemplate.jl/actions) +[![Test workflow status](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Test.yml/badge.svg?branch=main)](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Test.yml?query=branch%3Amain) +[![Lint workflow Status](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Lint.yml/badge.svg?branch=main)](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Lint.yml?query=branch%3Amain) +[![Docs workflow Status](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Docs.yml/badge.svg?branch=main)](https://github.com/abelsiqueira/COPIERTemplate.jl/actions/workflows/Docs.yml?query=branch%3Amain) +[![Coverage](https://codecov.io/gh/abelsiqueira/COPIERTemplate.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/abelsiqueira/COPIERTemplate.jl) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8350577.svg)](https://doi.org/10.5281/zenodo.8350577) -This is a [copier](https://copier.readthedocs.io) template/skeleton for Julia packages (see folder [template](template)). +This is + +- a [copier](https://copier.readthedocs.io) template/skeleton for Julia packages (see folder [template](template)). +- a package created with the template that wraps `copier` in Julia using `PythonCall`. -- It is opinionated but allows options +The template + +- is opinionated but allows options - Can be used in existing package (thanks for copier) - Automatically keeps track of changes in the template through Pull Requests Additional wishlist -- Use as template for other templates -- Allow using the template directly from Julia instead of installing copier (through PythonCall, possibly) +- Use as template for other templates (Maybe just use forks?) **But why?** @@ -28,22 +41,27 @@ I have done that in the past, but now I want even less manual intervention. ## How to install -1. Install [copier](https://copier.readthedocs.io). +> **Warning** +> +> There is no Windows support due to an issue with unsupported paths. +> See [Issue #21](https://github.com/abelsiqueira/COPIERTemplate.jl/pull/21). -1. Run copier with this template +1. Install this package, use the module and run `COPIERTemplate.generate(path)`. - ```bash - copier copy https://github.com/abelsiqueira/COPIERTemplate.jl YourPackage.jl - ``` + Alternatively, this can also be installed directly via [copier](https://copier.readthedocs.io), with the command -1. Follow the instructions. In particular you will need a UUID. Your Linux might have `uuidgen` installed, but you can also use Julia: + ```bash + copier copy https://github.com/abelsiqueira/COPIERTemplate.jl YourPackage.jl + ``` - ```bash - using UUIDs - uuid4() - ``` + Follow the instructions. In particular you will need a UUID. Your Linux might have `uuidgen` installed, but you can also use Julia: + + ```bash + using UUIDs + uuid4() + ``` -1. The resulting folder will not be a `git` package yet (to avoid trust issues), so you need to handle that yourself. First, install [`pre-commit`](https://pre-commit.com), and then issue: +2. The resulting folder will not be a `git` package yet (to avoid trust issues), so you need to handle that yourself. First, install [`pre-commit`](https://pre-commit.com), and then issue: ```bash cd YourPackage.jl @@ -57,13 +75,13 @@ I have done that in the past, but now I want even less manual intervention. It is common to have some pre-commit issues due to your package's name length triggering JuliaFormatter. -1. Create a repo on GitHub and push it +3. Create a repo on GitHub and push it ```bash git remote add origin https://github.com/UserName/PackageName.jl ``` -1. Create a `DOCUMENTER_KEY`, which will be used by for documentation purposes. +4. Create a `DOCUMENTER_KEY`, which will be used by for documentation purposes. ```bash pkg> activate --temp @@ -74,7 +92,7 @@ I have done that in the past, but now I want even less manual intervention. Follow the instruction in the terminal. -1. Create a Personal Access Token to be used by the Copier workflow. +5. Create a Personal Access Token to be used by the Copier workflow. 1. Go to . 2. Create a token with "Content", "Pull-request", and "Workflows" permissions. @@ -82,7 +100,7 @@ I have done that in the past, but now I want even less manual intervention. 4. Go to your YOUR_PACKAGE_URL/settings/secrets/actions. 5. Create a "New repository secret" named `COPIER_PAT`. -1. Before releasing, enable Zenodo integration at . +6. Before releasing, enable Zenodo integration at . ## What are all these files? diff --git a/copier.yml b/copier.yml index eb127050..7b0640e2 100644 --- a/copier.yml +++ b/copier.yml @@ -52,6 +52,8 @@ UseCirrusCI: _exclude: - .git + - .github/workflows/Docs.yml + - .github/workflows/Test.yml - CHANGELOG.md - copier.yml - LICENSE diff --git a/src/COPIERTemplate.jl b/src/COPIERTemplate.jl index 54c0bf1e..8f2ad29d 100644 --- a/src/COPIERTemplate.jl +++ b/src/COPIERTemplate.jl @@ -1,5 +1,27 @@ module COPIERTemplate -# Write your package code here. +using PythonCall, CondaPkg, UUIDs + +function __init__() + CondaPkg.add("copier") +end + +""" + generate(path, generate_missing_uuid = true; kwargs...) + +Runs the `copy` command of [copier](https://github.com/copier-org/copier) with the COPIERTemplate template. +Even though the template is available offline through this template, this uses the github URL to allow updating. + +The keyword arguments are passed directly to the `run_copy` function of `copier`. +If `generate_missing_uuid` is `true` and there is no `kwargs[:data]["PackageUUID"]`, then a UUID is generated for the package. +""" +function generate(path, generate_missing_uuid = true; kwargs...) + copier = PythonCall.pyimport("copier") + data = copy(get(kwargs, :data, Dict())) + if generate_missing_uuid && !("PackageUUID" in keys(data)) + data["PackageUUID"] = string(uuid4()) + end + copier.run_copy("https://github.com/abelsiqueira/COPIERTemplate.jl", path; kwargs..., data = data) +end end diff --git a/test/runtests.jl b/test/runtests.jl index 77df0a6a..19c83458 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,35 @@ using COPIERTemplate using Test -@testset "COPIERTemplate.jl" begin - # Write your tests here. +template_options = Dict( + "PackageName" => "Tmp", + "PackageUUID" => "1234", + "PackageOwner" => "test", + "AuthorName" => "Test", + "AuthorEmail" => "test@me.now", + "JuliaMinVersion" => "1.6", + "License" => "MIT", + "AddMacToCI" => true, + "AddWinToCI" => true, + "RunJuliaNightlyOnCI" => true, + "UseCirrusCI" => true, +) + +@testset "Compare folder generated by this call vs direct copier" begin + tmpdir1 = mktempdir() + tmpdir2 = mktempdir() + + COPIERTemplate.generate(tmpdir1; data = template_options) + bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_options]...) + run(`copier copy $bash_args https://github.com/abelsiqueira/COPIERTemplate.jl $tmpdir2`) + for (root, dirs, files) in walkdir(tmpdir1) + for file in files + file1 = joinpath(root, file) + file2 = replace(file1, tmpdir1 => tmpdir2) + lines1 = readlines(file1) + lines2 = readlines(file2) + diff = ["$line1 vs $line2" for (line1, line2) in zip(lines1, lines2) if line1 != line2] + @test diff == [] + end + end end