summaryrefslogtreecommitdiff
path: root/Tools/scripts/hackage-get-latest-version.sh
blob: 3ad21fcbd73aed5eb917a6dab7df4541e8328044 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh
#
# MAINTAINER: yuri@FreeBSD.org

set -e
set -o pipefail

export LC_ALL=C

##
## hackage-get-latest-version.sh: retrieves the latest version of a given Haskell package as registered on https://hackage.haskell.org
##

# args

PACKAGE_NAME="$1"

if [ -z "$PACKAGE_NAME" ]; then
	echo "Usage: $0 <package-name>"
	echo "Example: $0 cryptol"
	echo "Example: $0 ShellCheck"
	exit 1
fi

# check that packaged dependencies are installed

for dep in curl jq version_sort; do
	if ! which -s $dep; then
		echo "error: the '$dep' dependency is missing"
		if [ $dep = "curl" ]; then
			echo "... please install the 'curl' package"
		elif [ $dep = "jq" ]; then
			echo "... please install the 'jq' package"
		elif [ $dep = "version_sort" ]; then
			echo "... please install the 'libversion' package"
		fi
		exit 1
	fi
done


# MAIN

curl -H "Accept: application/json" https://hackage.haskell.org/package/$PACKAGE_NAME 2>/dev/null |
	grep -v "Package not found: No such package in package index" |
	jq -r 'keys[]' |
	version_sort |
	tail -1 ||
	! echo "failed to find the Haskell package '$PACKAGE_NAME'"