baobao_codegen/generation/
bao_toml.rs1use std::path::{Path, PathBuf};
4
5use baobao_core::{FileRules, GeneratedFile, Overwrite, Version};
6use baobao_manifest::Language;
7
8pub struct BaoToml {
10 pub name: String,
11 pub version: Version,
12 pub description: String,
13 pub language: Language,
14 pub overwrite: Overwrite,
15}
16
17impl BaoToml {
18 pub fn new(name: impl Into<String>, language: Language) -> Self {
19 Self {
20 name: name.into(),
21 version: Version::new(0, 1, 0),
22 description: "A CLI application".to_string(),
23 language,
24 overwrite: Overwrite::IfMissing,
25 }
26 }
27
28 pub fn with_version(mut self, version: Version) -> Self {
29 self.version = version;
30 self
31 }
32
33 pub fn with_description(mut self, description: String) -> Self {
34 self.description = description;
35 self
36 }
37
38 pub fn with_overwrite(mut self, overwrite: Overwrite) -> Self {
39 self.overwrite = overwrite;
40 self
41 }
42}
43
44impl GeneratedFile for BaoToml {
45 fn path(&self, base: &Path) -> PathBuf {
46 base.join("bao.toml")
47 }
48
49 fn rules(&self) -> FileRules {
50 FileRules {
51 overwrite: self.overwrite,
52 header: None,
53 }
54 }
55
56 fn render(&self) -> String {
57 format!(
58 r#"[cli]
59name = "{}"
60version = "{}"
61description = "{}"
62language = "{}"
63
64# Uncomment to add shared resources accessible in all handlers:
65# [context.database]
66# type = "sqlite"
67# env = "DATABASE_URL"
68# create_if_missing = true
69# journal_mode = "wal"
70# synchronous = "normal"
71# busy_timeout = 5000
72# foreign_keys = true
73# max_connections = 5
74#
75# [context.http]
76# type = "http"
77#
78# Supported types: sqlite, postgres, mysql, http
79
80[commands.hello]
81description = "Say hello"
82
83[[commands.hello.args]]
84name = "name"
85type = "string"
86required = false
87description = "Name to greet"
88
89[[commands.hello.flags]]
90name = "uppercase"
91type = "bool"
92short = "u"
93description = "Print in uppercase"
94"#,
95 self.name, self.version, self.description, self.language
96 )
97 }
98}