helix_core/compiler/workflow/
run.rs

1use std::path::PathBuf;
2use std::process::Command;
3use anyhow::{Result, Context};
4use crate::compiler::Compiler;
5use crate::compiler::optimizer::OptimizationLevel;
6pub fn run_project(
7    input: Option<PathBuf>,
8    args: Vec<String>,
9    optimize: u8,
10    verbose: bool,
11) -> Result<()> {
12    let project_dir = find_project_root()?;
13    let input_file = match input {
14        Some(path) => path,
15        None => {
16            let main_file = project_dir.join("src").join("main.hlx");
17            if main_file.exists() {
18                main_file
19            } else {
20                return Err(
21                    anyhow::anyhow!(
22                        "No input file specified and no src/main.hlx found.\n\
23                    Specify a file with: helix run <file.hlx>"
24                    ),
25                );
26            }
27        }
28    };
29    if verbose {
30        println!("🚀 Running HELIX project:");
31        println!("  Input: {}", input_file.display());
32        println!("  Optimization: Level {}", optimize);
33        if !args.is_empty() {
34            println!("  Arguments: {:?}", args);
35        }
36    }
37    let output_file = compile_for_run(&input_file, optimize, verbose)?;
38    execute_binary(&output_file, args, verbose)?;
39    Ok(())
40}
41fn compile_for_run(input: &PathBuf, optimize: u8, verbose: bool) -> Result<PathBuf> {
42    let project_dir = find_project_root()?;
43    let target_dir = project_dir.join("target");
44    std::fs::create_dir_all(&target_dir).context("Failed to create target directory")?;
45    let input_stem = input
46        .file_stem()
47        .and_then(|s| s.to_str())
48        .ok_or_else(|| anyhow::anyhow!("Invalid input filename"))?;
49    let output_file = target_dir.join(format!("{}.hlxb", input_stem));
50    if verbose {
51        println!("📦 Compiling for execution...");
52    }
53    let compiler = Compiler::builder()
54        .optimization_level(OptimizationLevel::from(optimize))
55        .compression(true)
56        .cache(true)
57        .verbose(verbose)
58        .build();
59    let binary = compiler.compile_file(input).context("Failed to compile HELIX file")?;
60    let serializer = crate::compiler::serializer::BinarySerializer::new(true);
61    serializer
62        .write_to_file(&binary, &output_file)
63        .context("Failed to write compiled binary")?;
64    if verbose {
65        println!("✅ Compiled successfully: {}", output_file.display());
66        println!("  Size: {} bytes", binary.size());
67    }
68    Ok(output_file)
69}
70fn execute_binary(
71    binary_path: &PathBuf,
72    args: Vec<String>,
73    verbose: bool,
74) -> Result<()> {
75    if verbose {
76        println!("▶️  Executing binary: {}", binary_path.display());
77    }
78    let mut cmd = Command::new("echo");
79    cmd.arg("HELIX Runtime not yet implemented");
80    cmd.arg("Binary compiled successfully:");
81    cmd.arg(binary_path.to_string_lossy().as_ref());
82    if !args.is_empty() {
83        cmd.arg("Arguments:");
84        for arg in &args {
85            cmd.arg(arg);
86        }
87    }
88    let output = cmd.output().context("Failed to execute binary")?;
89    if !output.status.success() {
90        return Err(
91            anyhow::anyhow!(
92                "Binary execution failed with exit code: {}", output.status.code()
93                .unwrap_or(- 1)
94            ),
95        );
96    }
97    if !output.stdout.is_empty() {
98        print!("{}", String::from_utf8_lossy(& output.stdout));
99    }
100    if !output.stderr.is_empty() {
101        eprint!("{}", String::from_utf8_lossy(& output.stderr));
102    }
103    Ok(())
104}
105fn find_project_root() -> Result<PathBuf> {
106    let mut current_dir = std::env::current_dir()
107        .context("Failed to get current directory")?;
108    loop {
109        let manifest_path = current_dir.join("project.hlx");
110        if manifest_path.exists() {
111            return Ok(current_dir);
112        }
113        if let Some(parent) = current_dir.parent() {
114            current_dir = parent.to_path_buf();
115        } else {
116            break;
117        }
118    }
119    Err(anyhow::anyhow!("No HELIX project found. Run 'helix init' first."))
120}