diff --git a/README.md b/README.md index e4b28a4..2d8e669 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,18 @@ |:---------------:|:-----------:| | [![Build Status](https://travis-ci.com/denoland/deno_install.svg?branch=master)](https://travis-ci.com/denoland/deno_install) | [![Build status](https://ci.appveyor.com/api/projects/status/gtekeaf7r60xa896?svg=true)](https://ci.appveyor.com/project/deno/deno-install) | -Downloads the latest Deno binary into $HOME/.deno/bin +Downloads the latest Deno binary into `$HOME/.deno/bin`. + +**Install with Python:** -Usage: ``` curl -sSf https://raw.githubusercontent.com/denoland/deno_install/master/install.py | python ``` + +**Install with PowerShell:** + +```powershell +iex (iwr https://raw.githubusercontent.com/denoland/deno_install/master/install.ps1) +``` + +_Note: Depending on your security settings, you may have to run `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser` first to allow downloaded scripts to be executed._ diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000..c0716db --- /dev/null +++ b/install.ps1 @@ -0,0 +1,82 @@ +# Copyright 2018 the Deno authors. All rights reserved. MIT license. +# TODO(everyone): Keep this script simple and easily auditable. + +$ErrorActionPreference = "Stop" + +# Enable TLS 1.2 since it is required for connections to GitHub. +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +# Helper functions for pretty terminal output. +function Write-Part ([string] $Text) { + Write-Host $Text -NoNewline +} +function Write-Emphasized ([string] $Text) { + Write-Host $Text -NoNewLine -ForegroundColor "Yellow" +} +function Write-Done { + Write-Host " done" -NoNewline -ForegroundColor "Green"; + Write-Host "." +} + +# Determine latest Deno release via GitHub API. +$latest_release_uri = "https://api.github.com/repos/denoland/deno/releases/latest" +Write-Part "Downloading "; Write-Emphasized $latest_release_uri; Write-Part "..." +$latest_release_json = Invoke-WebRequest -Uri $latest_release_uri +Write-Done + +Write-Part "Determining latest Deno release: " +$latest_release = ($latest_release_json | ConvertFrom-Json).tag_name +Write-Emphasized $latest_release; Write-Part "... " +Write-Done + +# Create ~\.deno\bin directory if it doesn't already exist +$deno_dir = "${Home}\.deno\bin" +if (-not (Test-Path $deno_dir)) { + Write-Part "Creating directory "; Write-Emphasized $deno_dir; Write-Part "..." + New-Item -Path $deno_dir -ItemType Directory | Out-Null + Write-Done +} + +# Download latest Deno release. +$zip_file = "${deno_dir}\deno_win_x64.zip" +$download_uri = "https://github.com/denoland/deno/releases/download/" + + "${latest_release}/deno_win_x64.zip" +Write-Part "Downloading "; Write-Emphasized $download_uri; Write-Part "..." +Invoke-WebRequest -Uri $download_uri -OutFile $zip_file +Write-Done + +# Extract deno.exe from .zip file. +Write-Part "Extracting "; Write-Emphasized $zip_file +Write-Part " into "; Write-Emphasized ${deno_dir}; Write-Part "..." +# Using -Force to overwrite deno.exe if it already exists +Expand-Archive -Path $zip_file -DestinationPath $deno_dir -Force +Write-Done + +# Remove .zip file. +Write-Part "Removing "; Write-Emphasized $zip_file; Write-Part "..." +Remove-Item -Path $zip_file +Write-Done + +# Get Path environment variable for the current user. +$user = [EnvironmentVariableTarget]::User +$path = [Environment]::GetEnvironmentVariable("PATH", $user) + +# Check whether Deno is in the Path. +$paths = $path -split ";" +$is_in_path = $paths -contains $deno_dir -or $paths -contains "${deno_dir}\" + +# Add Deno to PATH if it hasn't been added already. +if (-not $is_in_path) { + Write-Part "Adding "; Write-Emphasized $deno_dir; Write-Part " to the " + Write-Emphasized "PATH"; Write-Part " environment variable..." + [Environment]::SetEnvironmentVariable("PATH", "${path};${deno_dir}", $user) + # Add Deno to the PATH variable of the current terminal session + # so `deno` can be used immediately without restarting the terminal. + $env:PATH += ";${deno_dir}" + Write-Done +} + +Write-Host "" +Write-Host "Deno was installed successfully." -ForegroundColor "Green" +Write-Part "Run "; Write-Emphasized "deno --help"; Write-Host " to get started." +Write-Host ""