Skip to content

Commit

Permalink
Clean up scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 7, 2024
1 parent 8b9d0c5 commit c402912
Show file tree
Hide file tree
Showing 6 changed files with 693 additions and 330 deletions.
36 changes: 16 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ env:

defaults:
run:
shell: bash
shell: bash --noprofile --norc -CeEuxo pipefail {0}

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
steps:
- uses: taiki-e/checkout-action@v1
- name: Install Rust
run: rustup toolchain add ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
run: rustup toolchain add "${{ matrix.rust }}" --no-self-update && rustup default "${{ matrix.rust }}"
- run: rustup toolchain add nightly --no-self-update
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@cargo-minimal-versions
Expand All @@ -77,60 +77,58 @@ jobs:
- run: cargo install --path . --debug
- name: Test cargo llvm-cov nextest
run: |
set -eEuxo pipefail
cargo llvm-cov nextest --text --fail-under-lines 50
cargo llvm-cov nextest --text --fail-under-lines 50 --profile default --cargo-profile dev
cargo llvm-cov nextest --text --fail-under-lines 50 --profile ci
cargo llvm-cov nextest --text --fail-under-lines 50 --profile ci --cargo-profile dev
cd ../real1
cd -- ../real1
cargo llvm-cov nextest-archive --archive-file a.tar.zst
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
cargo llvm-cov report --nextest-archive-file a.tar.zst --fail-under-lines 70
rm a.tar.zst
rm -- a.tar.zst
cargo clean
cargo llvm-cov nextest-archive --archive-file a.tar.zst --release
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
cargo llvm-cov report --nextest-archive-file a.tar.zst --fail-under-lines 70
rm a.tar.zst
rm -- a.tar.zst
cargo clean
cargo llvm-cov nextest-archive --archive-file a.tar.zst --cargo-profile custom
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
cargo llvm-cov report --nextest-archive-file a.tar.zst --fail-under-lines 70
rm a.tar.zst
rm -- a.tar.zst
cargo clean
host=$(rustc -vV | grep '^host:' | cut -d' ' -f2)
host=$(rustc -vV | grep -E '^host:' | cut -d' ' -f2)
cargo llvm-cov nextest-archive --archive-file a.tar.zst --target "${host}"
cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70
cargo llvm-cov report --nextest-archive-file a.tar.zst --fail-under-lines 70
working-directory: tests/fixtures/crates/bin_crate
- name: Test nightly-specific options, old Cargo compatibility, trybuild compatibility
run: |
set -eEuxo pipefail
unset RUSTFLAGS
# Test nightly-specific options
git clone https://github.com/taiki-e/easytime.git
pushd easytime >/dev/null
pushd -- easytime >/dev/null
git checkout 7ecb6e6
cargo llvm-cov test --doctests --text --fail-under-lines 30
popd >/dev/null
pushd tests/fixtures/crates/cargo_config >/dev/null
pushd -- tests/fixtures/crates/cargo_config >/dev/null
# TODO: --fail-under-branches?
cargo llvm-cov test --branch --text --fail-under-lines 80
popd >/dev/null
pushd easytime >/dev/null
pushd -- easytime >/dev/null
cargo llvm-cov test --branch --doctests --text --fail-under-lines 30
popd >/dev/null
# Test minimum runnable Cargo version.
rustup toolchain add 1.60 --no-self-update
pushd easytime >/dev/null
pushd -- easytime >/dev/null
cargo +1.60 llvm-cov test --text --fail-under-lines 30
popd >/dev/null
# Test trybuild compatibility.
git clone --depth 1 https://github.com/taiki-e/easy-ext.git
pushd easy-ext >/dev/null
pushd -- easy-ext >/dev/null
cargo llvm-cov --text --test compiletest --fail-under-lines 70
popd >/dev/null
if: startsWith(matrix.rust, 'nightly')
Expand Down Expand Up @@ -172,7 +170,6 @@ jobs:
run: rustup update stable --no-self-update
- name: Install LLVM
run: |
set -eEuxo pipefail
if type -P clang-"${{ matrix.llvm }}" &>/dev/null; then
exit 0
fi
Expand All @@ -181,8 +178,8 @@ jobs:
curl --proto '=https' --tlsv1.2 -fsSL --retry 10 --retry-connrefused https://apt.llvm.org/llvm-snapshot.gpg.key \
| gpg --dearmor \
| sudo tee -- /etc/apt/keyrings/llvm-snapshot.gpg >/dev/null
echo "deb [signed-by=/etc/apt/keyrings/llvm-snapshot.gpg] http://apt.llvm.org/${codename}/ llvm-toolchain-${codename}-${{ matrix.llvm }} main" \
| sudo tee -- "/etc/apt/sources.list.d/llvm-toolchain-${codename}-${{ matrix.llvm }}.list" >/dev/null
sudo tee -- "/etc/apt/sources.list.d/llvm-toolchain-${codename}-${{ matrix.llvm }}.list" >/dev/null \
<<<"deb [signed-by=/etc/apt/keyrings/llvm-snapshot.gpg] http://apt.llvm.org/${codename}/ llvm-toolchain-${codename}-${{ matrix.llvm }} main"
sudo apt-get -o Acquire::Retries=10 -qq update
apt_packages=(
clang-"${{ matrix.llvm }}"
Expand All @@ -200,7 +197,6 @@ jobs:
- run: cargo install --path . --debug
- name: Test
run: |
set -eEuxo pipefail
export CC="clang-${{ matrix.llvm }}"
export CXX="clang++-${{ matrix.llvm }}"
export LLVM_COV="llvm-cov-${{ matrix.llvm }}"
Expand All @@ -220,11 +216,11 @@ jobs:
cargo +1.77 llvm-cov test --text --include-ffi --fail-under-lines 70 -vv
;;
18)
rustup toolchain add 1.78 --no-self-update
rustup toolchain add 1.78 1.81 --no-self-update
cargo clean
cargo +1.78 llvm-cov test --text --include-ffi --fail-under-lines 70 -vv
cargo clean
cargo +stable llvm-cov test --text --include-ffi --fail-under-lines 70 -vv
cargo +1.81 llvm-cov test --text --include-ffi --fail-under-lines 70 -vv
;;
*)
rustup toolchain add beta nightly --no-self-update
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:

