From 04dd68f5bb9b74dbf4a12406ec5ee5a6bf54fe34 Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Thu, 17 Oct 2024 16:02:32 +0800 Subject: [PATCH 1/3] use int hex literal in embed --- crates/moon/src/cli/tool/embed.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/moon/src/cli/tool/embed.rs b/crates/moon/src/cli/tool/embed.rs index 14bd3a6f..e843848b 100644 --- a/crates/moon/src/cli/tool/embed.rs +++ b/crates/moon/src/cli/tool/embed.rs @@ -61,7 +61,7 @@ pub fn run_embed_bin(cmd: Embed) -> anyhow::Result { let name = cmd.name.unwrap_or_else(|| "resource".to_string()); let mut content = format!( "// Generated by `moon tool embed --binary`{}, do not edit.\n\n\ - let {} : Bytes = b\"", + let {} : Bytes = Bytes::of([\n", if cmd.timestamp { format!(" on {}", chrono::Local::now().format("%Y-%m-%d %H:%M:%S")) } else { @@ -69,10 +69,17 @@ pub fn run_embed_bin(cmd: Embed) -> anyhow::Result { }, name ); - for byte in input.iter() { - content.push_str(&format!("\\x{:02x}", byte)); + + for (i, byte) in input.iter().enumerate() { + if i % 12 == 0 { + if i > 0 { + content.push('\n'); + } + content.push_str(" "); + } + content.push_str(&format!("0x{:02x}, ", byte)); } - content.push_str("\"\n"); + content.push_str("\n])\n"); std::fs::write(cmd.output, content).context("write output file")?; Ok(0) } From 339ac6b3c2fdd35922a0e9c3a02e55625b2a8d8e Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Thu, 17 Oct 2024 16:02:38 +0800 Subject: [PATCH 2/3] update tests --- crates/moon/tests/test_cases/mod.rs | 32 +++++++++++++++++-- .../test_cases/pre_build.in/src/lib/b.txt | 6 ++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index a8efe38f..e0731aeb 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -6905,9 +6905,35 @@ fn test_pre_build() { "#]], ); let content = read(dir.join("src/lib/b.mbt")); - assert!(content.contains("// Generated by `moon tool embed --binary`, do not edit.")); - assert!(content.contains(r#"let _b : Bytes = b"\x6d\x6f\x6f\x6e"#)); - assert!(content.contains(r#"\x0a\x67\x65\x6e\x65\x72\x61\x74\x65"#)); + check( + content, + expect![[r#" + // Generated by `moon tool embed --binary`, do not edit. + + let _b : Bytes = Bytes::of([ + 0x4d, 0x6f, 0x6f, 0x6e, 0x42, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, + 0x6e, 0x20, 0x65, 0x6e, 0x64, 0x2d, 0x74, 0x6f, 0x2d, 0x65, 0x6e, 0x64, + 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, + 0x20, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x20, 0x74, 0x6f, + 0x6f, 0x6c, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, + 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x65, 0x64, + 0x67, 0x65, 0x0a, 0x63, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x69, 0x6e, 0x67, + 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x57, 0x65, 0x62, 0x41, 0x73, + 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, + 0x49, 0x44, 0x45, 0x20, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, + 0x65, 0x6e, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x76, 0x61, 0x69, 0x6c, + 0x61, 0x62, 0x6c, 0x65, 0x20, 0x61, 0x74, 0x0a, 0x68, 0x74, 0x74, 0x70, + 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x72, 0x79, 0x2e, 0x6d, 0x6f, 0x6f, 0x6e, + 0x62, 0x69, 0x74, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x20, + 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x61, 0x6e, 0x79, 0x20, + 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6c, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x3b, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6f, 0x65, 0x73, 0x20, 0x6e, 0x6f, + 0x74, 0x20, 0x72, 0x65, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x6e, + 0x79, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x65, 0x69, 0x74, + 0x68, 0x65, 0x72, 0x2e, + ]) + "#]], + ); check( read(dir.join("src/lib/c.mbt")), expect![[r#" diff --git a/crates/moon/tests/test_cases/pre_build.in/src/lib/b.txt b/crates/moon/tests/test_cases/pre_build.in/src/lib/b.txt index 18e5c770..aea0aaaa 100644 --- a/crates/moon/tests/test_cases/pre_build.in/src/lib/b.txt +++ b/crates/moon/tests/test_cases/pre_build.in/src/lib/b.txt @@ -1,2 +1,4 @@ -moon -generate +MoonBit is an end-to-end programming language toolchain for cloud and edge +computing using WebAssembly. The IDE environment is available at +https://try.moonbitlang.com without any installation; it does not rely on any +server either. \ No newline at end of file From d2cf7649c822519b024e7eff1dcf86cfc5e88775 Mon Sep 17 00:00:00 2001 From: Li Junchen Date: Fri, 18 Oct 2024 10:14:41 +0800 Subject: [PATCH 3/3] fix Windows --- crates/moon/tests/test_cases/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index e0731aeb..384aa964 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -6865,6 +6865,11 @@ fn no_main_just_init() { #[test] fn test_pre_build() { let dir = TestDir::new("pre_build.in"); + + // replace CRLF with LF on Windows + let b_txt_path = dir.join("src/lib/b.txt"); + std::fs::write(&b_txt_path, read(&b_txt_path)).unwrap(); + check( get_stderr(&dir, ["check"]), expect![[r#"