-
Notifications
You must be signed in to change notification settings - Fork 15
feat: added xtasks fetch and code-gen to generate code
#51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e252637
feat: added xtasks `fetch` and `code-gen` to generate code from opena…
haydenhoang 7fa1e58
fix `Cargo.toml` package metadata
haydenhoang c1739da
lint fix
haydenhoang ea93b0a
fix: avoid compiling xtask for wasm
haydenhoang 741b11a
fix fmt and typesense/lib.rs docs test
haydenhoang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [alias] | ||
| xtask = "run --package xtask --" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| /target | ||
| Cargo.lock | ||
| .env | ||
|
|
||
| /typesense-data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| services: | ||
| typesense: | ||
| image: typesense/typesense:29.0 | ||
| restart: on-failure | ||
| ports: | ||
| - '8108:8108' | ||
| volumes: | ||
| - ./typesense-data:/data | ||
| command: '--data-dir /data --api-key=xyz --enable-cors' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "xtask" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
morenol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [dependencies] | ||
| reqwest = { version = "0.11", features = ["blocking"] } # "blocking" is simpler for scripts | ||
| anyhow = "1.0" | ||
| clap = { version = "4.0", features = ["derive"] } | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_yaml = "0.9" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use anyhow::{Context, Result}; | ||
| use clap::{Parser, ValueEnum}; | ||
| use std::env; | ||
| use std::fs; | ||
| use std::process::Command; | ||
| mod preprocess_openapi; | ||
| use preprocess_openapi::preprocess_openapi_file; | ||
|
|
||
| const SPEC_URL: &str = | ||
| "https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml"; | ||
|
|
||
| // Input spec file, expected in the project root. | ||
| const INPUT_SPEC_FILE: &str = "openapi.yml"; | ||
| const OUTPUT_PREPROCESSED_FILE: &str = "./preprocessed_openapi.yml"; | ||
|
|
||
| // Output directory for the generated code. | ||
| const OUTPUT_DIR: &str = "typesense_codegen"; | ||
|
|
||
| #[derive(Parser)] | ||
| #[command( | ||
| author, | ||
| version, | ||
| about = "A task runner for the typesense-rust project" | ||
| )] | ||
| struct Cli { | ||
| /// The list of tasks to run in sequence. | ||
| #[arg(required = true, value_enum)] | ||
| tasks: Vec<Task>, | ||
| } | ||
|
|
||
| #[derive(ValueEnum, Clone, Debug)] | ||
| #[clap(rename_all = "kebab-case")] // Allows us to type `code-gen` instead of `CodeGen` | ||
| enum Task { | ||
| /// Fetches the latest OpenAPI spec from [the Typesense repository](https://github.com/typesense/typesense-api-spec/blob/master/openapi.yml). | ||
| Fetch, | ||
| /// Generates client code from the spec file using the Docker container. | ||
| CodeGen, | ||
| } | ||
|
|
||
| fn main() -> Result<()> { | ||
| let cli = Cli::parse(); | ||
|
|
||
| for task in cli.tasks { | ||
| println!("▶️ Running task: {:?}", task); | ||
| match task { | ||
| Task::Fetch => task_fetch_api_spec()?, | ||
| Task::CodeGen => task_codegen()?, | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn task_fetch_api_spec() -> Result<()> { | ||
| println!("▶️ Running codegen task..."); | ||
|
|
||
| println!(" - Downloading spec from {}", SPEC_URL); | ||
| let response = | ||
| reqwest::blocking::get(SPEC_URL).context("Failed to download OpenAPI spec file")?; | ||
|
|
||
| if !response.status().is_success() { | ||
| anyhow::bail!("Failed to download spec: HTTP {}", response.status()); | ||
| } | ||
|
|
||
| let spec_content = response.text()?; | ||
| fs::write(INPUT_SPEC_FILE, spec_content) | ||
| .context(format!("Failed to write spec to {}", INPUT_SPEC_FILE))?; | ||
| println!(" - Spec saved to {}", INPUT_SPEC_FILE); | ||
|
|
||
| println!("✅ Fetch API spec task finished successfully."); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Task to generate client code from the OpenAPI spec using a Docker container. | ||
| fn task_codegen() -> Result<()> { | ||
| println!("▶️ Running codegen task via Docker..."); | ||
|
|
||
| println!("Preprocessing the Open API spec file..."); | ||
| preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE) | ||
| .expect("Preprocess failed, aborting!"); | ||
| // Get the absolute path to the project's root directory. | ||
| // std::env::current_dir() gives us the directory from which `cargo xtask` was run. | ||
| let project_root = env::current_dir().context("Failed to get current directory")?; | ||
|
|
||
| // Check if the input spec file exists before trying to run Docker. | ||
| let input_spec_path = project_root.join(INPUT_SPEC_FILE); | ||
| if !input_spec_path.exists() { | ||
| anyhow::bail!( | ||
| "Input spec '{}' not found in project root. Please add it before running.", | ||
| INPUT_SPEC_FILE | ||
| ); | ||
| } | ||
|
|
||
| // Construct the volume mount string for Docker. | ||
| // Docker needs an absolute path for the volume mount source. | ||
| // to_string_lossy() is used to handle potential non-UTF8 paths gracefully. | ||
| let volume_mount = format!("{}:/local", project_root.to_string_lossy()); | ||
| println!(" - Using volume mount: {}", volume_mount); | ||
|
|
||
| // Set up and run the Docker command. | ||
| println!(" - Starting Docker container..."); | ||
| let status = Command::new("docker") | ||
| .arg("run") | ||
| .arg("--rm") // Remove the container after it exits | ||
| .arg("-v") | ||
| .arg(volume_mount) // Mount the project root to /local in the container | ||
| .arg("openapitools/openapi-generator-cli") | ||
| .arg("generate") | ||
| .arg("-i") | ||
| .arg(format!("/local/{}", OUTPUT_PREPROCESSED_FILE)) // Input path inside the container | ||
| .arg("-g") | ||
| .arg("rust") | ||
| .arg("-o") | ||
| .arg(format!("/local/{}", OUTPUT_DIR)) // Output path inside the container | ||
| .arg("--additional-properties") | ||
| .arg("library=reqwest") | ||
| .arg("--additional-properties") | ||
| .arg("supportMiddleware=true") | ||
| .arg("--additional-properties") | ||
| .arg("useSingleRequestParameter=true") | ||
| // .arg("--additional-properties") | ||
| // .arg("useBonBuilder=true") | ||
| .status() | ||
| .context("Failed to execute Docker command. Is Docker installed and running?")?; | ||
|
|
||
| // Check if the command was successful. | ||
| if !status.success() { | ||
| anyhow::bail!("Docker command failed with status: {}", status); | ||
| } | ||
|
|
||
| println!("✅ Codegen task finished successfully."); | ||
| println!(" Generated code is available in '{}'", OUTPUT_DIR); | ||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.