diff options
author | Thomas Buckley-Houston <tom@tombh.co.uk> | 2022-07-16 13:56:39 -0400 |
---|---|---|
committer | Thomas Buckley-Houston <tom@tombh.co.uk> | 2022-07-16 17:18:39 -0400 |
commit | f34ccc73440085f37b44b52be2ada4439823a02a (patch) | |
tree | 1e73f4407e88bae7075108b599c593c0f883dab1 | |
parent | 77fd0e68b4b9e525d736d0e7773e1b3394404676 (diff) |
devops: Move random scripts into central ctl.sh
-rwxr-xr-x | contrib/release_if_new_version.sh | 49 | ||||
-rwxr-xr-x | contrib/upload_github_release_asset.sh | 65 | ||||
-rwxr-xr-x | ctl.sh | 29 | ||||
-rwxr-xr-x | interfacer/contrib/build_browsh.sh | 37 | ||||
-rwxr-xr-x | interfacer/contrib/get_browsh_version.sh | 9 | ||||
-rwxr-xr-x | interfacer/contrib/xpi2bin.sh | 15 | ||||
-rw-r--r-- | scripts/bundling.bash | 78 | ||||
-rw-r--r-- | scripts/common.bash | 7 | ||||
-rw-r--r-- | scripts/misc.bash | 11 | ||||
-rw-r--r-- | scripts/releasing.bash | 107 | ||||
-rwxr-xr-x | webext/contrib/bundle_webextension.sh | 55 | ||||
-rwxr-xr-x | webext/contrib/setup_node.sh | 11 | ||||
-rw-r--r-- | webext/package.json | 4 |
13 files changed, 234 insertions, 243 deletions
diff --git a/contrib/release_if_new_version.sh b/contrib/release_if_new_version.sh deleted file mode 100755 index 133b49b..0000000 --- a/contrib/release_if_new_version.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env bash - -set -e - -PROJECT_ROOT=$(git rev-parse --show-toplevel) - -browsh_version=$($PROJECT_ROOT/interfacer/contrib/get_browsh_version.sh) -latest_tagged_version=$(git tag --sort=v:refname --list 'v*.*.*' | tail -n1 | sed -e "s/^v//") - -echo "Browsh version: $browsh_version" -echo "Latest tag: $latest_tagged_version" - -if [[ "$browsh_version" == "$latest_tagged_version" ]]; then - echo "Not running release as there's no new version." - exit 0 -fi - -git tag v$browsh_version -git show v$browsh_version --quiet -git config --global user.email "builds@travis-ci.com" -git config --global user.name "Travis CI" -# `/dev/null` needed to prevent Github token appearing in logs -git push --tags --quiet https://$GITHUB_TOKEN@github.com/browsh-org/browsh > /dev/null 2>&1 - -git reset --hard v$browsh_version - -cd $PROJECT_ROOT/webext -BROWSH_ENV=RELEASE npm run build - -cd $PROJECT_ROOT/interfacer/src -curl -sL http://git.io/goreleaser | bash - -cd $HOME -git clone https://github.com/browsh-org/www.brow.sh.git -cd www.brow.sh -echo "latest_version: $browsh_version" > _data/browsh.yml -git add _data/browsh.yml -git commit -m "(Travis CI) Updated Browsh version to $browsh_version" -# `/dev/null` needed to prevent Github token appearing in logs -git push --quiet https://$GITHUB_TOKEN@github.com/browsh-org/www.brow.sh > /dev/null 2>&1 - -# Manually also include the signed Mozilla web extension in the release archives -$PROJECT_ROOT/contrib/upload_github_release_asset.sh \ - github_api_token=$GITHUB_TOKEN \ - owner=browsh-org \ - repo=browsh \ - tag=v$browsh_version \ - filename=$PROJECT_ROOT/webext/dist/web-ext-artifacts/browsh-$browsh_version-an+fx.xpi - diff --git a/contrib/upload_github_release_asset.sh b/contrib/upload_github_release_asset.sh deleted file mode 100755 index 18a1602..0000000 --- a/contrib/upload_github_release_asset.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env bash -# -# Author: Stefan Buck -# License: MIT -# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 -# -# -# This script accepts the following parameters: -# -# * owner -# * repo -# * tag -# * filename -# * github_api_token -# -# Script to upload a release asset using the GitHub API v3. -# -# Example: -# -# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip -# - -# Check dependencies. -set -e -xargs=$(which gxargs || which xargs) - -# Validate settings. -[ "$TRACE" ] && set -x - -CONFIG=$@ - -for line in $CONFIG; do - eval "$line" -done - -# Define variables. -GH_API="https://api.github.com" -GH_REPO="$GH_API/repos/$owner/$repo" -GH_TAGS="$GH_REPO/releases/tags/$tag" -AUTH="Authorization: token $github_api_token" -WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" -CURL_ARGS="-LJO#" - -if [[ "$tag" == 'LATEST' ]]; then - GH_TAGS="$GH_REPO/releases/latest" -fi - -# Validate token. -curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } - -# Read asset tags. -response=$(curl -sH "$AUTH" $GH_TAGS) - -# Get ID of the asset based on given filename. -eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=') -[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } - -# Upload asset -echo "Uploading asset... " - -# Construct url -GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)" - -curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET - @@ -0,0 +1,29 @@ +#!/bin/env bash +set -e + +function_to_run=$1 + +export PROJECT_ROOT && PROJECT_ROOT=$(git rev-parse --show-toplevel) +export GORELEASER_VERSION=1.10.2 +export GOBINDATA_VERSION=3.23.0 + +function _includes_path { + echo "$PROJECT_ROOT"/scripts +} + +function _load_includes { + for file in "$(_includes_path)"/*.bash; do + # shellcheck disable=1090 + source "$file" + done +} + +_load_includes + +if [[ $(type -t "$function_to_run") != function ]]; then + echo "Subcommand: '$function_to_run' not found." + exit 1 +fi + +shift +"$function_to_run" "$@" diff --git a/interfacer/contrib/build_browsh.sh b/interfacer/contrib/build_browsh.sh deleted file mode 100755 index ee7f0a4..0000000 --- a/interfacer/contrib/build_browsh.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash - -# This is for building a production version of Browsh. -# To build Browsh during development see: -# https://github.com/browsh-org/browsh#contributing - -# This script depends on Golang, go-bindata and curl - -set -e - -INTERFACER_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && cd ../ && pwd)" -cd "$INTERFACER_ROOT" - -# Install the tool to convert the web extenstion file into a Go-compatible binary -pushd ../webext -npm run get-gobindata -popd - -# Get the current Browsh version, in order to find the corresponding web extension release -version_file=$INTERFACER_ROOT/src/browsh/version.go -line=$(grep 'browshVersion' <"$version_file") -version=$(echo "$line" | grep -o '".*"' | sed 's/"//g') - -# Build the URI for the webextension file -base='https://github.com/browsh-org/browsh/releases/download' -release_url="$base/v$version/browsh-$version-an.fx.xpi" - -xpi_file=$INTERFACER_ROOT/src/browsh/browsh.xpi -destination=$INTERFACER_ROOT/src/browsh/webextension.go - -# Download the web extension -curl -L -o "$xpi_file" "$release_url" - -# Convert the web extension into binary data that can be compiled into a -# cross-platform Go binary. -XPI_FILE=$xpi_file BIN_FILE=$destination \ - "$INTERFACER_ROOT"/contrib/xpi2bin.sh diff --git a/interfacer/contrib/get_browsh_version.sh b/interfacer/contrib/get_browsh_version.sh deleted file mode 100755 index 6b2fece..0000000 --- a/interfacer/contrib/get_browsh_version.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -set -e - -PROJECT_ROOT=$(git rev-parse --show-toplevel) -version_file=$PROJECT_ROOT/interfacer/src/browsh/version.go -line=$(grep 'browshVersion' < $version_file) -version=$(echo $line | grep -o '".*"' | sed 's/"//g') -echo -n $version diff --git a/interfacer/contrib/xpi2bin.sh b/interfacer/contrib/xpi2bin.sh deleted file mode 100755 index 2566c63..0000000 --- a/interfacer/contrib/xpi2bin.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash -set -e - -INTERFACER_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && cd ../ && pwd )" - -go-bindata -version -go-bindata \ - -nocompress \ - -prefix $INTERFACER_ROOT \ - -pkg browsh \ - -o $BIN_FILE \ - $XPI_FILE - -ls -alh $INTERFACER_ROOT/src/browsh/webextension.go -echo "go-bindata exited with $(echo $?)" diff --git a/scripts/bundling.bash b/scripts/bundling.bash new file mode 100644 index 0000000..4e275cc --- /dev/null +++ b/scripts/bundling.bash @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +export WEBEXTENSION_GO=$PROJECT_ROOT/interfacer/src/browsh/webextension.go +export GOBINDATA_VERSION=3.23.0 + +# Convert the web extension into binary data that can be compiled into a +# cross-platform Go binary. +function xpi_to_bin() { + local xpi_file=$1 + local bin_file=$2 + + if ! command -v go-bindata &>/dev/null; then + echo "Installing \`go-bindata'..." + go install github.com/kevinburke/go-bindata/go-bindata@v"$GOBINDATA_VERSION" + go-bindata -version + fi + + go-bindata \ + -nocompress \ + -prefix "$PROJECT_ROOT/interfacer" \ + -pkg browsh \ + -o "$bin_file" \ + "$xpi_file" + + ls -alh "$WEBEXTENSION_GO" +} + +function build_webextension() { + local NODE_BIN=$PROJECT_ROOT/webext/node_modules/.bin + + cd "$PROJECT_ROOT"/webext && "$NODE_BIN"/webpack + cd "$PROJECT_ROOT"/webext/dist && rm ./*.map + if [ -f core ]; then + # Is this a core dump for some failed process? + rm core + fi + ls -alh . + "$NODE_BIN"/web-ext build --overwrite-dest + ls -alh web-ext-artifacts + + version=$("$PROJECT_ROOT"/ctl.sh browsh_version) + + local xpi_file=browsh-$version-an+fx.xpi + local zip_file=browsh-$version.zip + local source_dir=$PROJECT_ROOT/webext/dist/web-ext-artifacts + + if [ "$BROWSH_ENV" == "RELEASE" ]; then + # The signed version. There can only be one canonical XPI for each semantic + # version. + source_file=$source_dir/$xpi_file + bundle_file=$PROJECT_ROOT/interfacer/browsh.xpi + "$NODE_BIN"/web-ext sign --api-key "$MDN_USER" --api-secret "$MDN_KEY" + else + # TODO: This doesn't currently work with the Marionettte `tempAddon` + # installation method. Just use `web-ext run` and Browsh's `use-existing-ff` + # flag - which is better anyway as it auto-reloads the extension when files + # change. NB: If you fix this, don't forget to change the filename loaded + # by `Asset()` in `main.go`. + # In development/testing, we want to be able to bundle the webextension + # frequently without having to resort to version bumps. + source_file=$source_dir/$zip_file + bundle_file=$source_dir/browsh.zip + fi + + cp -f "$source_file" "$bundle_file" + echo "Bundling $source_file to $WEBEXTENSION_GO using internal path $bundle_file" + xpi2bin "$bundle_file" "$WEBEXTENSION_GO" +} + +function bundle_production_webextension() { + local version && version=$(browsh_version) + local base='https://github.com/browsh-org/browsh/releases/download' + local release_url="$base/v$version/browsh-$version-an.fx.xpi" + local xpi_file=$PROJECT_ROOT/interfacer/src/browsh/browsh.xpi + curl -L -o "$xpi_file" "$release_url" + + xpi2bin "$xpi_file" "$WEBEXTENSION_GO" +} diff --git a/scripts/common.bash b/scripts/common.bash new file mode 100644 index 0000000..20bcb04 --- /dev/null +++ b/scripts/common.bash @@ -0,0 +1,7 @@ +#!/bin/env bash + +function _panic() { + local message=$1 + echo >&2 "$message" + exit 1 +} diff --git a/scripts/misc.bash b/scripts/misc.bash new file mode 100644 index 0000000..0c80ae2 --- /dev/null +++ b/scripts/misc.bash @@ -0,0 +1,11 @@ +#!/bin/env bash + +function golang_lint_check() { + pushd "$PROJECT_ROOT"/interfacer || _panic + diff -u <(echo -n) <(gofmt -d ./) + popd || _panic +} + +function golang_lint_fix() { + gofmt -f ./interfacer +} diff --git a/scripts/releasing.bash b/scripts/releasing.bash new file mode 100644 index 0000000..b474a98 --- /dev/null +++ b/scripts/releasing.bash @@ -0,0 +1,107 @@ +#!/bin/env bash + +export BROWSH_VERSION +export LATEST_TAGGED_VERSION + +function _goreleaser_production() { + if ! command -v goreleaser &>/dev/null; then + echo "Installing \`goreleaser'..." + go install github.com/goreleaser/goreleaser@v"$GORELEASER_VERSION" + fi + pushd "$PROJECT_ROOT"/interfacer/src || _panic + goreleaser release + popd || _panic +} + +function _export_versions() { + BROWSH_VERSION=$(_parse_browsh_version) + LATEST_TAGGED_VERSION=$( + git tag --sort=v:refname --list 'v*.*.*' | tail -n1 | sed -e "s/^v//" + ) +} + +function _parse_browsh_version() { + version_file=$PROJECT_ROOT/interfacer/src/browsh/version.go + line=$(grep 'browshVersion' <"$version_file") + version=$(echo "$line" | grep -o '".*"' | sed 's/"//g') + echo -n "$version" +} + +function _is_new_version() { + _export_versions + [ "$BROWSH_VERSION" = "" ] && _panic "BROWSH_VERSION unset" + [ "$LATEST_TAGGED_VERSION" = "" ] && _panic "LATEST_TAGGED_VERSION unset" + [[ "$BROWSH_VERSION" != "$LATEST_TAGGED_VERSION" ]] +} + +function _tag_on_version_change() { + _export_versions + echo_versions + + if ! _is_new_version; then + echo "Not running release as there's no new version." + exit 0 + fi + + git tag v"$BROWSH_VERSION" + git show v"$BROWSH_VERSION" --quiet + git config --global user.email "ci@github.com" + git config --global user.name "Github Actions" + # `/dev/null` needed to prevent Github token appearing in logs + git push --tags --quiet https://"$GITHUB_TOKEN"@github.com/browsh-org/browsh >/dev/null 2>&1 + + git reset --hard v"$BROWSH_VERSION" +} + +function echo_versions() { + _export_versions + echo "Browsh Golang version: $BROWSH_VERSION" + echo "Git latest tag: $LATEST_TAGGED_VERSION" +} + +function browsh_version() { + _export_versions + echo -n "$BROWSH_VERSION" +} + +function github_actions_output_version_status() { + local status="false" + if _is_new_version; then + status="true" + fi + echo "::set-output name=is_new_version::$status" +} + +function npm_build_release() { + pushd "$PROJECT_ROOT"/webext || _panic + BROWSH_ENV=RELEASE npm run build_webextension + popd || _panic +} + +function update_browsh_website_with_new_version() { + pushd /tmp || _panic + git clone https://github.com/browsh-org/www.brow.sh.git + cd www.brow.sh || exit 1 + echo "latest_version: $BROWSH_VERSION" >_data/browsh.yml + git add _data/browsh.yml + git commit -m "Github Actions: updated Browsh version to $BROWSH_VERSION" + # `/dev/null` needed to prevent Github token appearing in logs + git push --quiet https://"$GITHUB_TOKEN"@github.com/browsh-org/www.brow.sh >/dev/null 2>&1 + popd || _panic +} + +function goreleaser_local_only() { + pushd "$PROJECT_ROOT"/interfacer || _panic + goreleaser release \ + --config "$PROJECT_ROOT"/goreleaser.yml \ + --snapshot \ + --rm-dist + popd || _panic +} + +function release() { + npm_build_release + _tag_on_version_change + _goreleaser_production + update_browsh_website_with_new_version +} diff --git a/webext/contrib/bundle_webextension.sh b/webext/contrib/bundle_webextension.sh deleted file mode 100755 index a7e5b2d..0000000 --- a/webext/contrib/bundle_webextension.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env bash -# Convert the Browsh webextension into embedable binary data so that we can -# distribute Browsh as a single static binary. - -# Requires the go-bindata binary, which seems to only be installed with: -# `go get -u gopkg.in/shuLhan/go-bindata.v3/...` - -set -e - -PROJECT_ROOT=$(git rev-parse --show-toplevel) - -NODE_BIN=$PROJECT_ROOT/webext/node_modules/.bin -destination=$PROJECT_ROOT/interfacer/src/browsh/webextension.go - -cd $PROJECT_ROOT/webext && $NODE_BIN/webpack -cd $PROJECT_ROOT/webext/dist && rm *.map -if [ -f core ] ; then - # Is this a core dump for some failed process? - rm core -fi -ls -alh . -$NODE_BIN/web-ext build --overwrite-dest -ls -alh web-ext-artifacts - -version=$($PROJECT_ROOT/interfacer/contrib/get_browsh_version.sh) - -xpi_file=browsh-$version-an+fx.xpi -zip_file=browsh-$version.zip -source_dir=$PROJECT_ROOT/webext/dist/web-ext-artifacts - -if [ "$BROWSH_ENV" == "RELEASE" ] -then - # The signed version. There can only be one canonical XPI for each semantic - # version. - source_file=$source_dir/$xpi_file - bundle_file=$PROJECT_ROOT/interfacer/browsh.xpi - $NODE_BIN/web-ext sign --api-key $MDN_USER --api-secret $MDN_KEY -else - # TODO: This doesn't currently work with the Marionettte `tempAddon` - # installation method. Just use `web-ext run` and Browsh's `use-existing-ff` - # flag - which is better anyway as it auto-reloads the extension when files - # change. NB: If you fix this, don't forget to change the filename loaded - # by `Asset()` in `main.go`. - # In development/testing, we want to be able to bundle the webextension - # frequently without having to resort to version bumps. - source_file=$source_dir/$zip_file - bundle_file=$source_dir/browsh.zip -fi - -cp -f $source_file $bundle_file - -echo "Bundling $source_file to $destination using internal path $bundle_file" - -XPI_FILE=$bundle_file BIN_FILE=$destination \ - $PROJECT_ROOT/interfacer/contrib/xpi2bin.sh diff --git a/webext/contrib/setup_node.sh b/webext/contrib/setup_node.sh deleted file mode 100755 index 54544ab..0000000 --- a/webext/contrib/setup_node.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -if ! type "nvm" > /dev/null; then - rm -rf ~/.nvm - NVM_VERSION=0.33.8 - curl -o- https://raw.githubusercontent.com/creationix/nvm/v$NVM_VERSION/install.sh | bash - source $HOME/.nvm/nvm.sh -fi - -nvm install # See `/.nvmrc` for current Node version - diff --git a/webext/package.json b/webext/package.json index 07e1135..91fbacb 100644 --- a/webext/package.json +++ b/webext/package.json @@ -1,7 +1,7 @@ { "scripts": { - "get-gobindata": "go install github.com/kevinburke/go-bindata/go-bindata@latest", - "build": "./contrib/bundle_webextension.sh", + "build_webextension": "../ctl.sh build_webextension", + "lint": "prettier --list-different "{src,test}/**/*.js", "test": "NODE_PATH=src:test mocha" }, "babel": { |