#!/usr/bin/env bash # Copyright (c) 2015-present, Facebook, Inc. All rights reserved. proc usage { echo "$[basename $0] [-h] [-w ] -d -f -- upgrade syntax from version specified by -f to the latest version installed in the system where: -h: show this text -w : change print-width of upgraded files -d : source code directory to be upgraded -b : path for backup directory -f : current version of the source file to be upgraded " !1 > !2; exit 1; } setglobal REASON_GIT = '"git@github.com:facebook/Reason.git'" setglobal MERLINEXTEND_GIT = '"git@github.com:def-lkb/merlin-extend.git'" setglobal OPAM_BIN = $[opam config var bin] setglobal PRINTWIDTH = '90' proc install_refmt { setglobal VERSION = $1 if [[ -f $OPAM_BIN/refmt-$VERSION ]] { echo "refmt-$VERSION already exists at $OPAM_BIN/refmt-$VERSION, skipping installation" return 0 } read -p "refmt-$VERSION is needed but not found at $OPAM_BIN/refmt-$VERSION, do you want me to install it? [Y/n]:" -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] { exit 1 } setglobal BUILD_DIR = $[mktemp -d -t reason_upgrade.XXXXXXXX] setglobal OPAM_DIR = ""$BUILD_DIR/opam"" setglobal REASON_DIR = ""$BUILD_DIR/reason"" setglobal MERLINEXTEND_DIR = ""$BUILD_DIR/merlin-extend"" echo "Initing opam at $OPAM_DIR" opam init --quiet -n --root $OPAM_DIR if [[ $? -ne 0 ]] { echo "Couldn't init opam at $OPAM_DIR, exiting" !1 > !2 exit 1; } git clone $REASON_GIT --branch $VERSION --depth 1 $REASON_DIR; if [[ $? -ne 0 ]] { echo "Couldn't clone reason from $REASON_GIT to $REASON_DIR, exiting" !1 > !2 exit 1; } git clone $MERLINEXTEND_GIT --depth 1 $MERLINEXTEND_DIR; if [[ $? -ne 0 ]] { echo "Couldn't clone merlin-extend from $MERLINEXTEND_GIT to $MERLINEXTEND_DIR, exiting" !1 > !2 exit 1; } opam pin add $MERLINEXTEND_DIR --root $OPAM_DIR -y if [[ $? -ne 0 ]] { echo "Couldn't opam pin $MERLINEXTEND_DIR into $OPAM_DIR" !1 > !2 exit 1; } opam pin add $REASON_DIR --root $OPAM_DIR -y if [[ $? -ne 0 ]] { echo "Couldn't opam pin $REASON_DIR into $OPAM_DIR" !1 > !2 exit 1; } echo "Installing refmt-$VERSION to OPAM_BIN/refmt-$VERSION" setglobal OPAM_BIN_SANDBOX = $[opam config var bin --root $OPAM_DIR] cp $OPAM_BIN_SANDBOX/refmt $OPAM_BIN/refmt-$VERSION echo "Removing sandbox $OPAM_BIN_SANDBOX" rm -rf $OPAM_BIN_SANDBOX } while getopts ':hf:d:w:b:' option { match $option { with h usage with d setglobal DIR = $OPTARG with b setglobal BACKUP_DIR = $OPTARG with f setglobal FROM = $OPTARG with w setglobal PRINTWIDTH = $OPTARG } } shift $shExpr('OPTIND - 1') if [[ -z $DIR ]] { echo "No -d provided" !1 > !2 usage } setglobal DIR = $[cd $DIR; pwd] if [[ -z $FROM ]] { echo "No -f provided" !1 > !2 usage } if [[ ! $FROM =~ ^([0-9]\.[0-9]\.[0-9])$ ]] { echo "version provided by -f should be in the form of x.y.z" !1 > !2 usage } install_refmt $FROM if [[ -z $BACKUP_DIR ]] { echo "No -b specified, default to use $DIR.backup" setglobal BACKUP_DIR = "$DIR.backup" } if [[ -d $BACKUP_DIR ]] { echo "Fail to backup: $BACKUP_DIR already exists, exiting" !1 > !2 usage } echo "Backing up at $BACKUP_DIR" cp -af $DIR $BACKUP_DIR find $DIR -type f -name "*.re" | while read file { set -x $OPAM_BIN/refmt-$VERSION -print binary_reason $file | $OPAM_BIN/refmt -print-width $PRINTWIDTH -use-stdin true -is-interface-pp false -parse binary_reason -print re > $file.new mv -f $file.new $file set +x } set -x find $DIR -type f -name "*.rei" | while read file { $OPAM_BIN/refmt-$VERSION -is-interface-pp true -print binary_reason $file | $OPAM_BIN/refmt -is-interface-pp true -print-width $PRINTWIDTH -use-stdin true -parse binary_reason -print re > $file.new mv -f $file.new $file } set +x echo "Done. Original files are backed up at $BACKUP_DIR"