@@ -101,31 +101,85 @@ function format_downstreampkgs(user_replacements)
101101 return merge (user_replacements, (; downstreampkgs))
102102end
103103
104- function set_default_template_path (template)
105- isabspath (template) && return template
106- return joinpath (pkgdir (ITensorPkgSkeleton), " templates" , template)
104+ const TEMPLATE_EXT = " .template"
105+ const TEMPLATE_ROOT = joinpath (pkgdir (ITensorPkgSkeleton), " templates" )
106+ const TEMPLATE_PATHS = Dict (
107+ " benchmark" => [" benchmark" ],
108+ " docs" => [" docs" , " README.md" ],
109+ " examples" => [" examples" ],
110+ " github" => [" .github" ],
111+ " gitignore" => [" .gitignore" ],
112+ " license" => [" LICENSE" ],
113+ " precommit" => [" .pre-commit-config.yaml" ],
114+ " project" => [" Project.toml" ],
115+ " src" => [" src" ],
116+ " test" => [" test" ]
117+ )
118+ function strip_template_ext (path)
119+ return endswith (path, TEMPLATE_EXT) ? path[1 : (end - length (TEMPLATE_EXT))] : path
107120end
108121
109- const TEMPLATE_EXT = " .template"
122+ function copy_template_path! (src, template_dir, dest_dir)
123+ if isdir (src)
124+ for (root, dirs, files) in walkdir (src)
125+ for dir in dirs
126+ mkpath (joinpath (dest_dir, relpath (joinpath (root, dir), template_dir)))
127+ end
128+ for file in files
129+ src_file = joinpath (root, file)
130+ rel = strip_template_ext (relpath (src_file, template_dir))
131+ dest_file = joinpath (dest_dir, rel)
132+ mkpath (dirname (dest_file))
133+ cp (src_file, dest_file)
134+ end
135+ end
136+ elseif isfile (src)
137+ rel = strip_template_ext (relpath (src, template_dir))
138+ dest_file = joinpath (dest_dir, rel)
139+ mkpath (dirname (dest_file))
140+ cp (src, dest_file)
141+ end
142+ return nothing
143+ end
110144
111145function prepare_template (template_dir)
112146 tmp_dir = mktempdir ()
113- for (root, dirs, files) in walkdir (template_dir)
114- for dir in dirs
115- mkpath (joinpath (tmp_dir, relpath (joinpath (root, dir), template_dir)))
116- end
117- for file in files
118- src = joinpath (root, file)
119- rel = relpath (src, template_dir)
120- if endswith (rel, TEMPLATE_EXT)
121- rel = rel[1 : (end - length (TEMPLATE_EXT))]
147+ copy_template_path! (template_dir, template_dir, tmp_dir)
148+ return tmp_dir
149+ end
150+
151+ function prepare_default_templates (templates)
152+ tmp_dir = mktempdir ()
153+ for template in templates
154+ paths = get (TEMPLATE_PATHS, template, nothing )
155+ isnothing (paths) &&
156+ throw (
157+ ArgumentError (
158+ " Unknown template \" $template \" . Options: $(collect (keys (TEMPLATE_PATHS))) "
159+ )
160+ )
161+ for path in paths
162+ src = joinpath (TEMPLATE_ROOT, " $path$TEMPLATE_EXT " )
163+ if ! isfile (src)
164+ src = joinpath (TEMPLATE_ROOT, path)
122165 end
123- cp (src, joinpath (tmp_dir, rel) )
166+ copy_template_path! (src, TEMPLATE_ROOT, tmp_dir )
124167 end
125168 end
126169 return tmp_dir
127170end
128171
172+ function prepare_templates (templates)
173+ templates_abs = filter (isabspath, templates)
174+ templates_default = setdiff (templates, templates_abs)
175+ prepared = String[]
176+ if ! isempty (templates_default)
177+ push! (prepared, prepare_default_templates (String .(templates_default)))
178+ end
179+ append! (prepared, prepare_template .(templates_abs))
180+ return prepared
181+ end
182+
129183function is_git_repo (path)
130184 return try
131185 LibGit2. GitRepo (path)
@@ -140,7 +194,7 @@ $(SIGNATURES)
140194
141195All available templates when constructing a package. Includes the following templates: `$(all_templates ()) `
142196"""
143- all_templates () = readdir ( joinpath ( pkgdir (ITensorPkgSkeleton), " templates " ))
197+ all_templates () = collect ( keys (TEMPLATE_PATHS ))
144198
145199"""
146200$(SIGNATURES)
@@ -242,10 +296,7 @@ function generate(
242296 # Process downstream package information.
243297 user_replacements = format_downstreampkgs (user_replacements)
244298 templates = setdiff (templates, ignore_templates)
245- # Fill in default path if missing.
246- templates = set_default_template_path .(templates)
247- # Copy templates to temp directories, stripping `.template` extension from filenames.
248- templates = prepare_template .(templates)
299+ templates = prepare_templates (templates)
249300 is_new_repo = ! is_git_repo (pkgpath)
250301 branch_name = default_branch_name ()
251302 user_replacements_pkgskeleton = to_pkgskeleton (user_replacements)
0 commit comments