From 935e4cf3c12407bcea03bc295224cfcaf331ffce Mon Sep 17 00:00:00 2001 From: "Dotan J. Nahum" Date: Mon, 21 Oct 2024 19:30:10 +0300 Subject: [PATCH] refactor: move gen into a crate - loco-gen --- Cargo.toml | 33 ++++---- loco-gen/Cargo.toml | 30 +++++++ {src/gen => loco-gen/src}/controller.rs | 5 +- src/gen/mod.rs => loco-gen/src/lib.rs | 81 +++++++++++-------- {src/gen => loco-gen/src}/mappings.json | 0 {src/gen => loco-gen/src}/model.rs | 33 +++++++- {src/gen => loco-gen/src}/scaffold.rs | 5 +- .../src}/templates/controller.t | 0 .../templates/controller/api/controller.t | 0 .../src}/templates/controller/api/test.t | 0 .../templates/controller/html/controller.t | 0 .../src}/templates/controller/html/view.t | 0 .../templates/controller/htmx/controller.t | 0 .../src}/templates/controller/htmx/view.t | 0 .../src}/templates/deployment_docker.t | 0 .../src}/templates/deployment_docker_ignore.t | 0 .../src}/templates/deployment_nginx.t | 0 .../src}/templates/deployment_shuttle.t | 0 .../templates/deployment_shuttle_config.t | 0 {src/gen => loco-gen/src}/templates/mailer.t | 0 .../src}/templates/mailer_html.t | 0 .../src}/templates/mailer_sub.t | 0 .../src}/templates/mailer_text.t | 0 .../src}/templates/migration.t | 0 {src/gen => loco-gen/src}/templates/model.t | 0 .../src}/templates/model_test.t | 0 .../src}/templates/request_test.t | 0 .../src}/templates/scaffold/api/controller.t | 0 .../src}/templates/scaffold/api/test.t | 0 .../src}/templates/scaffold/html/base.t | 0 .../src}/templates/scaffold/html/controller.t | 0 .../src}/templates/scaffold/html/view.t | 0 .../templates/scaffold/html/view_create.t | 0 .../src}/templates/scaffold/html/view_edit.t | 0 .../src}/templates/scaffold/html/view_list.t | 0 .../src}/templates/scaffold/html/view_show.t | 0 .../src}/templates/scaffold/htmx/base.t | 0 .../src}/templates/scaffold/htmx/controller.t | 0 .../src}/templates/scaffold/htmx/view.t | 0 .../templates/scaffold/htmx/view_create.t | 0 .../src}/templates/scaffold/htmx/view_edit.t | 0 .../src}/templates/scaffold/htmx/view_list.t | 0 .../src}/templates/scaffold/htmx/view_show.t | 0 .../src}/templates/scheduler.t | 0 {src/gen => loco-gen/src}/templates/task.t | 0 .../src}/templates/task_test.t | 0 {src/gen => loco-gen/src}/templates/worker.t | 0 .../src}/templates/worker_test.t | 0 {src/gen => loco-gen/src}/testutil.rs | 0 src/cli.rs | 75 +++++++++++------ src/errors.rs | 6 +- src/lib.rs | 1 - 52 files changed, 184 insertions(+), 85 deletions(-) create mode 100644 loco-gen/Cargo.toml rename {src/gen => loco-gen/src}/controller.rs (96%) rename src/gen/mod.rs => loco-gen/src/lib.rs (86%) rename {src/gen => loco-gen/src}/mappings.json (100%) rename {src/gen => loco-gen/src}/model.rs (69%) rename {src/gen => loco-gen/src}/scaffold.rs (97%) rename {src/gen => loco-gen/src}/templates/controller.t (100%) rename {src/gen => loco-gen/src}/templates/controller/api/controller.t (100%) rename {src/gen => loco-gen/src}/templates/controller/api/test.t (100%) rename {src/gen => loco-gen/src}/templates/controller/html/controller.t (100%) rename {src/gen => loco-gen/src}/templates/controller/html/view.t (100%) rename {src/gen => loco-gen/src}/templates/controller/htmx/controller.t (100%) rename {src/gen => loco-gen/src}/templates/controller/htmx/view.t (100%) rename {src/gen => loco-gen/src}/templates/deployment_docker.t (100%) rename {src/gen => loco-gen/src}/templates/deployment_docker_ignore.t (100%) rename {src/gen => loco-gen/src}/templates/deployment_nginx.t (100%) rename {src/gen => loco-gen/src}/templates/deployment_shuttle.t (100%) rename {src/gen => loco-gen/src}/templates/deployment_shuttle_config.t (100%) rename {src/gen => loco-gen/src}/templates/mailer.t (100%) rename {src/gen => loco-gen/src}/templates/mailer_html.t (100%) rename {src/gen => loco-gen/src}/templates/mailer_sub.t (100%) rename {src/gen => loco-gen/src}/templates/mailer_text.t (100%) rename {src/gen => loco-gen/src}/templates/migration.t (100%) rename {src/gen => loco-gen/src}/templates/model.t (100%) rename {src/gen => loco-gen/src}/templates/model_test.t (100%) rename {src/gen => loco-gen/src}/templates/request_test.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/api/controller.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/api/test.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/base.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/controller.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/view.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/view_create.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/view_edit.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/view_list.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/html/view_show.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/base.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/controller.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/view.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/view_create.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/view_edit.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/view_list.t (100%) rename {src/gen => loco-gen/src}/templates/scaffold/htmx/view_show.t (100%) rename {src/gen => loco-gen/src}/templates/scheduler.t (100%) rename {src/gen => loco-gen/src}/templates/task.t (100%) rename {src/gen => loco-gen/src}/templates/task_test.t (100%) rename {src/gen => loco-gen/src}/templates/worker.t (100%) rename {src/gen => loco-gen/src}/templates/worker_test.t (100%) rename {src/gen => loco-gen/src}/testutil.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 6fc54162e..63379c684 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["xtask", "loco-extras"] +members = ["xtask", "loco-extras", "loco-gen"] exclude = ["starters"] [workspace.package] @@ -26,7 +26,7 @@ default = ["auth_jwt", "cli", "with-db", "cache_inmem", "bg_redis", "bg_pg"] auth_jwt = ["dep:jsonwebtoken"] cli = ["dep:clap"] testing = ["dep:axum-test"] -with-db = ["dep:sea-orm", "dep:sea-orm-migration"] +with-db = ["dep:sea-orm", "dep:sea-orm-migration", "loco-gen/with-db"] channels = ["dep:socketioxide"] # Storage features all_storage = ["storage_aws_s3", "storage_azure", "storage_gcp"] @@ -39,6 +39,7 @@ bg_redis = ["dep:rusty-sidekiq", "dep:bb8"] bg_pg = ["dep:sqlx", "dep:ulid"] [dependencies] +loco-gen = { path = "./loco-gen" } backtrace_printer = { version = "1.3.0" } # cli @@ -56,8 +57,8 @@ sea-orm = { version = "1.0.0", features = [ tokio = { version = "1.33.0", default-features = false } # the rest -serde = "1" -serde_json = "1" +serde = { workspace = true } +serde_json = { workspace = true } serde_yaml = "0.9" serde_variant = "0.1.2" @@ -66,8 +67,8 @@ async-trait = { workspace = true } axum = { workspace = true } axum-extra = { version = "0.9", features = ["cookie"] } -regex = "1" -lazy_static = "1.4.0" +regex = { workspace = true } +lazy_static = { workspace = true } fs-err = "2.11.0" # mailer tera = "1.19.1" @@ -79,8 +80,8 @@ lettre = { version = "0.11.4", default-features = false, features = [ "tokio1-rustls-tls", ] } include_dir = "0.7.3" -thiserror = "1" -tracing = "0.1.40" +thiserror = { workspace = true } +tracing = { workspace = true } tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } tracing-appender = "0.2.3" @@ -103,12 +104,7 @@ ipnetwork = "0.20.0" axum-test = { version = "16.1.0", optional = true } -# gen -rrgen = "0.5.3" -chrono = "0.4.31" -cargo_metadata = "0.18.1" -dialoguer = "0.11.0" - +chrono = { workspace = true } cfg-if = "1" uuid = { version = "1.10.0", features = ["v4", "fast-rng"] } @@ -138,6 +134,14 @@ rusty-sidekiq = { version = "0.11.0", default-features = false, optional = true bb8 = { version = "0.8.1", optional = true } [workspace.dependencies] + +chrono = "0.4" +tracing = "0.1.40" +regex = "1" +thiserror = "1" +serde = "1" +serde_json = "1" +lazy_static = "1.4.0" async-trait = { version = "0.1.74" } axum = { version = "0.7.5", features = ["macros"] } tower = "0.4" @@ -168,6 +172,7 @@ features = [ features = ["testing"] [dev-dependencies] +cargo_metadata = "0.18.1" loco-rs = { path = ".", features = ["testing"] } rstest = "0.21.0" insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] } diff --git a/loco-gen/Cargo.toml b/loco-gen/Cargo.toml new file mode 100644 index 000000000..ad1023f48 --- /dev/null +++ b/loco-gen/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "loco-gen" +version = "0.1.0" +description = "Loco generators" +license.workspace = true +edition.workspace = true +rust-version.workspace = true + +[features] +with-db = [] + +[lib] +path = "src/lib.rs" + +[dependencies] + +lazy_static = { workspace = true } +rrgen = "0.5.3" +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +regex = { workspace = true } +tracing = { workspace = true } +chrono = { workspace = true } + +clap = { version = "4.4.7", features = ["derive"] } +tempfile = "3" +syn = "2" +dialoguer = "0.11" +duct = "0.13" diff --git a/src/gen/controller.rs b/loco-gen/src/controller.rs similarity index 96% rename from src/gen/controller.rs rename to loco-gen/src/controller.rs index c2a570465..892b443e2 100644 --- a/src/gen/controller.rs +++ b/loco-gen/src/controller.rs @@ -1,7 +1,7 @@ use rrgen::RRgen; use serde_json::json; -use crate::gen; +use crate as gen; const API_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/api/controller.t"); const API_CONTROLLER_TEST_T: &str = include_str!("templates/controller/api/test.t"); @@ -12,8 +12,7 @@ const HTMX_VIEW_T: &str = include_str!("templates/controller/htmx/view.t"); const HTML_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/html/controller.t"); const HTML_VIEW_T: &str = include_str!("templates/controller/html/view.t"); -use super::{collect_messages, AppInfo}; -use crate::Result; +use super::{collect_messages, AppInfo, Result}; pub fn generate( rrgen: &RRgen, diff --git a/src/gen/mod.rs b/loco-gen/src/lib.rs similarity index 86% rename from src/gen/mod.rs rename to loco-gen/src/lib.rs index 28c4bb0b9..c59ce1e09 100644 --- a/src/gen/mod.rs +++ b/loco-gen/src/lib.rs @@ -16,8 +16,6 @@ mod scaffold; mod testutil; use std::str::FromStr; -use crate::{config::Config, errors, Result}; - const CONTROLLER_T: &str = include_str!("templates/controller.t"); const CONTROLLER_TEST_T: &str = include_str!("templates/request_test.t"); @@ -51,6 +49,26 @@ const DEPLOYMENT_OPTIONS: &[(&str, DeploymentKind)] = &[ ("Nginx", DeploymentKind::Nginx), ]; +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("{0}")] + Message(String), + #[error(transparent)] + RRgen(#[from] rrgen::Error), + #[error(transparent)] + IO(#[from] std::io::Error), + #[error(transparent)] + Any(#[from] Box), +} + +impl Error { + pub fn msg(err: impl std::error::Error + Send + Sync + 'static) -> Self { + Self::Message(err.to_string()) //.bt() + } +} + +pub type Result = std::result::Result; + #[derive(Serialize, Deserialize, Debug)] struct FieldType { name: String, @@ -178,14 +196,24 @@ pub enum Component { /// Name of the thing to generate name: String, }, - Deployment {}, + Deployment { + fallback_file: Option, + asset_folder: Option, + host: String, + port: i32, + }, } pub struct AppInfo { pub app_name: String, } +/// Generate a component +/// +/// # Errors +/// +/// This function will return an error if it fails #[allow(clippy::too_many_lines)] -pub fn generate(component: Component, config: &Config, appinfo: &AppInfo) -> Result<()> { +pub fn generate(component: Component, appinfo: &AppInfo) -> Result<()> { let rrgen = RRgen::default(); /* (1) @@ -251,36 +279,25 @@ pub fn generate(component: Component, config: &Config, appinfo: &AppInfo) -> Res rrgen.generate(MAILER_TEXT_T, &vars)?; rrgen.generate(MAILER_HTML_T, &vars)?; } - Component::Deployment {} => { + Component::Deployment { + fallback_file, + asset_folder, + host, + port, + } => { let deployment_kind = match std::env::var("LOCO_DEPLOYMENT_KIND") { - Ok(kind) => kind.parse::().map_err(|_e| { - errors::Error::Message(format!("deployment {kind} not supported")) - })?, + Ok(kind) => kind + .parse::() + .map_err(|_e| Error::Message(format!("deployment {kind} not supported")))?, Err(_err) => prompt_deployment_selection().map_err(Box::from)?, }; match deployment_kind { DeploymentKind::Docker => { - let copy_asset_folder = &config - .server - .middlewares - .static_assets - .clone() - .map(|a| a.folder.path) - .unwrap_or_default(); - - let fallback_file = &config - .server - .middlewares - .static_assets - .clone() - .map(|a| a.fallback) - .unwrap_or_default(); - let vars = json!({ "pkg_name": appinfo.app_name, - "copy_asset_folder": copy_asset_folder, - "fallback_file": fallback_file + "copy_asset_folder": asset_folder.unwrap_or_default(), + "fallback_file": fallback_file.unwrap_or_default() }); rrgen.generate(DEPLOYMENT_DOCKER_T, &vars)?; rrgen.generate(DEPLOYMENT_DOCKER_IGNORE_T, &vars)?; @@ -295,15 +312,11 @@ pub fn generate(component: Component, config: &Config, appinfo: &AppInfo) -> Res rrgen.generate(DEPLOYMENT_SHUTTLE_CONFIG_T, &vars)?; } DeploymentKind::Nginx => { - let host = &config - .server - .host - .replace("http://", "") - .replace("https://", ""); + let host = host.replace("http://", "").replace("https://", ""); let vars = json!({ "pkg_name": appinfo.app_name, - "domain": &host, - "port": &config.server.port + "domain": host, + "port": port }); rrgen.generate(DEPLOYMENT_NGINX_T, &vars)?; } @@ -335,7 +348,7 @@ fn prompt_deployment_selection() -> Result { .items(&options) .default(0) .interact() - .map_err(errors::Error::msg)?; + .map_err(Error::msg)?; Ok(DEPLOYMENT_OPTIONS[selection].1.clone()) } diff --git a/src/gen/mappings.json b/loco-gen/src/mappings.json similarity index 100% rename from src/gen/mappings.json rename to loco-gen/src/mappings.json diff --git a/src/gen/model.rs b/loco-gen/src/model.rs similarity index 69% rename from src/gen/model.rs rename to loco-gen/src/model.rs index 32966b4ad..624d2764f 100644 --- a/src/gen/model.rs +++ b/loco-gen/src/model.rs @@ -5,7 +5,7 @@ use duct::cmd; use rrgen::RRgen; use serde_json::json; -use crate::{errors::Error, Result}; +use super::{Error, Result}; const MODEL_T: &str = include_str!("templates/model.t"); const MODEL_TEST_T: &str = include_str!("templates/model_test.t"); @@ -93,7 +93,7 @@ pub fn generate( mod tests { use std::{env, process::Command}; - use crate::gen::{ + use crate::{ testutil::{self, assert_cargo_check, assert_file, assert_single_file_match}, AppInfo, }; @@ -131,6 +131,35 @@ mod tests { #[test] fn test_can_generate_model() { + /* + issues: 1. setting current dir is too aggressive and fails other test that are run in parallel. + - possible solution to forget 'temp' and always generate in actual current dir, making sure to delete before and after + - or isolate these tests and run them with serial + - either way may be to gate under a test_generators feature flag + - or move all these into integration tests, but then need to expose the generators functions + - or add a magic env var, saying where the base app folder is, and have all gen operations start from there + - another solution is to use category_... and aggressively use cargo test filtering from now on + test_ (unit test) + gen_test (generators tests) + integration_(various integration) + - another solution: move gen into loco-gen make it a crate, then its testable in isolation in another ci job + + < the only reverse dep is the Error and Result types, which we can take care of > + + another insight: we don't need 'gen' when building to production. this is a dev only feature and dev only crate + [ ] refactor into() to be manual and take config, create a proper deployment and nginx params + generate should not know about loco config + [ ] create a thiserror result/error typeset + [ ] with-db feature should be irrelevant to gen, or propogate this feature from master crate + [ ] migrate all errors to gen local things + [ ] map error at the root calling gen to loco crate errors + [ ] refactor all gen namespaces and types into loco_gen + [ ] shuffle / move deps from loco to gen and take care of shared deps + + 2. running seaorm and migrations is too heavy + + 3. running cargo check may be too heavy, and requires 'cd' into the dir too + */ let rrgen = rrgen::RRgen::default(); with_new_app("saas", || { super::generate( diff --git a/src/gen/scaffold.rs b/loco-gen/src/scaffold.rs similarity index 97% rename from src/gen/scaffold.rs rename to loco-gen/src/scaffold.rs index 80e6bac3a..d057f482b 100644 --- a/src/gen/scaffold.rs +++ b/loco-gen/src/scaffold.rs @@ -1,7 +1,7 @@ use rrgen::RRgen; use serde_json::json; -use crate::gen; +use crate as gen; const API_CONTROLLER_SCAFFOLD_T: &str = include_str!("templates/scaffold/api/controller.t"); const API_CONTROLLER_TEST_T: &str = include_str!("templates/scaffold/api/test.t"); @@ -22,8 +22,7 @@ const HTML_VIEW_CREATE_SCAFFOLD_T: &str = include_str!("templates/scaffold/html/ const HTML_VIEW_SHOW_SCAFFOLD_T: &str = include_str!("templates/scaffold/html/view_show.t"); const HTML_VIEW_LIST_SCAFFOLD_T: &str = include_str!("templates/scaffold/html/view_list.t"); -use super::{collect_messages, model, AppInfo, MAPPINGS}; -use crate::{errors::Error, Result}; +use super::{collect_messages, model, AppInfo, Error, Result, MAPPINGS}; pub fn generate( rrgen: &RRgen, diff --git a/src/gen/templates/controller.t b/loco-gen/src/templates/controller.t similarity index 100% rename from src/gen/templates/controller.t rename to loco-gen/src/templates/controller.t diff --git a/src/gen/templates/controller/api/controller.t b/loco-gen/src/templates/controller/api/controller.t similarity index 100% rename from src/gen/templates/controller/api/controller.t rename to loco-gen/src/templates/controller/api/controller.t diff --git a/src/gen/templates/controller/api/test.t b/loco-gen/src/templates/controller/api/test.t similarity index 100% rename from src/gen/templates/controller/api/test.t rename to loco-gen/src/templates/controller/api/test.t diff --git a/src/gen/templates/controller/html/controller.t b/loco-gen/src/templates/controller/html/controller.t similarity index 100% rename from src/gen/templates/controller/html/controller.t rename to loco-gen/src/templates/controller/html/controller.t diff --git a/src/gen/templates/controller/html/view.t b/loco-gen/src/templates/controller/html/view.t similarity index 100% rename from src/gen/templates/controller/html/view.t rename to loco-gen/src/templates/controller/html/view.t diff --git a/src/gen/templates/controller/htmx/controller.t b/loco-gen/src/templates/controller/htmx/controller.t similarity index 100% rename from src/gen/templates/controller/htmx/controller.t rename to loco-gen/src/templates/controller/htmx/controller.t diff --git a/src/gen/templates/controller/htmx/view.t b/loco-gen/src/templates/controller/htmx/view.t similarity index 100% rename from src/gen/templates/controller/htmx/view.t rename to loco-gen/src/templates/controller/htmx/view.t diff --git a/src/gen/templates/deployment_docker.t b/loco-gen/src/templates/deployment_docker.t similarity index 100% rename from src/gen/templates/deployment_docker.t rename to loco-gen/src/templates/deployment_docker.t diff --git a/src/gen/templates/deployment_docker_ignore.t b/loco-gen/src/templates/deployment_docker_ignore.t similarity index 100% rename from src/gen/templates/deployment_docker_ignore.t rename to loco-gen/src/templates/deployment_docker_ignore.t diff --git a/src/gen/templates/deployment_nginx.t b/loco-gen/src/templates/deployment_nginx.t similarity index 100% rename from src/gen/templates/deployment_nginx.t rename to loco-gen/src/templates/deployment_nginx.t diff --git a/src/gen/templates/deployment_shuttle.t b/loco-gen/src/templates/deployment_shuttle.t similarity index 100% rename from src/gen/templates/deployment_shuttle.t rename to loco-gen/src/templates/deployment_shuttle.t diff --git a/src/gen/templates/deployment_shuttle_config.t b/loco-gen/src/templates/deployment_shuttle_config.t similarity index 100% rename from src/gen/templates/deployment_shuttle_config.t rename to loco-gen/src/templates/deployment_shuttle_config.t diff --git a/src/gen/templates/mailer.t b/loco-gen/src/templates/mailer.t similarity index 100% rename from src/gen/templates/mailer.t rename to loco-gen/src/templates/mailer.t diff --git a/src/gen/templates/mailer_html.t b/loco-gen/src/templates/mailer_html.t similarity index 100% rename from src/gen/templates/mailer_html.t rename to loco-gen/src/templates/mailer_html.t diff --git a/src/gen/templates/mailer_sub.t b/loco-gen/src/templates/mailer_sub.t similarity index 100% rename from src/gen/templates/mailer_sub.t rename to loco-gen/src/templates/mailer_sub.t diff --git a/src/gen/templates/mailer_text.t b/loco-gen/src/templates/mailer_text.t similarity index 100% rename from src/gen/templates/mailer_text.t rename to loco-gen/src/templates/mailer_text.t diff --git a/src/gen/templates/migration.t b/loco-gen/src/templates/migration.t similarity index 100% rename from src/gen/templates/migration.t rename to loco-gen/src/templates/migration.t diff --git a/src/gen/templates/model.t b/loco-gen/src/templates/model.t similarity index 100% rename from src/gen/templates/model.t rename to loco-gen/src/templates/model.t diff --git a/src/gen/templates/model_test.t b/loco-gen/src/templates/model_test.t similarity index 100% rename from src/gen/templates/model_test.t rename to loco-gen/src/templates/model_test.t diff --git a/src/gen/templates/request_test.t b/loco-gen/src/templates/request_test.t similarity index 100% rename from src/gen/templates/request_test.t rename to loco-gen/src/templates/request_test.t diff --git a/src/gen/templates/scaffold/api/controller.t b/loco-gen/src/templates/scaffold/api/controller.t similarity index 100% rename from src/gen/templates/scaffold/api/controller.t rename to loco-gen/src/templates/scaffold/api/controller.t diff --git a/src/gen/templates/scaffold/api/test.t b/loco-gen/src/templates/scaffold/api/test.t similarity index 100% rename from src/gen/templates/scaffold/api/test.t rename to loco-gen/src/templates/scaffold/api/test.t diff --git a/src/gen/templates/scaffold/html/base.t b/loco-gen/src/templates/scaffold/html/base.t similarity index 100% rename from src/gen/templates/scaffold/html/base.t rename to loco-gen/src/templates/scaffold/html/base.t diff --git a/src/gen/templates/scaffold/html/controller.t b/loco-gen/src/templates/scaffold/html/controller.t similarity index 100% rename from src/gen/templates/scaffold/html/controller.t rename to loco-gen/src/templates/scaffold/html/controller.t diff --git a/src/gen/templates/scaffold/html/view.t b/loco-gen/src/templates/scaffold/html/view.t similarity index 100% rename from src/gen/templates/scaffold/html/view.t rename to loco-gen/src/templates/scaffold/html/view.t diff --git a/src/gen/templates/scaffold/html/view_create.t b/loco-gen/src/templates/scaffold/html/view_create.t similarity index 100% rename from src/gen/templates/scaffold/html/view_create.t rename to loco-gen/src/templates/scaffold/html/view_create.t diff --git a/src/gen/templates/scaffold/html/view_edit.t b/loco-gen/src/templates/scaffold/html/view_edit.t similarity index 100% rename from src/gen/templates/scaffold/html/view_edit.t rename to loco-gen/src/templates/scaffold/html/view_edit.t diff --git a/src/gen/templates/scaffold/html/view_list.t b/loco-gen/src/templates/scaffold/html/view_list.t similarity index 100% rename from src/gen/templates/scaffold/html/view_list.t rename to loco-gen/src/templates/scaffold/html/view_list.t diff --git a/src/gen/templates/scaffold/html/view_show.t b/loco-gen/src/templates/scaffold/html/view_show.t similarity index 100% rename from src/gen/templates/scaffold/html/view_show.t rename to loco-gen/src/templates/scaffold/html/view_show.t diff --git a/src/gen/templates/scaffold/htmx/base.t b/loco-gen/src/templates/scaffold/htmx/base.t similarity index 100% rename from src/gen/templates/scaffold/htmx/base.t rename to loco-gen/src/templates/scaffold/htmx/base.t diff --git a/src/gen/templates/scaffold/htmx/controller.t b/loco-gen/src/templates/scaffold/htmx/controller.t similarity index 100% rename from src/gen/templates/scaffold/htmx/controller.t rename to loco-gen/src/templates/scaffold/htmx/controller.t diff --git a/src/gen/templates/scaffold/htmx/view.t b/loco-gen/src/templates/scaffold/htmx/view.t similarity index 100% rename from src/gen/templates/scaffold/htmx/view.t rename to loco-gen/src/templates/scaffold/htmx/view.t diff --git a/src/gen/templates/scaffold/htmx/view_create.t b/loco-gen/src/templates/scaffold/htmx/view_create.t similarity index 100% rename from src/gen/templates/scaffold/htmx/view_create.t rename to loco-gen/src/templates/scaffold/htmx/view_create.t diff --git a/src/gen/templates/scaffold/htmx/view_edit.t b/loco-gen/src/templates/scaffold/htmx/view_edit.t similarity index 100% rename from src/gen/templates/scaffold/htmx/view_edit.t rename to loco-gen/src/templates/scaffold/htmx/view_edit.t diff --git a/src/gen/templates/scaffold/htmx/view_list.t b/loco-gen/src/templates/scaffold/htmx/view_list.t similarity index 100% rename from src/gen/templates/scaffold/htmx/view_list.t rename to loco-gen/src/templates/scaffold/htmx/view_list.t diff --git a/src/gen/templates/scaffold/htmx/view_show.t b/loco-gen/src/templates/scaffold/htmx/view_show.t similarity index 100% rename from src/gen/templates/scaffold/htmx/view_show.t rename to loco-gen/src/templates/scaffold/htmx/view_show.t diff --git a/src/gen/templates/scheduler.t b/loco-gen/src/templates/scheduler.t similarity index 100% rename from src/gen/templates/scheduler.t rename to loco-gen/src/templates/scheduler.t diff --git a/src/gen/templates/task.t b/loco-gen/src/templates/task.t similarity index 100% rename from src/gen/templates/task.t rename to loco-gen/src/templates/task.t diff --git a/src/gen/templates/task_test.t b/loco-gen/src/templates/task_test.t similarity index 100% rename from src/gen/templates/task_test.t rename to loco-gen/src/templates/task_test.t diff --git a/src/gen/templates/worker.t b/loco-gen/src/templates/worker.t similarity index 100% rename from src/gen/templates/worker.t rename to loco-gen/src/templates/worker.t diff --git a/src/gen/templates/worker_test.t b/loco-gen/src/templates/worker_test.t similarity index 100% rename from src/gen/templates/worker_test.t rename to loco-gen/src/templates/worker_test.t diff --git a/src/gen/testutil.rs b/loco-gen/src/testutil.rs similarity index 100% rename from src/gen/testutil.rs rename to loco-gen/src/testutil.rs diff --git a/src/cli.rs b/src/cli.rs index 593dd6804..731bb8435 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -28,6 +28,7 @@ use std::path::PathBuf; use clap::{ArgAction, Parser, Subcommand}; use duct::cmd; +use loco_gen::{Component, ScaffoldKind}; use crate::{ app::{AppContext, Hooks}, @@ -35,8 +36,8 @@ use crate::{ create_app, create_context, list_endpoints, list_middlewares, run_scheduler, run_task, start, RunDbCommand, ServeParams, StartMode, }, + config::Config, environment::{resolve_from_env, Environment, DEFAULT_ENVIRONMENT}, - gen::{self, Component, ScaffoldKind}, logger, task, Error, }; #[derive(Parser)] @@ -190,7 +191,7 @@ enum ComponentArg { /// The kind of scaffold to generate #[clap(short, long, value_enum, group = "scaffold_kind_group")] - kind: Option, + kind: Option, /// Use HTMX scaffold #[clap(long, group = "scaffold_kind_group")] @@ -214,7 +215,7 @@ enum ComponentArg { /// The kind of controller actions to generate #[clap(short, long, value_enum, group = "scaffold_kind_group")] - kind: Option, + kind: Option, /// Use HTMX controller actions #[clap(long, group = "scaffold_kind_group")] @@ -249,26 +250,25 @@ enum ComponentArg { Deployment {}, } -impl TryFrom for Component { - type Error = crate::Error; - fn try_from(value: ComponentArg) -> Result { - match value { +impl ComponentArg { + fn into_gen_component(self, config: &Config) -> crate::Result { + match self { #[cfg(feature = "with-db")] - ComponentArg::Model { + Self::Model { name, link, migration_only, fields, - } => Ok(Self::Model { + } => Ok(Component::Model { name, link, migration_only, fields, }), #[cfg(feature = "with-db")] - ComponentArg::Migration { name } => Ok(Self::Migration { name }), + Self::Migration { name } => Ok(Component::Migration { name }), #[cfg(feature = "with-db")] - ComponentArg::Scaffold { + Self::Scaffold { name, fields, kind, @@ -290,9 +290,9 @@ impl TryFrom for Component { )); }; - Ok(Self::Scaffold { name, fields, kind }) + Ok(Component::Scaffold { name, fields, kind }) } - ComponentArg::Controller { + Self::Controller { name, actions, kind, @@ -314,17 +314,38 @@ impl TryFrom for Component { )); }; - Ok(Self::Controller { + Ok(Component::Controller { name, actions, kind, }) } - ComponentArg::Task { name } => Ok(Self::Task { name }), - ComponentArg::Scheduler {} => Ok(Self::Scheduler {}), - ComponentArg::Worker { name } => Ok(Self::Worker { name }), - ComponentArg::Mailer { name } => Ok(Self::Mailer { name }), - ComponentArg::Deployment {} => Ok(Self::Deployment {}), + Self::Task { name } => Ok(Component::Task { name }), + Self::Scheduler {} => Ok(Component::Scheduler {}), + Self::Worker { name } => Ok(Component::Worker { name }), + Self::Mailer { name } => Ok(Component::Mailer { name }), + Self::Deployment {} => { + let copy_asset_folder = &config + .server + .middlewares + .static_assets + .clone() + .map(|a| a.folder.path); + + let fallback_file = &config + .server + .middlewares + .static_assets + .clone() + .map(|a| a.fallback); + + Ok(Component::Deployment { + asset_folder: copy_asset_folder.clone(), + fallback_file: fallback_file.clone(), + host: config.server.host.clone(), + port: config.server.port, + }) + } } } } @@ -427,8 +448,7 @@ pub async fn playground() -> crate::Result { #[allow(clippy::cognitive_complexity)] pub async fn main() -> crate::Result<()> { use colored::Colorize; - - use crate::gen::AppInfo; + use loco_gen::AppInfo; let cli: Cli = Cli::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); @@ -513,9 +533,8 @@ pub async fn main() -> crate::Result<()> { run_scheduler::(&app_context, config.as_ref(), name, tag, list).await?; } Commands::Generate { component } => { - gen::generate( - component.try_into()?, - &config, + loco_gen::generate( + component.into_gen_component(&config)?, &AppInfo { app_name: H::app_name().to_string(), }, @@ -570,6 +589,7 @@ pub async fn main() -> crate::Result<()> { #[cfg(not(feature = "with-db"))] pub async fn main() -> crate::Result<()> { use colored::Colorize; + use loco_gen::AppInfo; let cli = Cli::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); @@ -647,7 +667,12 @@ pub async fn main() -> crate::Result<()> { run_scheduler::(&app_context, config.as_ref(), name, tag, list).await?; } Commands::Generate { component } => { - gen::generate::(component.try_into()?, &config)?; + gen::generate( + component.into_gen_component(&config)?, + &AppInfo { + app_name: H::app_name().to_string(), + }, + )?; } Commands::Version {} => { println!("{}", H::app_version(),); diff --git a/src/errors.rs b/src/errors.rs index 334e3e0f8..58fe9770e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -84,9 +84,6 @@ pub enum Error { #[error(transparent)] DB(#[from] sea_orm::DbErr), - #[error(transparent)] - RRgen(#[from] rrgen::Error), - #[error(transparent)] ParseAddress(#[from] AddressError), @@ -145,6 +142,9 @@ pub enum Error { #[error(transparent)] Cache(#[from] crate::cache::CacheError), + #[error(transparent)] + Generators(#[from] loco_gen::Error), + #[error(transparent)] Any(#[from] Box), } diff --git a/src/lib.rs b/src/lib.rs index 5b3568b78..ce662c702 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,6 @@ pub mod config; pub mod controller; pub mod environment; pub mod errors; -mod gen; pub mod hash; mod logger; pub mod mailer;