github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/scripts/install.ps1 (about)

     1  $ErrorActionPreference = 'stop'
     2  
     3  # GitHub Org and Repo to get archives from
     4  $GitHubOrg="micro"
     5  $GitHubRepo="micro"
     6  $githubHeader = @{}
     7  
     8  $MicroInstallDir="c:\micro"
     9  $MicroCliName = "micro.exe"
    10  $MicroCliPath = "${MicroInstallDir}\${MicroCliName}"
    11  
    12  if((Get-ExecutionPolicy) -gt 'RemoteSigned' -or (Get-ExecutionPolicy) -eq 'ByPass') {
    13      Write-Output "PowerShell requires an execution policy of 'RemoteSigned'."
    14      Write-Output "To make this change please run:"
    15      Write-Output "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
    16      break
    17  }
    18  
    19  # Change security protocol to support TLS 1.2 / 1.1 / 1.0 - old powershell uses TLS 1.0 as a default protocol
    20  [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
    21  
    22  Write-Output "Installing micro..."
    23  
    24  # Create micro install directory
    25  Write-Output "Creating $MicroInstallDir directory"
    26  New-Item -ErrorAction Ignore -Path $MicroInstallDir -ItemType "directory"
    27  if (!(Test-Path $MicroInstallDir -PathType Container)) {
    28      throw "Could not create $MicroInstallDir"
    29  }
    30  
    31  # Get the list of releases from GitHub
    32  Write-Output "Getting the latest micro release"
    33  $releases = Invoke-RestMethod -Headers $githubHeader -Uri "https://api.github.com/repos/${GitHubOrg}/${GitHubRepo}/releases" -Method Get
    34  if ($releases.Count -eq 0) {
    35      throw "No releases found in github.com/tickoalcantara12/micro repo"
    36  }
    37  
    38  # Filter windows binary and download archive
    39  $windowsAsset = $releases[0].assets | where-object { $_.name -Like "*windows-amd64.zip" }
    40  if (!$windowsAsset) {
    41      throw "Cannot find the windows micro archive"
    42  }
    43  
    44  $zipFilePath = $MicroInstallDir + "\" + $windowsAsset.name
    45  Write-Output "Downloading $zipFilePath ..."
    46  
    47  $githubHeader.Accept = "application/octet-stream"
    48  Invoke-WebRequest -Headers $githubHeader -Uri $windowsAsset.url -OutFile $zipFilePath
    49  if (!(Test-Path $zipFilePath -PathType Leaf)) {
    50      throw "Failed to download micro - $zipFilePath"
    51  }
    52  
    53  # Extract micro to ${MicroInstallDir}
    54  Write-Output "Extracting $zipFilePath..."
    55  Expand-Archive -Force -Path $zipFilePath -DestinationPath $MicroInstallDir
    56  if (!(Test-Path $MicroCliPath -PathType Leaf)) {
    57      throw "Failed to download micro archive - $zipFilePath"
    58  }
    59  
    60  # Check the micro version
    61  Invoke-Expression "$MicroCliPath --version"
    62  
    63  # Clean up zipfile
    64  Write-Output "Cleaning up $zipFilePath..."
    65  Remove-Item $zipFilePath -Force
    66  
    67  # Add MicroInstallDir directory to User Path environment variable
    68  Write-Output "Attempting to add $MicroInstallDir to User Path Environment variable..."
    69  $UserPathEnvionmentVar = [Environment]::GetEnvironmentVariable("PATH", "User")
    70  if($UserPathEnvionmentVar -like "*$MicroInstallDir*") {
    71      Write-Output "Skipping to add $MicroInstallDir to User Path - $UserPathEnvionmentVar"
    72  } else {
    73      [System.Environment]::SetEnvironmentVariable("PATH", $UserPathEnvionmentVar + ";$MicroInstallDir", "User")
    74      $UserPathEnvionmentVar = [Environment]::GetEnvironmentVariable("PATH", "User")
    75      Write-Output "Added $MicroInstallDir to User Path - $UserPathEnvionmentVar"
    76  }
    77  
    78  Write-Output "`r`nmicro has been installed successfully."
    79  Write-Output "To start contributing to micro please visit https://github.com/micro"