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

feat(sea-orm-cli): output log about generated file name. #735

Merged
merged 7 commits into from
Jun 12, 2022
Merged
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
12 changes: 12 additions & 0 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::ArgMatches;
use regex::Regex;
use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde};
use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr};
use tracing_subscriber::{prelude::*, EnvFilter};
use url::Url;

pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Error>> {
Expand All @@ -21,6 +22,17 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dy
.with_max_level(tracing::Level::DEBUG)
.with_test_writer()
.try_init();
} else {
let filter_layer = EnvFilter::try_new("sea_orm_codegen=info").unwrap();
let fmt_layer = tracing_subscriber::fmt::layer()
.with_target(false)
.with_level(false)
.without_time();

let _ = tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.try_init();
}

let max_connections = args
Expand Down
1 change: 1 addition & 0 deletions sea-orm-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ syn = { version = "^1", default-features = false, features = [
quote = "^1"
heck = "^0.3"
proc-macro2 = "^1"
tracing = { version = "0.1", features = ["log"] }

[dev-dependencies]
pretty_assertions = { version = "^0.7" }
64 changes: 64 additions & 0 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use sea_query::{ColumnDef, ColumnSpec, ColumnType};
use std::fmt::Write as FmtWrite;

#[derive(Clone, Debug)]
pub struct Column {
Expand Down Expand Up @@ -143,6 +144,33 @@ impl Column {
}
col_def
}

pub fn get_info(&self) -> String {
let mut info = String::new();
let type_info = self.get_rs_type().to_string().replace(' ', "");
let col_info = self.col_info();
write!(
&mut info,
"Column `{}`: {}{}",
self.name, type_info, col_info
)
.unwrap();
info
}

fn col_info(&self) -> String {
let mut info = String::new();
if self.auto_increment {
write!(&mut info, ", auto_increment").unwrap();
}
if self.not_null {
write!(&mut info, ", not_null").unwrap();
}
if self.unique {
write!(&mut info, ", unique").unwrap();
}
info
}
}

impl From<ColumnDef> for Column {
Expand Down Expand Up @@ -361,6 +389,42 @@ mod tests {
}
}

#[test]
fn test_get_info() {
let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into();
assert_eq!(column.get_info().as_str(), "Column `id`: Option<String>");

let column: Column = ColumnDef::new(Alias::new("id"))
.string()
.not_null()
.to_owned()
.into();
assert_eq!(column.get_info().as_str(), "Column `id`: String, not_null");

let column: Column = ColumnDef::new(Alias::new("id"))
.string()
.not_null()
.unique_key()
.to_owned()
.into();
assert_eq!(
column.get_info().as_str(),
"Column `id`: String, not_null, unique"
);

let column: Column = ColumnDef::new(Alias::new("id"))
.string()
.not_null()
.unique_key()
.auto_increment()
.to_owned()
.into();
assert_eq!(
column.get_info().as_str(),
"Column `id`: String, auto_increment, not_null, unique"
);
}

#[test]
fn test_from_column_def() {
let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into();
Expand Down
15 changes: 14 additions & 1 deletion sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::{collections::HashMap, str::FromStr};
use syn::{punctuated::Punctuated, token::Comma};
use tracing::info;

#[derive(Clone, Debug)]
pub struct EntityWriter {
Expand Down Expand Up @@ -94,6 +95,18 @@ impl EntityWriter {
self.entities
.iter()
.map(|entity| {
let entity_file = format!("{}.rs", entity.get_table_name_snake_case());
let column_info = entity
.columns
.iter()
.map(|column| column.get_info())
.collect::<Vec<String>>();

info!("Generating {}", entity_file);
for info in column_info.iter() {
info!(" > {}", info);
}

let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
let code_blocks = if expanded_format {
Expand All @@ -103,7 +116,7 @@ impl EntityWriter {
};
Self::write(&mut lines, code_blocks);
OutputFile {
name: format!("{}.rs", entity.get_table_name_snake_case()),
name: entity_file,
content: lines.join("\n\n"),
}
})
Expand Down