VisualStudio/scripts/modules.ps1

202 строки
6.8 KiB
PowerShell
Исходник Обычный вид История

2017-10-27 14:33:06 +03:00
Add-Type -AssemblyName "System.Core"
Add-Type -TypeDefinition @"
public class ScriptException : System.Exception
{
2017-10-28 15:28:35 +03:00
public int ExitCode { get; private set; }
public ScriptException(string message, int exitCode) : base(message)
2017-10-27 14:33:06 +03:00
{
2017-10-28 15:28:35 +03:00
this.ExitCode = exitCode;
2017-10-27 14:33:06 +03:00
}
}
"@
New-Module -ScriptBlock {
2017-11-01 06:32:08 +03:00
$rootDirectory = Split-Path ($PSScriptRoot)
$scriptsDirectory = Join-Path $rootDirectory "scripts"
2017-10-27 14:33:06 +03:00
$nuget = Join-Path $rootDirectory "tools\nuget\nuget.exe"
Export-ModuleMember -Variable scriptsDirectory,rootDirectory,nuget
}
New-Module -ScriptBlock {
function Die([int]$exitCode, [string]$message, [object[]]$output) {
#$host.SetShouldExit($exitCode)
2017-10-27 14:33:06 +03:00
if ($output) {
2017-10-31 05:09:45 +03:00
Write-Host $output
2017-10-27 14:33:06 +03:00
$message += ". See output above."
}
2017-10-28 15:28:35 +03:00
$hash = @{
Message = $message
ExitCode = $exitCode
Output = $output
}
Throw (New-Object -TypeName ScriptException -ArgumentList $message,$exitCode)
#throw $message
2017-10-27 14:33:06 +03:00
}
function Run-Command([scriptblock]$Command, [switch]$Fatal, [switch]$Quiet) {
2017-10-27 14:33:06 +03:00
$output = ""
$exitCode = 0
if ($Quiet) {
$output = & $command 2>&1 | %{ "$_" }
} else {
& $command
}
if (!$? -and $LastExitCode -ne 0) {
$exitCode = $LastExitCode
} elseif ($? -and $LastExitCode -ne 0) {
$exitCode = $LastExitCode
}
if ($exitCode -ne 0) {
if (!$Fatal) {
2017-10-31 05:09:45 +03:00
Write-Host "``$Command`` failed" $output
2017-10-27 14:33:06 +03:00
} else {
Die $exitCode "``$Command`` failed" $output
2017-10-27 14:33:06 +03:00
}
}
$output
}
2017-10-27 14:33:06 +03:00
function Run-Process([int]$Timeout, [string]$Command, [string[]]$Arguments, [switch]$Fatal = $false)
{
$args = ($Arguments | %{ "`"$_`"" })
[object[]] $output = "$Command " + $args
$exitCode = 0
$outputPath = [System.IO.Path]::GetTempFileName()
$process = Start-Process -PassThru -NoNewWindow -RedirectStandardOutput $outputPath $Command ($args | %{ "`"$_`"" })
Wait-Process -InputObject $process -Timeout $Timeout -ErrorAction SilentlyContinue
if ($process.HasExited) {
$output += Get-Content $outputPath
$exitCode = $process.ExitCode
} else {
2018-09-06 17:31:23 +03:00
$output += "Process timed out. Backtrace:"
$output += Get-DotNetStack $process.Id
$exitCode = 9999
}
Stop-Process -InputObject $process
Remove-Item $outputPath
if ($exitCode -ne 0) {
if (!$Fatal) {
2017-10-31 05:09:45 +03:00
Write-Host "``$Command`` failed" $output
2017-10-27 14:33:06 +03:00
} else {
Die $exitCode "``$Command`` failed" $output
}
}
$output
}
2017-10-27 14:33:06 +03:00
function Create-TempDirectory {
$path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
New-Item -Type Directory $path
}
Export-ModuleMember -Function Die,Run-Command,Run-Process,Create-TempDirectory
2017-10-27 14:33:06 +03:00
}
New-Module -ScriptBlock {
function Find-MSBuild() {
if (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\MSBuild.exe") {
$msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\MSBuild.exe"
2017-10-27 14:33:06 +03:00
}
elseif (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe") {
$msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
}
elseif (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe") {
$msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"
}
2017-10-27 14:33:06 +03:00
else {
Die("No suitable msbuild.exe found.")
}
$msbuild
}
2017-10-31 06:27:28 +03:00
function Build-Solution([string]$solution, [string]$target, [string]$configuration, [switch]$ForVSInstaller, [bool]$Deploy = $false) {
2017-10-27 14:33:06 +03:00
Run-Command -Fatal { & $nuget restore $solution -NonInteractive -Verbosity detailed }
$flag1 = ""
$flag2 = ""
if ($ForVSInstaller) {
$flag1 = "/p:IsProductComponent=true"
$flag2 = "/p:TargetVsixContainer=$rootDirectory\build\vsinstaller\GitHub.VisualStudio.vsix"
new-item -Path $rootDirectory\build\vsinstaller -ItemType Directory -Force | Out-Null
} elseif (!$Deploy) {
$configuration += "WithoutVsix"
$flag1 = "/p:Package=Skip"
}
2017-10-27 14:33:06 +03:00
$msbuild = Find-MSBuild
2018-09-25 23:42:24 +03:00
Write-Host "$msbuild $solution /target:$target /property:Configuration=$configuration /p:DeployExtension=false /verbosity:minimal /p:VisualStudioVersion=15.0 /bl:output.binlog $flag1 $flag2"
Run-Command -Fatal { & $msbuild $solution /target:$target /property:Configuration=$configuration /p:DeployExtension=false /verbosity:minimal /p:VisualStudioVersion=15.0 /bl:output.binlog $flag1 $flag2 }
2017-10-27 14:33:06 +03:00
}
Export-ModuleMember -Function Find-MSBuild,Build-Solution
}
New-Module -ScriptBlock {
function Find-Git() {
$git = (Get-Command 'git.exe').Path
if (!$git) {
$git = Join-Path $rootDirectory 'PortableGit\cmd\git.exe'
}
if (!$git) {
Die("Couldn't find installed an git.exe")
}
$git
}
function Push-Changes([string]$branch) {
Push-Location $rootDirectory
2017-10-31 05:09:45 +03:00
Write-Host "Pushing $Branch to GitHub..."
2017-10-27 14:33:06 +03:00
Run-Command -Fatal { & $git push origin $branch }
Pop-Location
}
function Update-Submodules {
2017-10-31 05:09:45 +03:00
Write-Host "Updating submodules..."
Write-Host ""
2017-10-27 14:33:06 +03:00
Run-Command -Fatal { git submodule init }
Run-Command -Fatal { git submodule sync }
Run-Command -Fatal { git submodule update --recursive --force }
}
function Clean-WorkingTree {
2017-10-31 05:09:45 +03:00
Write-Host "Cleaning work tree..."
Write-Host ""
2017-10-27 14:33:06 +03:00
Run-Command -Fatal { git clean -xdf }
Run-Command -Fatal { git submodule foreach git clean -xdf }
}
2017-10-31 06:00:05 +03:00
function Get-HeadSha {
Run-Command -Quiet { & $git rev-parse HEAD }
}
2017-10-27 14:33:06 +03:00
$git = Find-Git
2017-10-31 06:00:05 +03:00
Export-ModuleMember -Function Find-Git,Push-Changes,Update-Submodules,Clean-WorkingTree,Get-HeadSha
}
New-Module -ScriptBlock {
function Write-Manifest([string]$directory) {
Add-Type -Path (Join-Path $rootDirectory packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll)
$manifest = @{
NewestExtension = @{
Version = [string](Read-CurrentVersionVsix)
Commit = [string](Get-HeadSha)
}
}
$manifestPath = Join-Path $directory manifest
[Newtonsoft.Json.JsonConvert]::SerializeObject($manifest) | Out-File $manifestPath -Encoding UTF8
}
Export-ModuleMember -Function Write-Manifest
2017-10-27 14:33:06 +03:00
}