diff options
Diffstat (limited to 'cli/flags.rs')
-rw-r--r-- | cli/flags.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index a8a4ddba2..9282decce 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -22,6 +22,10 @@ pub enum DenoSubcommand { source_file: String, out_file: Option<PathBuf>, }, + Compile { + source_file: String, + out_file: Option<String>, + }, Completions { buf: Box<[u8]>, }, @@ -292,6 +296,8 @@ pub fn flags_from_vec_safe(args: Vec<String>) -> clap::Result<Flags> { doc_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("lint") { lint_parse(&mut flags, m); + } else if let Some(m) = matches.subcommand_matches("compile") { + compile_parse(&mut flags, m); } else { repl_parse(&mut flags, &matches); } @@ -341,6 +347,7 @@ If the flag is set, restrict these messages to errors.", ) .subcommand(bundle_subcommand()) .subcommand(cache_subcommand()) + .subcommand(compile_subcommand()) .subcommand(completions_subcommand()) .subcommand(doc_subcommand()) .subcommand(eval_subcommand()) @@ -408,6 +415,18 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { }; } +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()); + + flags.subcommand = DenoSubcommand::Compile { + source_file, + out_file, + }; +} + fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) { compile_args_parse(flags, matches); @@ -802,6 +821,32 @@ The installation root is determined, in order of precedence: These must be added to the path manually if required.") } +fn compile_subcommand<'a, 'b>() -> App<'a, 'b> { + compile_args(SubCommand::with_name("compile")) + .arg( + Arg::with_name("source_file") + .takes_value(true) + .required(true), + ) + .arg(Arg::with_name("out_file").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 + +The executable name is inferred by default: + - Attempt to take the file stem of the URL path. The above example would + become 'file_server'. + - If the file stem is something generic like 'main', 'mod', 'index' or 'cli', + and the path has no parent, take the file name of the parent path. Otherwise + settle with the generic name. + - If the resulting name has an '@...' suffix, strip it. + +Cross compiling binaries for different platforms is not currently possible.", + ) +} + fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> { compile_args(SubCommand::with_name("bundle")) .arg( @@ -3200,4 +3245,48 @@ mod tests { } ); } + + #[test] + fn compile() { + let r = flags_from_vec_safe(svec![ + "deno", + "compile", + "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: None + }, + ..Flags::default() + } + ); + } + + #[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"]); + 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()) + }, + unstable: true, + import_map_path: Some("import_map.json".to_string()), + no_remote: true, + config_path: Some("tsconfig.json".to_string()), + no_check: true, + reload: true, + lock: Some(PathBuf::from("lock.json")), + lock_write: true, + ca_file: Some("example.crt".to_string()), + ..Flags::default() + } + ); + } } |