Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assets/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function boot()
-- ...
end

function update()
-- ...
end

function render()
firefly.clear_screen(firefly.RED)
end
22 changes: 19 additions & 3 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
Lang::C => new_c(&args.name).context("new C project")?,
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
Lang::Python => bail!("Python is not supported yet"),
Lang::Lua => new_lua(&args.name).context("new Lua project")?,
Lang::Moon => new_moon(&args.name).context("new Moon project")?,
}
write_config(&args.name)?;
write_config(&lang, &args.name)?;
init_git(&args.name)?;
println!("✅ project created");
Ok(())
}

/// Create and dump firefly.toml config.
fn write_config(name: &str) -> Result<()> {
fn write_config(lang: &Lang, name: &str) -> Result<()> {
use std::fmt::Write;

let root = Path::new(name);
Expand All @@ -51,6 +52,11 @@ fn write_config(name: &str) -> Result<()> {
_ = writeln!(config, "author_name = \"{}\"", to_titlecase(&username));
_ = writeln!(config, "app_name = \"{}\"", to_titlecase(name));

if matches!(lang, Lang::Lua) {
_ = writeln!(config, "\n[files]");
_ = writeln!(config, r#"main = {{ path = "main.lua", copy = true }}"#);
}

std::fs::write(config_path, config).context("write config")?;
Ok(())
}
Expand All @@ -77,7 +83,8 @@ fn parse_lang(lang: &str) -> Result<Lang> {
"ts" | "typescript" | "js" | "javascript" => Lang::TS,
"cpp" | "c++" => Lang::Cpp,
"python" | "py" => Lang::Python,
"moon" | "moonbit" => Lang::Moon,
"moon" | "moonbit" | "mbt" => Lang::Moon,
"lua" => Lang::Lua,
_ => bail!("unsupported language: {lang}"),
};
Ok(result)
Expand Down Expand Up @@ -146,6 +153,15 @@ fn new_c_or_cpp(name: &str, main: &str) -> Result<()> {
Ok(())
}

/// Create a new Lua project.
fn new_lua(name: &str) -> Result<()> {
let mut c = Commander::default();
c.cd(name)?;
c.copy_asset(&["main.lua"], "main.lua")?;
Ok(())
}

/// Create a new Moon project.
fn new_moon(name: &str) -> Result<()> {
check_installed("Moon", "moon", "version")?;
let mut c = Commander::default();
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,5 @@ pub enum Lang {
Cpp,
Python,
Moon,
Lua,
}
20 changes: 20 additions & 0 deletions src/langs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn build_bin(config: &Config, args: &BuildArgs) -> anyhow::Result<()> {
Lang::C => build_c(config),
Lang::Cpp => build_cpp(config),
Lang::Python => build_python(config),
Lang::Lua => build_lua(config),
Lang::Moon => build_moon(config),
}?;
let bin_path = config.rom_path.join(BIN);
Expand Down Expand Up @@ -80,6 +81,9 @@ fn detect_lang(root: &Path) -> anyhow::Result<Lang> {
if root.join("main.cpp").exists() {
return Ok(Lang::Cpp);
}
if root.join("main.lua").exists() {
return Ok(Lang::Lua);
}
if root.join("src").join("main.c").exists() {
return Ok(Lang::C);
}
Expand Down Expand Up @@ -370,6 +374,22 @@ fn build_moon(config: &Config) -> anyhow::Result<()> {
Ok(())
}

// Build Lua project.
fn build_lua(config: &Config) -> anyhow::Result<()> {
let from_path = config.root_path.join("main.wasm");
if !from_path.is_file() {
let url = "https://github.com/firefly-zero/firefly-lua/releases/latest/download/main.wasm";
let resp = ureq::get(url).call().context("send request")?;
let mut file = std::fs::File::create(&from_path).context("open main.wasm")?;
let mut body = resp.into_body();
let mut body = body.as_reader();
std::io::copy(&mut body, &mut file)?;
}
let out_path = config.rom_path.join(BIN);
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
Ok(())
}

fn build_ts(_config: &Config) -> anyhow::Result<()> {
todo!("TypeScript is not supported yet")
}
Expand Down