Skip to content

Commit

Permalink
Add installation script (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddoktorski authored Aug 13, 2024
1 parent aa0971e commit 1466cc0
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# cairo-coverage

## Installation

To install the latest stable version of `cairo-coverage`, run:

```shell
curl -L https://raw.githubusercontent.com/software-mansion/cairo-coverage/main/scripts/install.sh | sh
```

If you want to install a specific version, run the following command with the requested version:

```shell
curl -L https://raw.githubusercontent.com/software-mansion/cairo-coverage/main/scripts/install.sh | sh -s -- v0.1.0
```

## Example

Let's say you have a following program
Expand Down
135 changes: 135 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/bin/sh

REPO="https://github.com/software-mansion/cairo-coverage"
BINARY_NAME="cairo-coverage"
LOCAL_BIN="${HOME}/.local/bin"

main () {
check_cmd curl
check_cmd tar

version=${1:-latest}
release_tag=$(curl -# --fail -Ls -H 'Accept: application/json' "${REPO}/releases/{$version}" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')

if [ -z "$release_tag" ]; then
echo "No such version $version, please pass correct one (e.g. v0.1.0)"
exit 1
fi

download_and_extract_binary "$release_tag"

add_binary_to_path

echo "${BINARY_NAME} (${release_tag}) has been installed successfully."
}

check_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "$1 is not available"
echo "Please install $1 and run the script again"
exit 1
fi
}

download_and_extract_binary() {
release_tag=$1

# Define the operating system and architecture
get_architecture
_arch="$RETVAL"

artifact_name=${BINARY_NAME}-${release_tag}-${_arch}

echo "Downloading and extracting ${artifact_name}..."
# Create a temporary directory
temp_dir=$(mktemp -d)

# Download and extract the archive
curl -L "${REPO}/releases/download/${release_tag}/${artifact_name}.tar.gz" | tar -xz -C "${temp_dir}"

# Move the binary to a LOCAL_BIN directory
mkdir -p "${LOCAL_BIN}"
mv "${temp_dir}/${artifact_name}/bin/${BINARY_NAME}" "${LOCAL_BIN}"

# Clean up temporary files
rm -rf "${temp_dir}"
}

get_architecture() {
_ostype="$(uname -s)"
_cputype="$(uname -m)"
_clibtype="gnu"

if [ "$_ostype" = Linux ] && ldd --_requested_version 2>&1 | grep -q 'musl'; then
_clibtype="musl"
fi

if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ] && sysctl hw.optional.x86_64 | grep -q ': 1'; then
_cputype=x86_64
fi

case "$_ostype" in
Linux)
_ostype=unknown-linux-$_clibtype
;;

Darwin)
_ostype=apple-darwin
;;
*)
err "unsupported OS type: $_ostype"
;;
esac

case "$_cputype" in
aarch64 | arm64)
_cputype=aarch64
;;

x86_64 | x86-64 | x64 | amd64)
_cputype=x86_64
;;
*)
err "unknown CPU type: $_cputype"
;;
esac

_arch="${_cputype}-${_ostype}"

RETVAL="$_arch"
}

add_binary_to_path() {
# Store the correct profile file (i.e. .profile for bash or .zshenv for ZSH).
case $SHELL in
*/zsh)
PROFILE=${ZDOTDIR-"$HOME"}/.zshenv
;;
*/bash)
PROFILE=$HOME/.bashrc
;;
*/fish)
PROFILE=$HOME/.config/fish/config.fish
;;
*/ash | */sh)
PROFILE=$HOME/.profile
;;
*)
echo "cairo-coverage: could not detect shell, manually add ${LOCAL_BIN} to your PATH."
exit 0
esac

# Only add cairo-coverage if it isn't already in PATH.
case ":$PATH:" in
*":${LOCAL_BIN}/${BINARY_NAME}:"*)
# The path is already in PATH, do nothing
;;
*)
# Add the cairo-coverage directory to the path
echo >> "$PROFILE" && echo "export PATH=\"\$PATH:$LOCAL_BIN\"" >> "$PROFILE"
;;
esac
}

set -e
main "$@"

0 comments on commit 1466cc0

Please sign in to comment.