FreshBuild.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
#Region '.\Private\Find-Sources.ps1' 0 function Find-Sources{ param( [PSObject]$Item=$null ) $scoopResult = & scoop search $item.name $chocoResult = & choco search $item.name $winGetResult = & winget search $item.name } #EndRegion '.\Private\Find-Sources.ps1' 10 #Region '.\Private\Get-ByChocolatey.ps1' 0 function Get-ByChocolatey { param( [PSObject]$Item=$null ) $parameters = @("install", $item.name, "-y") if($item.parameters){ $parameters += [string[]]$item.parameters; } Write-Host ("{0}: Parameters: {1}" -f $Item.name, $parameters) if($command) { if($item.elevate){ return Start-Process -Wait -Verb RunAs -FilePath $command -ArgumentList $parameters } else { return Start-Process -Wait -NoNewWindow -FilePath $command -ArgumentList $parameters } } else { throw ("No command matching {0} was found." -f $item.command); } } #EndRegion '.\Private\Get-ByChocolatey.ps1' 22 #Region '.\Private\Get-ByCommand.ps1' 0 function Get-ByCommand { param( [PSObject]$Item=$null ) if(-not $item.command) { throw "Expected command, none provided." } $command = (Get-Command $item.command).Source; $argList = @() if($item.parameters) { $argList += $item.parameters; } if($command) { if($item.elevate){ return Start-Process -Wait -Verb RunAs -FilePath $command -ArgumentList $argList } else { return Start-Process -Wait -NoNewWindow -FilePath $command -ArgumentList $argList } } else { throw ("No command matching {0} was found." -f $item.command); } } #EndRegion '.\Private\Get-ByCommand.ps1' 20 #Region '.\Private\Get-ByInstaller.ps1' 0 function Get-ByInstaller { param( [PSObject]$Item=$null ) $name = $item.Name $parameters = $item.parameters; $path = Join-Path $downloadFolder -ChildPath $item.fileName $destination = Get-Installer $item -Destination $path if($destination -ne $path){ throw "Not downloaded: $path, received $destination"; } if(Test-Path $path) { Write-Host ("{0}: Installing from {1}." -f $name, $path) $stdOut = "./standardOut.txt" $stdError = "./standardError.txt"; Remove-Item $stdError, $stdOut -ErrorAction SilentlyContinue $argList = @() switch ($Item.command) { 'msiexec' { $argList = @("/package", $path) + $parameters; $path = (Get-Command $Item.command).Source; } default { if($parameters) { $argList += $parameters; } } } if($path){ if($Item.elevate){ $process = Start-Process -FilePath $path ` -Verb RunAs ` -ArgumentList $argList ` -RedirectStandardOutput $stdOut ` -RedirectStandardError $stdError ` -Wait ` -PassThru } else { $process = Start-Process -FilePath $path ` -NoNewWindow ` -ArgumentList $argList ` -RedirectStandardOutput $stdOut ` -RedirectStandardError $stdError ` -Wait ` -PassThru } } else { throw "`$path is not set."; } if($process.ExitCode -ne 0){ Write-Host ("{0}: ExitCode: {1}" -f $Item.name, $process.ExitCode) Get-Content $stdOut -ErrorAction SilentlyContinue | Write-Host Get-Content $stdError -ErrorAction SilentlyContinue | Write-Error } Write-Host ("{0}: Install of {1} yielded: {2}" -f $name, $path, $process.ExitCode) } } #EndRegion '.\Private\Get-ByInstaller.ps1' 69 #Region '.\Private\Get-ByScoop.ps1' 0 function Get-ByScoop { param( [PSObject]$Item=$null ) $parameters = @("-c", $command, "install", $item.name) if($item.parameters){ $parameters += [string[]]$item.parameters; } if($command) { if($item.elevate){ return Start-Process -Wait -Verb RunAs -FilePath powershell -ArgumentList $parameters } else { return Start-Process -Wait -NoNewWindow -FilePath powershell -ArgumentList $parameters } } else { throw ("No command matching {0} was found." -f $item.command); } } #EndRegion '.\Private\Get-ByScoop.ps1' 20 #Region '.\Private\Get-ByScript.ps1' 0 function Get-ByScript { param( [PSObject]$Item=$null ) $script = $item.script; Write-Host ("{0}: Installing..." -f $item.name) $argList = @("-c", $script) if($item.parameters) { $argList += $item.parameters; } if($item.elevate) { try{ Start-Process -Wait -Verb RunAs -FilePath powershell -ArgumentList $argList } catch { Write-Error $_ } } else { try{ Start-Process -Wait -NoNewWindow -FilePath powershell -ArgumentList $argList } catch { Write-Error $_ } } Write-Host ("{0}: Installation complete." -f $item.name) } #EndRegion '.\Private\Get-ByScript.ps1' 25 #Region '.\Private\Get-ByWinGet.ps1' 0 function Get-ByWinget { param( [PSObject]$Item=$null ) $parameters = @("install", $item.name) if($item.parameters){ $parameters += [string[]]$item.parameters; } if($command) { if($item.elevate){ return Start-Process -Wait -Verb RunAs -FilePath $command -ArgumentList $parameters } else { return Start-Process -Wait -NoNewWindow -FilePath $command -ArgumentList $parameters } } else { throw ("No command matching {0} was found." -f $item.command); } } #EndRegion '.\Private\Get-ByWinGet.ps1' 20 #Region '.\Private\Get-Installer.ps1' 0 function Get-Installer { param ( [PSObject]$Item=$null, [string]$Destination ) if($Item.url -and $Item.url.length -gt 0) { if(Test-Path $Destination){ return $Destination; } Write-Host ("{0}: Downloading {1} to {2}" -f $Item.name, $Item.url, $Destination); Invoke-WebRequest $Item.url -OutFile $Destination if(-not(Test-Path $Destination)){ throw ("{0}: Error downloading file." -f $Item.name) } else { Write-Host ("{0}: Downloaded to: {1}" -f $Item.name, $Destination) return $Destination } } } #EndRegion '.\Private\Get-Installer.ps1' 24 #Region '.\Private\Install-Chocolatey.ps1' 0 function Install-Chocolatey { param ( [string] $InstallFolder ) $InstallDir = 'C:\ProgramData\chocoportable' if($installFolder) { if(-not (test-path $InstallFolder)) { new-item $InstallFolder -itemType Directory } $InstallDir = $InstallFolder } # Set directory for installation - Chocolatey does not lock # down the directory if not the default if (-not $env:ChocolateyInstall -or $InstallFolder) { $env:ChocolateyInstall = "$InstallDir" "`$env:ChocolateyInstall = `"$InstallDir`"" >> $PROFILE.AllUsersAllHosts Get-Content $PROFILE.AllUsersAllHosts | Write-Host } # If your PowerShell Execution policy is restrictive, you may # not be able to get around that. Try setting your session to # Bypass. Set-ExecutionPolicy Bypass -Scope Process -Force; # All install options - offline, proxy, etc at # https://chocolatey.org/install $url = 'https://community.chocolatey.org/install.ps1'; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString($url)) } function Uninstall-Chocolatey { $VerbosePreference = 'Continue' if (-not $env:ChocolateyInstall) { $message = @( "The ChocolateyInstall environment variable was not found." "Chocolatey is not detected as installed. Nothing to do." ) -join "`n" Write-Warning $message return } if (-not (Test-Path $env:ChocolateyInstall)) { $message = @( "No Chocolatey installation detected at '$env:ChocolateyInstall'." "Nothing to do." ) -join "`n" Write-Warning $message return } <# Using the .NET registry calls is necessary here in order to preserve environment variables embedded in PATH values; Powershell's registry provider doesn't provide a method of preserving variable references, and we don't want to accidentally overwrite them with absolute path values. Where the registry allows us to see "%SystemRoot%" in a PATH entry, PowerShell's registry provider only sees "C:\Windows", for example. #> $userKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment') $userPath = $userKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString() $machineKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\ControlSet001\Control\Session Manager\Environment\') $machinePath = $machineKey.GetValue('PATH', [string]::Empty, 'DoNotExpandEnvironmentNames').ToString() $backupPATHs = @( "User PATH: $userPath" "Machine PATH: $machinePath" ) $backupFile = "C:\PATH_backups_ChocolateyUninstall.txt" $backupPATHs | Set-Content -Path $backupFile -Encoding UTF8 -Force $warningMessage = @" This could cause issues after reboot where nothing is found if something goes wrong. In that case, look at the backup file for the original PATH values in '$backupFile'. "@ if ($userPath -like "*$env:ChocolateyInstall*") { Write-Verbose "Chocolatey Install location found in User Path. Removing..." Write-Warning $warningMessage $newUserPATH = @( $userPath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" } ) -join [System.IO.Path]::PathSeparator # NEVER use [Environment]::SetEnvironmentVariable() for PATH values; see https://github.com/dotnet/corefx/issues/36449 # This issue exists in ALL released versions of .NET and .NET Core as of 12/19/2019 $userKey.SetValue('PATH', $newUserPATH, 'ExpandString') } try { if ($machinePath -like "*$env:ChocolateyInstall*") { Write-Verbose "Chocolatey Install location found in Machine Path. Removing..." Write-Warning $warningMessage $newMachinePATH = @( $machinePath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -and $_ -ne "$env:ChocolateyInstall\bin" } ) -join [System.IO.Path]::PathSeparator # NEVER use [Environment]::SetEnvironmentVariable() for PATH values; see https://github.com/dotnet/corefx/issues/36449 # This issue exists in ALL released versions of .NET and .NET Core as of 12/19/2019 $machineKey.SetValue('PATH', $newMachinePATH, 'ExpandString') } } catch { #Write-Error $_ } # Adapt for any services running in subfolders of ChocolateyInstall $agentService = Get-Service -Name chocolatey-agent -ErrorAction SilentlyContinue if ($agentService -and $agentService.Status -eq 'Running') { $agentService.Stop() } # TODO: add other services here Remove-Item -Path $env:ChocolateyInstall -Recurse -Force 'ChocolateyInstall', 'ChocolateyLastPathUpdate' | ForEach-Object { foreach ($scope in 'User', 'Machine') { [Environment]::SetEnvironmentVariable($_, [string]::Empty, $scope) } } $machineKey.Close() $userKey.Close() if ($env:ChocolateyToolsLocation -and (Test-Path $env:ChocolateyToolsLocation)) { Remove-Item -Path $env:ChocolateyToolsLocation -Recurse -Force } foreach ($scope in 'User', 'Machine') { [Environment]::SetEnvironmentVariable('ChocolateyToolsLocation', [string]::Empty, $scope) } Write-Host "Completed uninstallation of Chocolatey." } #EndRegion '.\Private\Install-Chocolatey.ps1' 146 #Region '.\Private\Install-Scoop.ps1' 0 function Install-Scoop { param ( [string] $InstallFolder ) $item = ('{ "name": "Scoop", "source": "direct", "script": "iwr -useb get.scoop.sh | iex; scoop bucket add extras" }' | ConvertFrom-Json); if($InstallFolder) { $scoopdir = $env:SCOOP = "$InstallFolder" "`$env:SCOOP = `"$InstallFolder`"" >> $PROFILE.AllUsersAllHosts "`$scoopdir = `"$InstallFolder`"" >> $PROFILE.AllUsersAllHosts } if($env:SCOOP) { Write-Host "Installing scoop to ${env:SCOOP}." } Get-ByScript $item; } function Uninstall-Scoop{ $scoop = Get-Command scoop; if($scoop) { . $scoop export | ForEach-Object -process { . $scoop uninstall $_ } . $scoop uninstall scoop } } #EndRegion '.\Private\Install-Scoop.ps1' 35 #Region '.\Private\Install-Software.ps1' 0 function Install-Software{ param( [PSObject]$Item=$null ) Write-Host $Item switch($Item.source){ "winget" { $command= $env:USERPROFILE + "/AppData/Local/Microsoft/WindowsApps/winget.exe"; if(-not(Test-Path $command)) { Install-WinGet } $result = Get-ByWinget $item if($result) { Write-Host $result; } } "scoop" { $command= $env:USERPROFILE + "/scoop/shims/scoop.ps1"; if(-not(Test-Path $command)) { Install-Scoop } $result = Get-ByScoop $item if($result) { Write-Host $result; } } "choco" { $command="C:/ProgramData/chocolatey/bin/cinst.exe"; if(-not(Test-Path $command)) { Install-Chocolatey } $result = Get-ByChocolatey $item if($result) { Write-Host $result; } } default { try{ if($item.url.length -gt 0) { Get-ByInstaller $item } elseif($item.script) { Get-ByScript $item } elseif($item.command) { Get-ByCommand $item } } catch { Write-Error $_ throw $_ } } } } #EndRegion '.\Private\Install-Software.ps1' 63 #Region '.\Private\Install-WinGet.ps1' 0 function Install-WinGet { $item = ('{ "name": "WinGet", "source": "direct", "url": "", "script": "Add-AppxPackage https://aka.ms/getwinget -InstallAllResources" }' | ConvertFrom-Json); Get-ByScript $item; '{ // For documentation on these settings, see: https://aka.ms/winget-settings // "source": { // "autoUpdateIntervalInMinutes": 5 // }, "experimentalFeatures": { "experimentalMSStore": true } }' | Out-File "$env:USERPROFILE\AppData\Local\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json" } function Uninstall-WinGet { $winget = Get-AppxPackage Microsoft.DesktopAppInstaller; if ($winget) { $winget | Remove-AppxPackage } } #EndRegion '.\Private\Install-WinGet.ps1' 28 #Region '.\Private\Search-FreshBuild.ps1' 0 function Search-FreshBuild { [CmdletBinding()] param( [string]$jsonConfig = "$env:USERPROFILE/.freshBuild/FreshInstall.json", [string]$Exclude = $null, [string]$Include = $null, [switch]$Search = $false ) Write-Host "Seach: " if ($Include) { Write-Host ('Include: {0}' -f $Include) } if ($Exclude) { Write-Host ('Exclude: {0}' -f $Exclude) } } #EndRegion '.\Private\Search-FreshBuild.ps1' 13 #Region '.\Public\Initialize-FreshBuild.ps1' 0 function Initialize-FreshBuild { param( [Switch]$All = $false, [switch]$InstallWinGet = $false, [switch]$InstallChocolatey = $false, [switch]$InstallScoop = $false, [string]$InstallRoot = $null ) $freshbuildDir = Join-Path $Env:USERPROFILE '.freshbuild' $jsonFile = Join-Path $FreshBuildDir 'PackageManagers.json' if (!(Test-Path $freshbuildDir)) { $null = New-Item -Path $freshbuildDir -ItemType Directory -Force } if (Test-Path $jsonFile) { [string[]]$installed = Get-Content $jsonFile | ConvertFrom-Json; } else { [string[]]$installed = @(); } if ($InstallWinGet -or $All) { Install-WinGet; $installed += 'WinGet' } if ($installRoot) { if ($InstallChocolatey -or $All) { Install-Chocolatey -InstallFolder "$InstallRoot\Chocolatey"; $installed += 'Chocolatey' } if ($InstallScoop -or $All) { Install-Scoop -InstallFolder "$InstallRoot\Scoop"; $installed += 'Scoop' } } else { if ($InstallChocolatey -or $All) { Install-Chocolatey; $installed += 'Chocolatey' } if ($InstallScoop -or $All) { Install-Scoop; $installed += 'Scoop' } } $json = $installed | ConvertTo-Json; if (-not $json) { $json = '[]'; } $json | Out-File -Path $jsonFile } #EndRegion '.\Public\Initialize-FreshBuild.ps1' 48 #Region '.\Public\Start-FreshBuild.ps1' 0 function Start-FreshBuild { [CmdletBinding()] param( [string]$jsonConfig = "$env:USERPROFILE/.freshBuild/FreshInstall.json", [string]$Exclude = $null, [string]$Include = $null, [switch]$Step = $false, [switch]$UninstallWinGet=$false, [switch]$UninstallChocolatey=$false, [switch]$UninstallScoop=$false, [switch]$InstallWinGet=$false, [switch]$InstallChocolatey=$false, [switch]$InstallScoop=$false, [switch]$NoScript=$false, [switch]$UpdateJson=$false ) Push-Location try { $jsonUrl = "https://gist.github.com/sharpninja/2ad839cb141bc6b968278bd7416931ce/raw/" $defaultJsonConfig = "$env:USERPROFILE/.freshBuild/FreshInstall.json"; if($UpdateJson) { Invoke-WebRequest ` -Uri $jsonUrl ` -OutFile $defaultJsonConfig } if (-not (Test-Path $jsonConfig)) { if ($jsonConfig -eq $defaultJsonConfig) { Set-Location (Get-Module -Name "FreshBuild").Path New-Item $env:USERPROFILE/.freshBuild -ItemType Directory -ErrorAction SilentlyContinue Invoke-WebRequest ` -Uri $jsonUrl ` -OutFile $defaultJsonConfig } else { return -1; } } $items = (Get-Content $jsonConfig | ConvertFrom-Json).items $downloadFolder = "$env:USERPROFILE/.freshBuild/downloads" if($UninstallWinGet) {Uninstall-WinGet} if($UninstallChocolatey) {Uninstall-Chocolatey} if($UninstallScoop) {Uninstall-Scoop} if($InstallWinGet) {Install-WinGet} if($InstallChocolatey) {Install-Chocolatey} if($InstallScoop) {Install-Scoop} if($NoScript) {return 0;} if (-not (Test-Path $downloadFolder)) { New-Item $downloadFolder -ItemType Directory } foreach ($item in $items) { Write-Host $item.name if (-not ($item -is [string])) { if ((-not($Exclude -and ($item.name -Match $Exclude))) -and (($null -eq $Include) -or ($item.name -match $Include))) { Write-Host ("{0}: Beginning Installation." -f $item.name) [PSObject]$typed = $item; Install-Software -Item $typed Write-Host ("{0}: Finished Installation." -f $item.name) Write-Host } } if ($Step) { Read-Host "Hit enter for next step..." } } } finally { Pop-Location } } #EndRegion '.\Public\Start-FreshBuild.ps1' 83 |