Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deno compile --icon <ico> #25039

Merged
merged 4 commits into from
Aug 15, 2024
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +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 = "0.3.0"
napi_sym.workspace = true
node_resolver.workspace = true

Expand Down
13 changes: 12 additions & 1 deletion cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub struct CompileFlags {
pub args: Vec<String>,
pub target: Option<String>,
pub no_terminal: bool,
pub icon: Option<String>,
pub include: Vec<String>,
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -3861,6 +3868,7 @@ fn compile_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let args = script.collect();
let output = matches.remove_one::<String>("output");
let target = matches.remove_one::<String>("target");
let icon = matches.remove_one::<String>("icon");
let no_terminal = matches.get_flag("no-terminal");
let include = match matches.remove_many::<String>("include") {
Some(f) => f.collect(),
Expand All @@ -3874,6 +3882,7 @@ fn compile_parse(flags: &mut Flags, matches: &mut ArgMatches) {
args,
target,
no_terminal,
icon,
include,
});
}
Expand Down Expand Up @@ -9488,6 +9497,7 @@ mod tests {
args: vec![],
target: None,
no_terminal: false,
icon: None,
include: vec![]
}),
type_check_mode: TypeCheckMode::Local,
Expand All @@ -9499,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 {
Expand All @@ -9510,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()),
Expand Down
24 changes: 21 additions & 3 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,19 @@ 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)?
Expand Down Expand Up @@ -370,6 +379,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,
Expand Down
2 changes: 2 additions & 0 deletions cli/tools/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down