|
| 1 | +use std::ffi::OsString; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +use anyhow::{Context, Result, ensure}; |
| 6 | +use clap::Parser; |
| 7 | + |
| 8 | +const HYPERLIGHT_VERSION: &str = "0.13"; |
| 9 | + |
| 10 | +const GUEST_CARGO_TOML: &str = include_str!("guest/_Cargo.toml"); |
| 11 | +const GUEST_MAIN_RS: &str = include_str!("guest/_main.rs"); |
| 12 | +const HOST_CARGO_TOML: &str = include_str!("host/_Cargo.toml"); |
| 13 | +const HOST_MAIN_RS: &str = include_str!("host/_main.rs"); |
| 14 | +const GITIGNORE: &str = include_str!("_gitignore"); |
| 15 | + |
| 16 | +/// Scaffold a new Hyperlight project. |
| 17 | +#[derive(Parser, Debug)] |
| 18 | +#[command(name = "scaffold")] |
| 19 | +struct ScaffoldCli { |
| 20 | + /// Path to create the project at. The directory name is used as the crate |
| 21 | + /// name (like `cargo new`). |
| 22 | + path: PathBuf, |
| 23 | + |
| 24 | + /// Generate only a guest project instead of both host and guest. |
| 25 | + #[arg(long, default_value_t = false)] |
| 26 | + guest_only: bool, |
| 27 | +} |
| 28 | + |
| 29 | +pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> { |
| 30 | + let cli = ScaffoldCli::parse_from(args); |
| 31 | + |
| 32 | + let name = cli |
| 33 | + .path |
| 34 | + .file_name() |
| 35 | + .context("Invalid project path")? |
| 36 | + .to_str() |
| 37 | + .context("Project name must be valid UTF-8")?; |
| 38 | + |
| 39 | + ensure!(!name.is_empty(), "Project name must not be empty"); |
| 40 | + ensure!( |
| 41 | + !cli.path.exists(), |
| 42 | + "Directory '{}' already exists", |
| 43 | + cli.path.display() |
| 44 | + ); |
| 45 | + |
| 46 | + if cli.guest_only { |
| 47 | + write_guest(&cli.path, name)?; |
| 48 | + } else { |
| 49 | + let guest_name = format!("{name}-guest"); |
| 50 | + write_guest(&cli.path.join("guest"), &guest_name)?; |
| 51 | + write_host(&cli.path.join("host"), &format!("{name}-host"), &guest_name)?; |
| 52 | + } |
| 53 | + write_file(cli.path.join(".gitignore"), GITIGNORE)?; |
| 54 | + |
| 55 | + let dir = cli.path.display(); |
| 56 | + println!("Created project at '{dir}'\n"); |
| 57 | + if cli.guest_only { |
| 58 | + println!("Build:"); |
| 59 | + println!(" cd {dir} && cargo hyperlight build"); |
| 60 | + } else { |
| 61 | + println!("Build and run:"); |
| 62 | + println!(" cd {dir}/guest && cargo hyperlight build"); |
| 63 | + println!(" cd {dir}/host && cargo run"); |
| 64 | + } |
| 65 | + |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +fn write_guest(dir: &Path, name: &str) -> Result<()> { |
| 70 | + let cargo_toml = GUEST_CARGO_TOML |
| 71 | + .replace("{name}", name) |
| 72 | + .replace("{version}", HYPERLIGHT_VERSION); |
| 73 | + write_file(dir.join("Cargo.toml"), &cargo_toml)?; |
| 74 | + write_file(dir.join("src/main.rs"), GUEST_MAIN_RS)?; |
| 75 | + Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +fn write_host(dir: &Path, name: &str, guest_name: &str) -> Result<()> { |
| 79 | + let cargo_toml = HOST_CARGO_TOML |
| 80 | + .replace("{name}", name) |
| 81 | + .replace("{version}", HYPERLIGHT_VERSION); |
| 82 | + let main_rs = HOST_MAIN_RS |
| 83 | + .replace("{name}", name) |
| 84 | + .replace("{guest_name}", guest_name); |
| 85 | + write_file(dir.join("Cargo.toml"), &cargo_toml)?; |
| 86 | + write_file(dir.join("src/main.rs"), &main_rs)?; |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +fn write_file(path: impl AsRef<Path>, content: &str) -> Result<()> { |
| 91 | + let path = path.as_ref(); |
| 92 | + if let Some(parent) = path.parent() { |
| 93 | + fs::create_dir_all(parent) |
| 94 | + .with_context(|| format!("Failed to create directory '{}'", parent.display()))?; |
| 95 | + } |
| 96 | + fs::write(path, content).with_context(|| format!("Failed to write '{}'", path.display()))?; |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments