Skip to content

Commit 5a1333c

Browse files
authored
Runic formatter (#86)
1 parent 2740bfb commit 5a1333c

File tree

18 files changed

+285
-276
lines changed

18 files changed

+285
-276
lines changed

.JuliaFormatter.toml

Lines changed: 0 additions & 4 deletions
This file was deleted.

.github/workflows/FormatCheck.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ on:
77
tags: '*'
88
pull_request:
99

10+
permissions:
11+
contents: read
12+
actions: write
13+
pull-requests: write
14+
1015
jobs:
1116
format-check:
1217
name: "Format Check"

.pre-commit-config.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ci:
2-
skip: [julia-formatter]
2+
skip: [runic]
33

44
repos:
55
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -13,7 +13,8 @@ repos:
1313
- id: end-of-file-fixer
1414
exclude_types: [markdown] # incompatible with Literate.jl
1515

16-
- repo: "https://github.com/domluna/JuliaFormatter.jl"
17-
rev: v2.1.6
16+
- repo: https://github.com/fredrikekre/runic-pre-commit
17+
rev: v2.0.1
1818
hooks:
19-
- id: "julia-formatter"
19+
- id: runic
20+
exclude: (/.*)?templates(/.*)?$

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorPkgSkeleton"
22
uuid = "3d388ab1-018a-49f4-ae50-18094d5f71ea"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.3.17"
4+
version = "0.3.18"
55

66
[deps]
77
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"

src/ITensorPkgSkeleton.jl

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ITensorPkgSkeleton
44
# backwards-compatible way.
55
# See: https://discourse.julialang.org/t/is-compat-jl-worth-it-for-the-public-keyword/119041
66
if VERSION >= v"1.11.0-DEV.469"
7-
eval(Meta.parse("public all_templates, default_templates, generate"))
7+
eval(Meta.parse("public all_templates, default_templates, generate"))
88
end
99

1010
using DocStringExtensions: SIGNATURES
@@ -20,15 +20,15 @@ using Preferences: Preferences
2020
# Need to load to set the preferences.
2121
using Git_jll: Git_jll
2222
function use_system_git!()
23-
git_path = try
24-
readchomp(`which git`)
25-
catch
26-
nothing
27-
end
28-
if !isnothing(git_path)
29-
Preferences.set_preferences!("Git_jll", "git_path" => git_path)
30-
end
31-
return nothing
23+
git_path = try
24+
readchomp(`which git`)
25+
catch
26+
nothing
27+
end
28+
if !isnothing(git_path)
29+
Preferences.set_preferences!("Git_jll", "git_path" => git_path)
30+
end
31+
return nothing
3232
end
3333

3434
# Get the default branch name.
@@ -38,58 +38,58 @@ end
3838
# LibGit2.get(AbstractString, LibGit2.GitConfig(), "init.defaultBranch")
3939
# ```
4040
function default_branch_name()
41-
return try
42-
readchomp(`$(git()) config --get init.defaultBranch`)
43-
catch
44-
"main"
45-
end
41+
return try
42+
readchomp(`$(git()) config --get init.defaultBranch`)
43+
catch
44+
"main"
45+
end
4646
end
4747

4848
function change_branch_name(path, branch_name)
49-
cd(path) do
50-
original_branch_name = readchomp(`$(git()) branch --show-current`)
51-
run(`$(git()) branch -m $original_branch_name $branch_name`)
49+
cd(path) do
50+
original_branch_name = readchomp(`$(git()) branch --show-current`)
51+
run(`$(git()) branch -m $original_branch_name $branch_name`)
52+
return nothing
53+
end
5254
return nothing
53-
end
54-
return nothing
5555
end
5656

5757
function set_remote_url(path, pkgname, ghuser)
58-
url = "git@github.com:$ghuser/$pkgname.jl.git"
59-
cd(joinpath(path, pkgname)) do
60-
try
61-
@suppress begin
62-
run(`$(git()) ls-remote -h "$url" HEAD $("&>") /dev/null`)
63-
run(`$(git()) add origin $url`)
64-
end
65-
catch
58+
url = "git@github.com:$ghuser/$pkgname.jl.git"
59+
cd(joinpath(path, pkgname)) do
60+
try
61+
@suppress begin
62+
run(`$(git()) ls-remote -h "$url" HEAD $("&>") /dev/null`)
63+
run(`$(git()) add origin $url`)
64+
end
65+
catch
66+
end
67+
return nothing
6668
end
6769
return nothing
68-
end
69-
return nothing
7070
end
7171

7272
# https://pkgdocs.julialang.org/v1/api/#Pkg.develop
7373
function default_path()
74-
return joinpath(DEPOT_PATH[1], "dev")
74+
return joinpath(DEPOT_PATH[1], "dev")
7575
end
7676

7777
default_ghuser() = "ITensor"
7878
default_username() = "ITensor developers"
7979
default_useremail() = "support@itensor.org"
8080

8181
function default_user_replacements()
82-
return (
83-
ghuser=default_ghuser(), username=default_username(), useremail=default_useremail()
84-
)
82+
return (
83+
ghuser = default_ghuser(), username = default_username(), useremail = default_useremail(),
84+
)
8585
end
8686

8787
# See:
8888
# https://discourse.julialang.org/t/remove-a-field-from-a-namedtuple/34664
8989
# https://github.com/JuliaLang/julia/pull/55270
9090
# https://github.com/JuliaLang/julia/issues/34772
9191
function delete(nt::NamedTuple{names}, key::Symbol) where {names}
92-
return NamedTuple{filter(≠(key), names)}(nt)
92+
return NamedTuple{filter(≠(key), names)}(nt)
9393
end
9494

9595
#=
@@ -99,33 +99,33 @@ ITensorPkgSkeleton.generate("NewPkg"; downstreampkgs=["ITensors"])
9999
```
100100
=#
101101
function format_downstreampkgs(user_replacements)
102-
if !haskey(user_replacements, :downstreampkgs)
103-
return user_replacements
104-
end
105-
if isempty(user_replacements.downstreampkgs)
106-
return delete(user_replacements, :downstreampkgs)
107-
end
108-
downstreampkgs = ""
109-
for pkg in user_replacements.downstreampkgs
110-
downstreampkgs *= " - \'$(pkg)\'\n"
111-
end
112-
# Remove extraneous trailing newline character.
113-
downstreampkgs = chop(downstreampkgs)
114-
return merge(user_replacements, (; downstreampkgs))
102+
if !haskey(user_replacements, :downstreampkgs)
103+
return user_replacements
104+
end
105+
if isempty(user_replacements.downstreampkgs)
106+
return delete(user_replacements, :downstreampkgs)
107+
end
108+
downstreampkgs = ""
109+
for pkg in user_replacements.downstreampkgs
110+
downstreampkgs *= " - \'$(pkg)\'\n"
111+
end
112+
# Remove extraneous trailing newline character.
113+
downstreampkgs = chop(downstreampkgs)
114+
return merge(user_replacements, (; downstreampkgs))
115115
end
116116

117117
function set_default_template_path(template)
118-
isabspath(template) && return template
119-
return joinpath(pkgdir(ITensorPkgSkeleton), "templates", template)
118+
isabspath(template) && return template
119+
return joinpath(pkgdir(ITensorPkgSkeleton), "templates", template)
120120
end
121121

122122
function is_git_repo(path)
123-
return try
124-
LibGit2.GitRepo(path)
125-
return true
126-
catch
127-
return false
128-
end
123+
return try
124+
LibGit2.GitRepo(path)
125+
return true
126+
catch
127+
return false
128+
end
129129
end
130130

131131
"""
@@ -143,32 +143,32 @@ Default templates when constructing a package. Includes the following templates:
143143
default_templates() = all_templates()
144144

145145
function to_pkgskeleton(user_replacements)
146-
return Dict(uppercase.(string.(keys(user_replacements))) .=> values(user_replacements))
146+
return Dict(uppercase.(string.(keys(user_replacements))) .=> values(user_replacements))
147147
end
148148

149149
function pkg_registration_message(; pkgname, path)
150-
return """
151-
The package "$pkgname" has been generated at "$path".
152-
153-
To register the package in the [ITensor registry](https://github.com/ITensor/ITensorRegistry), first add
154-
the registry if you haven't already with:
155-
```julia
156-
julia> using Pkg: Pkg
157-
158-
julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry")
159-
```
160-
or:
161-
```julia
162-
julia> Pkg.Registry.add(url="git@github.com:ITensor/ITensorRegistry.git")
163-
```
164-
if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages.
165-
166-
Then, use `LocalRegistry.jl` to register the package. First, you should add `LocalRegistry.jl` in your global environment. Then, activate the package and call:
167-
```julia
168-
using LocalRegistry: LocalRegistry
169-
LocalRegistry.register()
170-
```
171-
"""
150+
return """
151+
The package "$pkgname" has been generated at "$path".
152+
153+
To register the package in the [ITensor registry](https://github.com/ITensor/ITensorRegistry), first add
154+
the registry if you haven't already with:
155+
```julia
156+
julia> using Pkg: Pkg
157+
158+
julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry")
159+
```
160+
or:
161+
```julia
162+
julia> Pkg.Registry.add(url="git@github.com:ITensor/ITensorRegistry.git")
163+
```
164+
if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages.
165+
166+
Then, use `LocalRegistry.jl` to register the package. First, you should add `LocalRegistry.jl` in your global environment. Then, activate the package and call:
167+
```julia
168+
using LocalRegistry: LocalRegistry
169+
LocalRegistry.register()
170+
```
171+
"""
172172
end
173173

174174
"""
@@ -215,41 +215,41 @@ julia> ITensorPkgSkeleton.generate("NewPkg"; path=mktempdir(), downstreampkgs=["
215215
- `year`: Replaces `{YEAR}` in the template. Year the package/repository was created. Defaults to the current year.
216216
"""
217217
function generate(
218-
pkgname;
219-
path=default_path(),
220-
templates=default_templates(),
221-
ignore_templates=[],
222-
user_replacements...,
223-
)
224-
pkgpath = joinpath(path, pkgname)
225-
user_replacements = merge(default_user_replacements(), user_replacements)
226-
# Process downstream package information.
227-
user_replacements = format_downstreampkgs(user_replacements)
228-
templates = setdiff(templates, ignore_templates)
229-
# Check if there are downstream tests.
230-
if haskey(user_replacements, :downstreampkgs) &&
231-
!isempty(user_replacements.downstreampkgs)
232-
templates = [templates; ["downstreampkgs"]]
233-
else
234-
templates = setdiff(templates, ["downstreampkgs"])
235-
end
236-
# Fill in default path if missing.
237-
templates = set_default_template_path.(templates)
238-
is_new_repo = !is_git_repo(pkgpath)
239-
branch_name = default_branch_name()
240-
user_replacements_pkgskeleton = to_pkgskeleton(user_replacements)
241-
@suppress PkgSkeleton.generate(
242-
pkgpath; templates, user_replacements=user_replacements_pkgskeleton
243-
)
244-
if is_new_repo
245-
# Change the default branch if this is a new repository.
246-
change_branch_name(pkgpath, branch_name)
247-
set_remote_url(path, pkgname, user_replacements.ghuser)
248-
end
249-
if is_new_repo
250-
println(pkg_registration_message(; pkgname, path))
251-
end
252-
return nothing
218+
pkgname;
219+
path = default_path(),
220+
templates = default_templates(),
221+
ignore_templates = [],
222+
user_replacements...,
223+
)
224+
pkgpath = joinpath(path, pkgname)
225+
user_replacements = merge(default_user_replacements(), user_replacements)
226+
# Process downstream package information.
227+
user_replacements = format_downstreampkgs(user_replacements)
228+
templates = setdiff(templates, ignore_templates)
229+
# Check if there are downstream tests.
230+
if haskey(user_replacements, :downstreampkgs) &&
231+
!isempty(user_replacements.downstreampkgs)
232+
templates = [templates; ["downstreampkgs"]]
233+
else
234+
templates = setdiff(templates, ["downstreampkgs"])
235+
end
236+
# Fill in default path if missing.
237+
templates = set_default_template_path.(templates)
238+
is_new_repo = !is_git_repo(pkgpath)
239+
branch_name = default_branch_name()
240+
user_replacements_pkgskeleton = to_pkgskeleton(user_replacements)
241+
@suppress PkgSkeleton.generate(
242+
pkgpath; templates, user_replacements = user_replacements_pkgskeleton
243+
)
244+
if is_new_repo
245+
# Change the default branch if this is a new repository.
246+
change_branch_name(pkgpath, branch_name)
247+
set_remote_url(path, pkgname, user_replacements.ghuser)
248+
end
249+
if is_new_repo
250+
println(pkg_registration_message(; pkgname, path))
251+
end
252+
return nothing
253253
end
254254

255255
end

templates/docs/docs/make.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ using {PKGNAME}: {PKGNAME}
22
using Documenter: Documenter, DocMeta, deploydocs, makedocs
33

44
DocMeta.setdocmeta!(
5-
{PKGNAME}, :DocTestSetup, :(using {PKGNAME}); recursive=true
5+
{PKGNAME}, :DocTestSetup, :(using {PKGNAME}); recursive = true
66
)
77

88
include("make_index.jl")
99

1010
makedocs(;
11-
modules=[{PKGNAME}],
12-
authors="ITensor developers <support@itensor.org> and contributors",
13-
sitename="{PKGNAME}.jl",
14-
format=Documenter.HTML(;
15-
canonical="https://itensor.github.io/{PKGNAME}.jl",
16-
edit_link="main",
17-
assets=["assets/favicon.ico", "assets/extras.css"],
18-
),
19-
pages=["Home" => "index.md", "Reference" => "reference.md"],
11+
modules = [{PKGNAME}],
12+
authors = "ITensor developers <support@itensor.org> and contributors",
13+
sitename = "{PKGNAME}.jl",
14+
format = Documenter.HTML(;
15+
canonical = "https://itensor.github.io/{PKGNAME}.jl",
16+
edit_link = "main",
17+
assets = ["assets/favicon.ico", "assets/extras.css"],
18+
),
19+
pages = ["Home" => "index.md", "Reference" => "reference.md"],
2020
)
2121

2222
deploydocs(;
23-
repo="github.com/ITensor/{PKGNAME}.jl", devbranch="main", push_preview=true
23+
repo = "github.com/ITensor/{PKGNAME}.jl", devbranch = "main", push_preview = true
2424
)

0 commit comments

Comments
 (0)