mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
chore(cli/flags): Use deno compile --output for the out file (#8563)
This commit is contained in:
parent
447f3fe410
commit
108972c966
4 changed files with 41 additions and 33 deletions
24
cli/flags.rs
24
cli/flags.rs
|
@ -24,7 +24,7 @@ pub enum DenoSubcommand {
|
|||
},
|
||||
Compile {
|
||||
source_file: String,
|
||||
out_file: Option<String>,
|
||||
output: Option<PathBuf>,
|
||||
},
|
||||
Completions {
|
||||
buf: Box<[u8]>,
|
||||
|
@ -419,11 +419,11 @@ fn compile_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
|||
compile_args_parse(flags, matches);
|
||||
|
||||
let source_file = matches.value_of("source_file").unwrap().to_string();
|
||||
let out_file = matches.value_of("out_file").map(|s| s.to_string());
|
||||
let output = matches.value_of("output").map(PathBuf::from);
|
||||
|
||||
flags.subcommand = DenoSubcommand::Compile {
|
||||
source_file,
|
||||
out_file,
|
||||
output,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -828,13 +828,19 @@ fn compile_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|||
.takes_value(true)
|
||||
.required(true),
|
||||
)
|
||||
.arg(Arg::with_name("out_file").takes_value(true))
|
||||
.arg(
|
||||
Arg::with_name("output")
|
||||
.long("output")
|
||||
.short("o")
|
||||
.help("Output file (defaults to $PWD/<inferred-name>)")
|
||||
.takes_value(true)
|
||||
)
|
||||
.about("Compile the script into a self contained executable")
|
||||
.long_about(
|
||||
"Compiles the given script into a self contained executable.
|
||||
deno compile --unstable https://deno.land/std/http/file_server.ts
|
||||
deno compile --unstable https://deno.land/std/examples/colors.ts color_util
|
||||
|
||||
deno compile --unstable --output /usr/local/bin/color_util https://deno.land/std/examples/colors.ts
|
||||
|
||||
The executable name is inferred by default:
|
||||
- Attempt to take the file stem of the URL path. The above example would
|
||||
become 'file_server'.
|
||||
|
@ -3258,7 +3264,7 @@ mod tests {
|
|||
Flags {
|
||||
subcommand: DenoSubcommand::Compile {
|
||||
source_file: "https://deno.land/std/examples/colors.ts".to_string(),
|
||||
out_file: None
|
||||
output: None
|
||||
},
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -3268,13 +3274,13 @@ mod tests {
|
|||
#[test]
|
||||
fn compile_with_flags() {
|
||||
#[rustfmt::skip]
|
||||
let r = flags_from_vec_safe(svec!["deno", "compile", "--unstable", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "https://deno.land/std/examples/colors.ts", "colors"]);
|
||||
let r = flags_from_vec_safe(svec!["deno", "compile", "--unstable", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--output", "colors", "https://deno.land/std/examples/colors.ts"]);
|
||||
assert_eq!(
|
||||
r.unwrap(),
|
||||
Flags {
|
||||
subcommand: DenoSubcommand::Compile {
|
||||
source_file: "https://deno.land/std/examples/colors.ts".to_string(),
|
||||
out_file: Some("colors".to_string())
|
||||
output: Some(PathBuf::from("colors"))
|
||||
},
|
||||
unstable: true,
|
||||
import_map_path: Some("import_map.json".to_string()),
|
||||
|
|
23
cli/main.rs
23
cli/main.rs
|
@ -155,7 +155,7 @@ fn get_types(unstable: bool) -> String {
|
|||
async fn compile_command(
|
||||
flags: Flags,
|
||||
source_file: String,
|
||||
out_file: Option<String>,
|
||||
output: Option<PathBuf>,
|
||||
) -> Result<(), AnyError> {
|
||||
if !flags.unstable {
|
||||
exit_unstable("compile");
|
||||
|
@ -166,14 +166,11 @@ async fn compile_command(
|
|||
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;
|
||||
let program_state = ProgramState::new(flags.clone())?;
|
||||
|
||||
let out_file =
|
||||
out_file.or_else(|| infer_name_from_url(module_specifier.as_url()));
|
||||
let out_file = match out_file {
|
||||
Some(out_file) => out_file,
|
||||
None => return Err(generic_error(
|
||||
"An executable name was not provided. One could not be inferred from the URL. Aborting.",
|
||||
)),
|
||||
};
|
||||
let output = output.or_else(|| {
|
||||
infer_name_from_url(module_specifier.as_url()).map(PathBuf::from)
|
||||
}).ok_or_else(|| generic_error(
|
||||
"An executable name was not provided. One could not be inferred from the URL. Aborting.",
|
||||
))?;
|
||||
|
||||
let module_graph = create_module_graph_and_maybe_check(
|
||||
module_specifier.clone(),
|
||||
|
@ -194,10 +191,10 @@ async fn compile_command(
|
|||
colors::green("Compile"),
|
||||
module_specifier.to_string()
|
||||
);
|
||||
create_standalone_binary(bundle_str.as_bytes().to_vec(), out_file.clone())
|
||||
create_standalone_binary(bundle_str.as_bytes().to_vec(), output.clone())
|
||||
.await?;
|
||||
|
||||
info!("{} {}", colors::green("Emit"), out_file);
|
||||
info!("{} {}", colors::green("Emit"), output.display());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -976,8 +973,8 @@ fn get_subcommand(
|
|||
}
|
||||
DenoSubcommand::Compile {
|
||||
source_file,
|
||||
out_file,
|
||||
} => compile_command(flags, source_file, out_file).boxed_local(),
|
||||
output,
|
||||
} => compile_command(flags, source_file, output).boxed_local(),
|
||||
DenoSubcommand::Fmt {
|
||||
check,
|
||||
files,
|
||||
|
|
|
@ -18,6 +18,7 @@ use std::io::Read;
|
|||
use std::io::Seek;
|
||||
use std::io::SeekFrom;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
@ -128,7 +129,7 @@ async fn run(source_code: String, args: Vec<String>) -> Result<(), AnyError> {
|
|||
/// and magic trailer to the currently executing binary.
|
||||
pub async fn create_standalone_binary(
|
||||
mut source_code: Vec<u8>,
|
||||
out_file: String,
|
||||
output: PathBuf,
|
||||
) -> Result<(), AnyError> {
|
||||
let original_binary_path = std::env::current_exe()?;
|
||||
let mut original_bin = tokio::fs::read(original_binary_path).await?;
|
||||
|
@ -142,17 +143,18 @@ pub async fn create_standalone_binary(
|
|||
final_bin.append(&mut source_code);
|
||||
final_bin.append(&mut trailer);
|
||||
|
||||
let out_file = if cfg!(windows) && !out_file.ends_with(".exe") {
|
||||
format!("{}.exe", out_file)
|
||||
} else {
|
||||
out_file
|
||||
};
|
||||
tokio::fs::write(&out_file, final_bin).await?;
|
||||
let output =
|
||||
if cfg!(windows) && output.extension().unwrap_or_default() != "exe" {
|
||||
PathBuf::from(output.display().to_string() + ".exe")
|
||||
} else {
|
||||
output
|
||||
};
|
||||
tokio::fs::write(&output, final_bin).await?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let perms = std::fs::Permissions::from_mode(0o777);
|
||||
tokio::fs::set_permissions(out_file, perms).await?;
|
||||
tokio::fs::set_permissions(output, perms).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -4547,8 +4547,9 @@ fn compile() {
|
|||
.current_dir(util::root_path())
|
||||
.arg("compile")
|
||||
.arg("--unstable")
|
||||
.arg("./std/examples/welcome.ts")
|
||||
.arg("--output")
|
||||
.arg(&exe)
|
||||
.arg("./std/examples/welcome.ts")
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap()
|
||||
|
@ -4577,8 +4578,9 @@ fn standalone_args() {
|
|||
.current_dir(util::root_path())
|
||||
.arg("compile")
|
||||
.arg("--unstable")
|
||||
.arg("./cli/tests/028_args.ts")
|
||||
.arg("--output")
|
||||
.arg(&exe)
|
||||
.arg("./cli/tests/028_args.ts")
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap()
|
||||
|
@ -4610,8 +4612,9 @@ fn standalone_no_module_load() {
|
|||
.current_dir(util::root_path())
|
||||
.arg("compile")
|
||||
.arg("--unstable")
|
||||
.arg("./cli/tests/standalone_import.ts")
|
||||
.arg("--output")
|
||||
.arg(&exe)
|
||||
.arg("./cli/tests/standalone_import.ts")
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap()
|
||||
|
|
Loading…
Reference in a new issue