|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use clap::{Parser, ValueEnum}; |
| 3 | +use std::env; |
| 4 | +use std::fs; |
| 5 | +use std::process::Command; |
| 6 | +mod preprocess_openapi; |
| 7 | +use preprocess_openapi::preprocess_openapi_file; |
| 8 | + |
| 9 | +const SPEC_URL: &str = |
| 10 | + "https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml"; |
| 11 | + |
| 12 | +// Input spec file, expected in the project root. |
| 13 | +const INPUT_SPEC_FILE: &str = "openapi.yml"; |
| 14 | +const OUTPUT_PREPROCESSED_FILE: &str = "./preprocessed_openapi.yml"; |
| 15 | + |
| 16 | +// Output directory for the generated code. |
| 17 | +const OUTPUT_DIR: &str = "typesense_codegen"; |
| 18 | + |
| 19 | +#[derive(Parser)] |
| 20 | +#[command( |
| 21 | + author, |
| 22 | + version, |
| 23 | + about = "A task runner for the typesense-rust project" |
| 24 | +)] |
| 25 | +struct Cli { |
| 26 | + /// The list of tasks to run in sequence. |
| 27 | + #[arg(required = true, value_enum)] |
| 28 | + tasks: Vec<Task>, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(ValueEnum, Clone, Debug)] |
| 32 | +#[clap(rename_all = "kebab-case")] // Allows us to type `code-gen` instead of `CodeGen` |
| 33 | +enum Task { |
| 34 | + /// Fetches the latest OpenAPI spec from [the Typesense repository](https://github.com/typesense/typesense-api-spec/blob/master/openapi.yml). |
| 35 | + Fetch, |
| 36 | + /// Generates client code from the spec file using the Docker container. |
| 37 | + CodeGen, |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(target_family = "wasm")] |
| 41 | +fn main() {} |
| 42 | + |
| 43 | +#[cfg(not(target_family = "wasm"))] |
| 44 | +fn main() -> Result<()> { |
| 45 | + let cli = Cli::parse(); |
| 46 | + |
| 47 | + for task in cli.tasks { |
| 48 | + println!("▶️ Running task: {:?}", task); |
| 49 | + match task { |
| 50 | + Task::Fetch => task_fetch_api_spec()?, |
| 51 | + Task::CodeGen => task_codegen()?, |
| 52 | + } |
| 53 | + } |
| 54 | + Ok(()) |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(not(target_family = "wasm"))] |
| 58 | +fn task_fetch_api_spec() -> Result<()> { |
| 59 | + println!("▶️ Running codegen task..."); |
| 60 | + |
| 61 | + println!(" - Downloading spec from {}", SPEC_URL); |
| 62 | + let response = |
| 63 | + reqwest::blocking::get(SPEC_URL).context("Failed to download OpenAPI spec file")?; |
| 64 | + |
| 65 | + if !response.status().is_success() { |
| 66 | + anyhow::bail!("Failed to download spec: HTTP {}", response.status()); |
| 67 | + } |
| 68 | + |
| 69 | + let spec_content = response.text()?; |
| 70 | + fs::write(INPUT_SPEC_FILE, spec_content) |
| 71 | + .context(format!("Failed to write spec to {}", INPUT_SPEC_FILE))?; |
| 72 | + println!(" - Spec saved to {}", INPUT_SPEC_FILE); |
| 73 | + |
| 74 | + println!("✅ Fetch API spec task finished successfully."); |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +/// Task to generate client code from the OpenAPI spec using a Docker container. |
| 80 | +fn task_codegen() -> Result<()> { |
| 81 | + println!("▶️ Running codegen task via Docker..."); |
| 82 | + |
| 83 | + println!("Preprocessing the Open API spec file..."); |
| 84 | + preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE) |
| 85 | + .expect("Preprocess failed, aborting!"); |
| 86 | + // Get the absolute path to the project's root directory. |
| 87 | + // std::env::current_dir() gives us the directory from which `cargo xtask` was run. |
| 88 | + let project_root = env::current_dir().context("Failed to get current directory")?; |
| 89 | + |
| 90 | + // Check if the input spec file exists before trying to run Docker. |
| 91 | + let input_spec_path = project_root.join(INPUT_SPEC_FILE); |
| 92 | + if !input_spec_path.exists() { |
| 93 | + anyhow::bail!( |
| 94 | + "Input spec '{}' not found in project root. Please add it before running.", |
| 95 | + INPUT_SPEC_FILE |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Construct the volume mount string for Docker. |
| 100 | + // Docker needs an absolute path for the volume mount source. |
| 101 | + // to_string_lossy() is used to handle potential non-UTF8 paths gracefully. |
| 102 | + let volume_mount = format!("{}:/local", project_root.to_string_lossy()); |
| 103 | + println!(" - Using volume mount: {}", volume_mount); |
| 104 | + |
| 105 | + // Set up and run the Docker command. |
| 106 | + println!(" - Starting Docker container..."); |
| 107 | + let status = Command::new("docker") |
| 108 | + .arg("run") |
| 109 | + .arg("--rm") // Remove the container after it exits |
| 110 | + .arg("-v") |
| 111 | + .arg(volume_mount) // Mount the project root to /local in the container |
| 112 | + .arg("openapitools/openapi-generator-cli") |
| 113 | + .arg("generate") |
| 114 | + .arg("-i") |
| 115 | + .arg(format!("/local/{}", OUTPUT_PREPROCESSED_FILE)) // Input path inside the container |
| 116 | + .arg("-g") |
| 117 | + .arg("rust") |
| 118 | + .arg("-o") |
| 119 | + .arg(format!("/local/{}", OUTPUT_DIR)) // Output path inside the container |
| 120 | + .arg("--additional-properties") |
| 121 | + .arg("library=reqwest") |
| 122 | + .arg("--additional-properties") |
| 123 | + .arg("supportMiddleware=true") |
| 124 | + .arg("--additional-properties") |
| 125 | + .arg("useSingleRequestParameter=true") |
| 126 | + // .arg("--additional-properties") |
| 127 | + // .arg("useBonBuilder=true") |
| 128 | + .status() |
| 129 | + .context("Failed to execute Docker command. Is Docker installed and running?")?; |
| 130 | + |
| 131 | + // Check if the command was successful. |
| 132 | + if !status.success() { |
| 133 | + anyhow::bail!("Docker command failed with status: {}", status); |
| 134 | + } |
| 135 | + |
| 136 | + println!("✅ Codegen task finished successfully."); |
| 137 | + println!(" Generated code is available in '{}'", OUTPUT_DIR); |
| 138 | + Ok(()) |
| 139 | +} |
0 commit comments