Skip to content

Commit 5fa2a92

Browse files
committed
Support builds for Moon
1 parent 8615faf commit 5fa2a92

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/commands/new.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
2525
Lang::Go => new_go(&args.name).context("new Go project")?,
2626
Lang::Rust => new_rust(&args.name).context("new Rust project")?,
2727
Lang::Zig => new_zig(&args.name).context("new Zig project")?,
28-
Lang::TS => todo!("TypeScript is not supported yet"),
28+
Lang::TS => bail!("TypeScript is not supported yet"),
2929
Lang::C => new_c(&args.name).context("new C project")?,
3030
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
31-
Lang::Python => todo!("Python is not supported yet"),
31+
Lang::Python => bail!("Python is not supported yet"),
32+
Lang::Moon => bail!("Moon starter project is not supported yet"),
3233
}
3334
write_config(&args.name)?;
3435
init_git(&args.name)?;

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,5 @@ pub enum Lang {
183183
C,
184184
Cpp,
185185
Python,
186+
Moon,
186187
}

src/langs.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn build_bin(config: &Config, args: &BuildArgs) -> anyhow::Result<()> {
2828
Lang::C => build_c(config),
2929
Lang::Cpp => build_cpp(config),
3030
Lang::Python => build_python(config),
31+
Lang::Moon => build_moon(config),
3132
}?;
3233
let bin_path = config.rom_path.join(BIN);
3334
if !bin_path.is_file() {
@@ -65,6 +66,9 @@ fn detect_lang(root: &Path) -> anyhow::Result<Lang> {
6566
if root.join("pyproject.toml").exists() {
6667
return Ok(Lang::Python);
6768
}
69+
if root.join("moon.pkg.json").exists() {
70+
return Ok(Lang::Moon);
71+
}
6872
if root.join("main.c").exists() {
6973
return Ok(Lang::C);
7074
}
@@ -308,6 +312,7 @@ fn find_wasi_sdk() -> anyhow::Result<PathBuf> {
308312
Ok(path)
309313
}
310314

315+
// Build Zig project.
311316
fn build_zig(config: &Config) -> anyhow::Result<()> {
312317
check_installed("Zig", "zig", "version")?;
313318
let mut cmd_args = vec!["build"];
@@ -331,6 +336,43 @@ fn build_zig(config: &Config) -> anyhow::Result<()> {
331336
Ok(())
332337
}
333338

339+
// Build Moon project.
340+
fn build_moon(config: &Config) -> anyhow::Result<()> {
341+
check_installed("Moon", "moon", "version")?;
342+
let mut cmd_args = vec!["build", "--target", "wasm"];
343+
if let Some(additional_args) = &config.compile_args {
344+
for arg in additional_args {
345+
cmd_args.push(arg.as_str());
346+
}
347+
}
348+
let output = Command::new("moon")
349+
.args(cmd_args)
350+
.current_dir(&config.root_path)
351+
.output()
352+
.context("run moon build")?;
353+
check_output(&output)?;
354+
355+
let from_dir = config
356+
.root_path
357+
.join("target")
358+
.join("wasm")
359+
.join("release")
360+
.join("build");
361+
let from_path = find_wasm(&from_dir)?;
362+
let out_path = config.rom_path.join(BIN);
363+
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
364+
std::fs::remove_file(from_path).context("remove wasm file")?;
365+
Ok(())
366+
}
367+
368+
fn build_ts(_config: &Config) -> anyhow::Result<()> {
369+
todo!("TypeScript is not supported yet")
370+
}
371+
372+
fn build_python(_config: &Config) -> anyhow::Result<()> {
373+
todo!("Python is not supported yet")
374+
}
375+
334376
/// Find a wasm binary in the given directory.
335377
fn find_wasm(from_dir: &Path) -> anyhow::Result<PathBuf> {
336378
let from_dir = std::fs::read_dir(from_dir)?;
@@ -353,14 +395,6 @@ fn find_wasm(from_dir: &Path) -> anyhow::Result<PathBuf> {
353395
}
354396
}
355397

356-
fn build_ts(_config: &Config) -> anyhow::Result<()> {
357-
todo!("TypeScript is not supported yet")
358-
}
359-
360-
fn build_python(_config: &Config) -> anyhow::Result<()> {
361-
todo!("Python is not supported yet")
362-
}
363-
364398
/// Convert a file system path to UTF-8 if possible.
365399
pub fn path_to_utf8(path: &Path) -> anyhow::Result<&str> {
366400
match path.to_str() {

0 commit comments

Comments
 (0)