Skip to content

Commit

Permalink
Set up meta redirects to release artifacts usnistgov#2
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitawootten-nist committed Aug 1, 2023
1 parent aa1b6fb commit cf84b49
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ help: ## Show this help message
{ printf "\033[32m%-30s\033[0m %s\n", $$1, $$2 }'

.PHONY: clean
clean: clean-modeldoc clean-site ## Clean all
clean: clean-modeldoc clean-site clean-release-assets ## Clean all

#
# Website generation / hugo
Expand All @@ -25,13 +25,13 @@ MODELDOC_REVISION_DATA_DIR:=$(patsubst %,$(MODELDOC_DATA_DIR)/%/,$(REVISIONS))
SITE_OUTPUT:=site/public

.PHONY: serve
serve: modeldoc ## Spin up a static web server for local dev
serve: modeldoc release-assets ## Spin up a static web server for local dev
cd site; hugo serve

.PHONY: site
site: $(SITE_OUTPUT) ## Build the site

$(SITE_OUTPUT): modeldoc
$(SITE_OUTPUT): modeldoc release-assets
cd site; hugo --minify

.PHONY: clean-site
Expand All @@ -53,3 +53,19 @@ $(MODELDOC_CONTENT_DIR)/%/:
clean-modeldoc: ## Clean model documentation
rm -fr $(MODELDOC_REVISION_CONTENT_DIR)
rm -fr $(MODELDOC_REVISION_DATA_DIR)

#
# Release assets link
#

RELEASE_ASSET_REDIRECTS_DIR:=site/content/release-assets/latest/

.PHONY: release-assets
release-assets: $(RELEASE_ASSET_REDIRECTS_DIR) ## Generate redirects to latest release's assets

$(RELEASE_ASSET_REDIRECTS_DIR):
./support/generate_release_assets_redirect.sh

.PHONY: clean-release-assets
clean-release-assets: ## Clean release redirects
rm -fr site/content/release-assets/latest/
3 changes: 2 additions & 1 deletion site/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public

# all directories here are generated on the fly
content/models/*/
data/models/*/
data/models/*/
content/release-assets/latest/*
12 changes: 12 additions & 0 deletions site/archetypes/alias.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en-us">

<head>
<title>{{ getenv "HUGO_TO_LINK" }}</title>
<link rel="canonical" href="{{ getenv "HUGO_TO_LINK" }}">
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url={{ getenv "HUGO_TO_LINK" }}">
</head>

</html>
79 changes: 79 additions & 0 deletions support/generate_release_assets_redirect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash

set -Eeuo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# The root of the OSCAL-Reference repository
ROOT_DIR="${SCRIPT_DIR}/.."
SITE_DIR="${ROOT_DIR}/site"
OSCAL_DIR="${ROOT_DIR}/support/OSCAL"

if ! [ -x "$(command -v hugo)" ]; then
echo 'Error: Hugo (hugo) is not in the PATH, is it installed?' >&2
exit 1
fi

if ! [ -x "$(command -v git)" ]; then
echo 'Error: Git (git) is not in the PATH, is it installed?' >&2
exit 1
fi

# The latest release tag
REVISION=$("$SCRIPT_DIR"/list_revisions.sh | tail -n 1)

#
# Git Submodule operations
#

SCRATCH_DIR="$(mktemp -d)"

{
echo "Creating temporary worktree at ${REVISION} in ${SCRATCH_DIR}" >&2
cd "${OSCAL_DIR}"
git worktree add --quiet --force "${SCRATCH_DIR}" "${REVISION}"
}

function cleanup() {
echo "Removing temporary worktree ${SCRATCH_DIR}" >&2
cd "${OSCAL_DIR}"
git worktree remove "${SCRATCH_DIR}"
rm -fr "${SCRATCH_DIR}"
}
trap cleanup EXIT

# Populate ROOT_METASCHEMAS with all metaschema basenames (oscal_catalog ... etc.)
# The find block:
# 1. Finds all files in the scratch directory's src/metaschema directory that match *_metaschema.xml
# 2. Exclude non-root metaschemas ("common" and "metadata" metaschemas)
# 3. Strip the path, leaving just the filename
# 4. Strip the "_metaschema.xml" suffix, leaving just "oscal_$name"
ROOT_METASCHEMAS=()
while IFS='' read -r metaschema; do
ROOT_METASCHEMAS+=("$metaschema")
done < <(find "${SCRATCH_DIR}/src/metaschema" \
-name '*_metaschema.xml' \
-a ! -name '*common*' \
-a ! -name '*metadata*' \
-exec basename {} _metaschema.xml ';')

# For each metaschema, append the appropriate schema and converter assets
ASSETS=(
"${ROOT_METASCHEMAS[@]/%/_schema.json}"
"${ROOT_METASCHEMAS[@]/%/_schema.xsd}"
"${ROOT_METASCHEMAS[@]/%/_json-to-xml-converter.xsl}"
"${ROOT_METASCHEMAS[@]/%/_xml-to-json-converter.xsl}"
)

OUTPUT_PATH=release-assets/latest
GH_RELEASES_URL=https://github.com/usnistgov/OSCAL/releases

{
cd "${SITE_DIR}"

rm -fr "content/${OUTPUT_PATH}/"

# For each asset, generate a new hugo page using the alias archetype
for asset in "${ASSETS[@]}"; do
HUGO_TO_LINK="${GH_RELEASES_URL}/download/${REVISION}/${asset}" hugo new --kind alias "content/${OUTPUT_PATH}/${asset}/index.html"
done
}

0 comments on commit cf84b49

Please sign in to comment.