From c0cda053496a3ea21ca18502dc835125f8b8483c Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Mon, 19 Aug 2024 10:35:58 -0700 Subject: [PATCH 1/2] use `just` [`just`][just] is a task manager/runner written in rust for any project. It makes executing common dev workflows commands simple and easily repeated. > [!tip] > See [`just` docs][just]. This also updates some other minor things. [just]: https://just.systems --- .github/workflows/binary-builds.yml | 30 +++------ .github/workflows/build-docs.yml | 1 + .github/workflows/bump_version.py | 40 ++++++++++++ .github/workflows/pre-commit-hooks.yml | 5 ++ .github/workflows/python-packaging.yml | 8 +-- .github/workflows/replace_version_spec.py | 30 --------- .github/workflows/run-dev-tests.yml | 11 ++-- .gitignore | 2 + cpp-linter-lib/README.md | 2 +- cpp-linter-lib/src/lib.rs | 6 +- justfile | 77 +++++++++++++++++++++++ 11 files changed, 148 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/bump_version.py delete mode 100644 .github/workflows/replace_version_spec.py create mode 100644 justfile diff --git a/.github/workflows/binary-builds.yml b/.github/workflows/binary-builds.yml index b40f72a..0af6935 100644 --- a/.github/workflows/binary-builds.yml +++ b/.github/workflows/binary-builds.yml @@ -12,13 +12,8 @@ on: branches: [main] env: - CARGO_INCREMENTAL: 0 - CARGO_NET_GIT_FETCH_WITH_CLI: true - CARGO_NET_RETRY: 10 CARGO_TERM_COLOR: always RUST_BACKTRACE: 1 - RUSTFLAGS: -D warnings - RUSTUP_MAX_RETRIES: 10 defaults: run: @@ -99,15 +94,6 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - if: startsWith(github.ref, 'refs/tags/') - with: - python-version: '3.x' - - - name: Increment version - if: startsWith(github.ref, 'refs/tags/') - run: python .github/workflows/replace_version_spec.py --new-version=${{ github.ref_name }} - - name: Setup Rust uses: dtolnay/rust-toolchain@stable with: @@ -119,13 +105,15 @@ jobs: with: tool: cross - - name: Build (native) - if: ${{ !matrix.cross }} - run: cargo build --manifest-path cpp-linter-lib/Cargo.toml --release --bin cpp-linter --target ${{ matrix.target }} ${{ matrix.vendered && '--features openssl-vendored' || '' }} - - - name: Build (cross) - if: matrix.cross - run: cross build --manifest-path cpp-linter-lib/Cargo.toml --release --bin cpp-linter --target ${{ matrix.target }} ${{ matrix.vendered && '--features openssl-vendored' || '' }} + - name: Build + run: >- + ${{ matrix.cross && 'cross' || 'cargo '}} + build + --manifest-path cpp-linter-lib/Cargo.toml + --bin cpp-linter + --release + --target ${{ matrix.target }} + ${{ matrix.vendered && '--features openssl-vendored' || '' }} - name: Prepare artifacts run: mv target/${{ matrix.target }}/release/cpp-linter${{ runner.os == 'Windows' && '.exe' || '' }} ./cpp-linter-${{ matrix.target }}${{ runner.os == 'Windows' && '.exe' || '' }} diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml index cb7260b..189c912 100644 --- a/.github/workflows/build-docs.yml +++ b/.github/workflows/build-docs.yml @@ -4,6 +4,7 @@ on: [push, workflow_dispatch] env: CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 jobs: cache-deps: diff --git a/.github/workflows/bump_version.py b/.github/workflows/bump_version.py new file mode 100644 index 0000000..4c02256 --- /dev/null +++ b/.github/workflows/bump_version.py @@ -0,0 +1,40 @@ +import argparse +from pathlib import Path +import sys +import re + +VER_PATTERN = re.compile(r'^version = "(\d+)\.(\d+)\.(\d+)[^"]*" # auto', re.MULTILINE) +VER_REPLACE = 'version = "%d.%d.%d" # auto' +COMPONENTS = ["major", "minor", "patch"] + + +class Args(argparse.Namespace): + component: str = "patch" + + +def replace(match: re.Match | None) -> str: + assert match is not None, "Could not find version in Cargo.toml" + ver = [int(x) for x in match.groups()[:3]] + assert len(ver) == 3 + print("old version:", ".".join([str(x) for x in ver])) + index = COMPONENTS.index(Args.component) + ver[index] += 1 + for i in range(index + 1, 3): + ver[i] = 0 + print("new version:", ".".join([str(x) for x in ver])) + return VER_REPLACE % tuple(ver) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("component", default="patch", choices=COMPONENTS) + parser.parse_args(namespace=Args) + cargo_path = Path("Cargo.toml") + doc = cargo_path.read_text(encoding="utf-8") + doc = VER_PATTERN.sub(replace, doc) + cargo_path.write_text(doc, encoding="utf-8", newline="\n") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/workflows/pre-commit-hooks.yml b/.github/workflows/pre-commit-hooks.yml index acbd5a9..d3c7166 100644 --- a/.github/workflows/pre-commit-hooks.yml +++ b/.github/workflows/pre-commit-hooks.yml @@ -12,6 +12,11 @@ jobs: cargo-tools: runs-on: ubuntu-latest + + env: + CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 + steps: - uses: actions/checkout@v4 - run: rustup update diff --git a/.github/workflows/python-packaging.yml b/.github/workflows/python-packaging.yml index 63963ad..eb03448 100644 --- a/.github/workflows/python-packaging.yml +++ b/.github/workflows/python-packaging.yml @@ -16,6 +16,10 @@ on: permissions: contents: read +env: + CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 + jobs: linux: runs-on: ubuntu-latest @@ -35,10 +39,6 @@ jobs: with: python-version: '3.x' - - name: Increment version - if: startsWith(github.ref, 'refs/tags/') - run: python .github/workflows/replace_version_spec.py --new-version=${{ github.ref_name }} - - name: Calculate openssl-vendored shell: bash id: is-openssl-vendored diff --git a/.github/workflows/replace_version_spec.py b/.github/workflows/replace_version_spec.py deleted file mode 100644 index 9603f21..0000000 --- a/.github/workflows/replace_version_spec.py +++ /dev/null @@ -1,30 +0,0 @@ -import argparse -from pathlib import Path -import sys - - -class Args(argparse.Namespace): - new_version: str = "2.0.0" - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("-n", "--new-version", required=True) - args = parser.parse_args(namespace=Args()) - cargo_path = Path("Cargo.toml") - if not cargo_path.exists(): - print("workspace Cargo.toml not in working directory") - return 1 - doc = cargo_path.read_text(encoding="utf-8") - version_pattern = 'version = "%s" # auto' - old_version = version_pattern % "2.0.0" - if old_version not in doc: - print("Could not find version in Cargo.toml:\n", doc) - return 1 - doc = doc.replace(old_version, version_pattern % args.new_version) - cargo_path.write_text(doc, encoding="utf-8") - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/.github/workflows/run-dev-tests.yml b/.github/workflows/run-dev-tests.yml index 1f36a2a..c3f2b3c 100644 --- a/.github/workflows/run-dev-tests.yml +++ b/.github/workflows/run-dev-tests.yml @@ -19,6 +19,7 @@ on: env: CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 jobs: cache-deps: @@ -63,10 +64,10 @@ jobs: # if: runner.os == 'Windows' # run: vcpkg install openssl - - name: Install cargo-nextest and cargo-llvm-cov + - name: Install third-party binaries uses: taiki-e/install-action@v2 with: - tool: cargo-nextest,cargo-llvm-cov,cargo-binstall + tool: cargo-nextest,cargo-llvm-cov,cargo-binstall,just - name: Install llvm-cov-pretty (HTML report generator) run: cargo binstall -y llvm-cov-pretty @@ -116,15 +117,13 @@ jobs: working-directory: cpp-linter-lib env: CLANG_VERSION: ${{ matrix.version }} - run: cargo llvm-cov --lib --no-report nextest + run: just test - name: Generate Coverage HTML report working-directory: cpp-linter-lib env: CLANG_VERSION: ${{ matrix.version }} - run: | - cargo llvm-cov report --json --output-path .coverage.json - llvm-cov-pretty .coverage.json + run: just pretty-cov - name: Upload coverage data uses: actions/upload-artifact@v4 diff --git a/.gitignore b/.gitignore index 2dbfc96..42d5bae 100644 --- a/.gitignore +++ b/.gitignore @@ -199,3 +199,5 @@ book # ignore generated files .cpp-linter_cache/ cpp-linter-py/docs/cli_args.rst +lcov.info +coverage.json diff --git a/cpp-linter-lib/README.md b/cpp-linter-lib/README.md index 99bc2af..0427147 100644 --- a/cpp-linter-lib/README.md +++ b/cpp-linter-lib/README.md @@ -11,4 +11,4 @@ library. See also the [CLI document hosted on github][gh-pages]. [pypi-org]: https://pypi.org/project/cpp-linter -[gh-pages]: https://cpp-linter.github.io/cpp_linter_rs/cli_args.html +[gh-pages]: https://cpp-linter.github.io/cpp_linter_rs/cli.html diff --git a/cpp-linter-lib/src/lib.rs b/cpp-linter-lib/src/lib.rs index 3cd6f28..6857c8a 100644 --- a/cpp-linter-lib/src/lib.rs +++ b/cpp-linter-lib/src/lib.rs @@ -1,6 +1,8 @@ -#![doc(html_logo_url = "https://github.com/cpp-linter/cpp-linter/raw/main/docs/_static/logo.png")] #![doc( - html_favicon_url = "https://github.com/cpp-linter/cpp-linter/raw/main/docs/_static/favicon.ico" + html_logo_url = "https://github.com/cpp-linter/cpp_linter_rs/raw/main/docs/theme/favicon.png" +)] +#![doc( + html_favicon_url = "https://github.com/cpp-linter/cpp_linter_rs/raw/main/docs/theme/favicon.png" )] #![doc = include_str!("../README.md")] diff --git a/justfile b/justfile new file mode 100644 index 0000000..faa676d --- /dev/null +++ b/justfile @@ -0,0 +1,77 @@ +set windows-shell := ["powershell.exe", "-NoLogo", "-Command"] + +# activate python venv +[group("python")] +[windows] +venv: + ./.env/Scripts/activate + +# activate python venv +[group("python")] +[linux] +venv: + . ./.env/bin/activate + +# install python bindings +[group("python")] +py-dev: + maturin dev --manifest-path cpp-linter-py/Cargo.toml + +# run the test suite +[group("code coverage")] +test: + cargo llvm-cov --no-report nextest --manifest-path cpp-linter-lib/Cargo.toml --lib + +# generate and open pretty coverage report +[group("code coverage")] +pretty-cov *args='': + cargo llvm-cov report --json --output-path coverage.json + @llvm-cov-pretty coverage.json {{ args }} + +# generate and open detailed coverage report +[group("code coverage")] +llvm-cov *args='': + cargo llvm-cov report --html {{ args }} + +# This is useful for IDE gutter indicators of line coverage. +# See Coverage Gutters ext in VSCode. +# generate lcov.info +[group("code coverage")] +lcov: + @cargo llvm-cov report --lcov --output-path lcov.info + +# serve docs +[group("docs")] +docs open='': + @mdbook serve docs {{ open }} + +# build docs +[group("docs")] +docs-build: + mdbook build docs --open + +# rust docs +[group("docs")] +docs-rs: + cargo doc --no-deps --lib --manifest-path cpp-linter-lib/Cargo.toml --open + +# run cpp-linter native binary +[group("bin")] +run *args: + cargo run --bin cpp-linter --manifest-path cpp-linter-lib/Cargo.toml -- {{ args }} + +# The tool parameter can be set to 'cross' when cross compiling. + +# build the native binary +[group("bin")] +build *args='': + cargo build --bin cpp-linter --manifest-path cpp-linter-lib/Cargo.toml {{ args }} + +# run clippy and rustfmt +lint: + cargo clippy --allow-staged --allow-dirty --fix + cargo fmt + +# bump version in root Cargo.toml +bump component='patch': + @python .github/workflows/bump_version.py {{ component }} From b10a034d2e433b969bc841d091f5feef7e6c41ae Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Mon, 19 Aug 2024 16:18:00 -0700 Subject: [PATCH 2/2] configure vscode tasks --- .github/workflows/bump_version.py | 33 ++--- .gitignore | 4 +- .vscode/extensions.json | 8 ++ .vscode/tasks.json | 222 ++++++++++++++++++++++++++++++ cpp_linter_rs.code-workspace | 15 -- 5 files changed, 250 insertions(+), 32 deletions(-) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/tasks.json delete mode 100644 cpp_linter_rs.code-workspace diff --git a/.github/workflows/bump_version.py b/.github/workflows/bump_version.py index 4c02256..e6d499a 100644 --- a/.github/workflows/bump_version.py +++ b/.github/workflows/bump_version.py @@ -5,34 +5,35 @@ VER_PATTERN = re.compile(r'^version = "(\d+)\.(\d+)\.(\d+)[^"]*" # auto', re.MULTILINE) VER_REPLACE = 'version = "%d.%d.%d" # auto' -COMPONENTS = ["major", "minor", "patch"] +COMPONENTS = ("major", "minor", "patch") -class Args(argparse.Namespace): +class Updater: component: str = "patch" - -def replace(match: re.Match | None) -> str: - assert match is not None, "Could not find version in Cargo.toml" - ver = [int(x) for x in match.groups()[:3]] - assert len(ver) == 3 - print("old version:", ".".join([str(x) for x in ver])) - index = COMPONENTS.index(Args.component) - ver[index] += 1 - for i in range(index + 1, 3): - ver[i] = 0 - print("new version:", ".".join([str(x) for x in ver])) - return VER_REPLACE % tuple(ver) + @staticmethod + def replace(match: re.Match[str]) -> str: + ver = [int(x) for x in match.groups()[: len(COMPONENTS)]] + for _ in range(len(ver) - 1, len(COMPONENTS)): + ver.append(0) + print("old version:", ".".join([str(x) for x in ver])) + index = COMPONENTS.index(Updater.component) + ver[index] += 1 + for i in range(index + 1, 3): + ver[i] = 0 + print("new version:", ".".join([str(x) for x in ver])) + return VER_REPLACE % tuple(ver) def main(): parser = argparse.ArgumentParser() parser.add_argument("component", default="patch", choices=COMPONENTS) - parser.parse_args(namespace=Args) + parser.parse_args(namespace=Updater) cargo_path = Path("Cargo.toml") doc = cargo_path.read_text(encoding="utf-8") - doc = VER_PATTERN.sub(replace, doc) + doc = VER_PATTERN.sub(Updater.replace, doc) cargo_path.write_text(doc, encoding="utf-8", newline="\n") + print("Updated version in Cargo.toml") return 0 diff --git a/.gitignore b/.gitignore index 42d5bae..5f9bc31 100644 --- a/.gitignore +++ b/.gitignore @@ -191,7 +191,9 @@ debug/ # End of https://www.toptal.com/developers/gitignore/api/rust,python # ignore vscode stuff -.vscode/ +.vscode/** +!.vscode/tasks.json +!.vscode/extensions.json # mdbook builds book diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..5dfb37f --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,8 @@ +{ + "recommendations": [ + "rust-lang.rust-analyzer", + "streetsidesoftware.code-spell-checker", + "fill-labs.dependi", + "wolfmah-vscode.just-syntax" + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..c775e0c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,222 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cargo", + "command": "run", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo run cpp-linter", + "args": [ + "--bin", + "cpp-linter", + "--manifest-path", + "cpp-linter-lib/Cargo.toml", + "--", + "${input:binArgs}" + ], + "group": "build" + }, + { + "type": "cargo", + "command": "clippy", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo clippy", + "args": [ + "--fix", + "--allow-dirty", + "--allow-staged" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "type": "cargo", + "command": "fmt", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo fmt", + "args": [], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": "build" + }, + { + "type": "cargo", + "command": "llvm-cov", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo llvm-cov nextest", + "args": [ + "--no-report", + "nextest", + "--lib", + "--manifest-path", + "cpp-linter-lib/Cargo.toml" + ], + "group": { + "kind": "test", + "isDefault": true + } + }, + { + "type": "cargo", + "command": "llvm-cov", + "problemMatcher": [], + "label": "rust: cargo llvm-cov report json", + "args": [ + "report", + "--json", + "--output-path", + "coverage.json" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": "test" + }, + { + "type": "shell", + "command": "llvm-cov-pretty", + "problemMatcher": [], + "label": "rust: cargo llvm-cov-pretty html", + "args": [ + "coverage.json", + "--open" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": "test" + }, + { + "type": "cargo", + "command": "llvm-cov", + "problemMatcher": [], + "label": "rust: cargo llvm-cov report html", + "args": [ + "report", + "--html", + "--open" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": "test" + }, + { + "type": "cargo", + "command": "llvm-cov", + "problemMatcher": [], + "label": "rust: cargo llvm-cov report lcov", + "args": [ + "report", + "--lcov", + "--output-path", + "lcov.info" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "group": "test" + }, + { + "type": "cargo", + "command": "doc", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo doc", + "args": [ + "--no-deps", + "--lib", + "--manifest-path", + "cpp-linter-lib/Cargo.toml", + "--open" + ], + "group": "build" + }, + { + "type": "shell", + "command": "mdbook", + "label": "mdbook: serve", + "args": [ + "serve", + "docs", + "--open" + ], + "problemMatcher": [], + "group": "build" + }, + { + "type": "shell", + "command": "mdbook", + "label": "mdbook: build", + "args": [ + "build", + "docs", + "--open" + ], + "presentation": { + "close": true, + "revealProblems": "onProblem", + "clear": true + }, + "problemMatcher": [], + "group": "build" + }, + { + "type": "shell", + "command": "python", + "label": "workspace: bump version", + "args": [ + ".github/workflows/bump_version.py", + "${input:bumpComponent}" + ], + "problemMatcher": [], + "group": "none" + } + ], + "inputs": [ + { + "type": "promptString", + "id": "binArgs", + "description": "Arguments for the cpp-linter binary executable", + "default": "-h" + }, + { + "type": "pickString", + "id": "bumpComponent", + "description": "Which version component to bump?", + "default": "patch", + "options": [ + "major", + "minor", + "patch" + ] + } + ] +} diff --git a/cpp_linter_rs.code-workspace b/cpp_linter_rs.code-workspace deleted file mode 100644 index 9d0d9d4..0000000 --- a/cpp_linter_rs.code-workspace +++ /dev/null @@ -1,15 +0,0 @@ -{ - "folders": [ - { - "path": "." - } - ], - "settings": {}, - "extensions": { - "recommendations": [ - "rust-lang.rust-analyzer", - "streetsidesoftware.code-spell-checker", - "fill-labs.dependi", - ] - } -}