Skip to content

Instantly share code, notes, and snippets.

@cdlm
Last active January 15, 2018 18:29
Show Gist options
  • Save cdlm/1918e01a9ab54e8616e8b6c4a33e6089 to your computer and use it in GitHub Desktop.
Save cdlm/1918e01a9ab54e8616e8b6c4a33e6089 to your computer and use it in GitHub Desktop.
Pharo image download/preparation script
#!/bin/bash
#
# Easier project setup for Pharo + Iceberg + Git.
#
# This script (re)downloads and prepares a ready-to-hack Pharo image, getting all
# necessary info from a small config file committed among the project's files.
#
set -euo pipefail
IFS=$'\n\t'
# ↑ bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
function die { echo "$@" 1>&2; exit 1; }
function download_to {
[[ $# -eq 2 ]] || "Usage: ${FUNCNAME[0]} filename url"
local dest="$1" url="$2"
curl --silent --location --compressed --output "$dest" "$url"
# or the same with wget
}
function pharo_fetch_image {
[[ $# -eq 1 ]] || die "Usage: ${FUNCNAME[0]} url"
local url="$1" downloaded tmp
# cache in a known place and continue?
tmp=$(mktemp -dt "pharo.XXXXXX") # should get cleaned up automatically
download_to "${tmp}/image.zip" "$url"
unzip -q "${tmp}/image.zip" -d "$tmp"
downloaded="$(ls "${tmp}"/*.image)" # ensures the image file exists
downloaded="${downloaded%.image}"
echo "$downloaded"
}
function pharo_rename {
[[ $# -ge 2 ]] || die "Usage: ${FUNCNAME[0]} [--copy] original new"
local copy='mv'
[[ "$1" = '--copy' ]] && { copy='cp'; shift; }
local original="$1" new="$2"
[ -e "${original}.image" ] || die "No original image: ${original}.image"
[ ! -e "${new}.image" ] || die "Destination file already exists: ${new}.image"
[ ! -e "${new}.changes" ] || die "Destination file already exists: ${new}.changes"
"$copy" "${original}.image" "${new}.image"
"$copy" "${original}.changes" "${new}.changes"
}
function pharo_delete {
[[ $# -ge 1 ]] || die "Usage: ${FUNCNAME[0]} basename..."
# while [[ $# -ge 1 ]]; do
# local name="$1"
# shift
for name in "$@"; do
rm -f "${name}.image"
rm -f "${name}.changes"
done
}
function pharo_backup {
[[ $# -eq 1 ]] || die "Usage: ${FUNCNAME[0]} name"
local name="$1" backup_stamp
backup_stamp="backup-$(date +%Y%m%d-%H%M)"
shopt -s nullglob
for image in ${name}-*.image; do # match any hash
image="${image%.image}"
pharo_rename "${image}" "${image}.${backup_stamp}"
done
}
function pharo_load {
[[ $# -eq 2 ]] || die "Usage: ${FUNCNAME[0] script image}"
local script="$1" image="$2"
for script_file in "load.st" "${script}.load.st" "local.st" "${script}.local.st"; do
if [[ -e "$script_file" ]]; then
${PHARO} "${image}.image" st --save --quit "$script_file"
fi
done
}
function pharo_prepare {
[[ $# -eq 3 ]] || die "Usage: ${FUNCNAME[0] base script new}"
local base="$1" script="$2" new="$3"
pharo_rename --copy "$base" "$new"
cp -f "${base}.sources" "$(dirname "$new")"
pharo_load "$script" "$new"
}
function pharo_build_image {
# Ensure environment variables have sensible values.
# You can use direnv to overrides
: "${PHARO_PROJECT:=pharo}"
: "${PHARO:=./pharo-ui}"
: "${PHARO_VERSION:=70}"
: "${PHARO_FILES:="http://files.pharo.org/get-files/${PHARO_VERSION}"}"
local fetched hash
local -a images=("$@")
[[ ${#images[@]} -eq 0 ]] && read -ra images < <( ls ./*.load.st )
[[ ${#images[@]} -eq 0 ]] && images=( "$PHARO_PROJECT" )
images=( "${images[@]%.load.st}" )
fetched="$(pharo_fetch_image "${PHARO_FILES}/pharo.zip")"
hash="${fetched##*-}" # strip everything but the short hex hash
for project in "${images[@]}"; do
pharo_delete "${project}.tmp"
pharo_prepare "$fetched" "$project" "${project}.tmp"
done
for project in "${images[@]}"; do
pharo_backup "${project}"
pharo_rename "${project}.tmp" "${project}-${hash}"
done
}
# run if called as a command, else we're being sourced as a library
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
pharo_build_image "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment