|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2021 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +# Build script to run installation tests on [GitHub][1]. |
| 20 | +# |
| 21 | +# [1]: https://help.github.com/en/articles/about-github-actions |
| 22 | + |
| 23 | +# shellcheck disable=SC2181 |
| 24 | + |
| 25 | +# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails: |
| 26 | +set -o pipefail |
| 27 | + |
| 28 | + |
| 29 | +# VARIABLES # |
| 30 | + |
| 31 | +# Get the path to the installation directory as the first argument to the build script: |
| 32 | +install_dir="$1" |
| 33 | + |
| 34 | +# Get the name of the desired package manager as the second argument to the build script: |
| 35 | +pm="$2" |
| 36 | + |
| 37 | +# Get the path to a log file as the third argument to the build script: |
| 38 | +log_file="$3" |
| 39 | + |
| 40 | +# Define the package to install: |
| 41 | +pkg_name='@stdlib/stdlib' |
| 42 | + |
| 43 | +# Define the package repository: |
| 44 | +pkg_github_url='https://github.com/stdlib-js/stdlib' |
| 45 | + |
| 46 | +# Define the repository tag/commit/branch to install: |
| 47 | +pkg_github_tag='develop' |
| 48 | + |
| 49 | +# Define the package version to install: |
| 50 | +pkg_version='latest' |
| 51 | + |
| 52 | +# Define a heartbeat interval to periodically print messages in order to prevent CI from prematurely ending a build due to long running commands: |
| 53 | +heartbeat_interval='30s' |
| 54 | + |
| 55 | +# Declare a variable for storing the heartbeat process id: |
| 56 | +heartbeat_pid="" |
| 57 | + |
| 58 | + |
| 59 | +# FUNCTIONS # |
| 60 | + |
| 61 | +# Error handler. |
| 62 | +# |
| 63 | +# $1 - error status |
| 64 | +on_error() { |
| 65 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 66 | + cleanup |
| 67 | + exit "$1" |
| 68 | +} |
| 69 | + |
| 70 | +# Runs clean-up tasks. |
| 71 | +cleanup() { |
| 72 | + stop_heartbeat |
| 73 | +} |
| 74 | + |
| 75 | +# Starts a heartbeat. |
| 76 | +# |
| 77 | +# $1 - heartbeat interval |
| 78 | +start_heartbeat() { |
| 79 | + echo 'Starting heartbeat...' >&2 |
| 80 | + |
| 81 | + # Create a heartbeat and send to background: |
| 82 | + heartbeat "$1" & |
| 83 | + |
| 84 | + # Capture the heartbeat pid: |
| 85 | + heartbeat_pid=$! |
| 86 | + echo "Heartbeat pid: ${heartbeat_pid}" >&2 |
| 87 | +} |
| 88 | + |
| 89 | +# Runs an infinite print loop. |
| 90 | +# |
| 91 | +# $1 - heartbeat interval |
| 92 | +heartbeat() { |
| 93 | + while true; do |
| 94 | + echo "$(date) - heartbeat..." >&2; |
| 95 | + sleep "$1"; |
| 96 | + done |
| 97 | +} |
| 98 | + |
| 99 | +# Stops the heartbeat print loop. |
| 100 | +stop_heartbeat() { |
| 101 | + echo 'Stopping heartbeat...' >&2 |
| 102 | + kill "${heartbeat_pid}" |
| 103 | +} |
| 104 | + |
| 105 | +# Prints a success message. |
| 106 | +print_success() { |
| 107 | + echo 'Success!' >&2 |
| 108 | +} |
| 109 | + |
| 110 | +# Performs initialization tasks. |
| 111 | +init() { |
| 112 | + echo 'Creating installation directory...' >&2 |
| 113 | + mkdir -p "${install_dir}" || return 1 |
| 114 | + |
| 115 | + echo 'Navigating to installation directory...' >&2 |
| 116 | + cd "${install_dir}" || return 1 |
| 117 | + |
| 118 | + echo 'Creating package.json...' >&2 |
| 119 | + echo '{"name":"test-install","version":"0.0.0","private":true}' > "${install_dir}"/package.json |
| 120 | + |
| 121 | + return 0 |
| 122 | +} |
| 123 | + |
| 124 | +# Performs installation cleanup tasks. |
| 125 | +install_clean() { |
| 126 | + echo 'Removing installation directory...' >&2 |
| 127 | + rm -rf "${install_dir}" || return 1 |
| 128 | + return 0 |
| 129 | +} |
| 130 | + |
| 131 | +# Installs a package via `npm`. |
| 132 | +npm_install() { |
| 133 | + init |
| 134 | + if [[ "$?" -ne 0 ]]; then |
| 135 | + echo "Encountered an error when performing pre-install initialization tasks." >&2 |
| 136 | + return 1 |
| 137 | + fi |
| 138 | + npm install "${pkg_name}@${pkg_version}" |
| 139 | + if [[ "$?" -ne 0 ]]; then |
| 140 | + echo "Installation of package ${pkg_name}@${pkg_version} via npm failed." >&2 |
| 141 | + return 1 |
| 142 | + fi |
| 143 | + install_clean |
| 144 | + if [[ "$?" -ne 0 ]]; then |
| 145 | + echo "Encountered an error when performing post-install cleanup tasks." >&2 |
| 146 | + return 1 |
| 147 | + fi |
| 148 | + return 0 |
| 149 | +} |
| 150 | + |
| 151 | +# Installs a package via `npm` from GitHub. |
| 152 | +npm_install_from_github() { |
| 153 | + init |
| 154 | + if [[ "$?" -ne 0 ]]; then |
| 155 | + echo "Encountered an error when performing pre-install initialization tasks." >&2 |
| 156 | + return 1 |
| 157 | + fi |
| 158 | + npm install "${pkg_github_url}#${pkg_github_tag}" |
| 159 | + if [[ "$?" -ne 0 ]]; then |
| 160 | + echo "Installation of package ${pkg_github_url}#${pkg_github_tag} via npm failed." >&2 |
| 161 | + return 1 |
| 162 | + fi |
| 163 | + install_clean |
| 164 | + if [[ "$?" -ne 0 ]]; then |
| 165 | + echo "Encountered an error when performing post-install cleanup tasks." >&2 |
| 166 | + return 1 |
| 167 | + fi |
| 168 | + return 0 |
| 169 | +} |
| 170 | + |
| 171 | +# Tests whether the project successfully installs via `npm`. |
| 172 | +# |
| 173 | +# $1 - log file |
| 174 | +test_npm_install() { |
| 175 | + echo 'Testing npm install...' >&2 |
| 176 | + npm_install >> "$1" 2>&1 |
| 177 | + if [[ "$?" -ne 0 ]]; then |
| 178 | + echo 'Installation failed.' >&2 |
| 179 | + echo 'Retry 1 of 1...' >&2 |
| 180 | + sleep 15s |
| 181 | + echo 'Testing npm install...' >&2 |
| 182 | + npm_install >> "$1" 2>&1 |
| 183 | + if [[ "$?" -ne 0 ]]; then |
| 184 | + echo 'Installation failed.' >&2 |
| 185 | + return 1 |
| 186 | + fi |
| 187 | + fi |
| 188 | + echo 'Successfully installed.' >&2 |
| 189 | + |
| 190 | + echo 'Testing npm install (via GitHub)...' >&2 |
| 191 | + npm_install_from_github >> "$1" 2>&1 |
| 192 | + if [[ "$?" -ne 0 ]]; then |
| 193 | + echo 'Installation (via GitHub) failed.' >&2 |
| 194 | + echo 'Retry 1 of 1...' >&2 |
| 195 | + sleep 15s |
| 196 | + echo 'Testing npm install (via GitHub)...' >&2 |
| 197 | + npm_install_from_github >> "$1" 2>&1 |
| 198 | + if [[ "$?" -ne 0 ]]; then |
| 199 | + echo 'Installation (via GitHub) failed.' >&2 |
| 200 | + return 1 |
| 201 | + fi |
| 202 | + fi |
| 203 | + echo 'Successfully installed (via GitHub).' >&2 |
| 204 | + |
| 205 | + return 0 |
| 206 | +} |
| 207 | + |
| 208 | +# Main execution sequence. |
| 209 | +main() { |
| 210 | + start_heartbeat "${heartbeat_interval}" |
| 211 | + |
| 212 | + echo "Package manager: ${pm}." >&2 |
| 213 | + |
| 214 | + # Note: please keep the list of package managers in alphabetical order... |
| 215 | + if [[ "${pm}" = "npm" ]]; then |
| 216 | + test_npm_install "${log_file}" |
| 217 | + if [[ "$?" -ne 0 ]]; then |
| 218 | + on_error 1 |
| 219 | + fi |
| 220 | + else |
| 221 | + echo "ERROR: unknown package manager: ${pm}." >&2 |
| 222 | + on_error 1 |
| 223 | + fi |
| 224 | + cleanup |
| 225 | + print_success |
| 226 | + exit 0 |
| 227 | +} |
| 228 | + |
| 229 | +# Set an error handler to print captured output and perform any clean-up tasks: |
| 230 | +trap 'on_error' ERR |
| 231 | + |
| 232 | +# Run main: |
| 233 | +main |
0 commit comments