forked from EpicsDAO/zapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EpicsDAO#17 from JonasCir/refactor-gen-method
use quote for code generation
- Loading branch information
Showing
27 changed files
with
659 additions
and
666 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;)* | ||
} | ||
} |
Oops, something went wrong.