defaults:
run:
shell: bash
shell: bash --noprofile --norc -CeEuxo pipefail {0}

jobs:
create-release:
Expand Down Expand Up @@ -59,14 +59,14 @@ jobs:
- uses: taiki-e/setup-cross-toolchain-action@v1
with:
target: ${{ matrix.target }}
- run: echo "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static" >>"${GITHUB_ENV}"
- run: printf '%s\n' "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static" >>"${GITHUB_ENV}"
if: contains(matrix.target, '-windows-msvc')
- run: echo "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static -C link-self-contained=yes" >>"${GITHUB_ENV}"
- run: printf '%s\n' "RUSTFLAGS=${RUSTFLAGS} -C target-feature=+crt-static -C link-self-contained=yes" >>"${GITHUB_ENV}"
if: contains(matrix.target, '-linux-musl')
# https://doc.rust-lang.org/rustc/platform-support.html
- run: echo "MACOSX_DEPLOYMENT_TARGET=10.12" >>"${GITHUB_ENV}"
- run: printf 'MACOSX_DEPLOYMENT_TARGET=10.12\n' >>"${GITHUB_ENV}"
if: matrix.target == 'x86_64-apple-darwin'
- run: echo "MACOSX_DEPLOYMENT_TARGET=11.0" >>"${GITHUB_ENV}"
- run: printf 'MACOSX_DEPLOYMENT_TARGET=11.0\n' >>"${GITHUB_ENV}"
if: matrix.target == 'aarch64-apple-darwin' || matrix.target == 'universal-apple-darwin'
- uses: taiki-e/upload-rust-binary-action@v1
with:
Expand Down
27 changes: 24 additions & 3 deletions .shellcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
# https://github.com/koalaman/shellcheck/blob/HEAD/shellcheck.1.md#rc-files

# See also:
# https://www.shellcheck.net/wiki/Optional
# https://github.com/koalaman/shellcheck/wiki/Optional
# https://google.github.io/styleguide/shellguide.html

