From 7dee018ed22ba7424f38c14644bea981bb023685 Mon Sep 17 00:00:00 2001 From: Daiki Arai Date: Fri, 8 Nov 2024 23:06:26 +0900 Subject: [PATCH] make arguments accessible anywhere for args.out --- openapi/src/lib.rs | 37 ++++++++++++++++++++++++++++++ openapi/src/main.rs | 47 ++++++++------------------------------- openapi/src/url_finder.rs | 4 +++- openapi/src/utils.rs | 4 +++- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/openapi/src/lib.rs b/openapi/src/lib.rs index 5591e65a4..3aa625a23 100644 --- a/openapi/src/lib.rs +++ b/openapi/src/lib.rs @@ -1,3 +1,6 @@ +use std::fmt::Debug; +use clap::Parser; + pub mod codegen; mod components; mod crate_inference; @@ -23,3 +26,37 @@ mod visitor; mod webhook; pub const STRIPE_TYPES: &str = "stripe_types"; + +#[derive(Debug, Parser)] +pub struct Command { + /// Input path for the OpenAPI spec, defaults to `spec3.sdk.json` + #[arg(default_value = "spec3.sdk.json")] + pub spec_path: String, + /// Output directory for generated code, defaults to `out` + #[arg(long, default_value = "out")] + pub out: String, + /// If not passed, skips the step of fetching the spec. Otherwise, `latest` for the + /// newest spec release, `current` for the version used in the latest codegen update, + /// or a specific version, such as `v171` + #[arg(long, value_parser = spec_fetch::parse_spec_version)] + pub fetch: Option, + /// Instead of writing files, generate a graph of dependencies in `graphviz` `DOT` format. Writes + /// to `graph.txt` + #[arg(long)] + pub graph: bool, + /// Update the Stripe API docs instead of using the existing data in the repo + #[arg(long)] + pub update_api_docs: bool, + /// The URL to target for the stripe docs. + #[arg(long, default_value = "https://stripe.com/docs/api")] + pub api_docs_url: String, + /// Skip the step of copying the generated code from `out` to `generated/`. + #[arg(long)] + pub dry_run: bool, +} + +impl Command { + pub fn parse() -> Self { + ::parse() + } +} \ No newline at end of file diff --git a/openapi/src/main.rs b/openapi/src/main.rs index 5c46e1f86..c9b3c3bee 100644 --- a/openapi/src/main.rs +++ b/openapi/src/main.rs @@ -1,48 +1,18 @@ -use std::fmt::Debug; use std::fs; use std::fs::File; use std::path::Path; use anyhow::{bail, Context, Result}; -use clap::Parser; use petgraph::dot::{Config, Dot}; use stripe_openapi_codegen::codegen::CodeGen; use stripe_openapi_codegen::crates::ALL_CRATES; use stripe_openapi_codegen::spec::Spec; -use stripe_openapi_codegen::spec_fetch; use stripe_openapi_codegen::spec_fetch::fetch_spec; use stripe_openapi_codegen::url_finder::{update_api_doc_data, UrlFinder}; use stripe_openapi_codegen::utils::write_to_file; +use stripe_openapi_codegen::Command; use tracing::info; -#[derive(Debug, Parser)] -struct Command { - /// Input path for the OpenAPI spec, defaults to `spec3.sdk.json` - #[arg(default_value = "spec3.sdk.json")] - spec_path: String, - /// Output directory for generated code, defaults to `out` - #[arg(long, default_value = "out")] - out: String, - /// If not passed, skips the step of fetching the spec. Otherwise, `latest` for the - /// newest spec release, `current` for the version used in the latest codegen update, - /// or a specific version, such as `v171` - #[arg(long, value_parser = spec_fetch::parse_spec_version)] - fetch: Option, - /// Instead of writing files, generate a graph of dependencies in `graphviz` `DOT` format. Writes - /// to `graph.txt` - #[arg(long)] - graph: bool, - /// Update the Stripe API docs instead of using the existing data in the repo - #[arg(long)] - update_api_docs: bool, - /// The URL to target for the stripe docs. - #[arg(long, default_value = "https://stripe.com/docs/api")] - api_docs_url: String, - /// Skip the step of copying the generated code from `out` to `generated/`. - #[arg(long)] - dry_run: bool, -} - fn main() -> Result<()> { tracing_subscriber::fmt::init(); let args = Command::parse(); @@ -89,10 +59,10 @@ fn main() -> Result<()> { let mut fmt_cmd = std::process::Command::new("cargo"); fmt_cmd.arg("+nightly").arg("fmt").arg("--"); for krate in &*ALL_CRATES { - fmt_cmd.arg(format!("out/{}/src/mod.rs", krate.generated_out_path())); + fmt_cmd.arg(format!("{}/{}/src/mod.rs", out_path.display(), krate.generated_out_path())); } - fmt_cmd.arg("out/async-stripe-webhook/mod.rs"); - fmt_cmd.arg("out/tests/mod.rs"); + fmt_cmd.arg(format!("{}/async-stripe-webhook/mod.rs", out_path.display())); + fmt_cmd.arg(format!("{}/tests/mod.rs", out_path.display())); if !args.dry_run { info!("Formatting generated files"); @@ -102,12 +72,13 @@ fn main() -> Result<()> { } info!("Copying generated files"); - run_rsync("out/crates/", "../generated/")?; - run_rsync("out/async-stripe-webhook/", "../async-stripe-webhook/src/generated/")?; - run_rsync("out/tests/", "../tests/tests/it/generated/")?; + run_rsync(&format!("{}/crates/", out_path.display()), "../generated/")?; + run_rsync(&format!("{}/async-stripe-webhook/", out_path.display()), + "../async-stripe-webhook/src/generated/")?; + run_rsync(&format!("{}/tests/", out_path.display()), "../tests/tests/it/generated/")?; std::process::Command::new("cp") - .arg("out/crate_info.md") + .arg(format!("{}/crate_info.md", out_path.display())) .arg("../crate_info.md") .output()?; } diff --git a/openapi/src/url_finder.rs b/openapi/src/url_finder.rs index 745d653ca..f8c7f04ec 100644 --- a/openapi/src/url_finder.rs +++ b/openapi/src/url_finder.rs @@ -7,6 +7,7 @@ use reqwest::blocking::Client; use tracing::{info, warn}; use crate::components::Components; +use crate::Command; // we use a common user agent, otherwise stripe rejects the connection const APP_USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"; @@ -24,8 +25,9 @@ impl UrlFinder { } pub fn url_for_object(&self, object: &str) -> Option { + let args = Command::parse(); let unprefixed_link = self.doc_links.get(object)?; - Some(format!("https://stripe.com/docs/api{}", unprefixed_link)) + Some(format!("{}{}", args.api_docs_url, unprefixed_link)) } } diff --git a/openapi/src/utils.rs b/openapi/src/utils.rs index 4a50a6252..f852a57bf 100644 --- a/openapi/src/utils.rs +++ b/openapi/src/utils.rs @@ -3,6 +3,7 @@ use std::io::Write; use std::path::{Path, PathBuf}; use anyhow::Context; +use crate::Command; /// Write to a file, starting paths from the `out` directory and ensuring existence /// of directories along the way. @@ -30,7 +31,8 @@ fn write_or_append_to_outfile, P: AsRef>( out_path: P, mut opts: OpenOptions, ) -> anyhow::Result<()> { - let mut base = PathBuf::from("out"); + let args = Command::parse(); + let mut base = PathBuf::from(&args.out); base.push(out_path); create_dir_all(base.parent().unwrap()) .with_context(|| format!("Could not create directories along path {}", base.display()))?;