Skip to main content

espforge_lib/compile/
mod.rs

1use anyhow::{Context, Result};
2use espforge_configuration::EspforgeConfiguration;
3use std::fs;
4use std::path::Path;
5
6use crate::parse::ConfigParser;
7
8mod assets;
9mod dependencies;
10mod generators;
11
12pub fn compile_project(config_path: &Path) -> Result<()> {
13    let compiler = ProjectCompiler::new(config_path)?;
14    compiler.run()
15}
16
17struct ProjectCompiler {
18    model: EspforgeConfiguration,
19}
20
21impl ProjectCompiler {
22    fn new(config_path: &Path) -> Result<Self> {
23        println!("🔍 Parsing configuration...");
24        let content = fs::read_to_string(config_path).context(format!(
25            "Failed to read configuration file: {}",
26            config_path.display()
27        ))?;
28
29        let parser = ConfigParser::new();
30        let model = parser.parse(&content)?;
31
32        Ok(Self { model })
33    }
34
35    fn run(&self) -> Result<()> {
36        println!("   Project: {}", self.model.get_name());
37        println!("   Chip:    {}", self.model.get_chip());
38        println!("   Runtime: {}", self.model.runtime_name());
39        println!("🔨 Generating artifacts...");
40
41        let project_dir = std::env::current_dir().context("Failed to get current directory")?;
42        let src_dir = project_dir.join("src");
43        
44        generators::generate_scaffold(&self.model, &project_dir)?;
45        
46        let additional_modules = assets::inject_app_code(&src_dir)?;
47        generators::setup_library_structure(&src_dir, &additional_modules)?;
48        generators::generate_component_code(&src_dir, &self.model)?;
49        generators::generate_entry_point(&src_dir, &self.model)?;
50        
51        dependencies::add_dependencies(&project_dir, &self.model)?;
52        assets::generate_wokwi_config(&project_dir, &self.model)?;
53        assets::copy_wokwi_files(&project_dir)?;
54
55        println!("✨ Rust project generated successfully!");
56        println!();
57        println!("To build run: cargo build");
58
59        Ok(())
60    }
61
62    // fn resolve_project_dir(&self) -> Result<PathBuf> {
63    //     Ok(self.base_dir.clone())
64    // }
65}