Skip to content

Commit

Permalink
allow override loco template (#1102)
Browse files Browse the repository at this point in the history
* allow override loco template

* tests

* remove generator check in all templates variation

* add more inforation

* add color

* rrgen template tests

* change snapshot name for window tests
  • Loading branch information
kaplanelad authored Jan 2, 2025
1 parent 337ced4 commit ab3bf0b
Show file tree
Hide file tree
Showing 135 changed files with 4,292 additions and 454 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/loco-gen-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ jobs:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

- name: docker deployment
run: cargo run -- generate deployment
run: cargo run -- generate deployment --kind docker
working-directory: ./examples/demo
env:
LOCO_DEPLOYMENT_KIND: docker
REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}}
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/loco-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ on:
- master
paths:
- "loco-new/**"
- "loco-gen/**"
pull_request:
paths:
- "loco-new/**"
- "loco-gen/**"

env:
RUST_TOOLCHAIN: stable
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ backtrace_printer = { version = "1.3.0" }

# cli
clap = { version = "4.4.7", features = ["derive"], optional = true }
colored = "2"
colored = { workspace = true }
reqwest = { version = "0.12.7", features = [
"charset",
"http2",
Expand Down Expand Up @@ -155,7 +155,7 @@ bb8 = { version = "0.8.1", optional = true }
scraper = { version = "0.21.0", optional = true }

[workspace.dependencies]

colored = { version = "2" }
chrono = { version = "0.4", features = ["serde"] }
tracing = "0.1.40"
regex = "1"
Expand Down
1 change: 1 addition & 0 deletions examples/demo/tests/cmd/cli.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Commands:
worker Generate worker
mailer Generate mailer
deployment Generate a deployment infrastructure
override Override templates and allows you to take control of them. You can always go back when deleting the local template
help Print this message or the help of the given subcommand(s)

Options:
Expand Down
7 changes: 5 additions & 2 deletions loco-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ path = "src/lib.rs"
[dependencies]

cruet = "0.14.0"
rrgen = "0.5.3"
rrgen = "0.5.5"
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
regex = { workspace = true }
tracing = { workspace = true }
chrono = { workspace = true }
colored = { workspace = true }

clap = { version = "4.4.7", features = ["derive"] }
dialoguer = "0.11"
duct = "0.13"
include_dir = { version = "0.7.4" }

[dev-dependencies]
tree-fs = { version = "0.2.1" }
syn = { version = "2", features = ["full"] }
insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] }
rstest = "0.23.0"
47 changes: 17 additions & 30 deletions loco-gen/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
use super::{AppInfo, GenerateResults, Result};
use crate as gen;
use rrgen::RRgen;
use serde_json::json;

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");

const HTMX_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/htmx/controller.t");
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, Result};
use std::path::Path;

pub fn generate(
rrgen: &RRgen,
name: &str,
actions: &[String],
kind: &gen::ScaffoldKind,
appinfo: &AppInfo,
) -> Result<String> {
) -> Result<GenerateResults> {
let vars = json!({"name": name, "actions": actions, "pkg_name": appinfo.app_name});
match kind {
gen::ScaffoldKind::Api => {
let res1 = rrgen.generate(API_CONTROLLER_CONTROLLER_T, &vars)?;
let res2 = rrgen.generate(API_CONTROLLER_TEST_T, &vars)?;
let messages = collect_messages(vec![res1, res2]);
Ok(messages)
}
gen::ScaffoldKind::Api => gen::render_template(rrgen, Path::new("controller/api"), &vars),
gen::ScaffoldKind::Html => {
let mut messages = Vec::new();
let res = rrgen.generate(HTML_CONTROLLER_CONTROLLER_T, &vars)?;
messages.push(res);
let mut gen_result =
gen::render_template(rrgen, Path::new("controller/html/controller.t"), &vars)?;
for action in actions {
let vars = json!({"name": name, "action": action, "pkg_name": appinfo.app_name});
messages.push(rrgen.generate(HTML_VIEW_T, &vars)?);
let res = gen::render_template(rrgen, Path::new("controller/html/view.t"), &vars)?;
gen_result.rrgen.extend(res.rrgen);
gen_result.local_templates.extend(res.local_templates);
}
Ok(collect_messages(messages))
Ok(gen_result)
}
gen::ScaffoldKind::Htmx => {
let mut messages = Vec::new();
let res = rrgen.generate(HTMX_CONTROLLER_CONTROLLER_T, &vars)?;
messages.push(res);
let mut gen_result =
gen::render_template(rrgen, Path::new("controller/htmx/controller.t"), &vars)?;
for action in actions {
let vars = json!({"name": name, "action": action, "pkg_name": appinfo.app_name});
messages.push(rrgen.generate(HTMX_VIEW_T, &vars)?);
let res = gen::render_template(rrgen, Path::new("controller/htmx/view.t"), &vars)?;
gen_result.rrgen.extend(res.rrgen);
gen_result.local_templates.extend(res.local_templates);
}
Ok(collect_messages(messages))
Ok(gen_result)
}
}
}
Loading

0 comments on commit ab3bf0b

Please sign in to comment.