# https://www.shellcheck.net/wiki/SC2292
# https://github.com/koalaman/shellcheck/wiki/SC2249
# enable=add-default-case

# https://github.com/koalaman/shellcheck/wiki/SC2244
enable=avoid-nullary-conditions

# https://github.com/koalaman/shellcheck/wiki/SC2312
# enable=check-extra-masked-returns

# https://github.com/koalaman/shellcheck/wiki/SC2310
# https://github.com/koalaman/shellcheck/wiki/SC2311
# enable=check-set-e-suppressed

# enable=check-unassigned-uppercase

# https://github.com/koalaman/shellcheck/wiki/SC2230
enable=deprecate-which

# https://github.com/koalaman/shellcheck/wiki/SC2248
enable=quote-safe-variables

# https://github.com/koalaman/shellcheck/wiki/SC2292
# https://google.github.io/styleguide/shellguide.html#s6.3-tests
enable=require-double-brackets

# https://www.shellcheck.net/wiki/SC2250
# https://github.com/koalaman/shellcheck/wiki/SC2250
# https://google.github.io/styleguide/shellguide.html#s5.6-variable-expansion
enable=require-variable-braces
2 changes: 1 addition & 1 deletion tools/.tidy-check-license-headers
Original file line number Diff line number Diff line change
@@ -1 +1 @@
git ls-files | grep -v '^tests/fixtures/'
git ls-files | grep -Ev '^tests/fixtures/'
103 changes: 53 additions & 50 deletions tools/publish.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0 OR MIT
set -eEuo pipefail
set -CeEuo pipefail
IFS=$'\n\t'
cd "$(dirname "$0")"/..

# shellcheck disable=SC2154
trap 's=$?; echo >&2 "$0: error on line "${LINENO}": ${BASH_COMMAND}"; exit ${s}' ERR
trap -- 's=$?; printf >&2 "%s\n" "${0##*/}:${LINENO}: \`${BASH_COMMAND}\` exit with ${s}"; exit ${s}' ERR
cd -- "$(dirname -- "$0")"/..

# Publish a new release.
#
Expand All @@ -26,7 +24,7 @@ retry() {
"$@"
}
bail() {
echo >&2 "error: $*"
printf >&2 'error: %s\n' "$*"
exit 1
}

