github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/install.ps1 (about)

     1  # Copyright 2022 The NATS Authors
     2  # Licensed under the Apache License, Version 2.0 (the "License");
     3  # you may not use this file except in compliance with the License.
     4  # You may obtain a copy of the License at
     5  #
     6  # http://www.apache.org/licenses/LICENSE-2.0
     7  #
     8  # Unless required by applicable law or agreed to in writing, software
     9  # distributed under the License is distributed on an "AS IS" BASIS,
    10  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  # See the License for the specific language governing permissions and
    12  # limitations under the License.
    13  
    14  #Requires -RunAsAdministrator
    15  
    16  $ErrorActionPreference = 'Stop'
    17  $CurrentNightlyUrl = "https://get-nats.io/current-nightly"
    18  $PlatformsUrl = "https://get-nats.io/synadia-nats-platforms.json"
    19  $NATSDir = "NATS"
    20  $OSInfo = "windows-amd64"
    21  $Stable = "stable"
    22  $Nightly = "nightly"
    23  $NscTool = "nsc"
    24  $NscExe = "nsc.exe"
    25  $NscZip = "nsc.zip"
    26  
    27  # ----------------------------------------------------------------------------------------------------
    28  # Functions
    29  # ----------------------------------------------------------------------------------------------------
    30  Function Read-NightlyVersion() {
    31  	if (!$_currentNightly) {
    32  		$temp = [string](Invoke-WebRequest -Uri $CurrentNightlyUrl)
    33  		$_currentNightly = $temp.Split("`r?`n")[0]
    34  	}
    35  	return $_currentNightly
    36  }
    37  
    38  Function Format-EndWithBackslash($s) {
    39  	if ($s.EndsWith("\")){
    40  		return $s
    41  	}
    42  	return $s + "\"
    43  }
    44  
    45  Function Format-DoesntEndWithBackslash($s) {
    46  	if ($s.EndsWith("\")){
    47  		return $s.Substring(0, $s.Length - 1)
    48  	}
    49  	return $s
    50  }
    51  
    52  Function Select-Folder() {
    53  	[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    54  	$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    55  	$FolderBrowserDialog.RootFolder = 'MyComputer'
    56  	[void] $FolderBrowserDialog.ShowDialog()
    57  	return $FolderBrowserDialog.SelectedPath
    58  }
    59  
    60  Function Invoke-Backup($BinDir, $Tool, $ExePath) {
    61  	if ( Test-Path $ExePath )
    62  	{
    63  		Write-Host "Backing up existing $Tool executable..."
    64  		for($i = 1; $i -lt 100; $i++) # I give up after 99 tries
    65  		{
    66  			$nn = "$BinDir$Tool-" + (Get-Date -Format "yyyyMMdd") + "-backup$i.exe"
    67  			if (!(Test-Path $nn))
    68  			{
    69  				Rename-Item -Path $ExePath -NewName $nn
    70  				return
    71  			}
    72  		}
    73  		Write-Host "Cannot backup $ExePath, all backups exist."
    74  		Exit -2
    75  	}
    76  }
    77  
    78  # ----------------------------------------------------------------------------------------------------
    79  # Execution
    80  # ----------------------------------------------------------------------------------------------------
    81  # They get to pick the folder given a default
    82  $tempDir = (Format-EndWithBackslash $Env:ProgramFiles) + $NATSDir
    83  $opt0 = New-Object System.Management.Automation.Host.ChoiceDescription "&Default Location","Default Location is $tempDir"
    84  $opt1 = New-Object System.Management.Automation.Host.ChoiceDescription "&Choose Location","Choose your location."
    85  $options = [System.Management.Automation.Host.ChoiceDescription[]]($opt0, $opt1)
    86  $result = $host.ui.PromptForChoice("Installation Location", "Where will the programs be installed? Default Location is $tempDir", $options, 0)
    87  if ($result -eq 1) {
    88  	$tempDir = Select-Folder
    89  	if (!$tempDir) {
    90  		Write-Host "You must pick a directory. Exiting"
    91  		Exit -1
    92  	}
    93  }
    94  $binDir = Format-EndWithBackslash $tempDir
    95  $binDirNoSlash = Format-DoesntEndWithBackslash $binDir
    96  if ( !(Test-Path $binDirNoSlash) ) {
    97  	New-Item $binDirNoSlash -ItemType Directory | Out-Null
    98  }
    99  
   100  # some local variables now that I have $binDir
   101  $nscExePath = $binDir + $NscExe
   102  $nscZipLocal =  $binDir + $NscZip
   103  
   104  # $channel Have the user pick which type of channel they want, i.e. stable or nightly.
   105  $opt0 = New-Object System.Management.Automation.Host.ChoiceDescription "&$Stable","Latest Stable Build."
   106  $opt1 = New-Object System.Management.Automation.Host.ChoiceDescription "&$Nightly","Current Nightly Build."
   107  $options = [System.Management.Automation.Host.ChoiceDescription[]]($opt0, $opt1)
   108  $result = $host.ui.PromptForChoice("$Channel Selection", "Which channel do you want to install?", $options, 0)
   109  switch ($result) {
   110  	0{$channel = $Stable}
   111  	1{$channel = $Nightly}
   112  }
   113  Write-Host ""
   114  
   115  # Add bin dir to path if not already in path
   116  Write-Host "Ensuring $binDirNoSlash is in the path..."
   117  $Machine = [EnvironmentVariableTarget]::Machine
   118  $Path = [Environment]::GetEnvironmentVariable('Path', $Machine)
   119  if (!(";$Path;".ToLower() -like "*;$binDirNoSlash;*".ToLower())) {
   120  	[Environment]::SetEnvironmentVariable('Path', "$Path;$binDirNoSlash", $Machine)
   121  	$Env:Path += ";$binDirNoSlash"
   122  }
   123  
   124  Write-Host "Downloading platform info..."
   125  $json = (Invoke-WebRequest https://get-nats.io/synadia-nats-platforms.json -ContentType "application/json" -UseBasicParsing) | ConvertFrom-Json
   126  $nscZipUrl = $json.$channel.platforms.$OSInfo.tools.$NscTool.zip_url
   127  
   128  if ($channel -eq $Nightly) {
   129  	$verNsc = Read-NightlyVersion
   130  	Write-Host "$NscTool $Nightly version $verNsc"
   131  	$nscZipUrl = $nscZipUrl.Replace("%NIGHTLY%", $verNsc)
   132  }
   133  else {
   134  	$verNsc = $json.$channel.platforms.$OSInfo.tools.$NscTool."version_tag"
   135  	Write-Host "$NscTool $Stable version $verNsc"
   136  }
   137  
   138  # Download the zip files
   139  Write-Host "Downloading archive $nscZipUrl..."
   140  Invoke-WebRequest -Uri $nscZipUrl -OutFile $nscZipLocal -UseBasicParsing
   141  
   142  # Backup existing versions now that the downloads worked
   143  Invoke-Backup $binDir $NscTool $nscExePath
   144  
   145  # nsc: Unzip, Remove Archive
   146  Write-Host "Installing $NscTool..."
   147  Expand-Archive -Path $nscZipLocal -DestinationPath $binDir
   148  Remove-Item $nscZipLocal
   149  
   150  Write-Host "Done!`r`n"