build-utils.ps1 7.9 KB
Newer Older
J
Jared Parsons 已提交
1 2 3 4 5 6 7 8
# Collection of powershell build utility functions that we use across our scripts.

Set-StrictMode -version 2.0
$ErrorActionPreference="Stop"

# Declare a number of useful variables for other scripts to use
[string]$repoDir = Resolve-Path (Join-Path $PSScriptRoot "..\..")
[string]$binariesDir = Join-Path $repoDir "Binaries"
9 10 11 12 13 14 15 16 17 18 19 20 21

# Handy function for executing a command in powershell and throwing if it 
# fails.  
# 
# Original sample came from: http://jameskovacs.com/2010/02/25/the-exec-problem/
function Exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd) { 
    $output = & $cmd 
    if (-not $?) {
        Write-Host $output
        throw $errorMessage 
    } 
}

J
Jared Parsons 已提交
22 23
# Ensure that NuGet is installed and return the path to the 
# executable to use.
24
function Ensure-NuGet() {
J
Jared Parsons 已提交
25
    Exec { & (Join-Path $PSScriptRoot "download-nuget.ps1") }
26 27 28
    $nuget = Join-Path $repoDir "NuGet.exe"
    return $nuget
}
J
Jared Parsons 已提交
29

J
Jared Parsons 已提交
30 31 32 33 34 35 36 37 38 39 40 41
# Ensure a basic tool used for building our Repo is installed and 
# return the path to it.
function Ensure-BasicTool([string]$name, [string]$version) {
    $p = Join-Path (Get-PackagesDir) "$($name).$($version)"
    if (-not (Test-Path $p)) {
        $nuget = Ensure-NuGet
        Exec { & $nuget install $name -OutputDirectory (Get-PackagesDir) -Version $version }
    }
    
    return $p
}

J
Jared Parsons 已提交
42 43
# Ensure that MSBuild is installed and return the path to the
# executable to use.
J
Jared Parsons 已提交
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
function Ensure-MSBuild([switch]$xcopy = $false) {
    $both = Get-MSBuildKindAndDir -xcopy:$xcopy
    $msbuildDir = $both[1]
    switch ($both[0]) {
        "xcopy" {
            $p = Get-PackageDir "RoslynTools.ReferenceAssemblies"
            ${env:TargetFrameworkRootPath} = Join-Path $p "tools\Framework"
            break;
        }
        "vscmd" {
            # Nothing to do here as the VS Dev CMD should set all appropriate environment
            # variables.
            break;
        }
        "vsinstall" {
            # Nothing to do here as the VS Dev CMD should set all appropriate environment
            # variables.
            break;
        }
        default {
            throw "Unknown MSBuild installation type $($both[0])"
        }
    }

    $p = Join-Path $msbuildDir "msbuild.exe"
J
Jared Parsons 已提交
69 70 71
    return $p
}

J
Jared Parsons 已提交
72
function Create-Directory([string]$dir) {
73
    New-Item $dir -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
J
Jared Parsons 已提交
74 75 76 77
}

# Return the version of the NuGet package as used in this repo
function Get-PackageVersion([string]$name) {
J
Jared Parsons 已提交
78
    $name = $name.Replace(".", "")
J
Jared Parsons 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
    $deps = Join-Path $repoDir "build\Targets\Dependencies.props"
    $nodeName = "$($name)Version"
    $x = [xml](Get-Content -raw $deps)
    $node = $x.Project.PropertyGroup[$nodeName]
    if ($node -eq $null) { 
        throw "Cannot find package $name in Dependencies.props"
    }

    return $node.InnerText
}

# Locate the directory where our NuGet packages will be deployed.  Needs to be kept in sync
# with the logic in Version.props
J
Jared Parsons 已提交
92
function Get-PackagesDir() {
J
Jared Parsons 已提交
93 94 95 96 97 98 99 100 101 102 103
    $d = $null
    if ($env:NUGET_PACKAGES -ne $null) {
        $d = $env:NUGET_PACKAGES
    }
    else {
        $d = Join-Path $env:UserProfile ".nuget\packages\"
    }

    Create-Directory $d
    return $d
}
104

J
Jared Parsons 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
# Locate the directory of a specific NuGet package which is restored via our main 
# toolset values.
function Get-PackageDir([string]$name, [string]$version = "") {
    if ($version -eq "") {
        $version = Get-PackageVersion $name
    }

    $p = Get-PackagesDir
    $p = Join-Path $p $name
    $p = Join-Path $p $version
    return $p
}

