Skip to content

Commit

Permalink
Merge pull request EpicsDAO#17 from JonasCir/refactor-gen-method
Browse files Browse the repository at this point in the history
use quote for code generation
  • Loading branch information
POPPIN-FUMI committed Sep 22, 2022
2 parents b101696 + d98a97a commit 54365d8
Show file tree
Hide file tree
Showing 27 changed files with 659 additions and 666 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ convert_case = "0.5.0"
spinners = "4.0.0"
quote = "1.0.20"
syn = "1.0.98"
proc-macro2 = { version = "1.0.32", default-features = false }
prettyplease = "0.1.16"

[dev-dependencies]
Expand Down
100 changes: 0 additions & 100 deletions src/g/entity.rs

This file was deleted.

66 changes: 66 additions & 0 deletions src/g/entity/creation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::g::{emit_generated_code, to_upper_camel};
use crate::style_print::log_success;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::path::Path;

pub(super) fn create_entity(model: &str, entity_src_dir: &Path) {
let file_content_tokens = create_model_tokens(model);

let file_path = emit_generated_code(
entity_src_dir,
&format!("{}.rs", model),
&file_content_tokens,
);

log_success(&format!(
"Successfully created `{}` entity file: {}",
model,
file_path.display()
));
}

fn create_model_tokens(model_str: &str) -> TokenStream {
let model = format_ident!("{}", model_str);
let cap_model = format_ident!("{}", to_upper_camel(model_str));

quote! {
use async_graphql::*;
use sea_orm::{entity::prelude::*, DeleteMany};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize, SimpleObject)]
#[sea_orm(table_name = #model)]
#[graphql(concrete(name = "#cap_model", params()))]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(indexed)]
pub created_at: DateTime,
#[sea_orm(indexed)]
pub updated_at: DateTime
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}

impl Entity {
pub fn find_by_id(id: i32) -> Select<Entity> {
Self::find().filter(Column::Id.eq(id))
}

pub fn delete_by_id(id: i32) -> DeleteMany<Entity> {
Self::delete_many().filter(Column::Id.eq(id))
}
}
}
}
18 changes: 18 additions & 0 deletions src/g/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::g::entity::{creation::create_entity, registration::register_entity};
use crate::style_print::log_error;
use std::fs;
use std::path::Path;

mod creation;
mod registration;

pub(in crate::g) fn process_entity(model: &str, gen_path: &Path) {
let entity_src_dir = gen_path.join("entity").join("src");

fs::create_dir_all(entity_src_dir.as_path()).unwrap_or_else(|why| {
log_error(&format!("! {:?}", why.kind()));
});

create_entity(model, &entity_src_dir);
register_entity(model, &entity_src_dir);
}
39 changes: 39 additions & 0 deletions src/g/entity/registration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::g::{emit_generated_code, read_dir};
use crate::style_print::log_success;

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::path::Path;

pub(super) fn register_entity(model: &str, entity_src_dir: &Path) {
let file_content_tokens = register_entity_tokens(entity_src_dir);

let file_path = emit_generated_code(entity_src_dir, "lib.rs", &file_content_tokens);

log_success(&format!(
"Successfully registered `{}` entity in {}",
model,
file_path.display()
));
}

fn register_entity_tokens(entity_src_dir: &Path) -> TokenStream {
let entity_files = read_dir(entity_src_dir).unwrap();
let mut entity_box = entity_files
.iter()
.cloned()
.filter(|i| i != "lib.rs")
.map(|i| i.replace(".rs", ""))
.collect::<Vec<_>>();
entity_box.sort();

let modules = entity_box
.iter()
.map(|i| format_ident!("{}", i))
.collect::<Vec<_>>();

quote! {
pub use async_graphql;
#(pub mod #modules;)*
}
}
Loading

0 comments on commit 54365d8

Please sign in to comment.