From 2eb70015e2599c06367476a8f5db3124985b3541 Mon Sep 17 00:00:00 2001 From: Morten Piibeleht Date: Wed, 21 May 2025 17:13:41 +1200 Subject: [PATCH] Function to generate full package stub --- Project.toml | 2 ++ src/Generator.jl | 81 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f097e20..17fd6c2 100644 --- a/Project.toml +++ b/Project.toml @@ -13,6 +13,7 @@ Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a" LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433" OpenSSH_jll = "9bd350c2-7e96-507f-8002-3f2e150b4e1b" Sass = "322a6be2-4ae8-5d68-aaf1-3e960788d1d9" +UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] AbstractTrees = "0.3, 0.4" @@ -22,6 +23,7 @@ Git = "1" Gumbo = "0.7, 0.8" OpenSSH_jll = "8, 9.9.1" Sass = "0.1, 0.2" +UUIDs = "1" julia = "1.6" [extras] diff --git a/src/Generator.jl b/src/Generator.jl index 4667845..672d525 100644 --- a/src/Generator.jl +++ b/src/Generator.jl @@ -4,6 +4,7 @@ Provides the functions related to generating documentation stubs. module Generator using DocStringExtensions +using UUIDs: uuid4 """ $(SIGNATURES) @@ -41,7 +42,12 @@ function make(pkgname; format = :html) makedocs($(sitename) format = $(fmtstr), - modules = [$(pkgname)] + modules = [$(pkgname)], + # The generated stub will not be in a Git repo usually, + # so we want to disable the remote URL generation (which otherwise + # tries to automatically determine the remote repo by inspecting + # the Git repo's remotes) + remotes=nothing ) # Documenter can also automatically deploy documentation to gh-pages. @@ -142,4 +148,77 @@ function index(pkgname) """ end +""" + +""" +function genpackage(name::AbstractString; destination::AbstractString=pwd(), force::Bool=false) + package_root = joinpath(destination, name) + package_uuid = string(uuid4()) + + mktempdir() do tmp_root + # Generate a basic Julia Project.toml for this package + open(joinpath(tmp_root, "Project.toml"), "w") do io + write(io, """ + name = "$(name)" + uuid = "$(package_uuid)" + version = "0.0.0" + """) + end + + open(joinpath(tmp_root, "Makefile"), "w") do io + write(io, """ + .PHONY: docs + docs: docs/Manifest.toml + \tjulia --project=docs docs/make.jl + + docs/Manifest.toml: docs/Project.toml Project.toml + \tjulia --project=docs -e 'using Pkg; Pkg.instantiate()' + """) + end + + let src = joinpath(tmp_root, "src") + mkpath(src) + open(joinpath(src, "$(name).jl"); write=true) do io + write(io, "module $(name)\n\nend") + end + end + + let docs = joinpath(tmp_root, "docs") + mkpath(docs) + open(joinpath(docs, "Project.toml"); write=true) do io + write(io, """ + [deps] + Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" + $(name) = "$(package_uuid)" + + [sources] + $(name) = { path = ".." } + """) + end + open(joinpath(docs, "make.jl"); write=true) do io + write(io, make(name)) + end + let docs_src = joinpath(docs, "src") + mkpath(docs_src) + open(joinpath(docs_src, "index.md"); write=true) do io + write(io, index(name)) + end + end + end + + if ispath(package_root) + if force && isdir(package_root) + @warn "Removing existing directory" package_root + rm(package_root; recursive=true) + else + error("Something exists at: $(package_root)") + end + end + @info "Generating $(name)" package_root + mv(tmp_root, package_root) + end + + return nothing +end + end