|
| 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.15"; |
| 9 | +// TODO: support aarch64-hyperlight-none when aarch64 guests are supported. |
| 10 | +const GUEST_ARCH: &str = "x86_64"; |
| 11 | + |
| 12 | +const GUEST_CARGO_TOML: &str = include_str!("guest/_Cargo.toml"); |
| 13 | +const GUEST_MAIN_RS: &str = include_str!("guest/_main.rs"); |
| 14 | +const HOST_CARGO_TOML: &str = include_str!("host/_Cargo.toml"); |
| 15 | +const HOST_MAIN_RS: &str = include_str!("host/_main.rs"); |
| 16 | +const GITIGNORE: &str = include_str!("_gitignore"); |
| 17 | + |
| 18 | +/// Create a new Hyperlight project. |
| 19 | +#[derive(Parser, Debug)] |
| 20 | +#[command(name = "new")] |
| 21 | +struct NewCli { |
| 22 | + /// Path to create the project at. The directory name is used as the crate |
| 23 | + /// name (like `cargo new`). |
| 24 | + path: PathBuf, |
| 25 | + |
| 26 | + /// Skip generating the host crate. |
| 27 | + #[arg(long, default_value_t = false)] |
| 28 | + no_host: bool, |
| 29 | + |
| 30 | + /// Skip generating the guest crate. |
| 31 | + #[arg(long, default_value_t = false, conflicts_with = "no_host")] |
| 32 | + no_guest: bool, |
| 33 | +} |
| 34 | + |
| 35 | +pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> { |
| 36 | + let cli = NewCli::parse_from(args); |
| 37 | + |
| 38 | + let name = cli |
| 39 | + .path |
| 40 | + .file_name() |
| 41 | + .context("Invalid project path")? |
| 42 | + .to_str() |
| 43 | + .context("Project name must be valid UTF-8")?; |
| 44 | + |
| 45 | + validate_name(name)?; |
| 46 | + ensure!( |
| 47 | + !cli.path.exists(), |
| 48 | + "Directory '{}' already exists", |
| 49 | + cli.path.display() |
| 50 | + ); |
| 51 | + |
| 52 | + match (cli.no_host, cli.no_guest) { |
| 53 | + (true, false) => { |
| 54 | + write_guest(&cli.path, name)?; |
| 55 | + } |
| 56 | + (false, true) => { |
| 57 | + write_host(&cli.path, name, &format!("{name}-guest"))?; |
| 58 | + } |
| 59 | + (false, false) => { |
| 60 | + let guest_name = format!("{name}-guest"); |
| 61 | + write_guest(&cli.path.join("guest"), &guest_name)?; |
| 62 | + write_host(&cli.path.join("host"), &format!("{name}-host"), &guest_name)?; |
| 63 | + } |
| 64 | + (true, true) => unreachable!("clap rejects --no-host and --no-guest together"), |
| 65 | + } |
| 66 | + write_file(cli.path.join(".gitignore"), GITIGNORE)?; |
| 67 | + |
| 68 | + let dir = cli.path.display(); |
| 69 | + println!("Created project at '{dir}'\n"); |
| 70 | + match (cli.no_host, cli.no_guest) { |
| 71 | + (true, false) => { |
| 72 | + println!("Build:"); |
| 73 | + println!(" cd {dir} && cargo hyperlight build"); |
| 74 | + } |
| 75 | + (false, true) => { |
| 76 | + println!("Build:"); |
| 77 | + println!(" cd {dir} && cargo build"); |
| 78 | + } |
| 79 | + (false, false) => { |
| 80 | + println!("Build and run:"); |
| 81 | + println!(" cd {dir}/guest && cargo hyperlight build"); |
| 82 | + println!(" cd {dir}/host && cargo run"); |
| 83 | + } |
| 84 | + (true, true) => unreachable!(), |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +fn write_guest(dir: &Path, name: &str) -> Result<()> { |
| 91 | + let cargo_toml = GUEST_CARGO_TOML |
| 92 | + .replace("{name}", name) |
| 93 | + .replace("{version}", HYPERLIGHT_VERSION); |
| 94 | + write_file(dir.join("Cargo.toml"), &cargo_toml)?; |
| 95 | + write_file(dir.join("src/main.rs"), GUEST_MAIN_RS)?; |
| 96 | + Ok(()) |
| 97 | +} |
| 98 | + |
| 99 | +fn write_host(dir: &Path, name: &str, guest_name: &str) -> Result<()> { |
| 100 | + let cargo_toml = HOST_CARGO_TOML |
| 101 | + .replace("{name}", name) |
| 102 | + .replace("{version}", HYPERLIGHT_VERSION); |
| 103 | + let main_rs = HOST_MAIN_RS |
| 104 | + .replace("{name}", name) |
| 105 | + .replace("{guest_name}", guest_name) |
| 106 | + .replace("{arch}", GUEST_ARCH); |
| 107 | + write_file(dir.join("Cargo.toml"), &cargo_toml)?; |
| 108 | + write_file(dir.join("src/main.rs"), &main_rs)?; |
| 109 | + Ok(()) |
| 110 | +} |
| 111 | + |
| 112 | +fn write_file(path: impl AsRef<Path>, content: &str) -> Result<()> { |
| 113 | + let path = path.as_ref(); |
| 114 | + if let Some(parent) = path.parent() { |
| 115 | + fs::create_dir_all(parent) |
| 116 | + .with_context(|| format!("Failed to create directory '{}'", parent.display()))?; |
| 117 | + } |
| 118 | + fs::write(path, content).with_context(|| format!("Failed to write '{}'", path.display()))?; |
| 119 | + Ok(()) |
| 120 | +} |
| 121 | + |
| 122 | +/// Validate that the name is usable as a Cargo package name. |
| 123 | +/// Mirrors the essential checks from `cargo new`. |
| 124 | +fn validate_name(name: &str) -> Result<()> { |
| 125 | + ensure!(!name.is_empty(), "project name must not be empty"); |
| 126 | + ensure!( |
| 127 | + name.chars() |
| 128 | + .all(|c| c.is_alphanumeric() || c == '-' || c == '_'), |
| 129 | + "invalid project name `{name}`: must contain only letters, numbers, `-`, or `_`" |
| 130 | + ); |
| 131 | + ensure!( |
| 132 | + name.chars() |
| 133 | + .next() |
| 134 | + .is_some_and(|c| c.is_alphabetic() || c == '_'), |
| 135 | + "invalid project name `{name}`: must start with a letter or `_`" |
| 136 | + ); |
| 137 | + let reserved = [ |
| 138 | + "test", |
| 139 | + "core", |
| 140 | + "std", |
| 141 | + "alloc", |
| 142 | + "proc_macro", |
| 143 | + "proc-macro", |
| 144 | + "self", |
| 145 | + "Self", |
| 146 | + "crate", |
| 147 | + "super", |
| 148 | + ]; |
| 149 | + ensure!( |
| 150 | + !reserved.contains(&name), |
| 151 | + "invalid project name `{name}`: it conflicts with a Rust built-in name" |
| 152 | + ); |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments