diff --git a/ci/release/_install.sh b/ci/release/_install.sh index b4d1c85f3..66cf1ac03 100755 --- a/ci/release/_install.sh +++ b/ci/release/_install.sh @@ -15,7 +15,7 @@ help() { cat < but the release archive in + ~/.cache/d2/release will remain. --uninstall: Uninstall the installed version of d2. The --method and --prefix flags must be the same @@ -74,8 +75,7 @@ Flags: package manager to uninstall instead. All downloaded archives are cached into ~/.cache/d2/release. use \$XDG_CACHE_HOME to change -path of the cached assets. Archives will be unarchived into ~/.local/share/d2/release. -Use \$XDG_DATA_HOME to adjust the path of the unarchived releases. +path of the cached assets. Release archives are unarchived into /usr/local/lib/d2/d2- note: Deleting the unarchived releases will cause --uninstall to stop working. @@ -105,21 +105,21 @@ main() { flag_nonemptyarg && shift "$FLAGSHIFT" VERSION=$FLAGARG ;; - tala-version) - flag_nonemptyarg && shift "$FLAGSHIFT" - TALA_VERSION=$FLAGARG + tala) + shift "$FLAGSHIFT" + TALA=${FLAGARG:-latest} ;; edge) flag_noarg && shift "$FLAGSHIFT" EDGE=1 echoerr "$FLAGRAW is currently unimplemented" - exit 1 + return 1 ;; method) flag_nonemptyarg && shift "$FLAGSHIFT" METHOD=$FLAGARG echoerr "$FLAGRAW is currently unimplemented" - exit 1 + return 1 ;; prefix) flag_nonemptyarg && shift "$FLAGSHIFT" @@ -153,8 +153,7 @@ main() { ARCH=$(arch) CACHE_DIR=$(cache_dir) mkdir -p "$CACHE_DIR" - DATA_DIR=$(data_dir) - mkdir -p "$DATA_DIR" + INSTALL_DIR=$PREFIX/lib/d2 if [ -n "${UNINSTALL-}" ]; then if ! command -v d2 >/dev/null; then @@ -162,7 +161,7 @@ main() { return 1 fi INSTALLED_VERSION="$(d2 version)" - if ! uninstall_standalone; then + if ! uninstall; then echoerr "failed to uninstall $INSTALLED_VERSION" return 1 fi @@ -171,11 +170,9 @@ main() { VERSION=${VERSION:-latest} if [ "$VERSION" = latest ]; then - VERSION=$(fetch_version_info) + fetch_release_info fi - # TODO: --tala, --tala-version - if command -v d2 >/dev/null; then INSTALLED_VERSION="$(d2 version)" if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then @@ -183,56 +180,77 @@ main() { return 0 fi log "uninstalling $INSTALLED_VERSION to install $VERSION" - if ! uninstall_standalone; then + if ! uninstall; then warn "failed to uninstall $INSTALLED_VERSION" fi fi - - install_standalone + install } -install_standalone() { +install() { + standalone_install + if [ "${TALA-}" ]; then + standalone_install_tala + fi + + COLOR=2 header success + log "Standalone release $ARCHIVE has been successfully installed into $PREFIX" + if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then + logcat >&2 </dev/null || true + sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true # The reason for checking whether bin is writable specifically is that on macOS you have # /usr/local owned by root but you don't need root to write to its subdirectories which # is all we want to do. @@ -251,37 +269,21 @@ cache_dir() { fi } -data_dir() { - if [ -n "${XDG_DATA_HOME-}" ]; then - echo "$XDG_DATA_HOME/d2/release" - elif [ -n "${HOME-}" ]; then - echo "$HOME/.local/d2/release" - else - echo "/tmp/d2-data/release" - fi -} - -fetch_version_info() { - req_version=$VERSION - if [ -e "$CACHE_DIR/$req_version.json" ]; then - echo "$VERSION" +fetch_release_info() { + if [ -n "${RELEASE_INFO-}" ]; then return 0 fi - log "fetching info on version $req_version" - rm -f "$CACHE_DIR/req_version.json" - if [ "$req_version" = latest ]; then - release_info_url="https://api.github.com/repos/$REPO/releases/$req_version" + log "fetching info on version $VERSION" + RELEASE_INFO=$(mktemp -d)/release-info.json + if [ "$VERSION" = latest ]; then + release_info_url="https://api.github.com/repos/$REPO/releases/$VERSION" else - release_info_url="https://api.github.com/repos/$REPO/releases/tags/$req_version" + release_info_url="https://api.github.com/repos/$REPO/releases/tags/$VERSION" fi - fetch_gh "$release_info_url" "$CACHE_DIR/$req_version.json" \ + fetch_gh "$release_info_url" "$RELEASE_INFO" \ 'application/json' - VERSION=$(cat "$CACHE_DIR/$req_version.json" | grep -m1 tag_name | sed 's/^.*: "\(.*\)",$/\1/g') - if [ "$req_version" = latest ]; then - sh_c mv "$CACHE_DIR/$req_version.json" "$CACHE_DIR/$VERSION.json" - fi - echo "$VERSION" + VERSION=$(cat "$RELEASE_INFO" | grep -m1 tag_name | sed 's/^.*: "\(.*\)",$/\1/g') } curl_gh() { diff --git a/ci/release/gen_install.sh b/ci/release/gen_install.sh index def56ac2f..b04325cc2 100755 --- a/ci/release/gen_install.sh +++ b/ci/release/gen_install.sh @@ -15,6 +15,7 @@ set -eu # - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/log.sh # - ./ci/sub/lib/flag.sh +# - ./ci/sub/lib/release.sh # - ./ci/release/_install.sh # # The last of which implements the installation logic. diff --git a/install.sh b/install.sh index 473731ecc..65b779364 100755 --- a/install.sh +++ b/install.sh @@ -9,6 +9,7 @@ set -eu # - ./ci/sub/lib/rand.sh # - ./ci/sub/lib/log.sh # - ./ci/sub/lib/flag.sh +# - ./ci/sub/lib/release.sh # - ./ci/release/_install.sh # # The last of which implements the installation logic. @@ -424,7 +425,7 @@ help() { cat < but the release archive in + ~/.cache/d2/release will remain. --uninstall: Uninstall the installed version of d2. The --method and --prefix flags must be the same @@ -483,8 +485,7 @@ Flags: package manager to uninstall instead. All downloaded archives are cached into ~/.cache/d2/release. use \$XDG_CACHE_HOME to change -path of the cached assets. Archives will be unarchived into ~/.local/share/d2/release. -Use \$XDG_DATA_HOME to adjust the path of the unarchived releases. +path of the cached assets. Release archives are unarchived into /usr/local/lib/d2/d2- note: Deleting the unarchived releases will cause --uninstall to stop working. @@ -514,21 +515,21 @@ main() { flag_nonemptyarg && shift "$FLAGSHIFT" VERSION=$FLAGARG ;; - tala-version) - flag_nonemptyarg && shift "$FLAGSHIFT" - TALA_VERSION=$FLAGARG + tala) + shift "$FLAGSHIFT" + TALA=${FLAGARG:-latest} ;; edge) flag_noarg && shift "$FLAGSHIFT" EDGE=1 echoerr "$FLAGRAW is currently unimplemented" - exit 1 + return 1 ;; method) flag_nonemptyarg && shift "$FLAGSHIFT" METHOD=$FLAGARG echoerr "$FLAGRAW is currently unimplemented" - exit 1 + return 1 ;; prefix) flag_nonemptyarg && shift "$FLAGSHIFT" @@ -562,8 +563,7 @@ main() { ARCH=$(arch) CACHE_DIR=$(cache_dir) mkdir -p "$CACHE_DIR" - DATA_DIR=$(data_dir) - mkdir -p "$DATA_DIR" + INSTALL_DIR=$PREFIX/lib/d2 if [ -n "${UNINSTALL-}" ]; then if ! command -v d2 >/dev/null; then @@ -571,7 +571,7 @@ main() { return 1 fi INSTALLED_VERSION="$(d2 version)" - if ! uninstall_standalone; then + if ! uninstall; then echoerr "failed to uninstall $INSTALLED_VERSION" return 1 fi @@ -580,11 +580,9 @@ main() { VERSION=${VERSION:-latest} if [ "$VERSION" = latest ]; then - VERSION=$(fetch_version_info) + fetch_release_info fi - # TODO: --tala, --tala-version - if command -v d2 >/dev/null; then INSTALLED_VERSION="$(d2 version)" if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then @@ -592,56 +590,77 @@ main() { return 0 fi log "uninstalling $INSTALLED_VERSION to install $VERSION" - if ! uninstall_standalone; then + if ! uninstall; then warn "failed to uninstall $INSTALLED_VERSION" fi fi - - install_standalone + install } -install_standalone() { +install() { + standalone_install + if [ "${TALA-}" ]; then + standalone_install_tala + fi + + COLOR=2 header success + log "Standalone release $ARCHIVE has been successfully installed into $PREFIX" + if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then + logcat >&2 </dev/null || true + sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true # The reason for checking whether bin is writable specifically is that on macOS you have # /usr/local owned by root but you don't need root to write to its subdirectories which # is all we want to do. @@ -660,37 +679,21 @@ cache_dir() { fi } -data_dir() { - if [ -n "${XDG_DATA_HOME-}" ]; then - echo "$XDG_DATA_HOME/d2/release" - elif [ -n "${HOME-}" ]; then - echo "$HOME/.local/d2/release" - else - echo "/tmp/d2-data/release" - fi -} - -fetch_version_info() { - req_version=$VERSION - if [ -e "$CACHE_DIR/$req_version.json" ]; then - echo "$VERSION" +fetch_release_info() { + if [ -n "${RELEASE_INFO-}" ]; then return 0 fi - log "fetching info on version $req_version" - rm -f "$CACHE_DIR/req_version.json" - if [ "$req_version" = latest ]; then - release_info_url="https://api.github.com/repos/$REPO/releases/$req_version" + log "fetching info on version $VERSION" + RELEASE_INFO=$(mktemp -d)/release-info.json + if [ "$VERSION" = latest ]; then + release_info_url="https://api.github.com/repos/$REPO/releases/$VERSION" else - release_info_url="https://api.github.com/repos/$REPO/releases/tags/$req_version" + release_info_url="https://api.github.com/repos/$REPO/releases/tags/$VERSION" fi - fetch_gh "$release_info_url" "$CACHE_DIR/$req_version.json" \ + fetch_gh "$release_info_url" "$RELEASE_INFO" \ 'application/json' - VERSION=$(cat "$CACHE_DIR/$req_version.json" | grep -m1 tag_name | sed 's/^.*: "\(.*\)",$/\1/g') - if [ "$req_version" = latest ]; then - sh_c mv "$CACHE_DIR/$req_version.json" "$CACHE_DIR/$VERSION.json" - fi - echo "$VERSION" + VERSION=$(cat "$RELEASE_INFO" | grep -m1 tag_name | sed 's/^.*: "\(.*\)",$/\1/g') } curl_gh() {