1
0
Fork 0
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:
Nayeem Rahman 2020-12-01 14:11:02 +00:00 committed by GitHub
parent 447f3fe410
commit 108972c966
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 33 deletions

View file

@ -24,7 +24,7 @@ pub enum DenoSubcommand {
}, },
Compile { Compile {
source_file: String, source_file: String,
out_file: Option<String>, output: Option<PathBuf>,
}, },
Completions { Completions {
buf: Box<[u8]>, buf: Box<[u8]>,
@ -419,11 +419,11 @@ fn compile_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
compile_args_parse(flags, matches); compile_args_parse(flags, matches);
let source_file = matches.value_of("source_file").unwrap().to_string(); 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 { flags.subcommand = DenoSubcommand::Compile {
source_file, source_file,
out_file, output,
}; };
} }
@ -828,13 +828,19 @@ fn compile_subcommand<'a, 'b>() -> App<'a, 'b> {
.takes_value(true) .takes_value(true)
.required(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") .about("Compile the script into a self contained executable")
.long_about( .long_about(
"Compiles the given script into a self contained executable. "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/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: The executable name is inferred by default:
- Attempt to take the file stem of the URL path. The above example would - Attempt to take the file stem of the URL path. The above example would
become 'file_server'. become 'file_server'.
@ -3258,7 +3264,7 @@ mod tests {
Flags { Flags {
subcommand: DenoSubcommand::Compile { subcommand: DenoSubcommand::Compile {
source_file: "https://deno.land/std/examples/colors.ts".to_string(), source_file: "https://deno.land/std/examples/colors.ts".to_string(),
out_file: None output: None
}, },
..Flags::default() ..Flags::default()
} }
@ -3268,13 +3274,13 @@ mod tests {
#[test] #[test]
fn compile_with_flags() { fn compile_with_flags() {
#[rustfmt::skip] #[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!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Compile { subcommand: DenoSubcommand::Compile {
source_file: "https://deno.land/std/examples/colors.ts".to_string(), source_file: "https://deno.land/std/examples/colors.ts".to_string(),
out_file: Some("colors".to_string()) output: Some(PathBuf::from("colors"))
}, },
unstable: true, unstable: true,
import_map_path: Some("import_map.json".to_string()), import_map_path: Some("import_map.json".to_string()),

View file

@ -155,7 +155,7 @@ fn get_types(unstable: bool) -> String {
async fn compile_command( async fn compile_command(
flags: Flags, flags: Flags,
source_file: String, source_file: String,
out_file: Option<String>, output: Option<PathBuf>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
if !flags.unstable { if !flags.unstable {
exit_unstable("compile"); exit_unstable("compile");
@ -166,14 +166,11 @@ async fn compile_command(
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?; let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;
let program_state = ProgramState::new(flags.clone())?; let program_state = ProgramState::new(flags.clone())?;
let out_file = let output = output.or_else(|| {
out_file.or_else(|| infer_name_from_url(module_specifier.as_url())); infer_name_from_url(module_specifier.as_url()).map(PathBuf::from)
let out_file = match out_file { }).ok_or_else(|| generic_error(
Some(out_file) => out_file, "An executable name was not provided. One could not be inferred from the URL. Aborting.",
None => return Err(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( let module_graph = create_module_graph_and_maybe_check(
module_specifier.clone(), module_specifier.clone(),
@ -194,10 +191,10 @@ async fn compile_command(
colors::green("Compile"), colors::green("Compile"),
module_specifier.to_string() 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?; .await?;
info!("{} {}", colors::green("Emit"), out_file); info!("{} {}", colors::green("Emit"), output.display());
Ok(()) Ok(())
} }
@ -976,8 +973,8 @@ fn get_subcommand(
} }
DenoSubcommand::Compile { DenoSubcommand::Compile {
source_file, source_file,
out_file, output,
} => compile_command(flags, source_file, out_file).boxed_local(), } => compile_command(flags, source_file, output).boxed_local(),
DenoSubcommand::Fmt { DenoSubcommand::Fmt {
check, check,
files, files,

View file

@ -18,6 +18,7 @@ use std::io::Read;
use std::io::Seek; use std::io::Seek;
use std::io::SeekFrom; use std::io::SeekFrom;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; 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. /// and magic trailer to the currently executing binary.
pub async fn create_standalone_binary( pub async fn create_standalone_binary(
mut source_code: Vec<u8>, mut source_code: Vec<u8>,
out_file: String, output: PathBuf,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let original_binary_path = std::env::current_exe()?; let original_binary_path = std::env::current_exe()?;
let mut original_bin = tokio::fs::read(original_binary_path).await?; 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 source_code);
final_bin.append(&mut trailer); final_bin.append(&mut trailer);
let out_file = if cfg!(windows) && !out_file.ends_with(".exe") { let output =
format!("{}.exe", out_file) if cfg!(windows) && output.extension().unwrap_or_default() != "exe" {
} else { PathBuf::from(output.display().to_string() + ".exe")
out_file } else {
}; output
tokio::fs::write(&out_file, final_bin).await?; };
tokio::fs::write(&output, final_bin).await?;
#[cfg(unix)] #[cfg(unix)]
{ {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
let perms = std::fs::Permissions::from_mode(0o777); let perms = std::fs::Permissions::from_mode(0o777);
tokio::fs::set_permissions(out_file, perms).await?; tokio::fs::set_permissions(output, perms).await?;
} }
Ok(()) Ok(())

View file

@ -4547,8 +4547,9 @@ fn compile() {
.current_dir(util::root_path()) .current_dir(util::root_path())
.arg("compile") .arg("compile")
.arg("--unstable") .arg("--unstable")
.arg("./std/examples/welcome.ts") .arg("--output")
.arg(&exe) .arg(&exe)
.arg("./std/examples/welcome.ts")
.stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
.spawn() .spawn()
.unwrap() .unwrap()
@ -4577,8 +4578,9 @@ fn standalone_args() {
.current_dir(util::root_path()) .current_dir(util::root_path())
.arg("compile") .arg("compile")
.arg("--unstable") .arg("--unstable")
.arg("./cli/tests/028_args.ts") .arg("--output")
.arg(&exe) .arg(&exe)
.arg("./cli/tests/028_args.ts")
.stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
.spawn() .spawn()
.unwrap() .unwrap()
@ -4610,8 +4612,9 @@ fn standalone_no_module_load() {
.current_dir(util::root_path()) .current_dir(util::root_path())
.arg("compile") .arg("compile")
.arg("--unstable") .arg("--unstable")
.arg("./cli/tests/standalone_import.ts") .arg("--output")
.arg(&exe) .arg(&exe)
.arg("./cli/tests/standalone_import.ts")
.stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
.spawn() .spawn()
.unwrap() .unwrap()