From 40fca68e54741dc9f10b8cfd4a0a92faf1e1b7d9 Mon Sep 17 00:00:00 2001 From: "Dotan J. Nahum" Date: Fri, 6 Dec 2024 09:59:25 +0200 Subject: [PATCH] restructure templates --- loco-gen/src/lib.rs | 33 +++++++--------- loco-gen/src/model.rs | 4 +- loco-gen/src/templates/controller.t | 35 ----------------- .../docker/docker.t} | 0 .../docker/ignore.t} | 0 .../nginx/nginx.t} | 0 .../shuttle/config.t} | 0 .../shuttle/shuttle.t} | 0 .../{mailer_html.t => mailer/html.t} | 0 loco-gen/src/templates/{ => mailer}/mailer.t | 0 .../{mailer_sub.t => mailer/subject.t} | 0 .../{mailer_text.t => mailer/text.t} | 0 .../src/templates/{ => migration}/migration.t | 0 loco-gen/src/templates/{ => model}/model.t | 0 .../templates/{model_test.t => model/test.t} | 0 loco-gen/src/templates/request_test.t | 39 ------------------- .../src/templates/{ => scheduler}/scheduler.t | 0 loco-gen/src/templates/{ => task}/task.t | 0 .../templates/{task_test.t => task/test.t} | 0 .../{worker_test.t => worker/test.t} | 0 loco-gen/src/templates/{ => worker}/worker.t | 0 21 files changed, 17 insertions(+), 94 deletions(-) delete mode 100644 loco-gen/src/templates/controller.t rename loco-gen/src/templates/{deployment_docker.t => deployment/docker/docker.t} (100%) rename loco-gen/src/templates/{deployment_docker_ignore.t => deployment/docker/ignore.t} (100%) rename loco-gen/src/templates/{deployment_nginx.t => deployment/nginx/nginx.t} (100%) rename loco-gen/src/templates/{deployment_shuttle_config.t => deployment/shuttle/config.t} (100%) rename loco-gen/src/templates/{deployment_shuttle.t => deployment/shuttle/shuttle.t} (100%) rename loco-gen/src/templates/{mailer_html.t => mailer/html.t} (100%) rename loco-gen/src/templates/{ => mailer}/mailer.t (100%) rename loco-gen/src/templates/{mailer_sub.t => mailer/subject.t} (100%) rename loco-gen/src/templates/{mailer_text.t => mailer/text.t} (100%) rename loco-gen/src/templates/{ => migration}/migration.t (100%) rename loco-gen/src/templates/{ => model}/model.t (100%) rename loco-gen/src/templates/{model_test.t => model/test.t} (100%) delete mode 100644 loco-gen/src/templates/request_test.t rename loco-gen/src/templates/{ => scheduler}/scheduler.t (100%) rename loco-gen/src/templates/{ => task}/task.t (100%) rename loco-gen/src/templates/{task_test.t => task/test.t} (100%) rename loco-gen/src/templates/{worker_test.t => worker/test.t} (100%) rename loco-gen/src/templates/{ => worker}/worker.t (100%) diff --git a/loco-gen/src/lib.rs b/loco-gen/src/lib.rs index 2f5f11daa..90ed56a93 100644 --- a/loco-gen/src/lib.rs +++ b/loco-gen/src/lib.rs @@ -15,30 +15,27 @@ mod scaffold; mod testutil; use std::{str::FromStr, sync::OnceLock}; -const CONTROLLER_T: &str = include_str!("templates/controller.t"); -const CONTROLLER_TEST_T: &str = include_str!("templates/request_test.t"); +const MAILER_T: &str = include_str!("templates/mailer/mailer.t"); +const MAILER_SUB_T: &str = include_str!("templates/mailer/subject.t"); +const MAILER_TEXT_T: &str = include_str!("templates/mailer/text.t"); +const MAILER_HTML_T: &str = include_str!("templates/mailer/html.t"); -const MAILER_T: &str = include_str!("templates/mailer.t"); -const MAILER_SUB_T: &str = include_str!("templates/mailer_sub.t"); -const MAILER_TEXT_T: &str = include_str!("templates/mailer_text.t"); -const MAILER_HTML_T: &str = include_str!("templates/mailer_html.t"); +const MIGRATION_T: &str = include_str!("templates/migration/migration.t"); -const MIGRATION_T: &str = include_str!("templates/migration.t"); +const TASK_T: &str = include_str!("templates/task/task.t"); +const TASK_TEST_T: &str = include_str!("templates/task/test.t"); -const TASK_T: &str = include_str!("templates/task.t"); -const TASK_TEST_T: &str = include_str!("templates/task_test.t"); +const SCHEDULER_T: &str = include_str!("templates/scheduler/scheduler.t"); -const SCHEDULER_T: &str = include_str!("templates/scheduler.t"); - -const WORKER_T: &str = include_str!("templates/worker.t"); -const WORKER_TEST_T: &str = include_str!("templates/worker_test.t"); +const WORKER_T: &str = include_str!("templates/worker/worker.t"); +const WORKER_TEST_T: &str = include_str!("templates/worker/test.t"); // Deployment templates -const DEPLOYMENT_DOCKER_T: &str = include_str!("templates/deployment_docker.t"); -const DEPLOYMENT_DOCKER_IGNORE_T: &str = include_str!("templates/deployment_docker_ignore.t"); -const DEPLOYMENT_SHUTTLE_T: &str = include_str!("templates/deployment_shuttle.t"); -const DEPLOYMENT_SHUTTLE_CONFIG_T: &str = include_str!("templates/deployment_shuttle_config.t"); -const DEPLOYMENT_NGINX_T: &str = include_str!("templates/deployment_nginx.t"); +const DEPLOYMENT_DOCKER_T: &str = include_str!("templates/deployment/docker/docker.t"); +const DEPLOYMENT_DOCKER_IGNORE_T: &str = include_str!("templates/deployment/docker/ignore.t"); +const DEPLOYMENT_SHUTTLE_T: &str = include_str!("templates/deployment/shuttle/shuttle.t"); +const DEPLOYMENT_SHUTTLE_CONFIG_T: &str = include_str!("templates/deployment/shuttle/config.t"); +const DEPLOYMENT_NGINX_T: &str = include_str!("templates/deployment/nginx/nginx.t"); const DEPLOYMENT_SHUTTLE_RUNTIME_VERSION: &str = "0.46.0"; diff --git a/loco-gen/src/model.rs b/loco-gen/src/model.rs index 0e1a74041..928f280be 100644 --- a/loco-gen/src/model.rs +++ b/loco-gen/src/model.rs @@ -8,8 +8,8 @@ use serde_json::json; use super::{Error, Result}; use crate::get_mappings; -const MODEL_T: &str = include_str!("templates/model.t"); -const MODEL_TEST_T: &str = include_str!("templates/model_test.t"); +const MODEL_T: &str = include_str!("templates/model/model.t"); +const MODEL_TEST_T: &str = include_str!("templates/model/test.t"); use super::{collect_messages, AppInfo}; diff --git a/loco-gen/src/templates/controller.t b/loco-gen/src/templates/controller.t deleted file mode 100644 index c1b4c202d..000000000 --- a/loco-gen/src/templates/controller.t +++ /dev/null @@ -1,35 +0,0 @@ -{% set file_name = name | snake_case -%} -{% set module_name = file_name | pascal_case -%} -to: src/controllers/{{ file_name }}.rs -skip_exists: true -message: "Controller `{{module_name}}` was added successfully." -injections: -- into: src/controllers/mod.rs - append: true - content: "pub mod {{ file_name }};" -- into: src/app.rs - after: "AppRoutes::" - content: " .add_route(controllers::{{ file_name }}::routes())" ---- -#![allow(clippy::unused_async)] -use loco_rs::prelude::*; -use axum::debug_handler; - - -#[debug_handler] -pub async fn echo(req_body: String) -> String { - req_body -} - -#[debug_handler] -pub async fn hello(State(_ctx): State) -> Result { - // do something with context (database, etc) - format::text("hello") -} - -pub fn routes() -> Routes { - Routes::new() - .prefix("{{ name | snake_case }}") - .add("/", get(hello)) - .add("/echo", post(echo)) -} diff --git a/loco-gen/src/templates/deployment_docker.t b/loco-gen/src/templates/deployment/docker/docker.t similarity index 100% rename from loco-gen/src/templates/deployment_docker.t rename to loco-gen/src/templates/deployment/docker/docker.t diff --git a/loco-gen/src/templates/deployment_docker_ignore.t b/loco-gen/src/templates/deployment/docker/ignore.t similarity index 100% rename from loco-gen/src/templates/deployment_docker_ignore.t rename to loco-gen/src/templates/deployment/docker/ignore.t diff --git a/loco-gen/src/templates/deployment_nginx.t b/loco-gen/src/templates/deployment/nginx/nginx.t similarity index 100% rename from loco-gen/src/templates/deployment_nginx.t rename to loco-gen/src/templates/deployment/nginx/nginx.t diff --git a/loco-gen/src/templates/deployment_shuttle_config.t b/loco-gen/src/templates/deployment/shuttle/config.t similarity index 100% rename from loco-gen/src/templates/deployment_shuttle_config.t rename to loco-gen/src/templates/deployment/shuttle/config.t diff --git a/loco-gen/src/templates/deployment_shuttle.t b/loco-gen/src/templates/deployment/shuttle/shuttle.t similarity index 100% rename from loco-gen/src/templates/deployment_shuttle.t rename to loco-gen/src/templates/deployment/shuttle/shuttle.t diff --git a/loco-gen/src/templates/mailer_html.t b/loco-gen/src/templates/mailer/html.t similarity index 100% rename from loco-gen/src/templates/mailer_html.t rename to loco-gen/src/templates/mailer/html.t diff --git a/loco-gen/src/templates/mailer.t b/loco-gen/src/templates/mailer/mailer.t similarity index 100% rename from loco-gen/src/templates/mailer.t rename to loco-gen/src/templates/mailer/mailer.t diff --git a/loco-gen/src/templates/mailer_sub.t b/loco-gen/src/templates/mailer/subject.t similarity index 100% rename from loco-gen/src/templates/mailer_sub.t rename to loco-gen/src/templates/mailer/subject.t diff --git a/loco-gen/src/templates/mailer_text.t b/loco-gen/src/templates/mailer/text.t similarity index 100% rename from loco-gen/src/templates/mailer_text.t rename to loco-gen/src/templates/mailer/text.t diff --git a/loco-gen/src/templates/migration.t b/loco-gen/src/templates/migration/migration.t similarity index 100% rename from loco-gen/src/templates/migration.t rename to loco-gen/src/templates/migration/migration.t diff --git a/loco-gen/src/templates/model.t b/loco-gen/src/templates/model/model.t similarity index 100% rename from loco-gen/src/templates/model.t rename to loco-gen/src/templates/model/model.t diff --git a/loco-gen/src/templates/model_test.t b/loco-gen/src/templates/model/test.t similarity index 100% rename from loco-gen/src/templates/model_test.t rename to loco-gen/src/templates/model/test.t diff --git a/loco-gen/src/templates/request_test.t b/loco-gen/src/templates/request_test.t deleted file mode 100644 index 08d02f0a1..000000000 --- a/loco-gen/src/templates/request_test.t +++ /dev/null @@ -1,39 +0,0 @@ -{% set file_name = name | snake_case -%} -{% set module_name = file_name | pascal_case -%} -to: tests/requests/{{ file_name }}.rs -skip_exists: true -message: "Tests for controller `{{module_name}}` was added successfully. Run `cargo test`." -injections: -- into: tests/requests/mod.rs - append: true - content: "pub mod {{ file_name }};" ---- -use {{pkg_name}}::app::App; -use loco_rs::testing::prelude::*; -use serial_test::serial; - -#[tokio::test] -#[serial] -async fn can_get_echo() { - request::(|request, _ctx| async move { - let payload = serde_json::json!({ - "foo": "bar", - }); - - let res = request.post("/{{ name | snake_case }}/echo").json(&payload).await; - assert_eq!(res.status_code(), 200); - assert_eq!(res.text(), serde_json::to_string(&payload).unwrap()); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_request_root() { - request::(|request, _ctx| async move { - let res = request.get("/{{ name | snake_case }}").await; - assert_eq!(res.status_code(), 200); - assert_eq!(res.text(), "hello"); - }) - .await; -} diff --git a/loco-gen/src/templates/scheduler.t b/loco-gen/src/templates/scheduler/scheduler.t similarity index 100% rename from loco-gen/src/templates/scheduler.t rename to loco-gen/src/templates/scheduler/scheduler.t diff --git a/loco-gen/src/templates/task.t b/loco-gen/src/templates/task/task.t similarity index 100% rename from loco-gen/src/templates/task.t rename to loco-gen/src/templates/task/task.t diff --git a/loco-gen/src/templates/task_test.t b/loco-gen/src/templates/task/test.t similarity index 100% rename from loco-gen/src/templates/task_test.t rename to loco-gen/src/templates/task/test.t diff --git a/loco-gen/src/templates/worker_test.t b/loco-gen/src/templates/worker/test.t similarity index 100% rename from loco-gen/src/templates/worker_test.t rename to loco-gen/src/templates/worker/test.t diff --git a/loco-gen/src/templates/worker.t b/loco-gen/src/templates/worker/worker.t similarity index 100% rename from loco-gen/src/templates/worker.t rename to loco-gen/src/templates/worker/worker.t