Skip to content

Commit

Permalink
Using PythonCall to enable running copier from the package
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira committed Sep 24, 2023
1 parent 0d25e31 commit 871c4a1
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 27 deletions.
1 change: 0 additions & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ jobs:
arch:
- x64
allow_failure: [false]

steps:
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.CondaPkg
*.jl.*.cov
*.jl.cov
*.jl.mem
Manifest.toml
docs/build/
*.rej
docs/build/
Manifest.toml
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
copier = ""
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ uuid = "5022dd56-1d41-4538-9f4c-b20739ff8283"
authors = ["Abel Soares Siqueira <[email protected]> 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"
62 changes: 41 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@

# COPIERTemplate.jl - Copier OPInionated Evolving Reusable Template

<!-- markdown-link-check-disable -->
[![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)
<!-- markdown-link-check-enable -->
[![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)); and
- a package created with the template that wraps `copier` in Julia using `PythonCall`.

- It 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
The template

- is opinionated but allows options;
- can be applied to existing packages (thanks to copier);
- is automatically reapplied through Pull Requests made by the Copier.yml workflow.

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?**

Expand All @@ -25,23 +38,30 @@ Because I have around 50 packages that follow similar configuration (but not equ
**What about mass updates using the GitHub API?**

I have done that in the past, but now I want even less manual intervention.
This will still require manual installation for the first time, and will still allow verifying the pull requests.

## How to install

1. Install [copier](https://copier.readthedocs.io).
> **Warning**
>
> It is unknown if the package works on Windows due to an issue with unsupported paths.
> See [Issue #21](https://github.com/abelsiqueira/COPIERTemplate.jl/pull/21).
> If it doesn't work, let us know, and use the alternative installation.
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:

Expand Down Expand Up @@ -77,10 +97,10 @@ I have done that in the past, but now I want even less manual intervention.
1. Create a Personal Access Token to be used by the Copier workflow.
1. Go to <https://github.com/settings/tokens>.
2. Create a token with "Content", "Pull-request", and "Workflows" permissions.
3. Copy the Token.
4. Go to your YOUR_PACKAGE_URL/settings/secrets/actions.
5. Create a "New repository secret" named `COPIER_PAT`.
1. Create a token with "Content", "Pull-request", and "Workflows" permissions.
1. Copy the Token.
1. Go to your YOUR_PACKAGE_URL/settings/secrets/actions.
1. Create a "New repository secret" named `COPIER_PAT`.
1. Before releasing, enable Zenodo integration at <https://zenodo.org/account/settings/github/>.
Expand Down Expand Up @@ -126,7 +146,7 @@ Installing pre-commit (`pre-commit install`) will make sure that it runs right t
Additionally, if you run `pre-commit run -a`, it runs all hooks, which can be used for Linting.
Some hooks in the `.pre-commit-config.yaml` file have configuration files of their own:
`.JuliaFormatter.toml`, `.markdownlint.json`, and `.yamllint.yml`.
`.JuliaFormatter.toml`, `.markdownlint.json`, `.markdown-link-config.json`, and `.yamllint.yml`.
Also slightly related, is the `.editorconfig` file, which tells your editor, if you install the coorect plugin, how to format some things.
Expand Down
2 changes: 2 additions & 0 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ UseCirrusCI:

_exclude:
- .git
- .github/workflows/Docs.yml
- .github/workflows/Test.yml
- CHANGELOG.md
- copier.yml
- LICENSE
Expand Down
24 changes: 23 additions & 1 deletion src/COPIERTemplate.jl
Original file line number Diff line number Diff line change
@@ -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
33 changes: 31 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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" => "[email protected]",
"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

0 comments on commit 871c4a1

Please sign in to comment.