From c93f208962565dea286a95f1c1329c2f6ea8208e Mon Sep 17 00:00:00 2001 From: Qinxuan Chen Date: Fri, 21 Jun 2024 16:26:36 +0800 Subject: [PATCH] ci: unify the toolchain and commands in dev env and CI (#192) * ci: unify the toolchain and commands in dev env and CI * add taplo.toml * move set-log.sh into scripts dir * rename job name: build => test * fix indent --- .github/workflows/lint.yml | 11 ++--- .github/workflows/test.yml | 9 ++-- Cargo.toml | 4 +- Makefile | 71 ++++++++++++++++++++++++++++++++ .rustfmt.toml => rustfmt.toml | 0 set-log.sh => scripts/set-log.sh | 0 scripts/setup-dev.sh | 25 +++++++++++ taplo.toml | 37 +++++++++++++++++ 8 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 Makefile rename .rustfmt.toml => rustfmt.toml (100%) rename set-log.sh => scripts/set-log.sh (100%) create mode 100755 scripts/setup-dev.sh create mode 100644 taplo.toml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3644720..a00a2af 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -36,19 +36,16 @@ jobs: restore-keys: | ${{ runner.os }}-cargo-lint-${{ hashFiles('**/Cargo.lock') }} ${{ runner.os }}-cargo-lint- - ${{ runner.os }}-cargo- + ${{ runner.os }}-cargo- - name: Run sccache uses: mozilla-actions/sccache-action@v0.0.5 - name: Install toolchain - uses: dtolnay/rust-toolchain@stable - with: - toolchain: 1.78.0 - components: rustfmt, clippy + run: make setup - name: Check format - run: cargo fmt --all -- --check + run: make fmt-check - name: Check clippy - run: cargo clippy --all-targets --all-features -- -D warnings + run: make clippy diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 25fefea..54a1c5b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ env: SCCACHE_GHA_ENABLED: true jobs: - build: + test: runs-on: ubuntu-latest steps: - name: Checkout sources @@ -42,10 +42,7 @@ jobs: uses: mozilla-actions/sccache-action@v0.0.5 - name: Install toolchain - uses: dtolnay/rust-toolchain@stable - with: - toolchain: 1.78.0 - components: rustfmt, clippy + run: make setup - name: Run tests - run: cargo test --verbose + run: make test diff --git a/Cargo.toml b/Cargo.toml index 596a69d..de1cfb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,11 +50,11 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] } [dev-dependencies] criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] } -pprof = { version = "0.13.0", features = ["flamegraph", "criterion"] } futures-util = "0.3.15" -jsonrpc-ws-server = { version = "18.0.0" } jsonrpc-http-server = { version = "18.0.0" } jsonrpc-pubsub = { version = "18.0.0" } +jsonrpc-ws-server = { version = "18.0.0" } +pprof = { version = "0.13.0", features = ["flamegraph", "criterion"] } [[bench]] name = "bench" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ce8d824 --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +.PHONY: setup +# Setup development environment +setup: + bash ./scripts/setup-dev.sh + +.PHONY: clean +# Cleanup compilation outputs +clean: + cargo clean + +.PHONY: fmt-check fmt +# Check the code format +fmt-check: + taplo fmt --check + cargo fmt --all -- --check +# Format the code +fmt: + taplo fmt + cargo fmt --all + +.PHONY: clippy clippy-release +# Run rust clippy with debug profile +clippy: + cargo clippy --all-targets --all-features -- -D warnings +# Run rust clippy with release profile +clippy-release: + cargo clippy --release --all-targets --all-features -- -D warnings + +.PHONY: check check-release +# Check code with debug profile +check: + cargo check +# Check code with release profile +check-release: + cargo check --release + +.PHONY: build build-release +# Build all binaries with debug profile +build: + cargo build +# Build all binaries with release profile +build-release: + cargo build --release + +.PHONY: test test-release +# Run all unit tests with debug profile +test: + cargo test --lib --all +# Run all unit tests with release profile +test-release: + cargo test --release --lib --all + +.PHONY: help +# Show help +help: + @echo '' + @echo 'Usage:' + @echo ' make [target]' + @echo '' + @echo 'Targets:' + @awk '/^[a-zA-Z\-\_0-9]+:/ { \ + helpMessage = match(lastLine, /^# (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")); \ + helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \ + printf "\033[36m%-30s\033[0m %s\n", helpCommand,helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) + +.DEFAULT_GOAL := help diff --git a/.rustfmt.toml b/rustfmt.toml similarity index 100% rename from .rustfmt.toml rename to rustfmt.toml diff --git a/set-log.sh b/scripts/set-log.sh similarity index 100% rename from set-log.sh rename to scripts/set-log.sh diff --git a/scripts/setup-dev.sh b/scripts/setup-dev.sh new file mode 100755 index 0000000..226817e --- /dev/null +++ b/scripts/setup-dev.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +function install_rustup { + echo "Installing Rust toolchain..." + if rustup --version &> /dev/null; then + echo "Rust toolchain has been installed" + else + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none + source "$HOME"/.cargo/env + fi + rustup show +} + +function install_cargo_binary { + CRATE_NAME=$1 + BIN_NAME=${2:-$1} + if command -v "$BIN_NAME" &> /dev/null; then + echo "$CRATE_NAME has been installed" + else + cargo install "$CRATE_NAME" --force --locked + fi +} + +install_rustup +install_cargo_binary "taplo-cli" "taplo" diff --git a/taplo.toml b/taplo.toml new file mode 100644 index 0000000..51542e8 --- /dev/null +++ b/taplo.toml @@ -0,0 +1,37 @@ +## https://taplo.tamasfe.dev/configuration/#configuration-file + +include = ["**/Cargo.toml"] +exclude = ["target/**/Cargo.toml"] + +[formatting] +# Align consecutive entries vertically. +align_entries = false +# Append trailing commas for multi-line arrays. +array_trailing_comma = true +# Expand arrays to multiple lines that exceed the maximum column width. +array_auto_expand = true +# Collapse arrays that don't exceed the maximum column width and don't contain comments. +array_auto_collapse = false +# Omit white space padding from single-line arrays +compact_arrays = true +# Omit white space padding from the start and end of inline tables. +compact_inline_tables = false +# Maximum column width in characters, affects array expansion and collapse, this doesn't take whitespace into account. +# Note that this is not set in stone, and works on a best-effort basis. +column_width = 160 +# Indent based on tables and arrays of tables and their subtables, subtables out of order are not indented. +indent_tables = false +# The substring that is used for indentation, should be tabs or spaces (but technically can be anything). +indent_string = ' ' +# Add trailing newline at the end of the file if not present. +trailing_newline = true +# Alphabetically reorder keys that are not separated by empty lines. +reorder_keys = false +# Maximum amount of allowed consecutive blank lines. This does not affect the whitespace at the end of the document, as it is always stripped. +allowed_blank_lines = 1 +# Use CRLF for line endings. +crlf = false + +[[rule]] +keys = ["workspace.dependencies", "dependencies", "dev-dependencies", "build-dependencies"] +formatting = { reorder_keys = true }