From 25494e9efd313819227d08ecc721af2969c53cac Mon Sep 17 00:00:00 2001 From: dorayx Date: Mon, 4 Sep 2023 02:54:23 +0800 Subject: [PATCH] feat(day01): initial monorepo using bazel with a rust example --- .bazelversion | 1 + .github/workflows/test.yml | 20 ++++++++++++++++++++ .gitignore | 26 ++++++++++++++++++++++++++ README.md | 5 +++++ WORKSPACE.bazel | 22 ++++++++++++++++++++++ challenges/day-01/BUILD.bazel | 26 ++++++++++++++++++++++++++ challenges/day-01/Cargo.lock | 7 +++++++ challenges/day-01/Cargo.toml | 8 ++++++++ challenges/day-01/README.md | 28 ++++++++++++++++++++++++++++ challenges/day-01/src/main.rs | 11 +++++++++++ challenges/day-01/src/substring.rs | 24 ++++++++++++++++++++++++ 11 files changed, 178 insertions(+) create mode 100644 .bazelversion create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 WORKSPACE.bazel create mode 100644 challenges/day-01/BUILD.bazel create mode 100644 challenges/day-01/Cargo.lock create mode 100644 challenges/day-01/Cargo.toml create mode 100644 challenges/day-01/README.md create mode 100644 challenges/day-01/src/main.rs create mode 100644 challenges/day-01/src/substring.rs diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..91e4a9f --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +6.3.2 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e3524e9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: Test + +on: + workflow_dispatch: + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: bazelbuild/setup-bazelisk@v2 + - uses: actions/cache@v3 + with: + path: "~/.cache/bazel" + key: bazel + - run: bazel build //... + - run: bazel test //... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72bacc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +## IDE +.idea + +## Bazel +# Ignore all bazel-* symlinks. There is no full list since this can change +# based on the name of the directory bazel is cloned into. +/bazel-* + +# Directories for the Bazel IntelliJ plugin containing the generated +# IntelliJ project files and plugin configuration. Seperate directories are +# for the IntelliJ, Android Studio and CLion versions of the plugin. +/.ijwb/ +/.aswb/ +/.clwb/ + +## Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/README.md b/README.md new file mode 100644 index 0000000..a80f684 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# 15-Day Coding Challenges + +| Day | Date | Challenge | Link | +|-----|------------|----------------------------------------------------------------------------------------------------------------|-------------------------------------------| +| 01 | 3 Sep 2023 | Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects | [challenges/day-01](./challenges/day-01) | diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel new file mode 100644 index 0000000..3f6ba78 --- /dev/null +++ b/WORKSPACE.bazel @@ -0,0 +1,22 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# Loads the `rules_rust` repository rule +# See https://github.com/bazelbuild/rules_rust/releases +http_archive( + name = "rules_rust", + sha256 = "db89135f4d1eaa047b9f5518ba4037284b43fc87386d08c1d1fe91708e3730ae", + urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.27.0/rules_rust-v0.27.0.tar.gz"], +) + +# Loads the `rules_rust_dependencies` and `rust_register_toolchains` function definitions +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") + +# Adds the necessary dependencies for the Rust rules +rules_rust_dependencies() + +# Registers Rust toolchains with the given versions and editions +rust_register_toolchains( + # Specifies the Rust edition to use for the registered toolchains + edition = "2021", + versions = ["1.72.0"], +) diff --git a/challenges/day-01/BUILD.bazel b/challenges/day-01/BUILD.bazel new file mode 100644 index 0000000..5cdd189 --- /dev/null +++ b/challenges/day-01/BUILD.bazel @@ -0,0 +1,26 @@ +# Loads the Rust rules and the `rust_binary` & `rust_test` function definitions +load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") + +# Declares a Rust binary target with the given name +rust_binary( + name = "day_01", + + # Specifies the source file for the binary + srcs = glob([ + "src/**/*.rs", + ]), + + # Specifies the Rust edition to use for this binary + edition = "2021", +) + +# Declares a Rust test target with the given name +rust_test( + name = "day_01_test", + + # Uses the same crate name as the underlying “:day_01” crate + crate = ":day_01", + + # Specifies the Rust edition to use for this test + edition = "2021", +) diff --git a/challenges/day-01/Cargo.lock b/challenges/day-01/Cargo.lock new file mode 100644 index 0000000..7dc86fc --- /dev/null +++ b/challenges/day-01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day-01" +version = "0.1.0" diff --git a/challenges/day-01/Cargo.toml b/challenges/day-01/Cargo.toml new file mode 100644 index 0000000..ac3eb44 --- /dev/null +++ b/challenges/day-01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day-01" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/challenges/day-01/README.md b/challenges/day-01/README.md new file mode 100644 index 0000000..bf0ada0 --- /dev/null +++ b/challenges/day-01/README.md @@ -0,0 +1,28 @@ +# Day 01 + +## Challenge description + +Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects. + +- [x] Monorepo +- [x] Bazel +- [x] Rust program +- [x] GitHub Action + +## Getting Started + +```bash +bazel run //challenges/day-01:day_01 +bazel test //challenges/day-01:day_01_test +``` + +## References + +- Doc: [Bazelisk](https://github.com/bazelbuild/bazelisk/blob/master/README.md) +- Doc: [Bazel Versions](https://bazel.build/release#support-matrix) +- Doc: [GitHub Action - bazelbuild/setup-bazelisk](https://github.com/bazelbuild/setup-bazelisk) +- API: [rust_binary](http://bazelbuild.github.io/rules_rust/flatten.html#rust_binary) +- API: [rust_test](http://bazelbuild.github.io/rules_rust/flatten.html#rust_test) +- Article: [kriscfoster/multi-language-bazel-monorepo](https://github.com/kriscfoster/multi-language-bazel-monorepo) +- Article: [Bazel with Rust](https://earthly.dev/blog/bazel-with-rust/) +- Article: [Building Rust Workspace with Bazel](https://www.tweag.io/blog/2023-07-27-building-rust-workspace-with-bazel/) diff --git a/challenges/day-01/src/main.rs b/challenges/day-01/src/main.rs new file mode 100644 index 0000000..0674297 --- /dev/null +++ b/challenges/day-01/src/main.rs @@ -0,0 +1,11 @@ +mod substring; + +fn main() { + let s = "Hello, World!"; + + let substring = substring::find_substring(s, "World"); + println!("substring: {:?}", substring); + + let new_string = substring::replace_substring(s, "World", "Rust"); + println!("New string: {}", new_string); +} diff --git a/challenges/day-01/src/substring.rs b/challenges/day-01/src/substring.rs new file mode 100644 index 0000000..7c4bc2d --- /dev/null +++ b/challenges/day-01/src/substring.rs @@ -0,0 +1,24 @@ +pub fn find_substring<'a>(s: &'a str, substring: &str) -> Option<&'a str> { + s.find(substring).map(|index| &s[index..index + substring.len()]) +} + +pub fn replace_substring(s: &str, from: &str, to: &str) -> String { + s.replace(from, to) +} + +#[cfg(test)] +mod tests { + use super::{find_substring, replace_substring}; + + #[test] + fn test_find_substring() { + assert_eq!(find_substring("hello", "ll"), Some("ll")); + assert_eq!(find_substring("hello", "world"), None); + } + + #[test] + fn test_replace_substring() { + assert_eq!(replace_substring("hello", "ll", "ww"), "hewwo"); + assert_eq!(replace_substring("hello", "world", "ww"), "hello"); + } +}