| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <#
- Install Visual C++ Build Tools (if missing), ensure OpenSSL env vars are set,
- and run cargo clean && cargo check in the src-tauri folder.
- Usage: Open an elevated (Administrator) PowerShell and run:
- .\install_build_tools_and_verify.ps1
- This script attempts to be idempotent and logs steps to the console.
- #>
- Set-StrictMode -Version Latest
- function Write-Log {
- param($msg)
- $t = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
- Write-Host "[$t] $msg"
- }
- function Ensure-Administrator {
- $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
- if (-not $isAdmin) {
- Write-Error "This script must be run as Administrator. Please re-open PowerShell as Administrator and re-run."
- exit 1
- }
- }
- function Ensure-OpenSSL-Installed {
- param($opensslRoot = "C:\Program Files\OpenSSL-Win64")
- if (-not (Test-Path $opensslRoot)) {
- Write-Error "OpenSSL root not found at $opensslRoot. Please install the full Win64 OpenSSL and re-run."
- exit 1
- }
- if (-not (Test-Path (Join-Path $opensslRoot "include\openssl"))) {
- Write-Error "OpenSSL include directory missing under $opensslRoot\include. Please install the Full OpenSSL distribution with developer files."
- exit 1
- }
- if (-not (Test-Path (Join-Path $opensslRoot "lib"))) {
- Write-Error "OpenSSL lib directory missing under $opensslRoot\lib. Please install the Full OpenSSL distribution with libraries."
- exit 1
- }
- return $opensslRoot
- }
- function Ensure-Choco {
- if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
- Write-Log "Chocolatey not found. Installing Chocolatey (will take a few minutes)..."
- Set-ExecutionPolicy Bypass -Scope Process -Force
- $script = 'https://community.chocolatey.org/install.ps1'
- try {
- iex ((New-Object System.Net.WebClient).DownloadString($script))
- } catch {
- Write-Error "Failed to install Chocolatey. Please install it manually and re-run this script."
- exit 1
- }
- # Refresh environment for choco
- $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
- } else {
- Write-Log "Chocolatey detected."
- }
- }
- function Install-VSBuildTools {
- # Install Visual Studio 2022 Build Tools with C++ workload
- if (Get-Command cl -ErrorAction SilentlyContinue) {
- Write-Log "MSVC (cl) already available."
- return
- }
- Write-Log "Installing Visual Studio 2022 Build Tools (Desktop development with C++) via Chocolatey..."
- $pkg = "visualstudio2022buildtools"
- $params = '--package-parameters "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"'
- $args = "install $pkg $params -y"
- $exit = & choco $args
- if ($LASTEXITCODE -ne 0) {
- Write-Error "Chocolatey installation of Visual Studio Build Tools failed. Please install manually from https://visualstudio.microsoft.com/visual-cpp-build-tools/"
- exit 1
- }
- Write-Log "Visual Studio Build Tools installation requested. It may take several minutes to complete."
- }
- function Find-VcVars {
- # Common path for VS Build Tools 2022
- $candidates = @(
- "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat",
- "C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat",
- "C:\Program Files (x86)\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat",
- "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
- )
- foreach ($p in $candidates) {
- if (Test-Path $p) { return $p }
- }
- return $null
- }
- function Configure-OpenSSL-Env {
- param($opensslRoot)
- Write-Log "Setting OpenSSL environment variables (system-wide)..."
- setx OPENSSL_DIR $opensslRoot | Out-Null
- setx OPENSSL_LIB_DIR (Join-Path $opensslRoot "lib") | Out-Null
- setx OPENSSL_INCLUDE_DIR (Join-Path $opensslRoot "include") | Out-Null
- # Append bin to PATH if not present
- $bin = (Join-Path $opensslRoot "bin")
- $currentPath = [Environment]::GetEnvironmentVariable("Path","Machine")
- if ($currentPath -notlike "*$bin*") {
- Write-Log "Adding $bin to system PATH"
- setx PATH "$currentPath;$bin" | Out-Null
- } else {
- Write-Log "OpenSSL bin already in PATH"
- }
- }
- function Run-Cargo-Under-VcVars {
- param($vcvarsPath, $projectDir)
- if (-not (Test-Path $vcvarsPath)) {
- Write-Error "vcvars batch not found at $vcvarsPath"
- exit 1
- }
- if (-not (Test-Path $projectDir)) {
- Write-Error "Project dir not found: $projectDir"
- exit 1
- }
- Write-Log "Running cargo clean && cargo check inside Developer Command Prompt context..."
- $cmd = "cmd /c `"$vcvarsPath`" && cd /d `"$projectDir`" && cargo clean && cargo check"
- Write-Log "Executing: $cmd"
- & cmd /c "`"$vcvarsPath`" && cd /d `"$projectDir`" && cargo clean && cargo check"
- if ($LASTEXITCODE -ne 0) {
- Write-Error "cargo check failed. See output above."
- exit 1
- }
- Write-Log "cargo check succeeded."
- }
- ### Main
- Ensure-Administrator
- $opensslRoot = Ensure-OpenSSL-Installed
- Configure-OpenSSL-Env -opensslRoot $opensslRoot
- Ensure-Choco
- Install-VSBuildTools
- # Try to find vcvars soon after installation
- $vcvars = Find-VcVars
- if (-not $vcvars) {
- Write-Log "vcvars64.bat not found automatically. You should open 'x64 Native Tools Command Prompt for VS' manually and run the cargo commands."
- Write-Log "Script attempted to install Build Tools; if installation is still running or requires reboot, please restart your machine and then open 'x64 Native Tools Command Prompt for VS'."
- exit 0
- }
- # Run cargo inside MSVC environment
- $proj = "D:\code\Buff\Buff\client-tauri\src-tauri"
- Run-Cargo-Under-VcVars -vcvarsPath $vcvars -projectDir $proj
- Write-Log "All done. If cargo check passed, try 'npm run tauri:dev' from project root."
|