diff options
Diffstat (limited to 'cli/installer.rs')
-rw-r--r-- | cli/installer.rs | 203 |
1 files changed, 21 insertions, 182 deletions
diff --git a/cli/installer.rs b/cli/installer.rs index d5e90a252..0cc25ff33 100644 --- a/cli/installer.rs +++ b/cli/installer.rs @@ -48,30 +48,17 @@ fn validate_exec_name(exec_name: &str) -> Result<(), Error> { } #[cfg(windows)] +/// On Windows if user is using Powershell .cmd extension is need to run the +/// installed module. +/// Generate batch script to satisfy that. fn generate_executable_file( file_path: PathBuf, - commands: Vec<String>, + args: Vec<String>, ) -> Result<(), Error> { - // On Windows if user is using Powershell .cmd extension is need to run the - // installed module. - // Generate batch script to satisfy that. - let template_header = "This executable is generated by Deno. Please don't modify it unless you know what it means."; - let commands: Vec<String> = - commands.iter().map(|c| format!("\"{}\"", c)).collect(); - // TODO: remove conditionals in generated scripts + let args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect(); let template = format!( - r#"% {} % -@IF EXIST "%~dp0\deno.exe" ( - "%~dp0\deno.exe" {} %* -) ELSE ( - @SETLOCAL - @SET PATHEXT=%PATHEXT:;.TS;=;% - {} %* -) -"#, - template_header, - commands[1..].join(" "), - commands.join(" ") + "% generated by deno install %\ndeno.exe {} %*\n", + args.join(" ") ); let file_path = file_path.with_extension(".cmd"); let mut file = File::create(&file_path)?; @@ -82,37 +69,18 @@ fn generate_executable_file( #[cfg(not(windows))] fn generate_executable_file( file_path: PathBuf, - commands: Vec<String>, + args: Vec<String>, ) -> Result<(), Error> { // On Windows if user is using Powershell .cmd extension is need to run the // installed module. // Generate batch script to satisfy that. - let template_header = "This executable is generated by Deno. Please don't modify it unless you know what it means."; - let commands: Vec<String> = - commands.iter().map(|c| format!("\"{}\"", c)).collect(); - - // TODO: remove conditionals in generated scripts + let args: Vec<String> = args.iter().map(|c| format!("\"{}\"", c)).collect(); let template = format!( r#"#!/bin/sh -# {} -basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") - -case \`uname\` in - *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; -esac - -if [ -x "$basedir/deno" ]; then - "$basedir/deno" {} "$@" - ret=$? -else - {} "$@" - ret=$? -fi -exit $ret +# generated by deno install +deno {} "$@" "#, - template_header, - commands[1..].join(" "), - commands.join(" ") + args.join(" "), ); let mut file = File::create(&file_path)?; file.write_all(template.as_bytes())?; @@ -197,7 +165,7 @@ pub fn install( }; }; - let mut executable_args = vec!["deno".to_string(), "run".to_string()]; + let mut executable_args = vec!["run".to_string()]; executable_args.extend_from_slice(&flags.to_permission_args()); executable_args.push(module_url.to_string()); executable_args.extend_from_slice(&args); @@ -270,39 +238,11 @@ mod tests { assert!(file_path.exists()); let content = fs::read_to_string(file_path).unwrap(); + // It's annoying when shell scripts don't have NL at the end. + assert_eq!(content.chars().last().unwrap(), '\n'); - let expected_content = if cfg!(windows) { - r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. % -@IF EXIST "%~dp0\deno.exe" ( - "%~dp0\deno.exe" "run" "http://localhost:4545/cli/tests/echo_server.ts" %* -) ELSE ( - @SETLOCAL - @SET PATHEXT=%PATHEXT:;.TS;=;% - "deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" %* -) -"# - } else { - r#"#!/bin/sh -# This executable is generated by Deno. Please don't modify it unless you know what it means. -basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") - -case \`uname\` in - *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; -esac - -if [ -x "$basedir/deno" ]; then - "$basedir/deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@" - ret=$? -else - "deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@" - ret=$? -fi -exit $ret -"# - }; - - assert_eq!(content, expected_content.to_string()); - + assert!(content + .contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#)); if let Some(home) = original_home { env::set_var("HOME", home); } @@ -314,7 +254,6 @@ exit $ret #[test] fn install_custom_dir() { let temp_dir = TempDir::new().expect("tempdir fail"); - install( DenoFlags::default(), Some(temp_dir.path().to_string_lossy().to_string()), @@ -331,38 +270,8 @@ exit $ret assert!(file_path.exists()); let content = fs::read_to_string(file_path).unwrap(); - - let expected_content = if cfg!(windows) { - r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. % -@IF EXIST "%~dp0\deno.exe" ( - "%~dp0\deno.exe" "run" "http://localhost:4545/cli/tests/echo_server.ts" %* -) ELSE ( - @SETLOCAL - @SET PATHEXT=%PATHEXT:;.TS;=;% - "deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" %* -) -"# - } else { - r#"#!/bin/sh -# This executable is generated by Deno. Please don't modify it unless you know what it means. -basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") - -case \`uname\` in - *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; -esac - -if [ -x "$basedir/deno" ]; then - "$basedir/deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@" - ret=$? -else - "deno" "run" "http://localhost:4545/cli/tests/echo_server.ts" "$@" - ret=$? -fi -exit $ret -"# - }; - - assert_eq!(content, expected_content.to_string()); + assert!(content + .contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#)); } #[test] @@ -389,38 +298,7 @@ exit $ret assert!(file_path.exists()); let content = fs::read_to_string(file_path).unwrap(); - - let expected_content = if cfg!(windows) { - r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. % -@IF EXIST "%~dp0\deno.exe" ( - "%~dp0\deno.exe" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" %* -) ELSE ( - @SETLOCAL - @SET PATHEXT=%PATHEXT:;.TS;=;% - "deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" %* -) -"# - } else { - r#"#!/bin/sh -# This executable is generated by Deno. Please don't modify it unless you know what it means. -basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") - -case \`uname\` in - *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; -esac - -if [ -x "$basedir/deno" ]; then - "$basedir/deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" "$@" - ret=$? -else - "deno" "run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar" "$@" - ret=$? -fi -exit $ret -"# - }; - - assert_eq!(content, expected_content.to_string()); + assert!(content.contains(r#""run" "--allow-read" "--allow-net" "http://localhost:4545/cli/tests/echo_server.ts" "--foobar""#)); } #[test] @@ -446,45 +324,6 @@ exit $ret assert!(file_path.exists()); let content = fs::read_to_string(file_path).unwrap(); - - let expected_content = if cfg!(windows) { - format!( - r#"% This executable is generated by Deno. Please don't modify it unless you know what it means. % -@IF EXIST "%~dp0\deno.exe" ( - "%~dp0\deno.exe" "run" "{}" %* -) ELSE ( - @SETLOCAL - @SET PATHEXT=%PATHEXT:;.TS;=;% - "deno" "run" "{}" %* -) -"#, - local_module_url.to_string(), - local_module_url.to_string() - ) - } else { - format!( - r#"#!/bin/sh -# This executable is generated by Deno. Please don't modify it unless you know what it means. -basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") - -case \`uname\` in - *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; -esac - -if [ -x "$basedir/deno" ]; then - "$basedir/deno" "run" "{}" "$@" - ret=$? -else - "deno" "run" "{}" "$@" - ret=$? -fi -exit $ret -"#, - local_module_url.to_string(), - local_module_url.to_string() - ) - }; - - assert_eq!(content, expected_content); + assert!(content.contains(&local_module_url.to_string())); } } |