Skip to content

Commit

Permalink
Merge pull request #278 from input-output-hk/mgalazyn/chore/fix-haddo…
Browse files Browse the repository at this point in the history
…cks-gha

Fix haddocks generation in GHA
  • Loading branch information
carbolymer authored Nov 9, 2023
2 parents 9382929 + c649457 commit fb1812f
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 21 deletions.
147 changes: 147 additions & 0 deletions .github/bin/haddocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/bin/bash
# Build haddock documentation and an index for all projects in the repository.
#
# usage:
# ./haddocks.sh directory [true|false]
#
# $1 - where to put the generated pages, this directory contents will be wiped
# out (so don't pass `/` or `./` - the latter will delete your 'dist-newstyle')
# (the default is './haddocks')
# $2 - whether to re-build haddocks with `cabal haddock` command or a component name
# (the default is true)
# $3 - cabal build directory
# (the default is "dist-newstyle")

set -euo pipefail

OUTPUT_DIR=${1:-"./haddocks"}
REGENERATE=${2:-"true"}
BUILD_DIR=${3:-"dist-newstyle"}
DRY_RUN="${DRY_RUN:-}"

GHC_VERSION=$(ghc --numeric-version)
OS_ARCH="$(cat "$BUILD_DIR/cache/plan.json" | jq -r '.arch + "-" + .os' | head -n 1 | xargs)"

if [ "${DRY_RUN}" == 1 ]; then
DRY_RUN_ARGS="--dry-run"
else
DRY_RUN_ARGS=""
fi

# we don't include `--use-index` option, because then quickjump data is not
# generated. This is not ideal, but there is no way to generate only top level
# `doc-index.html` file. With this approach we get:
# * `doc-index.json` and `doc-index.html` per package
# * we can generate top level `doc-index.json` (which will only work at the top
# level).
# * we could amend package level `doc-index.json` files, but it's enough ...
# this should be fixed upstream.
HADDOCK_OPTS=(
--builddir "${BUILD_DIR}"
--disable-optimization
--haddock-all
--haddock-internal
--haddock-html
--haddock-quickjump
--haddock-hyperlink-source
--haddock-option "--show-all"
--haddock-option "--use-unicode"
--disable-tests
$DRY_RUN_ARGS
)

# build documentation of all modules
if [ ${REGENERATE} == "true" ]; then
cabal haddock "${HADDOCK_OPTS[@]}" \
cardano-cli
elif [ ${REGENERATE} != "false" ]; then
cabal haddock "${HADDOCK_OPTS[@]}" ${REGENERATE}
fi

if [ "${DRY_RUN}" == 1 ]; then
echo "Exiting dry run"
exit 0
fi

mkdir -p "${OUTPUT_DIR}/_plan/"

# Build plan
for row in $(
cat dist-newstyle/cache/plan.json | \
jq -r '
.
| ."install-plan"[]
| select(.style == "local")
| { "dist-dir": ."dist-dir"
, "pkg-name": ."pkg-name"
, "component-name": ."component-name" | split(":") | join("_")
}
| @base64'
); do
json="$(echo "${row}" | base64 --decode)"

dist_dir="$(echo "$json" | jq -r '."dist-dir"')"

if [ -d "${dist_dir}" ]; then
echo "$json" > "${OUTPUT_DIR}/_plan/$(echo "$json" | sha256sum | cut -d ' ' -f 1).json"
fi
done

# Copy the docs
for json in "${OUTPUT_DIR}"/_plan/*.json; do
dist_dir="$(cat "$json" | jq -r '."dist-dir"')"
pkg_name="$(cat "$json" | jq -r '."pkg-name"')"
component_name="$(cat "$json" | jq -r '."component-name"')"

if [ -d "${dist_dir}" ]; then
for doc_index in $(find "${dist_dir}" -name doc-index.html); do
package_dir="$(dirname "$doc_index")"
package="$(echo "$(basename "${package_dir}")" | sed 's/-[0-9]\+\(\.[0-9]\+\)*//')"
echo "Copying package: ${package}"
mkdir -p "${OUTPUT_DIR}/${pkg_name}/${component_name}"
cp -r "${package_dir}"/* "${OUTPUT_DIR}/${pkg_name}/${component_name}"
echo "${OUTPUT_DIR}/${pkg_name}/${component_name}"
done
fi
done

# --read-interface options
interface_options () {
for json in "${OUTPUT_DIR}"/_plan/*.json; do
dist_dir="$(cat "$json" | jq -r '."dist-dir"')"
pkg_name="$(cat "$json" | jq -r '."pkg-name"')"
component_name="$(cat "$json" | jq -r '."component-name"')"

if [ -d "${dist_dir}" ]; then
for doc_index in $(find "${dist_dir}" -name doc-index.html); do
package_dir="$(dirname "$doc_index")"
package="$(echo "$(basename "${package_dir}")" | sed 's/-[0-9]\+\(\.[0-9]\+\)*//')"
if [ -f "${OUTPUT_DIR}/${pkg_name}/${component_name}/${package}.haddock" ]; then
echo "--read-interface=${pkg_name}/${component_name},${OUTPUT_DIR}/${pkg_name}/${component_name}/${package}.haddock"
fi
done
fi
done
}

haddock \
-o ${OUTPUT_DIR} \
--title "cardano-cli API" \
--package-name "Cardano CLI API" \
--gen-index \
--gen-contents \
--quickjump \
$(interface_options)

# Assemble a toplevel `doc-index.json` from package level ones.
#
echo "[]" > "${OUTPUT_DIR}/doc-index.json"
for file in $(ls $OUTPUT_DIR/*/*/doc-index.json); do
project=$(basename $(dirname $file));
jq -s \
".[0] + [.[1][] | (. + {link: (\"${project}/\" + .link)}) ]" \
"${OUTPUT_DIR}/doc-index.json" \
${file} \
> /tmp/doc-index.json
mv /tmp/doc-index.json "${OUTPUT_DIR}/doc-index.json"
done
36 changes: 15 additions & 21 deletions .github/workflows/github-page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: "Haddock documentation"
on:
push:
branches:
- master
- main

jobs:
build:
Expand All @@ -22,13 +22,7 @@ jobs:

env:
# Modify this value to "invalidate" the cabal cache.
CABAL_CACHE_VERSION: "2023-07-28"

# Modify this value to "invalidate" the secp cache.
SECP_CACHE_VERSION: "2022-12-30"

# current ref from: 27.02.2022
SECP256K1_REF: ac83be33d0956faf6b7f61a60ab524ef7d6a473a
CABAL_CACHE_VERSION: "2023-11-09"

concurrency:
group: >
Expand All @@ -52,24 +46,24 @@ jobs:
e+${{ matrix.cabal }}
f+${{ matrix.os }}
g+${{ (startsWith(github.ref, 'refs/heads/gh-readonly-queue/') && github.run_id) || github.event.pull_request.number || github.ref }}
- name: Install Haskell
uses: input-output-hk/actions/haskell@latest
id: setup-haskell
with:
ghc-version: ${{ matrix.ghc }}
cabal-version: ${{ matrix.cabal }}
- name: Install Haskell
uses: input-output-hk/actions/haskell@latest
id: setup-haskell
with:
ghc-version: ${{ matrix.ghc }}
cabal-version: ${{ matrix.cabal }}

- name: Install system dependencies
uses: input-output-hk/actions/base@latest
with:
use-sodium-vrf: true # default is true

- uses: actions/checkout@v3

- name: Cabal update
run: cabal update

- name: Disable all tests
run: |
cat > cabal.project.local <<EOF
Expand All @@ -83,7 +77,7 @@ jobs:
run: |
cabal build all --dry-run
mkdir ./haddocks
DRY_RUN=1 ./scripts/haddocs.sh ./haddocks true
DRY_RUN=1 ./.github/bin/haddocks.sh ./haddocks true
# For users who fork cardano-cli and want to define a writable cache, then can set up their own
# S3 bucket then define in their forked repository settings the following secrets:
Expand Down Expand Up @@ -128,7 +122,7 @@ jobs:
run: cabal build all

- name: Build Haddock documentation 🔧
run: ./scripts/haddocs.sh ./haddocks true
run: ./.github/bin/haddocks.sh ./haddocks true

- name: View HTML files
run: |
Expand All @@ -143,7 +137,7 @@ jobs:
path: ./haddocks

- name: Deploy documentation to gh-pages 🚀
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN || github.token }}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ See the [Contributing guide](CONTRIBUTING.md) for how to contribute to this proj
## Documentation

Development documentation can be found in [Cardano Node Wiki](https://github.com/input-output-hk/cardano-node-wiki/wiki).

Haddock documentation is available at: https://input-output-hk.github.io/cardano-cli/

0 comments on commit fb1812f

Please sign in to comment.