Skip to content
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

fix(next): some openapi-codegen args are ignored #633

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions openapi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fmt::Debug;
use clap::Parser;

pub mod codegen;
mod components;
mod crate_inference;
Expand All @@ -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<spec_fetch::SpecVersion>,
/// 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 {
<Self as Parser>::parse()
}
}
47 changes: 9 additions & 38 deletions openapi/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<spec_fetch::SpecVersion>,
/// 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();
Expand Down Expand Up @@ -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");
Expand All @@ -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()?;
}
Expand Down
4 changes: 3 additions & 1 deletion openapi/src/url_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -24,8 +25,9 @@ impl UrlFinder {
}

pub fn url_for_object(&self, object: &str) -> Option<String> {
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))
}
}

Expand Down
4 changes: 3 additions & 1 deletion openapi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -30,7 +31,8 @@ fn write_or_append_to_outfile<C: AsRef<[u8]>, P: AsRef<Path>>(
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()))?;
Expand Down