1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

feat(cli/installer.rs): Add DENO_INSTALL_ROOT (#4787)

This commit is contained in:
Nayeem Rahman 2020-04-16 23:15:42 +01:00 committed by GitHub
parent d359789c52
commit 5bfe3eb8f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 66 deletions

View file

@ -53,7 +53,7 @@ pub enum DenoSubcommand {
file: Option<String>, file: Option<String>,
}, },
Install { Install {
dir: Option<PathBuf>, root: Option<PathBuf>,
exe_name: String, exe_name: String,
module_url: String, module_url: String,
args: Vec<String>, args: Vec<String>,
@ -181,9 +181,12 @@ impl Flags {
} }
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES: static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
DENO_DIR Set deno's base directory DENO_DIR Set deno's base directory (defaults to $HOME/.deno)
DENO_INSTALL_ROOT Set deno install's output directory
(defaults to $HOME/.deno/bin)
NO_COLOR Set to disable color NO_COLOR Set to disable color
HTTP_PROXY Proxy address for HTTP requests (module downloads, fetch) HTTP_PROXY Proxy address for HTTP requests
(module downloads, fetch)
HTTPS_PROXY Same but for HTTPS"; HTTPS_PROXY Same but for HTTPS";
static DENO_HELP: &str = "A secure JavaScript and TypeScript runtime static DENO_HELP: &str = "A secure JavaScript and TypeScript runtime
@ -344,9 +347,9 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
permission_args_parse(flags, matches); permission_args_parse(flags, matches);
ca_file_arg_parse(flags, matches); ca_file_arg_parse(flags, matches);
let dir = if matches.is_present("dir") { let root = if matches.is_present("root") {
let install_dir = matches.value_of("dir").unwrap(); let install_root = matches.value_of("root").unwrap();
Some(PathBuf::from(install_dir)) Some(PathBuf::from(install_root))
} else { } else {
None None
}; };
@ -364,7 +367,7 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
let args = cmd_args[1..].to_vec(); let args = cmd_args[1..].to_vec();
flags.subcommand = DenoSubcommand::Install { flags.subcommand = DenoSubcommand::Install {
dir, root,
exe_name, exe_name,
module_url, module_url,
args, args,
@ -624,10 +627,9 @@ fn install_subcommand<'a, 'b>() -> App<'a, 'b> {
permission_args(SubCommand::with_name("install")) permission_args(SubCommand::with_name("install"))
.setting(AppSettings::TrailingVarArg) .setting(AppSettings::TrailingVarArg)
.arg( .arg(
Arg::with_name("dir") Arg::with_name("root")
.long("dir") .long("root")
.short("d") .help("Installation root")
.help("Installation directory (defaults to $HOME/.deno/bin)")
.takes_value(true) .takes_value(true)
.multiple(false)) .multiple(false))
.arg( .arg(
@ -647,15 +649,21 @@ fn install_subcommand<'a, 'b>() -> App<'a, 'b> {
.allow_hyphen_values(true) .allow_hyphen_values(true)
) )
.arg(ca_file_arg()) .arg(ca_file_arg())
.about("Install script as executable") .about("Install script as an executable")
.long_about( .long_about(
"Installs a script as executable. The default installation directory is "Installs a script as an executable in the installation root's bin directory.
$HOME/.deno/bin and it must be added to the path manually.
deno install --allow-net --allow-read file_server https://deno.land/std/http/file_server.ts deno install --allow-net --allow-read file_server https://deno.land/std/http/file_server.ts
deno install colors https://deno.land/std/examples/colors.ts deno install colors https://deno.land/std/examples/colors.ts
To change installation directory use -d/--dir flag: To change the installation root, use --root:
deno install --allow-net --allow-read -d /usr/local/bin file_server https://deno.land/std/http/file_server.ts") deno install --allow-net --allow-read --root /usr/local file_server https://deno.land/std/http/file_server.ts
The installation root is determined, in order of precedence:
- --root option
- DENO_INSTALL_ROOT environment variable
- $HOME/.deno
These must be added to the path manually if required.")
} }
fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> { fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> {
@ -2029,7 +2037,7 @@ mod tests {
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Install { subcommand: DenoSubcommand::Install {
dir: None, root: None,
exe_name: "deno_colors".to_string(), exe_name: "deno_colors".to_string(),
module_url: "https://deno.land/std/examples/colors.ts".to_string(), module_url: "https://deno.land/std/examples/colors.ts".to_string(),
args: vec![], args: vec![],
@ -2054,7 +2062,7 @@ mod tests {
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Install { subcommand: DenoSubcommand::Install {
dir: None, root: None,
exe_name: "file_server".to_string(), exe_name: "file_server".to_string(),
module_url: "https://deno.land/std/http/file_server.ts".to_string(), module_url: "https://deno.land/std/http/file_server.ts".to_string(),
args: vec![], args: vec![],
@ -2072,8 +2080,8 @@ mod tests {
let r = flags_from_vec_safe(svec![ let r = flags_from_vec_safe(svec![
"deno", "deno",
"install", "install",
"-d", "--root",
"/usr/local/bin", "/usr/local",
"-f", "-f",
"--allow-net", "--allow-net",
"--allow-read", "--allow-read",
@ -2086,7 +2094,7 @@ mod tests {
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Install { subcommand: DenoSubcommand::Install {
dir: Some(PathBuf::from("/usr/local/bin")), root: Some(PathBuf::from("/usr/local")),
exe_name: "file_server".to_string(), exe_name: "file_server".to_string(),
module_url: "https://deno.land/std/http/file_server.ts".to_string(), module_url: "https://deno.land/std/http/file_server.ts".to_string(),
args: svec!["arg1", "arg2"], args: svec!["arg1", "arg2"],
@ -2478,7 +2486,7 @@ mod tests {
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Install { subcommand: DenoSubcommand::Install {
dir: None, root: None,
exe_name: "deno_colors".to_string(), exe_name: "deno_colors".to_string(),
module_url: "https://deno.land/std/examples/colors.ts".to_string(), module_url: "https://deno.land/std/examples/colors.ts".to_string(),
args: vec![], args: vec![],

View file

@ -78,7 +78,10 @@ deno {} "$@"
Ok(()) Ok(())
} }
fn get_installer_dir() -> Result<PathBuf, Error> { fn get_installer_root() -> Result<PathBuf, Error> {
if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT").map(PathBuf::from) {
return env_dir.canonicalize();
}
// In Windows's Powershell $HOME environmental variable maybe null // In Windows's Powershell $HOME environmental variable maybe null
// if so use $USERPROFILE instead. // if so use $USERPROFILE instead.
let home = env::var("HOME") let home = env::var("HOME")
@ -96,23 +99,23 @@ fn get_installer_dir() -> Result<PathBuf, Error> {
let mut home_path = PathBuf::from(home_path); let mut home_path = PathBuf::from(home_path);
home_path.push(".deno"); home_path.push(".deno");
home_path.push("bin");
Ok(home_path) Ok(home_path)
} }
pub fn install( pub fn install(
flags: Flags, flags: Flags,
installation_dir: Option<PathBuf>, root: Option<PathBuf>,
exec_name: &str, exec_name: &str,
module_url: &str, module_url: &str,
args: Vec<String>, args: Vec<String>,
force: bool, force: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let installation_dir = if let Some(dir) = installation_dir { let root = if let Some(root) = root {
dir.canonicalize()? root.canonicalize()?
} else { } else {
get_installer_dir()? get_installer_root()?
}; };
let installation_dir = root.join("bin");
// ensure directory exists // ensure directory exists
if let Ok(metadata) = fs::metadata(&installation_dir) { if let Ok(metadata) = fs::metadata(&installation_dir) {
@ -268,8 +271,11 @@ mod tests {
} }
#[test] #[test]
fn install_custom_dir() { fn install_custom_dir_option() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
install( install(
Flags::default(), Flags::default(),
Some(temp_dir.path().to_path_buf()), Some(temp_dir.path().to_path_buf()),
@ -280,7 +286,35 @@ mod tests {
) )
.expect("Install failed"); .expect("Install failed");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) {
file_path = file_path.with_extension("cmd");
}
assert!(file_path.exists());
let content = fs::read_to_string(file_path).unwrap();
assert!(content
.contains(r#""run" "http://localhost:4545/cli/tests/echo_server.ts""#));
}
#[test]
fn install_custom_dir_env_var() {
let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
env::set_var("DENO_INSTALL_ROOT", temp_dir.path().to_path_buf());
install(
Flags::default(),
None,
"echo_test",
"http://localhost:4545/cli/tests/echo_server.ts",
vec![],
false,
)
.expect("Install failed");
let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }
@ -294,6 +328,8 @@ mod tests {
#[test] #[test]
fn install_with_flags() { fn install_with_flags() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
install( install(
Flags { Flags {
@ -310,7 +346,7 @@ mod tests {
) )
.expect("Install failed"); .expect("Install failed");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }
@ -323,6 +359,8 @@ mod tests {
#[test] #[test]
fn install_local_module() { fn install_local_module() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let local_module = env::current_dir().unwrap().join("echo_server.ts"); let local_module = env::current_dir().unwrap().join("echo_server.ts");
let local_module_url = Url::from_file_path(&local_module).unwrap(); let local_module_url = Url::from_file_path(&local_module).unwrap();
let local_module_str = local_module.to_string_lossy(); let local_module_str = local_module.to_string_lossy();
@ -337,7 +375,7 @@ mod tests {
) )
.expect("Install failed"); .expect("Install failed");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }
@ -350,6 +388,8 @@ mod tests {
#[test] #[test]
fn install_force() { fn install_force() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
install( install(
Flags::default(), Flags::default(),
@ -361,7 +401,7 @@ mod tests {
) )
.expect("Install failed"); .expect("Install failed");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }

View file

@ -279,7 +279,7 @@ async fn info_command(
async fn install_command( async fn install_command(
flags: Flags, flags: Flags,
dir: Option<PathBuf>, root: Option<PathBuf>,
exe_name: String, exe_name: String,
module_url: String, module_url: String,
args: Vec<String>, args: Vec<String>,
@ -292,7 +292,7 @@ async fn install_command(
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?; let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
let mut worker = create_main_worker(global_state, main_module.clone())?; let mut worker = create_main_worker(global_state, main_module.clone())?;
worker.preload_module(&main_module).await?; worker.preload_module(&main_module).await?;
installer::install(flags, dir, &exe_name, &module_url, args, force) installer::install(flags, root, &exe_name, &module_url, args, force)
.map_err(ErrBox::from) .map_err(ErrBox::from)
} }
@ -570,12 +570,12 @@ pub fn main() {
} }
DenoSubcommand::Info { file } => info_command(flags, file).boxed_local(), DenoSubcommand::Info { file } => info_command(flags, file).boxed_local(),
DenoSubcommand::Install { DenoSubcommand::Install {
dir, root,
exe_name, exe_name,
module_url, module_url,
args, args,
force, force,
} => install_command(flags, dir, exe_name, module_url, args, force) } => install_command(flags, root, exe_name, module_url, args, force)
.boxed_local(), .boxed_local(),
DenoSubcommand::Repl => run_repl(flags).boxed_local(), DenoSubcommand::Repl => run_repl(flags).boxed_local(),
DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(), DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(),

View file

@ -197,6 +197,8 @@ fn upgrade_in_tmpdir() {
#[test] #[test]
fn installer_test_local_module_run() { fn installer_test_local_module_run() {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let local_module = std::env::current_dir().unwrap().join("tests/echo.ts"); let local_module = std::env::current_dir().unwrap().join("tests/echo.ts");
let local_module_str = local_module.to_string_lossy(); let local_module_str = local_module.to_string_lossy();
deno::installer::install( deno::installer::install(
@ -208,7 +210,7 @@ fn installer_test_local_module_run() {
false, false,
) )
.expect("Failed to install"); .expect("Failed to install");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }
@ -235,6 +237,8 @@ fn installer_test_local_module_run() {
fn installer_test_remote_module_run() { fn installer_test_remote_module_run() {
let g = util::http_server(); let g = util::http_server();
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
deno::installer::install( deno::installer::install(
deno::flags::Flags::default(), deno::flags::Flags::default(),
Some(temp_dir.path().to_path_buf()), Some(temp_dir.path().to_path_buf()),
@ -244,7 +248,7 @@ fn installer_test_remote_module_run() {
false, false,
) )
.expect("Failed to install"); .expect("Failed to install");
let mut file_path = temp_dir.path().join("echo_test"); let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
file_path = file_path.with_extension("cmd"); file_path = file_path.with_extension("cmd");
} }
@ -1654,6 +1658,8 @@ fn cafile_install_remote_module() {
let g = util::http_server(); let g = util::http_server();
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap();
let deno_dir = TempDir::new().expect("tempdir fail"); let deno_dir = TempDir::new().expect("tempdir fail");
let cafile = util::root_path().join("cli/tests/tls/RootCA.pem"); let cafile = util::root_path().join("cli/tests/tls/RootCA.pem");
@ -1663,7 +1669,7 @@ fn cafile_install_remote_module() {
.arg("install") .arg("install")
.arg("--cert") .arg("--cert")
.arg(cafile) .arg(cafile)
.arg("--dir") .arg("--root")
.arg(temp_dir.path()) .arg(temp_dir.path())
.arg("echo_test") .arg("echo_test")
.arg("https://localhost:5545/cli/tests/echo.ts") .arg("https://localhost:5545/cli/tests/echo.ts")
@ -1671,7 +1677,7 @@ fn cafile_install_remote_module() {
.expect("Failed to spawn script"); .expect("Failed to spawn script");
assert!(install_output.status.success()); assert!(install_output.status.success());
let mut echo_test_path = temp_dir.path().join("echo_test"); let mut echo_test_path = bin_dir.join("echo_test");
if cfg!(windows) { if cfg!(windows) {
echo_test_path = echo_test_path.with_extension("cmd"); echo_test_path = echo_test_path.with_extension("cmd");
} }

View file

@ -874,14 +874,14 @@ Or you could import it into another ES module to consume:
### Installing executable scripts ### Installing executable scripts
Deno provides ability to easily install and distribute executable code via Deno provides `deno install` to easily install and distribute executable code.
`deno install` command.
`deno install [FLAGS...] [EXE_NAME] [URL] [SCRIPT_ARGS...]` will install the `deno install [FLAGS...] [EXE_NAME] [URL] [SCRIPT_ARGS...]` will install the
script available at `URL` under the name `EXE_NAME`. script available at `URL` under the name `EXE_NAME`.
This command is a thin wrapper that creates executable shell scripts which This command creates a thin, executable shell script which invokes `deno` using
invoke `deno` with specified permissions and CLI flags. the specified CLI flags and main module. It is place in the installation root's
`bin` directory.
Example: Example:
@ -893,36 +893,37 @@ $ deno install --allow-net --allow-read file_server https://deno.land/std/http/f
/Users/deno/.deno/bin/file_server /Users/deno/.deno/bin/file_server
``` ```
By default scripts are installed at `$HOME/.deno/bin` or To change the installation root, use `--root`:
`$USERPROFILE/.deno/bin` and one of that directories must be added to the path
manually. ```shell
$ deno install --allow-net --allow-read --root /usr/local file_server https://deno.land/std/http/file_server.ts
```
The installation root is determined, in order of precedence:
- `--root` option
- `DENO_INSTALL_ROOT` environment variable
- `$HOME/.deno`
These must be added to the path manually if required.
```shell ```shell
$ echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc $ echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc
``` ```
Installation directory can be changed using `-d/--dir` flag: You must specify permissions that will be used to run the script at installation
time.
```shell
$ deno install --allow-net --allow-read --dir /usr/local/bin file_server https://deno.land/std/http/file_server.ts
```
When installing a script you can specify permissions that will be used to run
the script.
Example:
```shell ```shell
$ deno install --allow-net --allow-read file_server https://deno.land/std/http/file_server.ts 8080 $ deno install --allow-net --allow-read file_server https://deno.land/std/http/file_server.ts 8080
``` ```
Above command creates an executable called `file_server` that runs with write The above command creates an executable called `file_server` that runs with
and read permissions and binds to port 8080. write and read permissions and binds to port 8080.
It is a good practice to use `import.meta.main` idiom for an entry point for For good practice, use the
executable file. See [`import.meta.main`](#testing-if-current-file-is-the-main-program) idiom to
[Testing if current file is the main program](#testing-if-current-file-is-the-main-program) specify the entry point in an executable script.
section.
Example: Example: