1use std::path::Path;
2
3use heck::ToPascalCase;
4use rand::rngs::StdRng;
5use rand::{RngCore, SeedableRng};
6use tracing::trace;
7
8use crate::utils::print_status_msg;
9
10macro_rules! project_file {
11 ($name:literal) => {
12 ($name, include_str!(concat!("project_template/", $name)))
13 };
14}
15
16const PROJECT_FILES: [(&str, &str); 9] = [
17 project_file!("Cargo.toml.template"),
18 project_file!("bacon.toml"),
19 project_file!(".gitignore"),
20 project_file!("src/main.rs"),
21 project_file!("src/migrations.rs"),
22 project_file!("static/css/main.css"),
23 project_file!("templates/index.html"),
24 project_file!("config/dev.toml"),
25 project_file!("config/prod.toml.example"),
26];
27
28#[derive(Debug, Clone, PartialEq, Eq, Hash)]
29pub enum CotSource<'a> {
30 Git,
31 #[allow(dead_code)] Path(&'a Path),
33 PublishedCrate,
34}
35
36impl CotSource<'_> {
37 fn as_cargo_toml_source(&self) -> String {
38 match self {
39 CotSource::Git => {
40 "package = \"cot\", git = \"https://github.com/cot-rs/cot.git\"".to_owned()
41 }
42 CotSource::Path(path) => {
43 format!(
44 "path = \"{}\"",
45 path.display().to_string().replace('\\', "\\\\")
46 )
47 }
48 CotSource::PublishedCrate => format!("version = \"{}\"", env!("CARGO_PKG_VERSION")),
49 }
50 }
51}
52
53pub fn new_project(
54 path: &Path,
55 project_name: &str,
56 cot_source: &CotSource<'_>,
57) -> anyhow::Result<()> {
58 print_status_msg("Creating", &format!("Cot project `{project_name}`"));
59
60 if path.exists() {
61 anyhow::bail!("destination `{}` already exists", path.display());
62 }
63
64 let project_struct_name = format!("{}Project", project_name.to_pascal_case());
65 let app_name = format!("{}App", project_name.to_pascal_case());
66 let cot_source = cot_source.as_cargo_toml_source();
67 let dev_secret_key = generate_secret_key();
68
69 for (file_name, content) in PROJECT_FILES {
70 let file_name = file_name.replace(".template", "");
73
74 let file_path = path.join(file_name);
75 trace!("Writing file: {:?}", file_path);
76
77 std::fs::create_dir_all(
78 file_path
79 .parent()
80 .expect("joined path should always have a parent"),
81 )?;
82
83 std::fs::write(
84 file_path,
85 content
86 .replace("{{ project_name }}", project_name)
87 .replace("{{ project_struct_name }}", &project_struct_name)
88 .replace("{{ app_name }}", &app_name)
89 .replace("{{ cot_source }}", &cot_source)
90 .replace("{{ dev_secret_key }}", &dev_secret_key),
91 )?;
92 }
93
94 Ok(())
95}
96
97fn generate_secret_key() -> String {
98 let mut rng = StdRng::from_os_rng();
102 let mut key = [0u8; 32];
103 rng.fill_bytes(&mut key);
104 hex::encode(key)
105}
106
107#[cfg(test)]
108mod tests {
109 use std::path::Path;
110
111 use super::*;
112
113 #[test]
114 fn as_cargo_toml_source_git() {
115 let source = CotSource::Git;
116 assert_eq!(
117 source.as_cargo_toml_source(),
118 "package = \"cot\", git = \"https://github.com/cot-rs/cot.git\""
119 );
120 }
121
122 #[test]
123 fn as_cargo_toml_source_path() {
124 let path = Path::new("/some/local/path");
125 let source = CotSource::Path(path);
126 assert_eq!(source.as_cargo_toml_source(), "path = \"/some/local/path\"");
127 }
128
129 #[test]
130 fn as_cargo_toml_source_path_windows() {
131 let path = Path::new("C:\\some\\local\\path");
132 let source = CotSource::Path(path);
133 assert_eq!(
134 source.as_cargo_toml_source(),
135 "path = \"C:\\\\some\\\\local\\\\path\""
136 );
137 }
138
139 #[test]
140 fn as_cargo_toml_source_published_crate() {
141 let source = CotSource::PublishedCrate;
142 assert_eq!(
143 source.as_cargo_toml_source(),
144 format!("version = \"{}\"", env!("CARGO_PKG_VERSION"))
145 );
146 }
147}