Skip to content

Instantly share code, notes, and snippets.

@gatlin

gatlin/hackagedocs

Forked from Fuuzetsu/hackagedocs
Last active Sep 30, 2015
Embed
What would you like to do?
Script for generating and uploading missing documentation for your Hackage packages. Now with fixed package links and contents page.
#!/usr/bin/env bash
###
# hdoc
# (c) Gatlin Johnson <gatlin@niltag.net>
#
###########
# Available under the following license:
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
###########
# Much of this code is stolen from:
#
# https://github.com/ekmett/lens/blob/master/scripts/hackage-docs.sh
#
# and
#
# https://gist.github.com/Fuuzetsu/8276421
#
# so, you know, big shout out to them.
#
# Usage: hdoc [pkg] [ver] [hackage_username]
#
# All arguments are optional and will be auto-detected
###
# make sure we are in fact in a cabal package directory
cabal_file=$(find . -maxdepth 1 -name "*.cabal" -print -quit)
if [ ! -f "$cabal_file" ];
then
echo "Run this script in a top level Haskell package directory."
exit 1
fi
# acquire package name
if [ -z ${1+x} ];
then
pkg=$(awk -F ":[[:space:]]*" 'tolower($1)=="name" { print $2 }' < "$cabal_file")
echo "Detected package: $pkg"
else
pkg=${1}
fi
# acquire package version
if [ -z ${2+x} ];
then
ver=$(awk -F ":[[:space:]]*" 'tolower($1)=="version" { print $2 }' < "$cabal_file")
echo "Detected version: $ver"
else
ver=${2}
fi
# acquire hackage username
if [ -z ${3+x} ];
then
read -p "Hackage username: " user
else
user=${3}
fi
# acquire hackage password
echo -n Hackage Password:
read -s password
echo ""
# Figure out which haddock flag to use for hyperlinked source
if haddock --hyperlinked-source >/dev/null
then
HYPERLINK_FLAG="--haddock-option=--hyperlinked-source"
else
HYPERLINK_FLAG="--hyperlink-source"
fi
# check for hscolour
ghc-pkg list 2>/dev/null | grep hscolour >/dev/null
HSCOLOUR=$?
if [ ! "${HSCOLOUR}" -eq "0" ];
then
echo "Please install HsColour and try again."
exit 1
fi
# build the package documentation
cabal configure
cabal build
cabal haddock $HYPERLINK_FLAG \
--html-location='/package/$pkg-$version/docs' \
--contents-location='/package/$pkg-$version'
S=$?
# on successful build, upload to hackage
if [ "${S}" -eq "0" ]; then
cd "dist/doc/html"
DDIR="${pkg}-${ver}-docs"
cp -r "${pkg}" "${DDIR}" && tar -c -v -z --format=ustar -f "${DDIR}.tar.gz" "${DDIR}"
CS=$?
if [ "${CS}" -eq "0" ]; then
echo "Uploading to Hackage…"
curl -X PUT -H 'Content-Type: application/x-tar' \
-H 'Content-Encoding: gzip' \
--data-binary "@${DDIR}.tar.gz" \
"https://${user}:${password}@hackage.haskell.org/package/${pkg}-${ver}/docs"
exit $?
else
echo "Error when packaging the documentation"
exit $CS
fi
else
echo "Error when trying to build the package."
exit $S
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment