From 872d4406d9a7dabd5663859a2872a2ce69fc11b3 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 12 Nov 2024 22:09:11 +0800 Subject: [PATCH] feat: initialize Signed-off-by: tison --- .cargo/config.toml | 19 ++++ .editorconfig | 11 +++ .github/semantic.yml | 42 ++++++++ .github/workflows/ci.yml | 87 +++++++++++++++++ .gitignore | 2 + Cargo.toml | 37 +++++++ LICENSE | 201 ++++++++++++++++++++++++++++++++++++++ README.md | 7 ++ licenserc.toml | 21 ++++ rust-toolchain.toml | 17 ++++ rustfmt.toml | 19 ++++ taplo.toml | 44 +++++++++ template/Cargo.toml | 29 ++++++ template/src/lib.rs | 13 +++ typos.toml | 18 ++++ xtask/Cargo.toml | 34 +++++++ xtask/src/main.rs | 202 +++++++++++++++++++++++++++++++++++++++ 17 files changed, 803 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .editorconfig create mode 100644 .github/semantic.yml create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 licenserc.toml create mode 100644 rust-toolchain.toml create mode 100644 rustfmt.toml create mode 100644 taplo.toml create mode 100644 template/Cargo.toml create mode 100644 template/src/lib.rs create mode 100644 typos.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bd307c3 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,19 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[alias] +x = "run --package x --" + +[env] +CARGO_WORKSPACE_DIR = { value = "", relative = true } diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..fd95893 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.toml] +indent_size = tab +tab_width = 2 diff --git a/.github/semantic.yml b/.github/semantic.yml new file mode 100644 index 0000000..2d005b9 --- /dev/null +++ b/.github/semantic.yml @@ -0,0 +1,42 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The pull request's title should be fulfilled the following pattern: +# +# [optional scope]: +# +# ... where valid types and scopes can be found below; for example: +# +# build(maven): One level down for native profile +# +# More about configurations on https://github.com/Ezard/semantic-prs#configuration + +enabled: true + +titleOnly: true + +types: + - feat + - fix + - docs + - style + - refactor + - perf + - test + - build + - ci + - chore + - revert + +targetUrl: https://github.com/fast/template/blob/main/.github/semantic.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0ee9eed --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,87 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: CI +on: + pull_request: + branches: [ main ] + push: + branches: [ main ] + +# Concurrency strategy: +# github.workflow: distinguish this workflow from others +# github.event_name: distinguish `push` event from `pull_request` event +# github.event.number: set to the number of the pull request if `pull_request` event +# github.run_id: otherwise, it's a `push` event, only cancel if we rerun the workflow +# +# Reference: +# https://docs.github.com/en/actions/using-jobs/using-concurrency +# https://docs.github.com/en/actions/learn-github-actions/contexts#github-context +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.number || github.run_id }} + cancel-in-progress: true + +jobs: + check: + name: Check + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Install toolchain + uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt,clippy + - uses: Swatinem/rust-cache@v2 + - uses: taiki-e/install-action@v2 + with: + tool: typos-cli,taplo-cli,hawkeye + - run: cargo +nightly x lint + + test: + name: Run tests + strategy: + matrix: + os: [ ubuntu-22.04, macos-14, windows-2022 ] + rust-version: [ "1.80.0", "stable" ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + - name: Delete rust-toolchain.toml + run: rm rust-toolchain.toml + - name: Install toolchain + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust-version }} + - name: Run unit tests + run: cargo x test --no-capture + shell: bash + + required: + name: Required + runs-on: ubuntu-22.04 + if: ${{ always() }} + needs: + - check + - test + steps: + - name: Guardian + run: | + if [[ ! ( \ + "${{ needs.check.result }}" == "success" \ + && "${{ needs.test.result }}" == "success" \ + ) ]]; then + echo "Required jobs haven't been completed successfully." + exit -1 + fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d074c09 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,37 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[workspace] +members = ["template", "xtask"] +resolver = "2" + +[workspace.package] +edition = "2021" +homepage = "https://github.com/fast/template" +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/fast/template" +rust-version = "1.80.0" + +[workspace.lints.rust] +unknown_lints = "deny" + +[workspace.lints.clippy] +dbg_macro = "deny" + +[workspace.metadata.release] +pre-release-commit-message = "chore: release v{{version}}" +shared-version = true +sign-tag = true +tag-name = "v{{version}}" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d6c9c1 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Fast template for developing a new Rust project + +Use this repository as a GitHub template to quickly start a new Rust project. + +## License + +This project is licensed under [Apache License, Version 2.0](LICENSE). diff --git a/licenserc.toml b/licenserc.toml new file mode 100644 index 0000000..2b80854 --- /dev/null +++ b/licenserc.toml @@ -0,0 +1,21 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +headerPath = "Apache-2.0.txt" + +includes = ['**/*.proto', '**/*.rs', '**/*.yml', '**/*.yaml', '**/*.toml'] + +[properties] +copyrightOwner = "FastLabs Developers" +inceptionYear = 2024 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..9f15326 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,17 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[toolchain] +channel = "stable" +components = ["cargo", "rustfmt", "clippy", "rust-analyzer"] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..4ca11ce --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,19 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +comment_width = 120 +format_code_in_doc_comments = true +group_imports = "StdExternalCrate" +imports_granularity = "Item" +wrap_comments = true diff --git a/taplo.toml b/taplo.toml new file mode 100644 index 0000000..8720420 --- /dev/null +++ b/taplo.toml @@ -0,0 +1,44 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include = ["Cargo.toml", "**/*.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 = true +# 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 = 80 +# 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 = true +# 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 diff --git a/template/Cargo.toml b/template/Cargo.toml new file mode 100644 index 0000000..ea5fd60 --- /dev/null +++ b/template/Cargo.toml @@ -0,0 +1,29 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[package] +name = "template" +version = "0.1.0" + +edition.workspace = true +homepage.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] + +[lints] +workspace = true diff --git a/template/src/lib.rs b/template/src/lib.rs new file mode 100644 index 0000000..97860a8 --- /dev/null +++ b/template/src/lib.rs @@ -0,0 +1,13 @@ +// Copyright 2024 FastLabs Developers +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/typos.toml b/typos.toml new file mode 100644 index 0000000..55bd36b --- /dev/null +++ b/typos.toml @@ -0,0 +1,18 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[default.extend-words] + +[files] +extend-exclude = [] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..aaefba8 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,34 @@ +# Copyright 2024 FastLabs Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +[package] +name = "x" +publish = false + +edition.workspace = true +homepage.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +rust-version.workspace = true + +[package.metadata.release] +release = false + +[dependencies] +clap = { version = "4.5.20", features = ["derive"] } +which = { version = "7.0.0" } + +[lints] +workspace = true diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..8f0513b --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,202 @@ +// Copyright 2024 FastLabs Developers +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::process::Command as StdCommand; + +use clap::Parser; +use clap::Subcommand; + +#[derive(Parser)] +struct Command { + #[clap(subcommand)] + sub: SubCommand, +} + +impl Command { + fn run(self) { + match self.sub { + SubCommand::Build(cmd) => cmd.run(), + SubCommand::Lint(cmd) => cmd.run(), + SubCommand::Test(cmd) => cmd.run(), + } + } +} + +#[derive(Subcommand)] +enum SubCommand { + #[clap(about = "Compile workspace packages.")] + Build(CommandBuild), + #[clap(about = "Run format and clippy checks.")] + Lint(CommandLint), + #[clap(about = "Run unit tests.")] + Test(CommandTest), +} + +#[derive(Parser)] +struct CommandBuild { + #[arg(long, help = "Assert that `Cargo.lock` will remain unchanged.")] + locked: bool, +} + +impl CommandBuild { + fn run(self) { + run_command(make_build_cmd(self.locked)); + } +} + +#[derive(Parser)] +struct CommandTest { + #[arg(long, help = "Run tests serially and do not capture output.")] + no_capture: bool, +} + +impl CommandTest { + fn run(self) { + run_command(make_test_cmd(self.no_capture, true, &[])); + } +} + +#[derive(Parser)] +#[clap(name = "lint")] +struct CommandLint { + #[arg(long, help = "Automatically apply lint suggestions.")] + fix: bool, +} + +impl CommandLint { + fn run(self) { + run_command(make_clippy_cmd(self.fix)); + run_command(make_format_cmd(self.fix)); + run_command(make_taplo_cmd(self.fix)); + run_command(make_typos_cmd()); + run_command(make_hawkeye_cmd(self.fix)); + } +} + +fn find_command(cmd: &str) -> StdCommand { + match which::which(cmd) { + Ok(exe) => { + let mut cmd = StdCommand::new(exe); + cmd.current_dir(env!("CARGO_WORKSPACE_DIR")); + cmd + } + Err(err) => { + panic!("{cmd} not found: {err}"); + } + } +} + +fn ensure_installed(bin: &str, crate_name: &str) { + if which::which(bin).is_err() { + let mut cmd = find_command("cargo"); + cmd.args(["install", crate_name]); + run_command(cmd); + } +} + +fn run_command(mut cmd: StdCommand) { + println!("{cmd:?}"); + let status = cmd.status().expect("failed to execute process"); + assert!(status.success(), "command failed: {status}"); +} + +fn make_build_cmd(locked: bool) -> StdCommand { + let mut cmd = find_command("cargo"); + cmd.args([ + "build", + "--workspace", + "--all-features", + "--tests", + "--examples", + "--benches", + "--bins", + ]); + if locked { + cmd.arg("--locked"); + } + cmd +} + +fn make_test_cmd(no_capture: bool, default_features: bool, features: &[&str]) -> StdCommand { + let mut cmd = find_command("cargo"); + cmd.args(["test", "--workspace"]); + if !default_features { + cmd.arg("--no-default-features"); + } + if !features.is_empty() { + cmd.args(["--features", features.join(",").as_str()]); + } + if no_capture { + cmd.args(["--", "--nocapture"]); + } + cmd +} + +fn make_format_cmd(fix: bool) -> StdCommand { + let mut cmd = find_command("cargo"); + cmd.args(["fmt", "--all"]); + if !fix { + cmd.arg("--check"); + } + cmd +} + +fn make_clippy_cmd(fix: bool) -> StdCommand { + let mut cmd = find_command("cargo"); + cmd.args([ + "clippy", + "--tests", + "--all-features", + "--all-targets", + "--workspace", + ]); + if fix { + cmd.args(["--allow-staged", "--allow-dirty", "--fix"]); + } else { + cmd.args(["--", "-D", "warnings"]); + } + cmd +} + +fn make_hawkeye_cmd(fix: bool) -> StdCommand { + ensure_installed("hawkeye", "hawkeye"); + let mut cmd = find_command("hawkeye"); + if fix { + cmd.args(["format", "--fail-if-updated=false"]); + } else { + cmd.args(["check"]); + } + cmd +} + +fn make_typos_cmd() -> StdCommand { + ensure_installed("typos", "typos-cli"); + find_command("typos") +} + +fn make_taplo_cmd(fix: bool) -> StdCommand { + ensure_installed("taplo", "taplo-cli"); + let mut cmd = find_command("taplo"); + if fix { + cmd.args(["format"]); + } else { + cmd.args(["format", "--check"]); + } + cmd +} + +fn main() { + let cmd = Command::parse(); + cmd.run() +}