Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/redirects_page.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Generate and Deploy Redirects

on:
workflow_dispatch:
push:
branches:
- master
paths:
- '**/Package.toml' # avoids redeploys on version or compat changes

jobs:
build-and-deploy:
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source

runs-on: ubuntu-latest

timeout-minutes: 15

steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- name: Install Julia
uses: julia-actions/setup-julia@5c9647d97b78a5debe5164e9eec09d653d29bd71 # v2.6.1
with:
version: '1'

- name: Run the "generate_redirects.jl" Julia script
run: julia generate_redirects.jl

- name: Upload static files as artifact
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1
with:
path: webroot/

- name: Deploy to GitHub Pages
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5
100 changes: 100 additions & 0 deletions generate_redirects.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import TOML

function get_repo(path::String)
return TOML.parsefile(joinpath(path, "Package.toml"))["repo"]
end

function get_packages_info()
reg = TOML.parsefile("Registry.toml")
return [(; name = dict["name"], path = dict["path"]) for (uuid, dict) in reg["packages"]]
end

function get_host(repo)
m = match(r"^https://([a-z\.]+)", repo)
if m === nothing
error("Repo url $(repr(repo)) did not match expected format")
end
return m[1]
end

# only for these hosts will we redirect automatically, for all others the user needs to click the link
function known_host(host)
host in ("github.com", "gitlab.com", "codeberg.org")
end

function package_path(args...)
# results in URLs like juliaregistries.github.io/General/packages/redirect_to_repo/SomePackage
joinpath("webroot", "packages", "redirect_to_repo", args...)
end

function create_redirect_page(; name, path)
repo = get_repo(path)
host = get_host(repo)
should_redirect = known_host(host)
meta_redirection = should_redirect ? """<meta http-equiv="refresh" content="0; url=$repo">""" : ""
message = if should_redirect
"""Redirecting to $name...<br><br>Click the link below if you are not redirected automatically to the registered repository for the Julia package $name<br><br><a href="$repo" rel="nofollow">$repo</a>"""
else
"""Click the link below to go to the registered repository for the Julia package $name<br><br><a href="$repo" rel="nofollow">$repo</a>"""
end
title = if should_redirect
"Redirecting to $name..."
else
"Confirm redirect to $name"
end

open(package_path(name * ".html"), "w") do io
write(io, """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$title</title>
$meta_redirection
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
.centered-div {
border: 3px solid #9558B2;
background-color: #f8e9ff;
border-radius: 10px;
padding: 20px;
margin: 20px;
text-align: center;
color: #333;
max-width: 30em;
word-wrap: break-word;
}
.centered-div a {
color: #9558B2;
font-weight: bold;
}
</style>
</head>
<body>
<div class="centered-div">
<p>$message</p>
</div>
</body>
</html>
""")
end
end

function main()
mkpath(package_path())
packages_info = get_packages_info()
for (; name, path) in packages_info
create_redirect_page(; name, path)
end
end

main()
Loading