From 9374b368c54486b01afad93d088445e0d1a0fc1b Mon Sep 17 00:00:00 2001 From: RakuJa Date: Mon, 11 Nov 2024 20:50:03 +0100 Subject: [PATCH] feat: revamp build system (#79) * download db before compile time automatically, using cargo-make * refactor docker build * solve CI failure --- .github/workflows/push.yml | 23 ++++++------- Cargo.toml | 2 +- Dockerfile | 5 ++- Makefile.toml | 62 ++++++++++++++++++++++++++++++++++ README.md | 28 ++++++++------- build/creature_core_db_init.rs | 2 +- setup.py | 47 ++++++++++++++++++++++++++ src/services/shop_service.rs | 8 ++--- 8 files changed, 144 insertions(+), 33 deletions(-) create mode 100644 Makefile.toml create mode 100644 setup.py diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 5f914e0..9bb7656 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -8,19 +8,16 @@ permissions: contents: read jobs: - clippy_check: + format_and_lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Run Clippy - run: cargo clippy --all-targets --all-features - format: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - components: rustfmt - - uses: mbrobbel/rustfmt-check@master + - uses: actions-rs/toolchain@v1 with: - token: ${{ secrets.GITHUB_TOKEN }} + toolchain: stable + override: true + - uses: davidB/rust-cargo-make@v1 + - uses: actions/checkout@v1 + - name: Run rustfmt and clippy executing setup if needed + run: cargo make format-and-lint + env: + DATABASE_URL: "sqlite://database.db" diff --git a/Cargo.toml b/Cargo.toml index d8a3bec..a1b9091 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ anyhow = "1.0.93" serde = { version = "1.0.214", features = ["derive"] } serde_json = "1.0.132" strum = {version="0.26.3", features = ["derive"]} -fastrand = "2.1.1" +fastrand = "2.2.0" counter = "0.6.0" ordered-float = { version = "4", features = ["serde"]} num-traits = "0.2.19" diff --git a/Dockerfile b/Dockerfile index 5de8d4a..09d6eb7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,8 +13,11 @@ RUN apk add build-base RUN apk add musl-dev +RUN apk add python3 + # Build the project with optimizations -RUN cargo build --target x86_64-unknown-linux-musl --release +RUN cargo install --no-default-features --force cargo-make +RUN cargo make bybe-docker-release # Stage 2: Create a minimal runtime image FROM alpine:latest diff --git a/Makefile.toml b/Makefile.toml new file mode 100644 index 0000000..875268d --- /dev/null +++ b/Makefile.toml @@ -0,0 +1,62 @@ +[tasks.clean] +command = "cargo" +args = ["clean"] + +[tasks.prebuild] +command = "python3" +args = ["setup.py", "--db_version", "2.3.0"] + +[tasks.format] +install_crate = "rustfmt" +command = "cargo" +args = ["fmt"] + +[tasks.lint] +command = "cargo" +args = ["clippy", "--all-targets", "--all-features"] +dependencies = ["clean", "prebuild"] + +[tasks.build] +command = "cargo" +args = ["build"] +dependencies = ["clean", "prebuild"] + +[tasks.build-release] +command = "cargo" +args = ["build", "--release"] +dependencies = ["clean", "prebuild"] + +[tasks.build-docker-release] +command = "cargo" +args = ["build", "--target", "x86_64-unknown-linux-musl", "--release"] +dependencies = ["clean", "prebuild"] + +[tasks.test] +command = "cargo" +args = ["test"] +dependencies = ["clean", "prebuild"] + +[tasks.bybe-build] +dependencies = [ + "format", + "build", + "clippy", + "test" +] + +[tasks.bybe-release] +dependencies = [ + "test", + "build-release" +] + +[tasks.bybe-docker-release] +dependencies = [ + "build-docker-release" +] + +[tasks.format-and-lint] +dependencies = [ + "format", + "lint" +] diff --git a/README.md b/README.md index f64dd5e..4492b9c 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,7 @@ ) ![SQLite](https://img.shields.io/badge/sqlite-%2307405e.svg?style=for-the-badge&logo=sqlite&logoColor=white) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) - - -[![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-orange.svg)](https://sonarcloud.io/summary/new_code?id=RakuJa_BYBE) +[![Built with cargo-make](https://sagiegurari.github.io/cargo-make/assets/badges/cargo-make.svg)](https://sagiegurari.github.io/cargo-make) # BYBE - Backend @@ -34,30 +32,34 @@ Built using: 2. Populate the SQLite database (public release date TBA). 3. Clone this repository: -``` +```bash git clone https://github.com/RakuJa/BYBE ``` 4. Navigate to the project's main directory. -5. Build the project: - +5. Build the project running all the tests and downloading the db (required only once): +```bash +cargo make bybe-build ``` +6. Build the project + +```bash cargo build ``` 6. Set DATABASE_URL variable to SQLite db path 7. Run the backend in development mode: -``` +```bash cargo run ``` 8. To instead deploy the production build, run: -``` +```bash cargo build --release ``` -``` +```bash cargo run ``` @@ -65,17 +67,17 @@ cargo run 1. Install Docker on your local machine 2. Clone the repository or download the ZIP -``` +```bash git clone https://github.com/RakuJa/BYBE ``` 3. Go to the local BYBE project folder 4. Build docker image of bybe using -``` +```bash docker build -t bybe . ``` 5. Run the image -``` +```bash docker run -p 25566:25566 --name bybe-container bybe ``` @@ -87,5 +89,5 @@ If you like this tool, consider supporting me: Also consider supporting [TheAsel](https://github.com/TheAsel), the frontend developer. Thank you! -## BYBE - Client +## BYBE-Portable If you were looking for the BYBE Local Application, it can be found [Here](https://github.com/rakuJa/BYBE-desktop) diff --git a/build/creature_core_db_init.rs b/build/creature_core_db_init.rs index c10204c..b3004a1 100644 --- a/build/creature_core_db_init.rs +++ b/build/creature_core_db_init.rs @@ -24,7 +24,7 @@ pub async fn create_creature_core_table(conn: &Pool) -> Result<()> { source TEXT NOT NULL DEFAULT '', remaster BOOL NOT NULL DEFAULT 0, alignment TEXT NOT NULL DEFAULT NO - )" + )", ) .execute(conn) .await?; diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..fc9d09e --- /dev/null +++ b/setup.py @@ -0,0 +1,47 @@ +# setup.py +import os +import urllib.request +import getopt, sys +from typing import Optional + +# Remove 1st argument from the +# list of command line arguments +argumentList = sys.argv[1:] + +# Options +options = "hd:" + +# Long options +long_options = ["help", "db_version="] + +def handle_command_line_arguments() -> Optional[str]: + try: + # Parsing argument + arguments, values = getopt.getopt(argumentList, options, long_options) + + # checking each argument + for currentArgument, currentValue in arguments: + + if currentArgument in ("-h", "--help"): + print("This script downloads or creates necessary file to build BYBE. \n" + "Should be executed the first time the project is built in the machine or when resetting the database \n" + "Pass the --db_version or -v argument to input a specific BYBE-DB version to download (>= 2.3.0)") + elif currentArgument in ("-d", "--db_version"): + return currentValue + except getopt.error: + pass + +def main(): + # Check if the file already exists or needs downloading + db_version: str = handle_command_line_arguments() or "2.3.0" + print(db_version) + remote_url: str = f"https://github.com/RakuJa/BYBE-DB/releases/download/v{db_version}/database.db" + destination_file: str = "database.db" + if not os.path.exists(destination_file): + print("Downloading the database file...") + urllib.request.urlretrieve(remote_url, destination_file) + else: + print("Database file already exists, skipping download.") + +if __name__ == '__main__': + main() diff --git a/src/services/shop_service.rs b/src/services/shop_service.rs index 74a6e64..92ab770 100644 --- a/src/services/shop_service.rs +++ b/src/services/shop_service.rs @@ -221,7 +221,7 @@ mod tests { #[case] expected: (i64, i64, i64, i64), ) { let result = calculate_n_of_equippable_values(input_n_of_equippables, input_percentages); - assert_eq!(true, result.is_ok()); + assert!(result.is_ok()); assert_eq!(expected, result.unwrap()); } @@ -233,7 +233,7 @@ mod tests { #[case] expected: (i64, i64, i64, i64), ) { let result = calculate_n_of_equippable_values(input_n_of_equippables, input_percentages); - assert_eq!(true, result.is_ok()); + assert!(result.is_ok()); assert_eq!(expected, result.unwrap()); } @@ -246,7 +246,7 @@ mod tests { #[case] expected: (i64, i64, i64, i64), ) { let result = calculate_n_of_equippable_values(input_n_of_equippables, input_percentages); - assert_eq!(true, result.is_ok()); + assert!(result.is_ok()); assert_eq!(expected, result.unwrap()); } @@ -260,7 +260,7 @@ mod tests { #[case] expected: (i64, i64, i64, i64), ) { let result = calculate_n_of_equippable_values(input_n_of_equippables, input_percentages); - assert_eq!(true, result.is_ok()); + assert!(result.is_ok()); assert_eq!(expected, result.unwrap()); } }