From 1b6aab979d175d5c487a4b4726171cad8a7c3be8 Mon Sep 17 00:00:00 2001 From: Ubiratan Soares Date: Sun, 23 Jun 2024 20:25:27 -0300 Subject: [PATCH] Simplifies project setup and task runner --- .github/actions/setup-rust/action.yaml | 6 +- .github/renovate.json | 62 ++----------- .github/workflows/cd.yml | 11 ++- .github/workflows/ci.yml | 22 ++--- .tool-versions | 1 - docs/contributing.md | 4 +- docs/development.md | 36 +++----- justfile | 61 ------------- krabby.sh | 120 +++++++++++++++++++++++++ scripts/cargo-binstaller.sh | 40 --------- scripts/cargo-plugins.sh | 13 --- scripts/flex-build.sh | 62 ------------- scripts/install-requirements.sh | 24 ----- scripts/prepare-release.sh | 11 --- 14 files changed, 150 insertions(+), 323 deletions(-) delete mode 100644 .tool-versions delete mode 100644 justfile create mode 100755 krabby.sh delete mode 100755 scripts/cargo-binstaller.sh delete mode 100755 scripts/cargo-plugins.sh delete mode 100755 scripts/flex-build.sh delete mode 100755 scripts/install-requirements.sh delete mode 100755 scripts/prepare-release.sh diff --git a/.github/actions/setup-rust/action.yaml b/.github/actions/setup-rust/action.yaml index 92ad04d..0de4492 100644 --- a/.github/actions/setup-rust/action.yaml +++ b/.github/actions/setup-rust/action.yaml @@ -15,10 +15,6 @@ runs: - name: Install asdf uses: asdf-vm/actions/setup@05e0d2ed97b598bfce82fd30daf324ae0c4570e6 # v3.0.2 - - name: Install local required tools - shell: bash - run: ./scripts/install-requirements.sh - - name: Install additional tools (Mac-only) if: runner.os == 'macOS' shell: bash @@ -31,4 +27,4 @@ runs: - name: Setup Rust and Cargo plugins shell: bash - run: just setup + run: ./krabby.sh setup diff --git a/.github/renovate.json b/.github/renovate.json index 58afc36..8ecb383 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -44,64 +44,12 @@ }, { "customType": "regex", - "depNameTemplate": "cargo-nextest", - "packageNameTemplate": "nextest-rs/nextest", - "datasourceTemplate": "github-releases", - "extractVersionTemplate": "^cargo-nextest-(?.*)$", - "fileMatch": [ - "(^|/)cargo-plugins\\.toml" - ], - "matchStrings": [ - "cargo-nextest\\s*=\\s*\"(?\\d+\\.\\d+(\\.\\d+)?)\"" - ] - }, - { - "customType": "regex", - "depNameTemplate": "cargo-cyclonedx", - "packageNameTemplate": "CycloneDX/cyclonedx-rust-cargo", - "datasourceTemplate": "github-releases", - "extractVersionTemplate": "^cargo-cyclonedx-(?.*)$", - "fileMatch": [ - "(^|/)cargo-plugins\\.toml" - ], - "matchStrings": [ - "cargo-cyclonedx\\s*=\\s*\"(?\\d+\\.\\d+(\\.\\d+)?)\"" - ] - }, - { - "customType": "regex", - "depNameTemplate": "cargo-msrv", - "packageNameTemplate": "foresterre/cargo-msrv", - "datasourceTemplate": "github-releases", - "fileMatch": [ - "(^|/)cargo-plugins\\.toml" - ], - "matchStrings": [ - "cargo-msrv\\s*=\\s*\"(?\\d+\\.\\d+(\\.\\d+)?)\"" - ] - }, - { - "customType": "regex", - "depNameTemplate": "cargo-deny", - "packageNameTemplate": "EmbarkStudios/cargo-deny", - "datasourceTemplate": "github-releases", - "fileMatch": [ - "(^|/)cargo-plugins\\.toml" - ], - "matchStrings": [ - "cargo-deny\\s*=\\s*\"(?\\d+\\.\\d+(\\.\\d+)?)\"" - ] - }, - { - "customType": "regex", - "depNameTemplate": "cargo-get", - "packageNameTemplate": "nicolaiunrein/cargo-get", - "datasourceTemplate": "github-releases", - "fileMatch": [ - "(^|/)cargo-plugins\\.toml" - ], + "datasourceTemplate": "git-refs", + "currentValueTemplate": "main", + "packageNameTemplate": "https://github.com/dotanuki-labs/callinectes", + "fileMatch": ["(^|/)krabby\\.sh$"], "matchStrings": [ - "cargo-get\\s*=\\s*\"(?\\d+\\.\\d+(\\.\\d+)?)\"" + "readonly callinectes=\"ghcr.io/dotanuki-labs/callinectes:(?.*?)\"" ] } ] diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 985aa4f..5aa4be1 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -20,7 +20,7 @@ jobs: uses: ./.github/actions/setup-rust - name: Cross-compilation supported targets - run: just assemble + run: ./krabby.sh assemble - name: Archive binaries uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3 @@ -36,14 +36,11 @@ jobs: - name: Project Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Setup CI - uses: ./.github/actions/setup-rust - - name: Publish on crates.io run: cargo publish --token ${{ secrets.CRATESIO_PUBLISH_TOKEN }} - name: Check supply-chain issues - run: just security + run: ./krabby.sh security - name: Fetch executable from previous build uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7 @@ -52,7 +49,9 @@ jobs: - name: Prepare to release id: prepare-release - run: ./scripts/prepare-release.sh + run: | + version=$(grep 'version' Cargo.toml | head -1 | sed "s/version[[:space:]]=[[:space:]]//g" | tr -d '"') + echo "version=$version" >>"$GITHUB_OUTPUT" - name: Publish draft release on Github uses: softprops/action-gh-release@a74c6b72af54cfa997e81df42d94703d6313a2d0 # v2.0.6 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 273e90d..31f2632 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,14 +42,8 @@ jobs: - name: Lint Bash scripts uses: dotanuki-labs/github-actions/quality/bash@main - - name: Setup CI - uses: ./.github/actions/setup-rust - - name: Lint project - run: just lint - - - name: Check MSRV - run: just msrv + run: ./krabby.sh lint tests: runs-on: ubuntu-22.04 @@ -59,14 +53,11 @@ jobs: - name: Project Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Lint Bash scripts - uses: dotanuki-labs/github-actions/quality/bash@main - - name: Setup CI uses: ./.github/actions/setup-rust - - name: Run tests - run: just tests + - name: Run Rust tests + run: ./krabby.sh tests build: needs: check-docs @@ -83,7 +74,7 @@ jobs: uses: ./.github/actions/setup-rust - name: Cross-compilation against some targets - run: just assemble + run: ./krabby.sh assemble - name: Archive binaries uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3 @@ -99,11 +90,8 @@ jobs: - name: Project Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Setup CI - uses: ./.github/actions/setup-rust - - name: Check supply-chain issues - run: just security + run: ./krabby.sh security - name: Archive SBOM uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3 diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 1ca81da..0000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -just 1.29.1 diff --git a/docs/contributing.md b/docs/contributing.md index 57c9c85..81baab4 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -58,11 +58,11 @@ Please check them out. - Ensure you have a proper running environment for your fork, e.g. running unit tests locally ```bash -just tests +./krabby.sh tests ``` - Code your changes 🔥 -- Use our `just` recipes to verify [what we execute on CI](https://github.com/dotanuki-labs/gradle-wrapper-validator/blob/main/.github/workflows/ci.yml) +- Use our `krabby` tasks to verify [what we execute on CI](https://github.com/dotanuki-labs/gradle-wrapper-validator/blob/main/.github/workflows/ci.yml) - Raise your Pull Request 🚀 - Fill the description with our [pull request template](https://github.com/dotanuki-labs/.github/blob/main/.github/PULL_REQUEST_TEMPLATE.md) - Ensure you've your PR [linked with a proper issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) diff --git a/docs/development.md b/docs/development.md index c3d0c75..de19e3d 100644 --- a/docs/development.md +++ b/docs/development.md @@ -14,37 +14,25 @@ Please ensure you have those installed on your system. ## Project setup -To get started, install additional required tools with `asdf`: - -```bash -./scripts/install-requirements.sh -``` - -which will install -[just](https://just.systems) -for your user. - -This project uses `just` as a task runner and +This project uses the `krabby.sh` as a task runner and defines a few recipes to make things straightforward. You can check them by running: ```bash -just - -Available recipes: - setup # Setups Rust toolchain and Cargo extensions - msrv # Checks minimum supported Rust toolchain version - lint # Checks code formatting and smells - compile # Quick compiles this project and catches errors - tests # Runs project tests - assemble # Builds release binaries for all supported compilation targets - security # Runs supply-chain checks and generates SecOps artifacts +./krabby.sh + +Available tasks: + setup # Setups Rust toolchain and Cargo extensions + lint # Check code formatting and smells + tests # Run tests for Rust modules and integration tests + assemble # Builds binaries according to the environment (local or CI) + security # Run security checks and generates supply-chain artifacts ``` We definitely recommend getting started by setting up the latest version of Rust along with -all required Cargo subcommands by running: +by running: ```bash -just setup +./krabby.sh setup ``` ## Code Style @@ -84,7 +72,7 @@ and all required status checks must pass. This project adopts [GiHub Actions](https://github.com/dotanuki-labs/gradle-wrapper-validator/actions) -as it CI system. Most of the verifications we'll run on CI are provided by the `just` recipes, +as it CI system. Most of the verifications we'll run on CI are provided by `krabby.sh` tasks, as previously mentioned. In addition to that, we also run a specific `Job` to enforce code quality standards for docs, diff --git a/justfile b/justfile deleted file mode 100644 index 3eee492..0000000 --- a/justfile +++ /dev/null @@ -1,61 +0,0 @@ -# General SDLC tasks driven by Just -# https://just.systems - -_default: - @just --list --unsorted - -# Setups Rust toolchain and Cargo extensions -setup: - @echo "→ Installing and activate Rust toolchain" - rustup show active-toolchain - @echo - @echo "→ Installing Cargo Binstall" - ./scripts/cargo-binstaller.sh - @echo - @echo "→ Installing Cargo extensions" - ./scripts/cargo-plugins.sh - @echo - -# Checks minimum supported Rust toolchain version -msrv: - @echo "→ Checking minimum supported Rust toolchain version (MSRV)" - cargo msrv verify - @echo - -# Checks code formatting and smells -lint: - @echo "→ Checking code formatting (rustfmt)" - cargo fmt --check - @echo - - @echo "→ Checking code smells (clippy)" - cargo clippy --all-targets --all-features -- -D warnings - @echo - -# Quick compiles this project and catches errors -compile: - @echo "→ Compiling project and checking errors" - cargo check - @echo - -# Runs project tests -tests: - @echo "→ Running project tests" - cargo nextest run - @echo - -# Builds release binaries for all supported compilation targets -assemble: - @echo "→ Building binaries for all supported targets" - ./scripts/flex-build.sh - @echo - -# Runs supply-chain checks and generates SecOps artifacts -security: - @echo "→ Enforcing constraints over dependencies" - cargo deny check - @echo - - @echo "→ Generating SBOMs" - cargo cyclonedx --format json - @echo diff --git a/krabby.sh b/krabby.sh new file mode 100755 index 0000000..cdcc50d --- /dev/null +++ b/krabby.sh @@ -0,0 +1,120 @@ +#! /usr/bin/env bash +# Copyright 2024 Dotanuki Labs +# SPDX-License-Identifier: MIT + +set -e + +dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$dir" + +readonly callinectes="ghcr.io/dotanuki-labs/callinectes:ae3795ac90f810c36a8475deb62ac855b501b296" +readonly task="$1" + +usage() { + echo + echo "Available tasks:" + echo + echo "setup # Installs required Cargo extensions" + echo "lint # Check code formatting and smells" + echo "tests # Run tests for Rust modules and integration tests" + echo "assemble # Builds binaries according to the environment (local or CI)" + echo "security # Run security checks and generates supply-chain artifacts" + echo +} + +setup_rust_toolchain() { + echo "🔥 Installing and activating Rust toolchain" + rustup show active-toolchain + echo +} + +check_code_smells() { + echo + echo "🔥 Checking code smells for Rust code" + echo + docker run --rm -v "${PWD}:/usr/src" "$callinectes" code +} + +run_cargo_tests() { + echo + echo "🔥 Running unit + integration tests for Rust code" + echo + cargo test + echo +} + +build_binaries() { + echo + echo "🔥 Building project according to environment" + echo + local gha_runner="${RUNNER_OS:-local}" + local platform + + echo "Detected environment → $gha_runner" + + case "$gha_runner" in + "local") + cargo build --release + exit 0 + ;; + "macOS") + platform="apple-darwin" + ;; + "Linux") + platform="unknown-linux-gnu" + ;; + *) + echo "Error: unsupported environment → $gha_runner" + echo + exit 1 + ;; + esac + + local output_dir="target/ci" + + for arch in x86_64 aarch64; do + local target="$arch-$platform" + rustup target add "$target" + cargo build --release --target "$target" + + local binary="target/$target/release/gwv" + cp "$binary" "$output_dir"/gwv-"$target" + chmod +x "$output_dir"/gwv-"$target" + sha256sum "$binary" >>"$output_dir"/gwv-"$target"-sha256 + done +} + +check_supply_chain() { + echo + echo "🔥 Checking dependencies and supply-chain" + echo + docker run --rm -v "${PWD}:/usr/src" "$callinectes" deps +} + +if [[ -z "$task" ]]; then + usage + exit 0 +fi + +case "$task" in +"setup") + setup_rust_toolchain + ;; +"lint") + check_code_smells + ;; +"tests") + run_cargo_tests + ;; +"assemble") + build_binaries + ;; +"security") + check_supply_chain + ;; +*) + echo "Error: unsupported task → $task" + usage + exit 1 + ;; +esac diff --git a/scripts/cargo-binstaller.sh b/scripts/cargo-binstaller.sh deleted file mode 100755 index c294cab..0000000 --- a/scripts/cargo-binstaller.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# Copyright 2024 Dotanuki Labs -# SPDX-License-Identifier: MIT - -set -euo pipefail - -cd "$(mktemp -d)" - -base_url="https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-" - -os="$(uname -s)" - -if [ "$os" == "Darwin" ]; then - url="${base_url}universal-apple-darwin.zip" - curl -LO --proto '=https' --tlsv1.2 -sSf "$url" - unzip cargo-binstall-universal-apple-darwin.zip - -elif [ "$os" == "Linux" ]; then - machine="$(uname -m)" - target="${machine}-unknown-linux-musl" - if [ "$machine" == "armv7" ]; then - target="${target}eabihf" - fi - - url="${base_url}${target}.tgz" - curl -L --proto '=https' --tlsv1.2 -sSf "$url" | tar -xvzf - -else - echo "Unsupported OS ${os}" - exit 1 -fi - -./cargo-binstall -y --force cargo-binstall - -CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" - -if ! [[ ":$PATH:" == *":$CARGO_HOME/bin:"* ]]; then - echo - printf "Your path is missing %s, you might want to add it.\n" "$CARGO_HOME/bin" - echo -fi diff --git a/scripts/cargo-plugins.sh b/scripts/cargo-plugins.sh deleted file mode 100755 index ffd72e5..0000000 --- a/scripts/cargo-plugins.sh +++ /dev/null @@ -1,13 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2024 Dotanuki Labs -# SPDX-License-Identifier: MIT - -set -euo pipefail - -dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "${dir%/*}" - -grep "cargo-" "cargo-plugins.toml" | - tr -d '\"' | - sed "s/[[:space:]]=[[:space:]]/@/g" | - xargs -L1 -I {} cargo-binstall {} --no-confirm --force --quiet diff --git a/scripts/flex-build.sh b/scripts/flex-build.sh deleted file mode 100755 index 5de5ca0..0000000 --- a/scripts/flex-build.sh +++ /dev/null @@ -1,62 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2024 Dotanuki Labs -# SPDX-License-Identifier: MIT - -set -euo pipefail - -# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables -readonly platform="${RUNNER_OS:-local}" -readonly output_dir="target/ci" - -build() { - local target="$1" - - rustup target add "$target" - cargo build --release --target "$target" - - local binary="target/$target/release/gwv" - cp "$binary" "$output_dir"/gwv-"$target" - chmod +x "$output_dir"/gwv-"$target" - sha256sum "$binary" >>"$output_dir"/gwv-"$target"-sha256 -} - -local_build() { - cargo build --release -} - -ci_build_mac() { - for arch in x86_64 aarch64; do - build "$arch-apple-darwin" - done -} - -ci_build_linux() { - for arch in x86_64 aarch64; do - build "$arch-unknown-linux-gnu" - done -} - -echo - -dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "${dir%/*}" -rm -rf "$output_dir" && mkdir -p "$output_dir" - -case "$platform" in -"local") - ci_build_mac - ;; -"macOS") - ci_build_mac - ;; -"Linux") - ci_build_linux - ;; -*) - echo "Error: unsupported platform → $platform" - echo - exit 1 - ;; -esac - -echo diff --git a/scripts/install-requirements.sh b/scripts/install-requirements.sh deleted file mode 100755 index 688e8ad..0000000 --- a/scripts/install-requirements.sh +++ /dev/null @@ -1,24 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2024 Dotanuki Labs -# SPDX-License-Identifier: MIT - -set -euo pipefail - -dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "${dir%/*}" - -echo "Setting up required tools" - -if ! which asdf >/dev/null; then - echo -e "Error : 'asdf' required but not available" - echo - exit 1 -fi - -echo -asdf plugin add just || true -asdf install -echo - -echo "Done!" -echo diff --git a/scripts/prepare-release.sh b/scripts/prepare-release.sh deleted file mode 100755 index 1e5496f..0000000 --- a/scripts/prepare-release.sh +++ /dev/null @@ -1,11 +0,0 @@ -#! /usr/bin/env bash -# Copyright 2024 Dotanuki Labs -# SPDX-License-Identifier: MIT - -set -e - -dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "${dir%/*}" - -version=$(cargo get package.version) -echo "version=$version" >>"$GITHUB_OUTPUT"