feat: add scripts to model

This commit is contained in:
2025-02-18 20:44:26 +01:00
parent 61cd888620
commit 84219f46e0
7 changed files with 352 additions and 13 deletions

View File

@@ -19,3 +19,6 @@ kdl = "6.3.3"
walkdir = "2.5.0"
minijinja = "2.7.0"
glob = "0.3.2"
serde_json = "1.0.138"
syntect = "5.2.0"
syntect-assets = "0.23.6"

View File

@@ -3,6 +3,13 @@ use std::{net::SocketAddr, path::PathBuf};
use clap::{Parser, Subcommand};
use kdl::KdlDocument;
use rusty_s3::{Bucket, Credentials, S3Action};
use syntect::{
easy::HighlightLines,
highlighting::{Style, ThemeSet},
parsing::SyntaxSet,
util::{as_24_bit_terminal_escaped, LinesWithEndings},
};
use syntect_assets::assets::HighlightingAssets;
use tokio::io::AsyncWriteExt;
use crate::{
@@ -31,6 +38,8 @@ enum Commands {
Template {},
Info {},
Serve {
#[arg(env = "FOREST_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr,
@@ -93,6 +102,23 @@ pub async fn execute() -> anyhow::Result<()> {
tracing::trace!("found context: {:?}", context);
}
Commands::Info {} => {
let output = serde_json::to_string_pretty(&context)?;
let assets = HighlightingAssets::from_binary();
let theme = assets.get_theme("OneHalfDark");
let ss = SyntaxSet::load_defaults_nonewlines();
let syntax = ss.find_syntax_by_extension("json").unwrap();
let mut h = HighlightLines::new(syntax, theme);
for line in LinesWithEndings::from(&output) {
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ss).unwrap();
print!("{}", as_24_bit_terminal_escaped(&ranges[..], true));
}
println!()
}
Commands::Template {} => {
tracing::info!("templating");

View File

@@ -1,14 +1,15 @@
use std::{collections::BTreeMap, fmt::Debug, path::PathBuf};
use kdl::{KdlDocument, KdlEntry, KdlNode, KdlValue};
use serde::Serialize;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct Context {
pub project: Project,
pub plan: Option<Plan>,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct Plan {
pub name: String,
}
@@ -38,7 +39,8 @@ impl TryFrom<KdlDocument> for Plan {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum ProjectPlan {
Local { path: PathBuf },
NoPlan,
@@ -66,7 +68,8 @@ impl TryFrom<&KdlNode> for ProjectPlan {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum GlobalVariable {
Map(BTreeMap<String, GlobalVariable>),
String(String),
@@ -125,7 +128,7 @@ impl TryFrom<&KdlValue> for GlobalVariable {
}
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Serialize, Default)]
pub struct Global {
items: BTreeMap<String, GlobalVariable>,
}
@@ -153,14 +156,15 @@ impl TryFrom<&KdlNode> for Global {
}
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Serialize, Default)]
pub enum TemplateType {
#[default]
Jinja2,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct Templates {
#[serde(rename = "type")]
pub ty: TemplateType,
pub path: String,
pub output: PathBuf,
@@ -219,13 +223,50 @@ impl TryFrom<&KdlNode> for Templates {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct Action {}
#[derive(Debug, Clone, Serialize)]
pub struct Scripts {
pub path: PathBuf,
pub actions: BTreeMap<String, Action>,
}
impl TryFrom<&KdlNode> for Scripts {
type Error = anyhow::Error;
fn try_from(value: &KdlNode) -> Result<Self, Self::Error> {
let val = Self {
path: value
.get("path")
.and_then(|p| p.as_string())
.map(PathBuf::from)
.unwrap_or(PathBuf::from("scripts/")),
actions: value
.children()
.and_then(|c| c.get("actions"))
.and_then(|a| a.children())
.map(|d| {
d.nodes()
.iter()
.map(|n| (n.name().value().to_string(), Action {}))
.collect::<BTreeMap<String, Action>>()
})
.unwrap_or_default(),
};
Ok(val)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Project {
pub name: String,
pub description: Option<String>,
pub plan: Option<ProjectPlan>,
pub global: Global,
pub templates: Option<Templates>,
pub scripts: Option<Scripts>,
}
impl TryFrom<KdlDocument> for Project {
@@ -274,6 +315,10 @@ impl TryFrom<KdlDocument> for Project {
.get("templates")
.map(|t| t.try_into())
.transpose()?,
scripts: project_children
.get("scripts")
.map(|m| m.try_into())
.transpose()?,
})
}
}