Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

support fetching xz tarballs #10

Closed
wants to merge 8 commits into from
55 changes: 53 additions & 2 deletions bin/nvh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ NVH_NODE_DOWNLOAD_MIRROR=${NVH_NODE_DOWNLOAD_MIRROR:-https://nodejs.org/download
NVH_NODE_DOWNLOAD_MIRROR=${NVH_NODE_DOWNLOAD_MIRROR%/}
readonly NVH_NODE_DOWNLOAD_MIRROR

# Using xz instead of gzip is enabled by default, if xz compatibility checks pass.
# User may set NVH_USE_XZ to 0 to disable, or set to anything else to force-enable.
# May be overridden by command line flags.

g_xz_force_enabled="false"
if [[ "${NVH_USE_XZ}" = "0" ]]; then
NVH_USE_XZ="false"
elif [[ -n "${NVH_USE_XZ+defined}" ]]; then
NVH_USE_XZ="true"
g_xz_force_enabled="true"
else
NVH_USE_XZ="true"
fi

# NVH_PRESERVE_NPM tested with -z -n
# made readonly after CLI option parsing

Expand Down Expand Up @@ -294,6 +308,9 @@ Options:
-h, --help Display help information
--insecure Turn off certificate checking for https requests (may be needed from behind a proxy server)
-p, --preserve Preserve npm and npx during install of node
--use-xz Download xz-compressed tarballs, if available and extractable by tar, during install of node (this is the default)
--force-use-xz Download xz-compressed tarballs. May fail if xz tarball is not available and extractable
--no-use-xz Download gzip-compressed tarballs, rather than xz-compressed ones, during install of node
-V, --version Output version of nvh

Aliases:
Expand Down Expand Up @@ -515,6 +532,18 @@ function is_ok() {
fi
}

#
# Synopsis: can_use_xz
#

function can_use_xz() {
# nvh uses xz on Linux and macOS at this time, if the system supports it
if ! ([[ "$(uname -s)" = "Linux" ]] && command -v xz &> /dev/null) \
&& ! ([[ "$(uname -s)" = "Darwin" ]] && [[ "$(sw_vers -productVersion | cut -d '.' -f 2)" -gt "8" ]]); then
return 1
fi
}

#
# Synopsis: display_tarball_platform
#
Expand Down Expand Up @@ -568,7 +597,11 @@ function display_compatible_file_field {

function tarball_url() {
local version="$1"
echo "${g_mirror_url}/${version}/node-${version}-$(display_tarball_platform).tar.gz"

local ext="gz"
if [[ "${NVH_USE_XZ}" = "true" ]]; then local ext="xz"; fi

echo "${g_mirror_url}/${version}/node-${version}-$(display_tarball_platform).tar.${ext}"
}

#
Expand Down Expand Up @@ -658,20 +691,35 @@ function install() {
fi
fi

if [[ "${NVH_USE_XZ}" = true ]] && [[ "S{g_xz_force_enabled}" = "false" ]]; then
can_use_xz || NVH_USE_XZ="false"
fi

echo
install_log "installing" "${g_mirror_folder_name}/${resolved_version}"

if [[ "${NVH_USE_XZ}" = "true" ]] && [[ "${g_xz_force_enabled}" = "false" ]]; then
local resolved_major_version
resolved_major_version="$(echo ${resolved_version#v} | cut -d '.' -f '1')"
if [[ "${resolved_major_version}" -lt 4 ]]; then
NVH_USE_XZ="false"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: the Node version check could be moved back into can_use_xz().

Not sure which I prefer, but putting it in can_use_xz() could be a bit more DRY, due to not having to check for NVH_USE_XZ again.

Could gate any of the checks in can_use_xz() (e.g. just the platform checks) with: if [[ "${g_xz_force_enabled}" = "false" ]]; then if need be.

fi
fi

local url="$(tarball_url "${resolved_version}")"
is_ok "${url}" || abort "download preflight failed for '$resolved_version' (${url})"

local tarflags="-zx"
if [[ "${NVH_USE_XZ}" = "true" ]]; then local tarflags="-Jx"; fi

install_log "mkdir" "${dir}"
mkdir -p "${dir}" || abort "sudo required (or change ownership, or define NVH_PREFIX)"
touch "${dir}/nvh.lock"

cd "${dir}" || abort "Failed to cd to directory"

install_log "fetch" "${url}"
do_get "${url}" | tar -zx --strip-components=1
do_get "${url}" | tar "${tarflags}" --strip-components=1
[[ "${GET_OPTIONS_SHOW_PROGRESS}" = "true" ]] && erase_line
rm -f "${dir}/nvh.lock"

Expand Down Expand Up @@ -1152,6 +1200,9 @@ function main() {
--insecure) set_insecure ;;
-p|--preserve) NVH_PRESERVE_NPM="true" ;;
--no-preserve) NVH_PRESERVE_NPM="" ;;
--use-xz) NVH_USE_XZ="true"; g_xz_force_enabled="false" ;;
--force-use-xz) NVH_USE_XZ="true"; g_xz_force_enabled="true" ;;
--no-use-xz) NVH_USE_XZ="false" ;;
-V|--version) echo "${VERSION}"; exit ;;
-*) abort "unrecognised option '$1'"; exit 1 ;;
exec) unprocessed_args=("$@"); break ;;
Expand Down