Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion resources/external.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# This file corresponds very closely to external.inc.sh in keymanapp/keyboards
#----------------------------------------------------------------------------------------

. "./tools/jq.inc.sh"

retrieve_external_model() {
# Assume we are starting in the correct folder
[ -f external_source ] || die "No external_source file found"
Expand Down Expand Up @@ -108,8 +110,40 @@ retrieve_external_binary_model() {
[[ $filename =~ ^/ ]] && die "path cannot start with /"
local path=
[[ ! $filename =~ .model_info$ ]] && path=source/
curl -s -L "$url" --output "$path$filename" --create-dirs || die "Unable to download $filename"
curl -s -L "$url" --output "$path$filename" --create-dirs || retrieve_cached_model "$path" "$filename"
echo "$sha256 $path$filename" | sha256sum -c --quiet || die "Invalid checksum for $filename"
done < external_source
}

#
# Download cached file from downloads.keyman when external source is missing
#
retrieve_cached_model() {
local path="$1"
local filename="$2"
builder_warn "Unable to download $filename so getting cached file from downloads.keyman.com"

local extension="${filename##*.}"
local model_id="${filename%.model*}"

local query="https://api.keyman.com/model/?q=${model_id}"
case "$extension" in
js)
local js_filename=`curl "$query" | $JQ -r '.[].jsFilename'`
curl -s -L "$js_filename" --output "$path$filename" --create-dirs || die "Unable to download $js_filename"
;;
kmp)
local kmp_filename=`curl "$query" | $JQ -r '.[].packageFilename'`
curl -s -L "$kmp_filename" --output "$path$filename" --create-dirs || die "Unable to download $kmp_filename"
;;
model_info)
# .model_info is downloaded up a level (not at $path)
local version=`curl "$query" | $JQ -r '.[].version'`
local model_info_filename="https://downloads.keyman.com/models/${model_id}/${version}/${model_id}.model_info"
curl -s -L "$model_info_filename" --output "$model_id.model_info" --create-dirs || die "Unable to download $model_info_filename"
;;
*)
die "$path $filename had unexpected extension (expecting js, kmp, or model_info)"
;;
esac
}
32 changes: 32 additions & 0 deletions tools/jq.inc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# Setup JQ environment variable according to the user's system
#
# Windows: ./jq-win64.exe
# Linux/macOS: jq
#

## START STANDARD BUILD SCRIPT INCLUDE
# adjust relative paths as necessary
JQ_THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
# . "${THIS_SCRIPT%/*}/build-utils.sh"
## END STANDARD BUILD SCRIPT INCLUDE

case "${OSTYPE}" in
"cygwin")
JQ=$(dirname "$JQ_THIS_SCRIPT")/jq-win64.exe
;;
"msys")
JQ=$(dirname "$JQ_THIS_SCRIPT")/jq-win64.exe
;;
*)
JQ=jq
;;
esac

readonly JQ

# JQ with inplace file replacement
function jqi() {
cat <<< "$($JQ -c "$1" < "$2")" > "$2"
}