Skip to content

Commit

Permalink
allow override loco template
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Dec 26, 2024
1 parent 481e1d0 commit e249077
Show file tree
Hide file tree
Showing 21 changed files with 862 additions and 445 deletions.
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
6 changes: 4 additions & 2 deletions loco-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 }
Expand All @@ -24,9 +24,11 @@ tracing = { workspace = true }
chrono = { 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"
53 changes: 22 additions & 31 deletions loco-gen/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
use rrgen::RRgen;
use serde_json::json;

use super::{AppInfo, Result};
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 rrgen::{GenResult, RRgen};
use serde_json::json;
use std::path::Path;

pub fn generate(
rrgen: &RRgen,
name: &str,
actions: &[String],
kind: &gen::ScaffoldKind,
appinfo: &AppInfo,
) -> Result<String> {
) -> Result<Vec<GenResult>> {
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)?);
gen_result.extend(gen::render_template(
rrgen,
Path::new("controller/html/view.t"),
&vars,
)?);
}
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)?);
gen_result.extend(gen::render_template(
rrgen,
Path::new("controller/htmx/view.t"),
&vars,
)?);
}
Ok(collect_messages(messages))
Ok(gen_result)
}
}
}
Loading

0 comments on commit e249077

Please sign in to comment.