style: apply cargo fmt
This commit is contained in:
parent
534ed83419
commit
d205d722aa
17 changed files with 390 additions and 315 deletions
|
|
@ -1,22 +1,19 @@
|
|||
use std::{ffi::OsStr, path::Path};
|
||||
use attribute_derive::FromAttr;
|
||||
use std::{ffi::OsStr, path::Path};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use argh::FromArgs;
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use crate::generators::{SourceNode, SourceNodeContainer};
|
||||
|
||||
// use gen_migrations::generate_create_table_sql;
|
||||
// use gen_repositories::{generate_repositories_source_files, SourceNodeContainer};
|
||||
|
||||
pub mod generators;
|
||||
pub mod models;
|
||||
pub mod parse_models;
|
||||
pub mod generators;
|
||||
|
||||
#[derive(FromAttr, PartialEq, Debug, Default)]
|
||||
#[attribute(ident = sql_generator_model)]
|
||||
pub struct SqlGeneratorModelAttr {
|
||||
table_name: Option<String>
|
||||
table_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(FromAttr, PartialEq, Debug, Default)]
|
||||
|
|
@ -25,20 +22,19 @@ pub struct SqlGeneratorFieldAttr {
|
|||
is_primary: Option<bool>,
|
||||
is_unique: Option<bool>,
|
||||
reverse_relation_name: Option<String>,
|
||||
|
||||
|
||||
/// to indicate that this field will be used to obtains entities
|
||||
/// our framework will generate methods for all fields that is an entrypoint
|
||||
is_query_entrypoint: Option<bool>
|
||||
is_query_entrypoint: Option<bool>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
/// Generate SQL CREATE TABLE migrations
|
||||
#[argh(subcommand, name = "gen-migrations")]
|
||||
struct GenerateMigration {
|
||||
/// path of file where to write all in one generated SQL migration
|
||||
#[argh(option, short = 'o')]
|
||||
output: Option<String>
|
||||
output: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
|
|
@ -47,7 +43,7 @@ struct GenerateMigration {
|
|||
struct GenerateRepositories {
|
||||
/// path of the directory that contains repositories
|
||||
#[argh(option, short = 'o')]
|
||||
output: Option<String>
|
||||
output: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
|
|
@ -67,9 +63,9 @@ struct GeneratorArgs {
|
|||
/// path of the directory containing models
|
||||
#[argh(option, short = 'm')]
|
||||
models_path: Option<String>,
|
||||
|
||||
|
||||
#[argh(subcommand)]
|
||||
nested: GeneratorArgsSubCommands
|
||||
nested: GeneratorArgsSubCommands,
|
||||
}
|
||||
|
||||
fn write_source_code(base_path: &Path, snc: SourceNodeContainer) -> Result<()> {
|
||||
|
|
@ -78,7 +74,7 @@ fn write_source_code(base_path: &Path, snc: SourceNodeContainer) -> Result<()> {
|
|||
SourceNode::File(code) => {
|
||||
println!("writing file {:?}", path);
|
||||
std::fs::write(path, code)?;
|
||||
},
|
||||
}
|
||||
SourceNode::Directory(dir) => {
|
||||
for node in dir {
|
||||
write_source_code(&path, node)?;
|
||||
|
|
@ -92,11 +88,14 @@ pub fn main() -> Result<()> {
|
|||
let args: GeneratorArgs = argh::from_env();
|
||||
let project_root = &args.project_root.unwrap_or(".".to_string());
|
||||
let project_root_path = Path::new(&project_root);
|
||||
eprintln!("Using project root at: {:?}", &project_root_path.canonicalize()?);
|
||||
eprintln!(
|
||||
"Using project root at: {:?}",
|
||||
&project_root_path.canonicalize()?
|
||||
);
|
||||
if !project_root_path.exists() {
|
||||
return Err(anyhow!("Could not resolve project root path."));
|
||||
}
|
||||
|
||||
|
||||
// check Cargo.toml
|
||||
let main_manifest_location = "Cargo.toml";
|
||||
let main_manifest_path = project_root_path.join(main_manifest_location);
|
||||
|
|
@ -117,13 +116,17 @@ pub fn main() -> Result<()> {
|
|||
if !models_mod_path.exists() {
|
||||
return Err(anyhow!("Could not resolve models modules."));
|
||||
}
|
||||
if models_mod_path.file_name().map(|x| x == OsStr::new("mod.rs")).unwrap_or(false) {
|
||||
if models_mod_path
|
||||
.file_name()
|
||||
.map(|x| x == OsStr::new("mod.rs"))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
models_mod_path.pop();
|
||||
}
|
||||
eprintln!("Found models in project, parsing models");
|
||||
let models = parse_models::parse_models_from_module(&models_mod_path)?;
|
||||
dbg!(&models);
|
||||
|
||||
|
||||
match args.nested {
|
||||
GeneratorArgsSubCommands::GenerateRepositories(opts) => {
|
||||
eprintln!("Generating repositories…");
|
||||
|
|
@ -136,7 +139,7 @@ pub fn main() -> Result<()> {
|
|||
let snc = generators::repositories::generate_repositories_source_files(&models)?;
|
||||
dbg!(&snc);
|
||||
write_source_code(&repositories_mod_path, snc)?;
|
||||
},
|
||||
}
|
||||
GeneratorArgsSubCommands::GenerateMigration(opts) => {
|
||||
eprintln!("Generating migrations…");
|
||||
let sql_code = generators::migrations::generate_create_table_sql(&models)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue