Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Sep 17, 2024
1 parent bce22cc commit d2a5573
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 143 deletions.
65 changes: 10 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ example EXAMPLE:
alias e := example

install:
just clean release
cargo install --path libninja --force

check:
Expand Down Expand Up @@ -55,10 +54,7 @@ patch: test
just version patch
just publish

clean MODE='debug':
checkexec ${CARGO_TARGET_DIR:-target}/{{MODE}}/libninja $(fd . -H core/template) -- cargo clean --package libninja

rust: clean
rust:
cargo run -- gen --name PetStore --output-dir gen/rust --generator rust data/openapi-spec/petstore/petstore.yaml --github libninjacom/petstore-rs --version 0.1.0

generate:
Expand Down
4 changes: 3 additions & 1 deletion codegen_rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ anyhow = "1.0.89"
quote = "1.0.35"
proc-macro2 = "1.0.86"
convert_case = "0.6.0"
regex = "1.10.6"
regex = "1.10.6"
walkdir = "2.5.0"
kurtbuilds_std_ext = "0.1.1"
12 changes: 7 additions & 5 deletions codegen_rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ pub fn make_lib_rs(spec: &HirSpec, extras: &Extras, cfg: &Config) -> File<TokenS

let client_name = struct_Client.name.clone();

let struct_Client = struct_Client.to_rust_code();

let serde = extras
.needs_serde()
.then(|| {
Expand Down Expand Up @@ -71,14 +69,18 @@ pub fn make_lib_rs(spec: &HirSpec, extras: &Extras, cfg: &Config) -> File<TokenS
File {
attributes: vec![],
doc: None,
imports: vec![],
imports: vec![
import!(std::sync, OnceLock),
import!(std::borrow, Cow),
import!(httpclient, Client),
],
items: vec![
Item::Block(base64_import),
Item::Block(serde),
Item::Block(static_shared_http_client),
Item::Block(shared_oauth2_flow),
Item::Block(fluent_request),
Item::Block(struct_Client),
Item::Class(struct_Client),
Item::Block(impl_Client),
Item::Block(security),
],
Expand Down Expand Up @@ -175,7 +177,7 @@ pub fn struct_Client(spec: &HirSpec, opt: &Config) -> Class<TokenStream> {
fields: instance_fields,
methods,
vis: Visibility::Public,
imports: vec![import!(httpclient, Client)],
imports: vec![],
..Class::default()
}
}
Expand Down
37 changes: 23 additions & 14 deletions codegen_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
fs,
path::{Path, PathBuf},
};
use std_ext::PathExt;

pub type Modified = HashSet<PathBuf>;

Expand Down Expand Up @@ -49,22 +50,30 @@ pub fn generate_rust_library(spec: HirSpec, cfg: Config) -> Result<()> {
}

fn remove_old_files(dest: &Path, modified: &HashSet<PathBuf>) -> Result<()> {
let mut to_delete: Vec<_> = fs::read_dir(dest.join("examples"))
for f in walkdir::WalkDir::new(dest.join("src")) {
let f = f.unwrap();
let f = f.into_path();
if f.as_os_str() == "./src/request.rs" {
dbg!(f.ext_str());
}
}
let to_delete = walkdir::WalkDir::new(dest.join("src"))
.into_iter()
.flatten()
.chain(fs::read_dir(dest.join("src")).into_iter().flatten())
.flat_map(|e| e.ok())
.map(|e| e.path())
.filter(|e| e.ends_with(".rs"))
.filter(|e| {
fs::read_to_string(&e)
.map(|content| !content.contains("libninja: static"))
.chain(walkdir::WalkDir::new(dest.join("examples")).into_iter())
.filter_map(|e| e.ok())
.map(|e| e.into_path())
.filter(|p| p.ext_str() == "rs")
.filter(|e| !modified.contains(e))
.filter(|p| {
!fs::read_to_string(&p)
.map(|content| content.contains("libninja: static"))
.unwrap_or(false)
})
.collect();
to_delete.retain(|f| !modified.contains(f));
for file in to_delete {
fs::remove_file(file)?;
});
let to_delete = to_delete.collect::<Vec<_>>();
dbg!(&to_delete);
for e in to_delete {
fs::remove_file(&e)?;
eprintln!("{}: Remove unused file.", e.display());
}
Ok(())
}
Expand Down
Loading

0 comments on commit d2a5573

Please sign in to comment.