espforge_lib/examples/
template.rs1use super::config::ConfigFile;
2use super::fs::OutputDirectory;
3use crate::cli::model::{ExampleConfig, ExportResult};
4use anyhow::{Context, Result, anyhow};
5use espforge_examples::EXAMPLES_DIR;
6use std::fs;
7use std::path::Path;
8
9pub struct ExampleExporter;
10
11impl Default for ExampleExporter {
12 fn default() -> Self {
13 Self::new()
14 }
15}
16
17impl ExampleExporter {
18 pub fn new() -> Self {
19 Self
20 }
21
22 pub fn export(&self, config: &ExampleConfig, output: &OutputDirectory) -> Result<ExportResult> {
23 let template = ExampleTemplate::find(&config.template_name)?;
24 template.extract_to(output.path())?;
25
26 let app_rust_dir = output.path().join("app").join("rust");
27 let src_dir = output.path().join("src");
28
29 if app_rust_dir.exists() {
30 if !src_dir.exists() {
31 fs::create_dir_all(&src_dir).context("Failed to create src directory")?;
32 }
33
34 for entry in fs::read_dir(&app_rust_dir).context("Failed to read app/rust directory")? {
35 let entry = entry?;
36 let path = entry.path();
37 if path.is_file() {
38 if let Some(file_name) = path.file_name() {
39 let dest = src_dir.join(file_name);
40 fs::rename(&path, &dest).context("Failed to move file to src")?;
41 }
42 }
43 }
44
45 let app_dir = output.path().join("app");
46 if app_dir.exists() {
47 let _ = fs::remove_dir_all(&app_dir);
48 }
49 }
50
51 let config_file = ConfigFile::locate(output.path())?;
52 config_file.update(config)?;
53
54 let final_name = config_file.rename_to(&config.project_name)?;
55
56 Ok(ExportResult {
57 project_name: config.project_name.clone(),
58 output_file: format!("{}.yaml", final_name),
59 })
60 }
61}
62
63struct ExampleTemplate {
64 dir: &'static include_dir::Dir<'static>,
65}
66
67impl ExampleTemplate {
68 fn find(name: &str) -> Result<Self> {
69 let dir = Self::search_in_catalog(name)
70 .ok_or_else(|| anyhow!("Example template '{}' not found", name))?;
71
72 Ok(Self { dir })
73 }
74
75 fn search_in_catalog(name: &str) -> Option<&'static include_dir::Dir<'static>> {
76 EXAMPLES_DIR
77 .dirs()
78 .flat_map(|category| category.dirs())
79 .find(|example| example.path().file_name().and_then(|n| n.to_str()) == Some(name))
80 }
81
82 fn extract_to(&self, target: &Path) -> Result<()> {
83 extract_recursive(self.dir, target, self.dir.path())
84 .context("Failed to extract example files to disk")
85 }
86}
87
88fn extract_recursive(
89 dir: &include_dir::Dir,
90 base_path: &Path,
91 root_prefix: &Path,
92) -> std::io::Result<()> {
93 let dir_path = dir.path();
95 let relative_dir_path = dir_path
96 .strip_prefix(root_prefix)
97 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
98
99 let dest_dir = base_path.join(relative_dir_path);
100 if !dest_dir.exists() {
101 fs::create_dir_all(&dest_dir)?;
102 }
103
104 for file in dir.files() {
105 let path = file.path();
106 let relative_path = path
107 .strip_prefix(root_prefix)
108 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
109
110 let dest_path = base_path.join(relative_path);
111
112 if let Some(parent) = dest_path.parent() {
113 fs::create_dir_all(parent)?;
114 }
115
116 fs::write(dest_path, file.contents())?;
117 }
118
119 for subdir in dir.dirs() {
120 extract_recursive(subdir, base_path, root_prefix)?;
121 }
122
123 Ok(())
124}