Skip to content

Commit b504f18

Browse files
authored
Dispatch logic on versions when deploying (#2695)
1 parent 3782b30 commit b504f18

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Changed
99

10+
* Refactored `deploydocs` internals to allow dispatch on the `versions` keyword, intended as a non-public API for DocumenterVitepress which cannot use the default versioning mechanism during deployment ([#2695]).
1011
* Added anchor links to admonition blocks, making it possible to create direct links to specific admonitions. ([#2505], [#2676], [#2688])
1112
* Added different banners for dev and unreleased docs ([#2382], [#2682])
1213
* Added an API function `writer_supports_ansicolor` for a Documenter writer to indicate that it supports rendering ANSI-colored strings. This is useful for new backends that may be implemented. ([#2490])
@@ -2038,6 +2039,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
20382039
[#2685]: https://github.com/JuliaDocs/Documenter.jl/issues/2685
20392040
[#2692]: https://github.com/JuliaDocs/Documenter.jl/pull/2692
20402041
[#2693]: https://github.com/JuliaDocs/Documenter.jl/issues/2693
2042+
[#2695]: https://github.com/JuliaDocs/Documenter.jl/issues/2695
20412043
[JuliaLang/julia#36953]: https://github.com/JuliaLang/julia/issues/36953
20422044
[JuliaLang/julia#38054]: https://github.com/JuliaLang/julia/issues/38054
20432045
[JuliaLang/julia#39841]: https://github.com/JuliaLang/julia/issues/39841

src/deploydocs.jl

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,10 @@ function deploydocs(;
245245
if deploy_decision.all_ok
246246
deploy_branch = deploy_decision.branch
247247
deploy_repo = deploy_decision.repo
248-
deploy_subfolder = deploy_decision.subfolder
249248
deploy_is_preview = deploy_decision.is_preview
250249

251-
# Non-versioned docs: deploy to root
252-
if versions === nothing && !deploy_is_preview
253-
deploy_subfolder = nothing
254-
end
250+
# this dispatches on `versions` for a non-public API for DocumenterVitepress
251+
deploy_subfolder = determine_deploy_subfolder(deploy_decision, versions)
255252

256253
# Install dependencies when applicable.
257254
if deps !== nothing
@@ -317,7 +314,6 @@ function deploydocs(;
317314
return
318315
end
319316

320-
321317
function _get_inventory_version(objects_inv)
322318
return open(objects_inv) do input
323319
for line in eachline(input)
@@ -428,46 +424,8 @@ function git_push(
428424
write(joinpath(dirname, "CNAME"), cname)
429425
end
430426

431-
if versions === nothing
432-
# If the documentation is unversioned and deployed to root, we generate a
433-
# siteinfo.js file that would disable the version selector in the docs
434-
HTMLWriter.generate_siteinfo_file(deploy_dir, nothing)
435-
else
436-
# Generate siteinfo-file with DOCUMENTER_CURRENT_VERSION
437-
# Determine if this is a development version (e.g., "dev" or "latest")
438-
is_dev_version = (subfolder == devurl || subfolder == "latest")
439-
HTMLWriter.generate_siteinfo_file(deploy_dir, subfolder, is_dev_version)
440-
441-
# Expand the users `versions` vector
442-
entries, symlinks = HTMLWriter.expand_versions(dirname, versions)
443-
444-
# Create the versions.js file containing a list of `entries`.
445-
# This must always happen after the folder copying.
446-
HTMLWriter.generate_version_file(joinpath(dirname, "versions.js"), entries, symlinks)
447-
448-
# Create the index.html file to redirect ./stable or ./dev.
449-
# This must always happen after the folder copying.
450-
HTMLWriter.generate_redirect_file(joinpath(dirname, "index.html"), entries)
451-
452-
# generate the symlinks, make sure we don't overwrite devurl
453-
cd(dirname) do
454-
for kv in symlinks
455-
i = findfirst(x -> x.first == devurl, symlinks)
456-
if i === nothing
457-
rm_and_add_symlink(kv.second, kv.first)
458-
else
459-
throw(
460-
ArgumentError(
461-
string(
462-
"link `$(kv)` cannot overwrite ",
463-
"`devurl = $(devurl)` with the same name."
464-
)
465-
)
466-
)
467-
end
468-
end
469-
end
470-
end
427+
# this dispatches on `versions` for a non-public API for DocumenterVitepress
428+
postprocess_before_push(versions; subfolder, devurl, deploy_dir, dirname)
471429

472430
# Add, commit, and push the docs to the remote.
473431
run(`$(git()) add -A -- ':!.documenter-identity-file.tmp' ':!**/.documenter-identity-file.tmp'`)
@@ -559,6 +517,68 @@ function git_push(
559517
return
560518
end
561519

520+
# Run arbitrary logic (for example, creating siteinfo and version files)
521+
# on the documentation with the new additions before the changes are pushed to the remote.
522+
# The logic depends on the versioning scheme defined via `versions`.
523+
# This function was factored out as part of a non-public API via dispatch on the `versions` keyword arg
524+
# to `deploydocs`, for use in DocumenterVitepress because it cannot use the default versioning.
525+
function postprocess_before_push(versions::Nothing; subfolder, devurl, deploy_dir, dirname)
526+
# If the documentation is unversioned and deployed to root, we generate a
527+
# siteinfo.js file that would disable the version selector in the docs
528+
HTMLWriter.generate_siteinfo_file(deploy_dir, nothing)
529+
return
530+
end
531+
532+
function postprocess_before_push(versions::AbstractVector; subfolder, devurl, deploy_dir, dirname)
533+
# Generate siteinfo-file with DOCUMENTER_CURRENT_VERSION
534+
# Determine if this is a development version (e.g., "dev" or "latest")
535+
is_dev_version = (subfolder == devurl || subfolder == "latest")
536+
HTMLWriter.generate_siteinfo_file(deploy_dir, subfolder, is_dev_version)
537+
538+
# Expand the users `versions` vector
539+
entries, symlinks = HTMLWriter.expand_versions(dirname, versions)
540+
541+
# Create the versions.js file containing a list of `entries`.
542+
# This must always happen after the folder copying.
543+
HTMLWriter.generate_version_file(joinpath(dirname, "versions.js"), entries, symlinks)
544+
545+
# Create the index.html file to redirect ./stable or ./dev.
546+
# This must always happen after the folder copying.
547+
HTMLWriter.generate_redirect_file(joinpath(dirname, "index.html"), entries)
548+
549+
# generate the symlinks, make sure we don't overwrite devurl
550+
return cd(dirname) do
551+
for kv in symlinks
552+
i = findfirst(x -> x.first == devurl, symlinks)
553+
if i === nothing
554+
rm_and_add_symlink(kv.second, kv.first)
555+
else
556+
throw(
557+
ArgumentError(
558+
string(
559+
"link `$(kv)` cannot overwrite ",
560+
"`devurl = $(devurl)` with the same name."
561+
)
562+
)
563+
)
564+
end
565+
end
566+
end
567+
end
568+
569+
# Determine the subfolder to deploy to given the `deploy_decision` and the `versions`.
570+
# Either return a `String` or `nothing` to deploy to the root folder.
571+
# This function was factored out as part of a non-public API via dispatch on the `versions` keyword arg
572+
# to `deploydocs`, for use in DocumenterVitepress because it cannot use the default versioning.
573+
function determine_deploy_subfolder(deploy_decision, versions::Nothing)
574+
# Non-versioned docs: deploy to root unless it's a preview
575+
return deploy_decision.is_preview ? deploy_decision.subfolder : nothing
576+
end
577+
578+
function determine_deploy_subfolder(deploy_decision, versions::AbstractVector)
579+
return deploy_decision.subfolder
580+
end
581+
562582
function rm_and_add_symlink(target, link)
563583
if ispath(link) || islink(link)
564584
@warn "removing `$(link)` and linking `$(link)` to `$(target)`."

0 commit comments

Comments
 (0)