From b99229faedf99334ac33b2a8ededfad78ec1e2c8 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 21 Nov 2024 14:07:31 -0500 Subject: [PATCH] Add generate function --- .JuliaFormatter.toml | 1 + .pre-commit-config.yaml | 13 ++++ Project.toml | 10 +++ README.md | 10 ++- src/ITensorPkgSkeleton.jl | 66 ++++++++++++++++++- templates/default/.pre-commit-config.yaml | 13 ++++ .../default/src/{PKGNAME.jl => {PKGNAME}.jl} | 0 7 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 templates/default/.pre-commit-config.yaml rename templates/default/src/{PKGNAME.jl => {PKGNAME}.jl} (100%) diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 4c49a86..376ec44 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -1,3 +1,4 @@ # See https://domluna.github.io/JuliaFormatter.jl/stable/ for a list of options style = "blue" +ignore = ["templates"] indent = 2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..bff1fb7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + exclude_types: [markdown] # incompatible with Literate.jl +- repo: https://github.com/qiaojunfeng/pre-commit-julia-format + rev: v0.2.0 + hooks: + - id: julia-format diff --git a/Project.toml b/Project.toml index daa1705..e5cc650 100644 --- a/Project.toml +++ b/Project.toml @@ -3,8 +3,18 @@ uuid = "3d388ab1-018a-49f4-ae50-18094d5f71ea" authors = ["ITensor developers and contributors"] version = "0.1.0" +[deps] +Git = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2" +Git_jll = "f8c6e375-362e-5223-8a59-34ff63f689eb" +PkgSkeleton = "d254efa0-af53-535e-b7f1-03c1c9fbcbe7" +Preferences = "21216c6a-2e73-6563-6e65-726566657250" + [compat] Aqua = "0.8.9" +Git = "1.3.1" +Git_jll = "2.46.2" +PkgSkeleton = "1.3.1" +Preferences = "1.4.3" Test = "1.10" julia = "1.10" diff --git a/README.md b/README.md index 0449c60..78020a7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ITensorPkgSkeleton +# ITensorPkgSkeleton.jl [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://ITensor.github.io/ITensorPkgSkeleton.jl/stable/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://ITensor.github.io/ITensorPkgSkeleton.jl/dev/) @@ -6,3 +6,11 @@ [![Coverage](https://codecov.io/gh/ITensor/ITensorPkgSkeleton.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ITensor/ITensorPkgSkeleton.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) [![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) + +```julia +using ITensorPkgSkeleton: ITensorPkgSkeleton +# This step might be required to circumvent issues with +# the version of git installed by `Git.jl`. +ITensorPkgSkeleton.use_system_git!() +ITensorPkgSkeleton.generate("NewPkg") +``` diff --git a/src/ITensorPkgSkeleton.jl b/src/ITensorPkgSkeleton.jl index c698797..43522ae 100644 --- a/src/ITensorPkgSkeleton.jl +++ b/src/ITensorPkgSkeleton.jl @@ -1,5 +1,69 @@ module ITensorPkgSkeleton -# Write your package code here. +using Git: git +using Git_jll: Git_jll +using PkgSkeleton: PkgSkeleton +using Preferences: Preferences + +# Configure `Git.jl`/`Git_jll.jl` to +# use the local installation of git. +using Preferences: Preferences +function use_system_git!() + git_path = try + readchomp(`which git`) + catch + nothing + end + if !isnothing(git_path) + Preferences.set_preferences!("Git_jll", "git_path" => git_path) + end +end + +# Get the default branch name. +# This might be an alternative but it doesn't work for some reason: +# ```julia +# using LibGit2: LibGit2 +# LibGit2.get(AbstractString, LibGit2.GitConfig(), "init.defaultBranch") +# ``` +function default_branch_name() + return try + readchomp(`$(git()) config --get init.defaultBranch`) + catch + "main" + end +end + +function change_branch_name(path, branch_name) + original_dir = pwd() + cd(path) + original_branch_name = readchomp(`$(git()) branch --show-current`) + run(`$(git()) branch -m $original_branch_name $branch_name`) + cd(original_dir) + return nothing +end + +function generate(pkg_name) + # TODO: Use `joinpath(first(DEPOT_PATH), "dev", pkg_name)`? + pkg_path = joinpath(homedir(), ".julia", "dev", pkg_name) + template_dir = joinpath(pkgdir(ITensorPkgSkeleton), "templates", "default") + + branch_name = default_branch_name() + ## TODO: Allow customization of these, currently + ## they are hardcoded in the template. + user_replacements = Dict([ + "GHUSER" => "ITensor", + "USERNAME" => "ITensor developers", + "USEREMAIL" => "support@itensor.org", + ]) + PkgSkeleton.generate( + pkg_path; + templates=[template_dir], + user_replacements, + ) + + # Change the default branch. + change_branch_name(pkg_path, branch_name) + return nothing +end end diff --git a/templates/default/.pre-commit-config.yaml b/templates/default/.pre-commit-config.yaml new file mode 100644 index 0000000..bff1fb7 --- /dev/null +++ b/templates/default/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + exclude_types: [markdown] # incompatible with Literate.jl +- repo: https://github.com/qiaojunfeng/pre-commit-julia-format + rev: v0.2.0 + hooks: + - id: julia-format diff --git a/templates/default/src/PKGNAME.jl b/templates/default/src/{PKGNAME}.jl similarity index 100% rename from templates/default/src/PKGNAME.jl rename to templates/default/src/{PKGNAME}.jl