118 119 120 121 122 123
# The intent of this script is to locate and return the path to the MSBuild directory that
# we should use for bulid operations.  The preference order for MSBuild to use is as 
# follows
#
#   1. MSBuild from an active VS command prompt
#   2. MSBuild from a machine wide VS install
J
Jared Parsons 已提交
124
#   3. MSBuild from the xcopy toolset 
125 126
#
# This function will return two values: the kind of MSBuild chosen and the MSBuild directory.
J
Jared Parsons 已提交
127 128 129 130 131 132 133
function Get-MSBuildKindAndDir([switch]$xcopy = $false) {

    if ($xcopy) { 
        Write-Output "xcopy"
        Write-Output (Get-MSBuildDirXCopy)
        return
    }
134 135 136 137 138 139

    # MSBuild from an active VS command prompt.  
    if (${env:VSINSTALLDIR} -ne $null) {

        # This line deliberately avoids using -ErrorAction.  Inside a VS command prompt
        # an MSBuild command should always be available.
J
Fixup  
Jared Parsons 已提交
140 141 142 143 144 145 146
        $command = (Get-Command msbuild -ErrorAction SilentlyContinue)
        if ($command -ne $null) {
            $p = Split-Path -parent $command.Path
            Write-Output "vscmd"
            Write-Output $p
            return
        }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    }

    # Look for a valid VS installation
    try {
        $p = Get-VisualStudioDir
        $p = Join-Path $p "MSBuild\15.0\Bin"
        Write-Output "vsinstall"
        Write-Output $p
        return
    }
    catch { 
        # Failures are expected here when no VS installation is present on the 
        # machine.
    }

J
Jared Parsons 已提交
162 163 164 165 166 167 168 169 170 171 172 173
    Write-Output "xcopy"
    Write-Output (Get-MSBuildDirXCopy)
    return
}

# Locate the xcopy version of MSBuild
function Get-MSBuildDirXCopy() {
    $version = "0.1.2"
    $name = "RoslynTools.MSBuild"
    $p = Ensure-BasicTool $name $version
    $p = Join-Path $p "tools\msbuild"
    return $p
174 175
}

J
Jared Parsons 已提交
176 177
function Get-MSBuildDir([switch]$xcopy = $false) {
    $both = Get-MSBuildKindAndDir -xcopy:$xcopy
178 179 180 181 182 183
    return $both[1]
}

# Get the directory of the first Visual Studio which meets our minimal 
# requirements for the Roslyn repo
function Get-VisualStudioDir() {
J
Jared Parsons 已提交
184
    $vswhere = Join-Path (Ensure-BasicTool "vswhere" "1.0.50") "tools\vswhere.exe"
185 186 187 188 189 190 191 192 193 194
    $output = & $vswhere -requires Microsoft.Component.MSBuild -format json | Out-String
    if (-not $?) {
        throw "Could not locate a valid Visual Studio"
    }

    $j = ConvertFrom-Json $output
    $p = $j[0].installationPath
    return $p
}

J
Jared Parsons 已提交
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
# Clear out the NuGet package cache
function Clear-PackageCache() {
    $nuget = Ensure-NuGet
    Exec { & $nuget locals all -clear }
}

# Restore a single project
function Restore-Project([string]$fileName, [string]$nuget, [string]$msbuildDir) {
    $nugetConfig = Join-Path $repoDir "nuget.config"
    $filePath = Join-Path $repoDir $fileName
    Exec { & $nuget restore -verbosity quiet -configfile $nugetConfig -MSBuildPath $msbuildDir -Project2ProjectTimeOut 1200 $filePath }
}

# Restore all of the projects that the repo consumes
function Restore-Packages([switch]$clean = $false, [string]$msbuildDir = "", [string]$project = "") {
    $nuget = Ensure-NuGet
    if ($msbuildDir -eq "") {
        $msbuildDir = Get-MSBuildDir
    }

    Write-Host "Restore using MSBuild at $msbuildDir"

    if ($clean) {
        Write-Host "Deleting project.lock.json files"
        Get-ChildItem $repoDir -re -in project.lock.json | Remove-Item
    }

    if ($project -ne "") {
        Write-Host "Restoring project $project"
        Restore-Project -fileName $project -nuget $nuget -msbuildDir $msbuildDir
    }
    else {
        $all = @(
            "Toolsets:build\ToolsetPackages\project.json",
            "Toolsets (Dev14 VS SDK build tools):build\ToolsetPackages\dev14.project.json",
            "Toolsets (Dev15 VS SDK RC build tools):build\ToolsetPackages\dev15rc.project.json",
            "Samples:src\Samples\Samples.sln",
            "Templates:src\Setup\Templates\Templates.sln",
            "Toolsets Compiler:build\Toolset\Toolset.csproj",
            "Roslyn:Roslyn.sln",
            "DevDivInsertionFiles:src\Setup\DevDivInsertionFiles\DevDivInsertionFiles.sln",
            "DevDiv Roslyn Packages:src\Setup\DevDivPackages\Roslyn\project.json",
            "DevDiv Debugger Packages:src\Setup\DevDivPackages\Debugger\project.json")

        foreach ($cur in $all) {
            $both = $cur.Split(':')
            Write-Host "Restoring $($both[0])"
            Restore-Project -fileName $both[1] -nuget $nuget -msbuildDir $msbuildDir
        }
    }
}

# Restore all of the projects that the repo consumes
function Restore-All([switch]$clean = $false, [string]$msbuildDir = "") {
    Restore-Packages -clean:$clean -msbuildDir $msbuildDir
}
J
Jared Parsons 已提交
251