Skip to content

Commit

Permalink
Add generate function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Nov 21, 2024
1 parent f1ed985 commit 0131120
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 2 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# See https://domluna.github.io/JuliaFormatter.jl/stable/ for a list of options
style = "blue"
ignore = ["templates"]
indent = 2
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ uuid = "3d388ab1-018a-49f4-ae50-18094d5f71ea"
authors = ["ITensor developers <[email protected]> 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"

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# 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/)
[![Build Status](https://github.com/ITensor/ITensorPkgSkeleton.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/ITensor/ITensorPkgSkeleton.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![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")
```
72 changes: 71 additions & 1 deletion src/ITensorPkgSkeleton.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
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 default_path()
# TODO: Use something like `joinpath(first(DEPOT_PATH), "dev", pkg_name)`
# to make it more general.
return joinpath(homedir(), ".julia", "dev")
end

function generate(pkg_name; path=default_path())
pkg_path = joinpath(path, pkg_name)
# TODO: Turn this into a keyword argument.
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" => "[email protected]",
])
PkgSkeleton.generate(
pkg_path;
templates=[template_dir],
user_replacements,
)

# Change the default branch.
change_branch_name(pkg_path, branch_name)
return nothing
end

end
13 changes: 13 additions & 0 deletions templates/default/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
8 changes: 8 additions & 0 deletions templates/default/test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@eval module $(gensym())
using {PKGNAME}: {PKGNAME}
using Test: @test, @testset

@testset "{PKGNAME}" begin
# Tests go here.
end
end
15 changes: 15 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@eval module $(gensym())
using ITensorPkgSkeleton: ITensorPkgSkeleton
using Test: @test, @testset

@testset "ITensorPkgSkeleton" begin
path = tempdir()
@test isnothing(ITensorPkgSkeleton.generate("NewPkg"; path))
@test isdir(joinpath(path, "NewPkg"))
@test isdir(joinpath(path, "NewPkg", ".github"))
@test isdir(joinpath(path, "NewPkg", "benchmark"))
@test isdir(joinpath(path, "NewPkg", "docs"))
@test isdir(joinpath(path, "NewPkg", "src"))
@test isdir(joinpath(path, "NewPkg", "test"))
end
end

0 comments on commit 0131120

Please sign in to comment.