Skip to content

Instantly share code, notes, and snippets.

@samoht
Last active May 15, 2016 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samoht/1630dc8c4e38a6221a21 to your computer and use it in GitHub Desktop.
Save samoht/1630dc8c4e38a6221a21 to your computer and use it in GitHub Desktop.
Gather the sources to build an opam project
#!/usr/bin/env bash
# Instructions. You need:
# - a working opam environment
# - an opam file in your current directory (to describe your project)
# - possibly some packages path-pinned in the current switch
#
# Run the scripts, and then check the contents of sources/
set -eu
NAME=$(hexdump -n 2 -e '/2 "%u"' /dev/urandom)
CURRENT_SWITCH=$(opam switch show)
OPAMVAR_ocaml_version=$(opam config var ocaml-version)
OPAMVAR_compiler=$(opam config var compiler)
OPAMVAR_preinstalled=$(opam config var preinstalled)
OPAMVAR_os=$(opam config var os)
export OPAMVAR_ocaml_version
export OPAMVAR_compiler
export OPAMVAR_preinstalled
export OPAMVAR_os
echo "OPAMROOT=$(opam config var root)"
echo "ocaml-version=${OPAMVAR_ocaml_version}"
echo "compiler=${OPAMVAR_compiler}"
echo "preinstalled=${OPAMVAR_preinstalled}"
echo "os=${OPAMVAR_os}"
OUTPUT=/tmp/pkgs-${NAME}.json
export OPAMYES=1
# create a new switch
opam switch "${NAME}" -A system
# pin the package
opam pin add "${NAME}.pkg" . -n
# ask the solver to install the package, and parse the JSON result
unset OPAMYES
export OPAMNO=1
opam install "${NAME}" --dry-run --json="${OUTPUT}"
ALL_PACKAGES=$(jq '.[] | map(select(.install)) | map( [.install.name, .install.version] | join(".")) | join(" ")' "${OUTPUT}")
opam unpin "${NAME}" -y
opam switch "${CURRENT_SWITCH}"
# create and populate sources/
rm -rf sources
mkdir -p sources
CURRENT_DIR=$(pwd)
cd sources
BASE_PKGS="base-threads base-unix base-bigarray base-bytes"
for pkg in ${BASE_PKGS} ${ALL_PACKAGES//\"}; do
echo "Adding ${pkg}"
# check if it is a local pin
if [ "${pkg}" != "${NAME}.pkg" ]; then
pinned=$(opam info "${pkg}" -f pinned)
case "${pinned}" in
"") opam source "${pkg}";;
"path")
localPath=$(opam info "${pkg}" -f upstream-url)
ln -s "${localPath}" "${pkg}";;
*) echo "Error: opam info ${pkg} -f pinned"; exit 1;;
esac
fi
done
cd "${CURRENT_DIR}"
opam switch remove "${NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment