Setup.ps1 (6357B)
1 # Copyright (c) Microsoft Corporation. 2 # Licensed under the MIT License. 3 4 <# 5 .SYNOPSIS 6 Sets up dependencies required to build and work with the SFS Client. 7 8 .DESCRIPTION 9 This script will install all of the dependencies required to build and work with the SFS Client. 10 Use this on Windows platforms in a PowerShell session. 11 12 .EXAMPLE 13 PS> .\scripts\Setup.ps1 14 #> 15 16 $ErrorActionPreference = "Stop" 17 18 $GitRoot = (Resolve-Path (&git -C $PSScriptRoot rev-parse --show-toplevel)).Path 19 20 function Update-Env { 21 $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User") 22 } 23 24 function Install-Python { 25 Write-Host -ForegroundColor Cyan "Installing latest Python if it's not installed" 26 27 # We use the "python --version" command to check if it exists because by default Windows now comes 28 # with a python.exe from the Store that simply opens the Store when someone tries to use python.exe 29 python --version 2>&1 | Out-Null 30 if (!$?) { 31 winget install python 32 if (!$?) { 33 Write-Host -ForegroundColor Red "Failed to install Python" 34 exit 1 35 } 36 } 37 } 38 39 function Install-PipDependencies { 40 Write-Host -ForegroundColor Cyan "`nInstalling dependencies using pip" 41 42 # Upgrade pip and install requirements. Filter out output for dependencies that are already installed 43 $PipInstalledPackageString = "Requirement already satisfied" 44 python -m pip install --upgrade pip | Select-String -Pattern $PipInstalledPackageString -NotMatch 45 46 # Installing to user site packages 47 $PipReqs = Join-Path $PSScriptRoot "pip.requirements.txt" -Resolve 48 pip install --user -r $PipReqs | Select-String -Pattern $PipInstalledPackageString -NotMatch 49 } 50 51 function Install-CMake { 52 Write-Host -ForegroundColor Cyan "`nInstalling cmake if it's not installed" 53 54 # Installing cmake from winget because it adds to PATH 55 try { 56 cmake --version 57 } 58 catch { 59 winget install cmake 60 if (!$?) { 61 Write-Host -ForegroundColor Red "Failed to install cmake" 62 exit 1 63 } 64 65 # Update env with newly installed cmake 66 Update-Env 67 } 68 } 69 70 function Install-CppBuildTools { 71 Write-Host -ForegroundColor Cyan "`nInstalling C++ Builds tools if they are not installed" 72 73 # Instaling vswhere, which will be used to query for the required build tools 74 try { 75 vswhere -? 2>&1 | Out-Null 76 } 77 catch { 78 winget install vswhere 79 if (!$?) { 80 Write-Host -ForegroundColor Red "Failed to install vswhere" 81 exit 1 82 } 83 } 84 85 # - Microsoft.VisualStudio.Workload.VCTools is the C++ workload in the Visual Studio Build Tools 86 # - Microsoft.VisualStudio.Workload.NativeDesktop is the C++ workload that comes pre-installed in the github runner image 87 $ExistingBuildTools = vswhere -products * -requires Microsoft.VisualStudio.Workload.VCTools Microsoft.VisualStudio.Workload.NativeDesktop -requiresAny -format json | ConvertFrom-Json 88 if ($null -eq $ExistingBuildTools) 89 { 90 Write-Host "`nTools not found, installing..." 91 92 # --wait makes the install synchronous 93 winget install Microsoft.VisualStudio.2022.BuildTools --silent --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --remove Microsoft.VisualStudio.Component.VC.CMake.Project" 94 if (!$?) { 95 Write-Host -ForegroundColor Red "Failed to install build tools" 96 exit 1 97 } 98 } 99 } 100 101 function Install-Vcpkg { 102 Write-Host -ForegroundColor Cyan "`nSetting up vcpkg" 103 104 if (!(Test-Path vcpkg)) { 105 Write-Host "Cloning vcpkg repo" 106 git clone https://github.com/microsoft/vcpkg $GitRoot\vcpkg 107 } 108 else { 109 Write-Host "Checking if vcpkg repo has new commits" 110 $NoUpdatesString = "Already up to date." 111 git -C "$GitRoot\vcpkg" pull --show-forced-updates | Select-String -Pattern $NoUpdatesString -NotMatch 112 } 113 114 # Bootstrapping on every setup updates the vcpkg.exe and solves potential issues with VS Build Tools not being found 115 & "$GitRoot\vcpkg\bootstrap-vcpkg.bat" 116 } 117 118 function Set-GitHooks { 119 Write-Host -ForegroundColor Cyan "`nSetting Git hooks" 120 121 $HookDestDir = Join-Path $GitRoot "\.git\hooks" -Resolve 122 $GitHooks = @{"pre-commit-wrapper.sh" = "pre-commit" } 123 foreach ($i in $GitHooks.GetEnumerator()) { 124 $HookSrc = Join-Path $GitRoot $i.Name -Resolve 125 $HookDest = Join-Path $HookDestDir $i.Value 126 127 # If the destination doesn't exist or is different than the one in the source, we'll copy it over. 128 if (-not (Test-Path $HookDest) -or (Get-FileHash $HookDest).Hash -ne (Get-FileHash $HookSrc).Hash) { 129 Copy-Item -Path $HookSrc -Destination $HookDest -Force | Out-Null 130 Write-Host -ForegroundColor Cyan "Setup git $($i.Value) hook with $HookSrc" 131 } 132 } 133 } 134 135 function Set-Aliases { 136 Write-Host -ForegroundColor Cyan "`nSetting aliases" 137 138 $PythonScriptsDir = python -c "import os,sysconfig;print(sysconfig.get_path('scripts',f'{os.name}_user'))" 139 140 $Aliases = [ordered]@{ 141 "build" = "$GitRoot\scripts\Build.ps1" 142 "clang-format" = "$PythonScriptsDir\clang-format.exe" 143 "cmake-format" = "$PythonScriptsDir\cmake-format.exe" 144 "test" = "$GitRoot\scripts\Test.ps1" 145 "vcpkg" = "$GitRoot\vcpkg\vcpkg.exe" 146 } 147 foreach ($i in $Aliases.GetEnumerator()) { 148 $Alias = $i.Name 149 $Target = $i.Value 150 151 # If the alias doesn't exist or is set to something different than the one we want to target, we'll add it. 152 $AliasDoesNotExist = !(Get-Alias $Alias -ErrorAction SilentlyContinue) 153 if ($AliasDoesNotExist -or (Get-Alias $Alias).Definition -ne $Target) { 154 if (!$AliasDoesNotExist) { 155 Remove-Alias $Alias -Scope Global 156 } 157 New-Alias -Name $Alias -Value $Target -Scope Global 158 Write-Host "Setup alias $Alias" 159 } 160 } 161 } 162 163 Install-Python 164 Install-PipDependencies 165 Install-CMake 166 Install-CppBuildTools 167 Install-Vcpkg 168 Set-GitHooks 169 Set-Aliases