@@ -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
66if 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" ))
88end
99
1010using DocStringExtensions: SIGNATURES
@@ -20,15 +20,15 @@ using Preferences: Preferences
2020# Need to load to set the preferences.
2121using Git_jll: Git_jll
2222function 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
3232end
3333
3434# Get the default branch name.
3838# LibGit2.get(AbstractString, LibGit2.GitConfig(), "init.defaultBranch")
3939# ```
4040function 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
4646end
4747
4848function 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
5555end
5656
5757function 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
7070end
7171
7272# https://pkgdocs.julialang.org/v1/api/#Pkg.develop
7373function default_path ()
74- return joinpath (DEPOT_PATH [1 ], " dev" )
74+ return joinpath (DEPOT_PATH [1 ], " dev" )
7575end
7676
7777default_ghuser () = " ITensor"
7878default_username () = " ITensor developers"
7979default_useremail () = " support@itensor.org"
8080
8181function 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+ )
8585end
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
9191function delete (nt:: NamedTuple{names} , key:: Symbol ) where {names}
92- return NamedTuple {filter(≠(key), names)} (nt)
92+ return NamedTuple {filter(≠(key), names)} (nt)
9393end
9494
9595#=
@@ -99,33 +99,33 @@ ITensorPkgSkeleton.generate("NewPkg"; downstreampkgs=["ITensors"])
9999```
100100=#
101101function 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))
115115end
116116
117117function 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)
120120end
121121
122122function 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
129129end
130130
131131"""
@@ -143,32 +143,32 @@ Default templates when constructing a package. Includes the following templates:
143143default_templates () = all_templates ()
144144
145145function 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))
147147end
148148
149149function 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+ """
172172end
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"""
217217function 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
253253end
254254
255255end
0 commit comments