Expand All @@ -41,6 +39,11 @@ fi
if [[ $# -gt 1 ]]; then
bail "invalid argument '$2'"
fi
if { sed --help 2>&1 || true; } | grep -Eq -e '-i extension'; then
in_place=(-i '')
else
in_place=(-i)
fi

# Make sure there is no uncommitted change.
git diff --exit-code
Expand All @@ -52,12 +55,15 @@ if gh release view "${tag}" &>/dev/null; then
fi

# Make sure that the release was created from an allowed branch.
if ! git branch | grep -q '\* main$'; then
if ! git branch | grep -Eq '\* main$'; then
bail "current branch is not 'main'"
fi
if git remote -v | grep -F origin | grep -Eq 'github\.com[:/]taiki-e/'; then
bail "cannot publish a new release from fork repository"
fi

release_date=$(date -u '+%Y-%m-%d')
tags=$(git --no-pager tag | (grep -E "^${tag_prefix}[0-9]+" || true))
tags=$(git --no-pager tag | { grep -E "^${tag_prefix}[0-9]+" || true; })
if [[ -n "${tags}" ]]; then
# Make sure the same release does not exist in changelog.
if grep -Eq "^## \\[${version//./\\.}\\]" "${changelog}"; then
Expand All @@ -67,11 +73,12 @@ if [[ -n "${tags}" ]]; then
bail "link to ${version} already exist in ${changelog}"
fi
# Update changelog.
remote_url=$(grep -E '^\[Unreleased\]: https://' "${changelog}" | sed 's/^\[Unreleased\]: //; s/\.\.\.HEAD$//')
remote_url=$(grep -E '^\[Unreleased\]: https://' "${changelog}" | sed -E 's/^\[Unreleased\]: //; s/\.\.\.HEAD$//')
prev_tag="${remote_url#*/compare/}"
remote_url="${remote_url%/compare/*}"
sed -i "s/^## \\[Unreleased\\]/## [Unreleased]\\n\\n## [${version}] - ${release_date}/" "${changelog}"
sed -i "s#^\[Unreleased\]: https://.*#[Unreleased]: ${remote_url}/compare/${tag}...HEAD\\n[${version}]: ${remote_url}/compare/${prev_tag}...${tag}#" "${changelog}"
sed -E "${in_place[@]}" \
-e "s/^## \\[Unreleased\\]/## [Unreleased]\\n\\n## [${version}] - ${release_date}/" \
-e "s#^\[Unreleased\]: https://.*#[Unreleased]: ${remote_url}/compare/${tag}...HEAD\\n[${version}]: ${remote_url}/compare/${prev_tag}...${tag}#" "${changelog}"
if ! grep -Eq "^## \\[${version//./\\.}\\] - ${release_date}$" "${changelog}"; then
bail "failed to update ${changelog}"
fi
Expand All @@ -94,84 +101,80 @@ changes=$(parse-changelog "${changelog}" "${version}")
if [[ -z "${changes}" ]]; then
bail "changelog for ${version} has no body"
fi
echo "============== CHANGELOG =============="
echo "${changes}"
echo "======================================="
printf '============== CHANGELOG ==============\n'
printf '%s\n' "${changes}"
printf '=======================================\n'

metadata=$(cargo metadata --format-version=1 --no-deps)
prev_version=''
docs=()
for readme in $(git ls-files '*README.md'); do
docs+=("${readme}")
lib="$(dirname "${readme}")/src/lib.rs"
lib="$(dirname -- "${readme}")/src/lib.rs"
if [[ -f "${lib}" ]]; then
docs+=("${lib}")
fi
done
changed_paths=("${changelog}" "${docs[@]}")
for id in $(jq <<<"${metadata}" '.workspace_members[]'); do
pkg=$(jq <<<"${metadata}" ".packages[] | select(.id == ${id})")
publish=$(jq <<<"${pkg}" -r '.publish')
# Publishing is unrestricted if null, and forbidden if an empty array.
if [[ "${publish}" == "[]" ]]; then
continue
fi
name=$(jq <<<"${pkg}" -r '.name')
actual_version=$(jq <<<"${pkg}" -r '.version')
if [[ -z "${prev_version:-}" ]]; then
prev_version="${actual_version}"
# Publishing is unrestricted if null, and forbidden if an empty array.
for pkg in $(jq -c '. as $metadata | .workspace_members[] as $id | $metadata.packages[] | select(.id == $id and .publish != [])' <<<"${metadata}"); do
eval "$(jq -r '@sh "NAME=\(.name) ACTUAL_VERSION=\(.version) manifest_path=\(.manifest_path)"' <<<"${pkg}")"
if [[ -z "${prev_version}" ]]; then
prev_version="${ACTUAL_VERSION}"
fi
# Make sure that the version number of all publishable workspace members matches.
if [[ "${actual_version}" != "${prev_version}" ]]; then
bail "publishable workspace members must be version '${prev_version}', but package '${name}' is version '${actual_version}'"
if [[ "${ACTUAL_VERSION}" != "${prev_version}" ]]; then
bail "publishable workspace members must be version '${prev_version}', but package '${NAME}' is version '${ACTUAL_VERSION}'"
fi

manifest_path=$(jq <<<"${pkg}" -r '.manifest_path')
changed_paths+=("${manifest_path}")
# Update version in Cargo.toml.
sed -i -e "s/^version = \"${prev_version}\" #publish:version/version = \"${version}\" #publish:version/g" "${manifest_path}"
if ! grep -Eq "^version = \"${prev_version}\" #publish:version" "${manifest_path}"; then
bail "not found '#publish:version' in version in ${manifest_path}"
fi
sed -E "${in_place[@]}" "s/^version = \"${prev_version}\" #publish:version/version = \"${version}\" #publish:version/g" "${manifest_path}"
# Update '=' requirement in Cargo.toml.
for manifest in $(git ls-files '*Cargo.toml'); do
if grep -Eq "^${name} = \\{ version = \"=${prev_version}\"" "${manifest}"; then
sed -i -E -e "s/^${name} = \\{ version = \"=${prev_version}\"/${name} = { version = \"=${version}\"/g" "${manifest}"
if grep -Eq "^${NAME} = \\{ version = \"=${prev_version}\"" "${manifest}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \\{ version = \"=${prev_version}\"/${NAME} = { version = \"=${version}\"/g" "${manifest}"
fi
done
# Update version in readme and lib.rs.
for path in "${docs[@]}"; do
# TODO: handle pre-release
if [[ "${version}" == "0.0."* ]]; then
# 0.0.x -> 0.0.x2
if grep -Eq "^${name} = \"${prev_version}\"" "${path}"; then
sed -i -E -e "s/^${name} = \"${prev_version}\"/${name} = \"${version}\"/g" "${path}"
# 0.0.x -> 0.0.y
if grep -Eq "^${NAME} = \"${prev_version}\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \"${prev_version}\"/${NAME} = \"${version}\"/g" "${path}"
fi
if grep -Eq "^${name} = \\{ version = \"${prev_version}\"" "${path}"; then
sed -i -E -e "s/^${name} = \\{ version = \"${prev_version}\"/${name} = { version = \"${version}\"/g" "${path}"
if grep -Eq "^${NAME} = \\{ version = \"${prev_version}\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \\{ version = \"${prev_version}\"/${NAME} = { version = \"${version}\"/g" "${path}"
fi
elif [[ "${version}" == "0."* ]]; then
prev_major_minor="${prev_version%.*}"
major_minor="${version%.*}"
if [[ "${prev_major_minor}" != "${major_minor}" ]]; then
# 0.x -> 0.x2
# 0.x.* -> 0.x2
if grep -Eq "^${name} = \"${prev_major_minor}(\\.[0-9]+)?\"" "${path}"; then
sed -i -E -e "s/^${name} = \"${prev_major_minor}(\\.[0-9]+)?\"/${name} = \"${major_minor}\"/g" "${path}"
# 0.x -> 0.y
# 0.x.* -> 0.y
if grep -Eq "^${NAME} = \"${prev_major_minor}(\\.[0-9]+)?\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \"${prev_major_minor}(\\.[0-9]+)?\"/${NAME} = \"${major_minor}\"/g" "${path}"
fi
if grep -Eq "^${name} = \\{ version = \"${prev_major_minor}(\\.[0-9]+)?\"" "${path}"; then
sed -i -E -e "s/^${name} = \\{ version = \"${prev_major_minor}(\\.[0-9]+)?\"/${name} = { version = \"${major_minor}\"/g" "${path}"
if grep -Eq "^${NAME} = \\{ version = \"${prev_major_minor}(\\.[0-9]+)?\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \\{ version = \"${prev_major_minor}(\\.[0-9]+)?\"/${NAME} = { version = \"${major_minor}\"/g" "${path}"
fi
fi
else
prev_major="${prev_version%%.*}"
major="${version%%.*}"
if [[ "${prev_major}" != "${major}" ]]; then
# x -> x2
# x.* -> x2
# x.*.* -> x2
if grep -Eq "^${name} = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"" "${path}"; then
sed -i -E -e "s/^${name} = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"/${name} = \"${major}\"/g" "${path}"
# x -> y
# x.* -> y
# x.*.* -> y
if grep -Eq "^${NAME} = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"/${NAME} = \"${major}\"/g" "${path}"
fi
if grep -Eq "^${name} = \\{ version = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"" "${path}"; then
sed -i -E -e "s/^${name} = \\{ version = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"/${name} = { version = \"${major}\"/g" "${path}"
if grep -Eq "^${NAME} = \\{ version = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"" "${path}"; then
sed -E "${in_place[@]}" "s/^${NAME} = \\{ version = \"${prev_major}(\\.[0-9]+(\\.[0-9]+)?)?\"/${NAME} = { version = \"${major}\"/g" "${path}"
fi
fi
fi
Expand Down
Loading

0 comments on commit c402912

Please sign in to comment.