diff --git a/Project.toml b/Project.toml index 6ee809c..0d4b957 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ITensorPkgSkeleton" uuid = "3d388ab1-018a-49f4-ae50-18094d5f71ea" -version = "0.3.30" +version = "0.3.31" authors = ["ITensor developers and contributors"] [workspace] diff --git a/src/ITensorPkgSkeleton.jl b/src/ITensorPkgSkeleton.jl index 409df3f..bc644ee 100644 --- a/src/ITensorPkgSkeleton.jl +++ b/src/ITensorPkgSkeleton.jl @@ -101,31 +101,85 @@ function format_downstreampkgs(user_replacements) return merge(user_replacements, (; downstreampkgs)) end -function set_default_template_path(template) - isabspath(template) && return template - return joinpath(pkgdir(ITensorPkgSkeleton), "templates", template) +const TEMPLATE_EXT = ".template" +const TEMPLATE_ROOT = joinpath(pkgdir(ITensorPkgSkeleton), "templates") +const TEMPLATE_PATHS = Dict( + "benchmark" => ["benchmark"], + "docs" => ["docs", "README.md"], + "examples" => ["examples"], + "github" => [".github"], + "gitignore" => [".gitignore"], + "license" => ["LICENSE"], + "precommit" => [".pre-commit-config.yaml"], + "project" => ["Project.toml"], + "src" => ["src"], + "test" => ["test"] +) +function strip_template_ext(path) + return endswith(path, TEMPLATE_EXT) ? path[1:(end - length(TEMPLATE_EXT))] : path end -const TEMPLATE_EXT = ".template" +function copy_template_path!(src, template_dir, dest_dir) + if isdir(src) + for (root, dirs, files) in walkdir(src) + for dir in dirs + mkpath(joinpath(dest_dir, relpath(joinpath(root, dir), template_dir))) + end + for file in files + src_file = joinpath(root, file) + rel = strip_template_ext(relpath(src_file, template_dir)) + dest_file = joinpath(dest_dir, rel) + mkpath(dirname(dest_file)) + cp(src_file, dest_file) + end + end + elseif isfile(src) + rel = strip_template_ext(relpath(src, template_dir)) + dest_file = joinpath(dest_dir, rel) + mkpath(dirname(dest_file)) + cp(src, dest_file) + end + return nothing +end function prepare_template(template_dir) tmp_dir = mktempdir() - for (root, dirs, files) in walkdir(template_dir) - for dir in dirs - mkpath(joinpath(tmp_dir, relpath(joinpath(root, dir), template_dir))) - end - for file in files - src = joinpath(root, file) - rel = relpath(src, template_dir) - if endswith(rel, TEMPLATE_EXT) - rel = rel[1:(end - length(TEMPLATE_EXT))] + copy_template_path!(template_dir, template_dir, tmp_dir) + return tmp_dir +end + +function prepare_default_templates(templates) + tmp_dir = mktempdir() + for template in templates + paths = get(TEMPLATE_PATHS, template, nothing) + isnothing(paths) && + throw( + ArgumentError( + "Unknown template \"$template\". Options: $(collect(keys(TEMPLATE_PATHS)))" + ) + ) + for path in paths + src = joinpath(TEMPLATE_ROOT, "$path$TEMPLATE_EXT") + if !isfile(src) + src = joinpath(TEMPLATE_ROOT, path) end - cp(src, joinpath(tmp_dir, rel)) + copy_template_path!(src, TEMPLATE_ROOT, tmp_dir) end end return tmp_dir end +function prepare_templates(templates) + templates_abs = filter(isabspath, templates) + templates_default = setdiff(templates, templates_abs) + prepared = String[] + if !isempty(templates_default) + push!(prepared, prepare_default_templates(String.(templates_default))) + end + append!(prepared, prepare_template.(templates_abs)) + return prepared +end + function is_git_repo(path) return try LibGit2.GitRepo(path) @@ -140,7 +194,7 @@ $(SIGNATURES) All available templates when constructing a package. Includes the following templates: `$(all_templates())` """ -all_templates() = readdir(joinpath(pkgdir(ITensorPkgSkeleton), "templates")) +all_templates() = collect(keys(TEMPLATE_PATHS)) """ $(SIGNATURES) @@ -242,10 +296,7 @@ function generate( # Process downstream package information. user_replacements = format_downstreampkgs(user_replacements) templates = setdiff(templates, ignore_templates) - # Fill in default path if missing. - templates = set_default_template_path.(templates) - # Copy templates to temp directories, stripping `.template` extension from filenames. - templates = prepare_template.(templates) + templates = prepare_templates(templates) is_new_repo = !is_git_repo(pkgpath) branch_name = default_branch_name() user_replacements_pkgskeleton = to_pkgskeleton(user_replacements) diff --git a/templates/github/.github/ISSUE_TEMPLATE/BUG_REPORT.md.template b/templates/.github/ISSUE_TEMPLATE/BUG_REPORT.md.template similarity index 100% rename from templates/github/.github/ISSUE_TEMPLATE/BUG_REPORT.md.template rename to templates/.github/ISSUE_TEMPLATE/BUG_REPORT.md.template diff --git a/templates/github/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md.template b/templates/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md.template similarity index 100% rename from templates/github/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md.template rename to templates/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md.template diff --git a/templates/github/.github/PULL_REQUEST_TEMPLATE.md.template b/templates/.github/PULL_REQUEST_TEMPLATE.md.template similarity index 100% rename from templates/github/.github/PULL_REQUEST_TEMPLATE.md.template rename to templates/.github/PULL_REQUEST_TEMPLATE.md.template diff --git a/templates/github/.github/dependabot.yml.template b/templates/.github/dependabot.yml.template similarity index 100% rename from templates/github/.github/dependabot.yml.template rename to templates/.github/dependabot.yml.template diff --git a/templates/github/.github/workflows/CompatHelper.yml.template b/templates/.github/workflows/CompatHelper.yml.template similarity index 100% rename from templates/github/.github/workflows/CompatHelper.yml.template rename to templates/.github/workflows/CompatHelper.yml.template diff --git a/templates/github/.github/workflows/Documentation.yml.template b/templates/.github/workflows/Documentation.yml.template similarity index 100% rename from templates/github/.github/workflows/Documentation.yml.template rename to templates/.github/workflows/Documentation.yml.template diff --git a/templates/github/.github/workflows/FormatCheck.yml.template b/templates/.github/workflows/FormatCheck.yml.template similarity index 100% rename from templates/github/.github/workflows/FormatCheck.yml.template rename to templates/.github/workflows/FormatCheck.yml.template diff --git a/templates/github/.github/workflows/FormatPullRequest.yml.template b/templates/.github/workflows/FormatPullRequest.yml.template similarity index 100% rename from templates/github/.github/workflows/FormatPullRequest.yml.template rename to templates/.github/workflows/FormatPullRequest.yml.template diff --git a/templates/github/.github/workflows/IntegrationTest.yml.template b/templates/.github/workflows/IntegrationTest.yml.template similarity index 100% rename from templates/github/.github/workflows/IntegrationTest.yml.template rename to templates/.github/workflows/IntegrationTest.yml.template diff --git a/templates/github/.github/workflows/IntegrationTestRequest.yml.template b/templates/.github/workflows/IntegrationTestRequest.yml.template similarity index 100% rename from templates/github/.github/workflows/IntegrationTestRequest.yml.template rename to templates/.github/workflows/IntegrationTestRequest.yml.template diff --git a/templates/github/.github/workflows/Registrator.yml.template b/templates/.github/workflows/Registrator.yml.template similarity index 100% rename from templates/github/.github/workflows/Registrator.yml.template rename to templates/.github/workflows/Registrator.yml.template diff --git a/templates/github/.github/workflows/TagBot.yml.template b/templates/.github/workflows/TagBot.yml.template similarity index 100% rename from templates/github/.github/workflows/TagBot.yml.template rename to templates/.github/workflows/TagBot.yml.template diff --git a/templates/github/.github/workflows/Tests.yml.template b/templates/.github/workflows/Tests.yml.template similarity index 100% rename from templates/github/.github/workflows/Tests.yml.template rename to templates/.github/workflows/Tests.yml.template diff --git a/templates/github/.github/workflows/VersionCheck.yml.template b/templates/.github/workflows/VersionCheck.yml.template similarity index 100% rename from templates/github/.github/workflows/VersionCheck.yml.template rename to templates/.github/workflows/VersionCheck.yml.template diff --git a/templates/gitignore/.gitignore.template b/templates/.gitignore.template similarity index 100% rename from templates/gitignore/.gitignore.template rename to templates/.gitignore.template diff --git a/templates/precommit/.pre-commit-config.yaml.template b/templates/.pre-commit-config.yaml.template similarity index 100% rename from templates/precommit/.pre-commit-config.yaml.template rename to templates/.pre-commit-config.yaml.template diff --git a/templates/license/LICENSE.template b/templates/LICENSE.template similarity index 100% rename from templates/license/LICENSE.template rename to templates/LICENSE.template diff --git a/templates/project/Project.toml.template b/templates/Project.toml.template similarity index 100% rename from templates/project/Project.toml.template rename to templates/Project.toml.template diff --git a/templates/docs/README.md.template b/templates/README.md.template similarity index 100% rename from templates/docs/README.md.template rename to templates/README.md.template diff --git a/templates/benchmark/benchmark/benchmarks.jl.template b/templates/benchmark/benchmarks.jl.template similarity index 100% rename from templates/benchmark/benchmark/benchmarks.jl.template rename to templates/benchmark/benchmarks.jl.template diff --git a/templates/docs/docs/Project.toml.template b/templates/docs/Project.toml.template similarity index 100% rename from templates/docs/docs/Project.toml.template rename to templates/docs/Project.toml.template diff --git a/templates/docs/docs/make.jl.template b/templates/docs/make.jl.template similarity index 100% rename from templates/docs/docs/make.jl.template rename to templates/docs/make.jl.template diff --git a/templates/docs/docs/make_index.jl.template b/templates/docs/make_index.jl.template similarity index 100% rename from templates/docs/docs/make_index.jl.template rename to templates/docs/make_index.jl.template diff --git a/templates/docs/docs/make_readme.jl.template b/templates/docs/make_readme.jl.template similarity index 100% rename from templates/docs/docs/make_readme.jl.template rename to templates/docs/make_readme.jl.template diff --git a/templates/docs/docs/src/assets/CCQ-dark.png.template b/templates/docs/src/assets/CCQ-dark.png.template similarity index 100% rename from templates/docs/docs/src/assets/CCQ-dark.png.template rename to templates/docs/src/assets/CCQ-dark.png.template diff --git a/templates/docs/docs/src/assets/CCQ.png.template b/templates/docs/src/assets/CCQ.png.template similarity index 100% rename from templates/docs/docs/src/assets/CCQ.png.template rename to templates/docs/src/assets/CCQ.png.template diff --git a/templates/docs/docs/src/assets/extras.css.template b/templates/docs/src/assets/extras.css.template similarity index 100% rename from templates/docs/docs/src/assets/extras.css.template rename to templates/docs/src/assets/extras.css.template diff --git a/templates/docs/docs/src/assets/favicon.ico.template b/templates/docs/src/assets/favicon.ico.template similarity index 100% rename from templates/docs/docs/src/assets/favicon.ico.template rename to templates/docs/src/assets/favicon.ico.template diff --git a/templates/docs/docs/src/assets/logo-dark.png.template b/templates/docs/src/assets/logo-dark.png.template similarity index 100% rename from templates/docs/docs/src/assets/logo-dark.png.template rename to templates/docs/src/assets/logo-dark.png.template diff --git a/templates/docs/docs/src/assets/logo.png.template b/templates/docs/src/assets/logo.png.template similarity index 100% rename from templates/docs/docs/src/assets/logo.png.template rename to templates/docs/src/assets/logo.png.template diff --git a/templates/docs/docs/src/reference.md.template b/templates/docs/src/reference.md.template similarity index 100% rename from templates/docs/docs/src/reference.md.template rename to templates/docs/src/reference.md.template diff --git a/templates/examples/examples/Project.toml.template b/templates/examples/Project.toml.template similarity index 100% rename from templates/examples/examples/Project.toml.template rename to templates/examples/Project.toml.template diff --git a/templates/examples/examples/README.jl.template b/templates/examples/README.jl.template similarity index 100% rename from templates/examples/examples/README.jl.template rename to templates/examples/README.jl.template diff --git a/templates/src/src/{PKGNAME}.jl.template b/templates/src/{PKGNAME}.jl.template similarity index 100% rename from templates/src/src/{PKGNAME}.jl.template rename to templates/src/{PKGNAME}.jl.template diff --git a/templates/test/test/Project.toml.template b/templates/test/Project.toml.template similarity index 100% rename from templates/test/test/Project.toml.template rename to templates/test/Project.toml.template diff --git a/templates/test/test/runtests.jl.template b/templates/test/runtests.jl.template similarity index 100% rename from templates/test/test/runtests.jl.template rename to templates/test/runtests.jl.template diff --git a/templates/test/test/test_aqua.jl.template b/templates/test/test_aqua.jl.template similarity index 100% rename from templates/test/test/test_aqua.jl.template rename to templates/test/test_aqua.jl.template diff --git a/templates/test/test/test_basics.jl.template b/templates/test/test_basics.jl.template similarity index 100% rename from templates/test/test/test_basics.jl.template rename to templates/test/test_basics.jl.template