Skip to content

Commit

Permalink
ci: unify the toolchain and commands in dev env and CI (#192)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
koushiro authored Jun 21, 2024
1 parent d466f18 commit c93f208
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]

- 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
9 changes: 3 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:
SCCACHE_GHA_ENABLED: true

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand All @@ -42,10 +42,7 @@ jobs:
uses: mozilla-actions/[email protected]

- 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
71 changes: 71 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions scripts/setup-dev.sh
Original file line number Diff line number Diff line change
@@ -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"
37 changes: 37 additions & 0 deletions taplo.toml
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit c93f208

Please sign in to comment.