#!/bin/bash set -e root=$(cd $(dirname ${0})/..; pwd) node_modules=${root}/node_modules src=${root}/src third_party=${root}/third_party tools=${root}/tools identifier=coremltools virtualenv=${third_party}/virtualenv/${identifier} bold() { echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" } git_sync () { mkdir -p "${third_party}" if [ -d "${third_party}/${1}" ]; then git -C "${third_party}/${1}" pull --quiet --prune else git -C "${third_party}" clone --quiet --recursive ${2} fi git -C "${third_party}/${1}" submodule sync --quiet git -C "${third_party}/${1}" submodule update --quiet --init --recursive } clean() { bold "coreml clean" rm -rf ${third_party}/coremltools } sync() { bold "coreml sync" git_sync coremltools https://github.com/apple/coremltools.git } install() { bold "coreml install" [ -n "$(python3 -m pip list --format columns --disable-pip-version-check | grep -w virtualenv)" ] || python3 -m pip install --force-reinstall --user --quiet virtualenv [ -d "${virtualenv}" ] || python3 -m virtualenv --quiet -p python3 ${virtualenv} source ${virtualenv}/bin/activate python3 -m pip install --quiet ${third_party}/${identifier} deactivate } schema() { bold "coreml schema" ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case -r coreml -o ${src}/coreml-proto.js ${third_party}/${identifier}/mlmodel/format/Model.proto node ${tools}/update_pbjs.js array ${src}/coreml-proto.js floatValue float 2 } convert() { bold "coreml convert" source ${virtualenv}/bin/activate python3 -m pip install --quiet onnx python3 -m pip install --quiet sklearn python3 ${tools}/coreml-script.py convert ${1} deactivate } while [ "$#" != 0 ]; do command="$1" && shift case "${command}" in "clean") clean;; "sync") sync;; "install") install;; "schema") schema;; "convert") convert ${1} && shift;; esac done