From c61cf12d535d1109bc2ac02f3f94ceec355a22a0 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 14 Aug 2024 16:51:24 +0530 Subject: [PATCH 1/4] feat: `deno compile --icon ` --- Cargo.lock | 5 ++--- cli/Cargo.toml | 3 ++- cli/args/flags.rs | 9 +++++++++ cli/standalone/binary.rs | 20 +++++++++++++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43f85f2d9a4eda..d097c0ebaaac6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4162,9 +4162,8 @@ dependencies = [ [[package]] name = "libsui" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2fedcf6cb4dd935f94a90e1c4300c727fe7112b8455615e902828c7401f84d" +version = "0.2.0" +source = "git+https://github.com/denoland/sui?branch=win_icons#e10b539b153df72c91f70721ce49ee702380ce38" dependencies = [ "editpe", "libc", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 0e7050d92621cc..7df5e0fe57cd87 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -79,7 +79,8 @@ deno_semver = "=0.5.10" deno_task_shell = "=0.17.0" deno_terminal.workspace = true eszip = "=0.73.0" -libsui = "0.1.0" +# libsui = "0.1.0" +libsui = { git = "https://github.com/denoland/sui", branch = "win_icons" } napi_sym.workspace = true node_resolver.workspace = true diff --git a/cli/args/flags.rs b/cli/args/flags.rs index b9908f413a60d6..8bf9194c01c9b9 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -121,6 +121,7 @@ pub struct CompileFlags { pub args: Vec, pub target: Option, pub no_terminal: bool, + pub icon: Option, pub include: Vec, } @@ -1765,6 +1766,12 @@ supported in canary. .help("Hide terminal on Windows") .action(ArgAction::SetTrue), ) + .arg( + Arg::new("icon") + .long("icon") + .help("Set the icon of the executable on Windows (.ico)") + .value_parser(value_parser!(String)) + ) .arg(executable_ext_arg()) .arg(env_file_arg()) .arg(script_arg().required(true).trailing_var_arg(true)) @@ -3861,6 +3868,7 @@ fn compile_parse(flags: &mut Flags, matches: &mut ArgMatches) { let args = script.collect(); let output = matches.remove_one::("output"); let target = matches.remove_one::("target"); + let icon = matches.remove_one::("icon"); let no_terminal = matches.get_flag("no-terminal"); let include = match matches.remove_many::("include") { Some(f) => f.collect(), @@ -3874,6 +3882,7 @@ fn compile_parse(flags: &mut Flags, matches: &mut ArgMatches) { args, target, no_terminal, + icon, include, }); } diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs index 78978cefc9ecb6..6357669e05f6af 100644 --- a/cli/standalone/binary.rs +++ b/cli/standalone/binary.rs @@ -187,10 +187,15 @@ fn write_binary_bytes( let target = compile_flags.resolve_target(); if target.contains("linux") { - libsui::Elf::new(&original_bin).append(&writer, &mut file_writer)?; + libsui::Elf::new(&original_bin).append("d3n0l4nd", &writer, &mut file_writer)?; } else if target.contains("windows") { - libsui::PortableExecutable::from(&original_bin)? - .write_resource("d3n0l4nd", writer)? + let mut pe = libsui::PortableExecutable::from(&original_bin)?; + if let Some(icon) = compile_flags.icon.as_ref() { + let icon = std::fs::read(icon)?; + pe = pe.set_icon(&icon)?; + } + + pe.write_resource("d3n0l4nd", writer)? .build(&mut file_writer)?; } else if target.contains("darwin") { libsui::Macho::from(original_bin)? @@ -370,6 +375,15 @@ impl<'a> DenoCompileBinaryWriter<'a> { } set_windows_binary_to_gui(&mut original_binary)?; } + if compile_flags.icon.is_some() { + let target = compile_flags.resolve_target(); + if !target.contains("windows") { + bail!( + "The `--icon` flag is only available when targeting Windows (current: {})", + target, + ) + } + } self.write_standalone_binary( writer, original_binary, From 531bec8eb38af004436b08101569318cb8df93ce Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 14 Aug 2024 22:51:00 +0530 Subject: [PATCH 2/4] Update sui --- Cargo.lock | 5 +++-- cli/Cargo.toml | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d097c0ebaaac6c..2cf752ae702700 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4162,8 +4162,9 @@ dependencies = [ [[package]] name = "libsui" -version = "0.2.0" -source = "git+https://github.com/denoland/sui?branch=win_icons#e10b539b153df72c91f70721ce49ee702380ce38" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d0f34a58599483cd44a31ff3622dbcca0c50679af60f98b705069dc729e70cf" dependencies = [ "editpe", "libc", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 7df5e0fe57cd87..4f5acbf8c69e6e 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -79,8 +79,7 @@ deno_semver = "=0.5.10" deno_task_shell = "=0.17.0" deno_terminal.workspace = true eszip = "=0.73.0" -# libsui = "0.1.0" -libsui = { git = "https://github.com/denoland/sui", branch = "win_icons" } +libsui = "0.3.0" napi_sym.workspace = true node_resolver.workspace = true From 820eea146c3bd0e7169b5de1e2997f2643ffe5d4 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 14 Aug 2024 23:04:45 +0530 Subject: [PATCH 3/4] Format --- cli/standalone/binary.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs index 6357669e05f6af..9d6d5262e822f8 100644 --- a/cli/standalone/binary.rs +++ b/cli/standalone/binary.rs @@ -187,14 +187,18 @@ fn write_binary_bytes( let target = compile_flags.resolve_target(); if target.contains("linux") { - libsui::Elf::new(&original_bin).append("d3n0l4nd", &writer, &mut file_writer)?; + libsui::Elf::new(&original_bin).append( + "d3n0l4nd", + &writer, + &mut file_writer, + )?; } else if target.contains("windows") { let mut pe = libsui::PortableExecutable::from(&original_bin)?; if let Some(icon) = compile_flags.icon.as_ref() { let icon = std::fs::read(icon)?; pe = pe.set_icon(&icon)?; } - + pe.write_resource("d3n0l4nd", writer)? .build(&mut file_writer)?; } else if target.contains("darwin") { From ff0409a8bfabc49c79e72e2969e4f186e3a9c137 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 14 Aug 2024 23:18:06 +0530 Subject: [PATCH 4/4] Fix tests --- cli/args/flags.rs | 4 +++- cli/tools/compile.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 8bf9194c01c9b9..ba7b0a80e28e78 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -9497,6 +9497,7 @@ mod tests { args: vec![], target: None, no_terminal: false, + icon: None, include: vec![] }), type_check_mode: TypeCheckMode::Local, @@ -9508,7 +9509,7 @@ mod tests { #[test] fn compile_with_flags() { #[rustfmt::skip] - let r = flags_from_vec(svec!["deno", "compile", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--unsafely-ignore-certificate-errors", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--location", "https:foo", "--allow-read", "--allow-net", "--v8-flags=--help", "--seed", "1", "--no-terminal", "--output", "colors", "--env=.example.env", "https://examples.deno.land/color-logging.ts", "foo", "bar", "-p", "8080"]); + let r = flags_from_vec(svec!["deno", "compile", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--unsafely-ignore-certificate-errors", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--location", "https:foo", "--allow-read", "--allow-net", "--v8-flags=--help", "--seed", "1", "--no-terminal", "--icon", "favicon.ico", "--output", "colors", "--env=.example.env", "https://examples.deno.land/color-logging.ts", "foo", "bar", "-p", "8080"]); assert_eq!( r.unwrap(), Flags { @@ -9519,6 +9520,7 @@ mod tests { args: svec!["foo", "bar", "-p", "8080"], target: None, no_terminal: true, + icon: Some(String::from("favicon.ico")), include: vec![] }), import_map_path: Some("import_map.json".to_string()), diff --git a/cli/tools/compile.rs b/cli/tools/compile.rs index 6cf2ccf77114e3..90ee0e2706356c 100644 --- a/cli/tools/compile.rs +++ b/cli/tools/compile.rs @@ -361,6 +361,7 @@ mod test { args: Vec::new(), target: Some("x86_64-unknown-linux-gnu".to_string()), no_terminal: false, + icon: None, include: vec![], }, &std::env::current_dir().unwrap(), @@ -385,6 +386,7 @@ mod test { args: Vec::new(), target: Some("x86_64-pc-windows-msvc".to_string()), include: vec![], + icon: None, no_terminal: false, }, &std::env::current_dir().unwrap(),