From 260a2f1a8affdb4dbd012f2d80f80d57f2cc84d0 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 2 May 2020 20:43:53 +0000 Subject: [PATCH] add ripgrep --- _common/github.js | 54 ++++++++++++++++++--------- rg/releases.js | 18 +++++++++ rg/rg.bash | 89 ++++++++++++++++++++++++++++++++++++++++++++ ripgrep/releases.js | 18 +++++++++ ripgrep/ripgrep.bash | 9 +++++ 5 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 rg/releases.js create mode 100644 rg/rg.bash create mode 100644 ripgrep/releases.js create mode 100644 ripgrep/ripgrep.bash diff --git a/_common/github.js b/_common/github.js index cc542f0..0fcc7ba 100644 --- a/_common/github.js +++ b/_common/github.js @@ -2,33 +2,44 @@ // this may need customizations between packages const osMap = { - macos: /\b(mac|darwin|iPhone|iOS|iPad)/i, + macos: /\b(apple|mac|darwin|iPhone|iOS|iPad)/i, linux: /\b(linux)/i, win: /\b(win|microsoft|msft)/i, sunos: /\b(sun)/i, aix: /\b(aix)/i }; -const archMap = { - amd64: /(amd64|x64|[_\-]64)/i, - x86: /\b(x86)(?![_\-]64)/i, +// evaluation order matters +// (i.e. otherwise x86 and x64 can cross match) +var archArr = [ + 'amd64', // first and most likely match + 'arm64', + 'x86', + 'ppc64le', + 'ppc64', + 'armv7l', + 'armv6l', + 's390x' +]; +var archMap = { + amd64: /(amd.?64|x64|[_\-]64)/i, + x86: /(86)\b/i, ppc64le: /\b(ppc64le)/i, ppc64: /\b(ppc64)\b/i, - i686: /\b(i686)\b/i, arm64: /\b(arm64|arm)/i, armv7l: /\b(armv?7l)/i, armv6l: /\b(armv?6l)/i, s390x: /\b(s390x)/i }; -const fileExtMap = { +var fileExtMap = { deb: /\.deb$/i, pkg: /\.pkg$/i, exe: /\.exe$/i, msi: /\.msi$/i, zip: /\.zip$/i, - tar: /\.(tar(\.?(gz)?)|tgz)/i, - '7z': /\.7;$/i + tar: /\.tar\..*$/i, + '7z': /\.7z$/i }; /** @@ -66,12 +77,18 @@ function getAllReleases(request, owner = 'BurntSushi', repo = 'ripgrep') { const name = asset['name']; const os = - Object.keys(osMap).find((regKey) => { - name.match(osMap[regKey]); - }) || 'linux'; - const arch = Object.keys(archMap).find((regKey) => - name.match(archMap[regKey]) - ); + Object.keys(osMap).find(function (regKey) { + //console.log('github release os:', name, regKey, osMap[regKey]); + return osMap[regKey].test(name); + }) || 'unknown'; + var arch; + archArr.some(function (regKey) { + //console.log('github release arch:', name, regKey, archMap[regKey]); + arch = name.match(archMap[regKey]) && regKey; + if (arch) { + return true; + } + })[0]; let fileExt = ''; Object.keys(fileExtMap).find((regKey) => { @@ -85,10 +102,11 @@ function getAllReleases(request, owner = 'BurntSushi', repo = 'ripgrep') { all.releases.push({ download: asset['browser_download_url'], - date: release['published_at'], - version: release['tag_name'], - lts: !release['prerelease'], - ext: fileExt, + date: (release['published_at'] || '').replace(/T.*/, ''), + version: release['tag_name'], // TODO tags aren't always semver / sensical + lts: /\b(lts)\b/.test(release['tag_name']), + channel: !release['prerelease'] ? 'stable' : 'beta', + ext: fileExt.slice(1), arch, os }); diff --git a/rg/releases.js b/rg/releases.js new file mode 100644 index 0000000..17c194f --- /dev/null +++ b/rg/releases.js @@ -0,0 +1,18 @@ +'use strict'; + +var github = require('../_common/github.js'); +var owner = 'BurntSushi'; +var repo = 'ripgrep'; + +module.exports = function (request) { + return github(request, owner, repo).then(function (all) { + return all; + }); +}; + +if (module === require.main) { + module.exports(require('@root/request')).then(function (all) { + console.log(JSON.stringify(all)); + //console.log(JSON.stringify(all, null, 2)); + }); +} diff --git a/rg/rg.bash b/rg/rg.bash new file mode 100644 index 0000000..51c3e59 --- /dev/null +++ b/rg/rg.bash @@ -0,0 +1,89 @@ +# title: Ripgrep +# homepage: https://github.com/BurntSushi/ripgrep +# tagline: a modern drop-in grep replacement +# alias: rg +# description: | +# 'rg' is a drop-in replacement for 'grep', that respects '.gitignore' and '.ignore', has all of the sensible default options you want (colors, numbers, etc) turned on by default, is written in Rust, and simply outperforms grep in every imaginable way. R.I.P. grep. +# examples: | +# +# ```bash +# rg # searches recursively, ignoing .git, node_modules, etc +# ``` +# +# ```bash +# rg 'function doStuff' +# ``` +# +# ```bash +# rg 'doStuff\(.*\)' +# ``` + +set -e +set -u +set -o pipefail + +# Use the script's first argument or the supplied WEBI_VERSION or '' +WEBI_VERSION=${1:-${WEBI_VERSION:-}} + +# Set a temporary directory, if not already set +WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-ripgrep.XXXXXXXX)"} + +################### +# Get WEBI vars # +################### + +# The WEBI bootstrap will define these +# but each script should be testable in its own right + +if [ -z "${WEBI_PKG_URL}" ]; then + release_tab="${WEBI_HOST}/api/releases/ripgrep@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1" + WEBI_CSV=$(curl -fsSL "$release_tab" -H "User-Agent: $(uname -a)") + WEBI_CHANNEL=$(echo $WEBI_TAB | cut -d ',' -f 3) + if [ "error" == "$WEBI_CHANNEL" ]; then + echo "could not find release for ripgrep v${WEBI_VERSION}" + exit 1 + fi + WEBI_VERSION=$(echo $WEBI_TAB | cut -d ',' -f 1) + WEBI_PKG_URL=$(echo $WEBI_TAB | cut -d ',' -f 9) + WEBI_PKG_FILE="$WEBI_TMP/$(echo $WEBI_PKG_URL | sed s:.*/::)" +fi + +################### +# Install ripgrep # +################### + +new_rg="${HOME}/.local/bin/rg" + +# Test for existing version +set +e +cur_rg="$(command -v rg)" +set -e +if [ -n "$cur_rg" ]; then + cur_ver=$(rg --version | head -n 1 | cut -d ' ' -f 2) + if [ "$cur_ver" == "$WEBI_VERSION" ]; then + echo "ripgrep v$WEBI_VERSION already installed at $cur_rg" + exit 0 + elif [ "$cur_rg" != "$new_rg" ]; then + echo "WARN: possible conflict with ripgrep v$WEBI_VERSION at $cur_rg" + fi +fi + +# TODO move download to the webi bootstrap +echo Downloading ripgrep v"${WEBI_VERSION}" from "${WEBI_PKG_URL}" +curl -fsSL "${WEBI_PKG_URL}" -o "${WEBI_PKG_FILE}" +pushd "${WEBI_TMP}" 2>&1 >/dev/null + echo Installing ripgrep v${WEBI_VERSION} as "$new_rg" + tar xf "${WEBI_PKG_FILE}" + rm "${WEBI_PKG_FILE}" + mv ./ripgrep-*/rg "${HOME}/.local/bin/" +popd 2>&1 >/dev/null + +################### +# Update PATH # +################### + +# TODO get better output from pathman / output the path to add as return to webi bootstrap +pathman add "$HOME/.local/bin/" + +echo "Installed 'rg'" +echo "" diff --git a/ripgrep/releases.js b/ripgrep/releases.js new file mode 100644 index 0000000..17c194f --- /dev/null +++ b/ripgrep/releases.js @@ -0,0 +1,18 @@ +'use strict'; + +var github = require('../_common/github.js'); +var owner = 'BurntSushi'; +var repo = 'ripgrep'; + +module.exports = function (request) { + return github(request, owner, repo).then(function (all) { + return all; + }); +}; + +if (module === require.main) { + module.exports(require('@root/request')).then(function (all) { + console.log(JSON.stringify(all)); + //console.log(JSON.stringify(all, null, 2)); + }); +} diff --git a/ripgrep/ripgrep.bash b/ripgrep/ripgrep.bash new file mode 100644 index 0000000..faf0f17 --- /dev/null +++ b/ripgrep/ripgrep.bash @@ -0,0 +1,9 @@ +# title: Ripgrep (alias) +# homepage: https://webinstall.dev/rg +# tagline: `ripgrep` (project) is an alias for `rg` (command) +# alias: rg +# description: | +# See https://webinstall.dev/rg + +echo "'ripgrep' (project) is an alias for 'rg' (command)" +curl -fsSL https://webinstall.dev/rg@${WEBI_VERSION:-} | bash -- 2.25.1