Functions/InvokeMSBuildSSDT.ps1

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


function Invoke-MsBuildSSDT {
           <#
.SYNOPSIS
Build a datbase project/solution using Microsoft.Data.Tools.MSBuild
.DESCRIPTION
Using MSBUild, the database project/solution is comiled using Microsoft.Data.Tools.MSBuild. This can be downloaded by using Install-MicrosoftDataToolsMSBuild
.PARAMETER DatabaseSolutionFilePath
Mandatory - Filepath to the sln/sqlproj file.
.PARAMETER DataToolsFilePath
Mandatory - Location of the Micorosft.Data.Tools.MSBuild. Required for SSDTPath and ExtensionsReferences.
.PARAMETER MSBuildVersionNumber
Optional - Specify if using version 14 or 15. Will default to 15 if left blank. Can be installed by using Install-VSBuildTools2017.
.INPUTS
N/A
.OUTPUTS
N/A
.EXAMPLE
$sqlproj = $PSSCriptRoot\solution.sln
$workingFolder = $PSScriptRoot
New-Item -ItemType Directory -Force -Path $WorkingFolder
$msBuildDataTools = Join-Path $WorkingFolder "\Microsoft.Data.Tools.Msbuild\lib\net46"
if ((Test-Path $msBuildDataTools) -eq $false) {
    Install-MicrosoftDataToolsMSBuild -WorkingFolder $workingFolder
}
if ((Test-Path $msBuildDataTools) -eq $false) {
    Write-Output "Oh! It looks like MSBuildDataTools did not download."
}
 
    Invoke-MsBuildSSDT -DatabaseSolutionFilePath $sqlproj -DataToolsFilePath $msBuildDataTools
    if ($LASTEXITCODE -ne 0){
        Throw
    }
 
.NOTES
  N/A
#>

    param ( [string] $DatabaseSolutionFilePath
        , [string] $DataToolsFilePath
        , [string]$MSBuildVersionNumber)

    if ([string]::IsNullOrEmpty($MSBuildVersionNumber)) {
        $MSBuildVersionNumber = "15.0"
    }
    if ($MSBuildVersionNumber -eq "15.0") {
        $filepath = "C:\Program Files (x86)\Microsoft Visual Studio\2017"
        $folders = Get-ChildItem $filepath
        $MsBuildInstalled = 0
        foreach ($folder in $folders) {
            $MsbuildPath = Join-Path $filepath "$folder\MSBuild\15.0\Bin"
            if ((Test-Path $MsbuildPath) -eq $true) {
                Write-Host "MsBuild found!" -ForegroundColor Green -BackgroundColor Yellow
                $MsBuild = Join-Path $MsbuildPath "msbuild.exe"
                $MsBuildInstalled = 1
                break
            }
        }
        if ($MsBuildInstalled -eq 0) {
            Write-Error "Install Visual Studio Tools 2017 MSBuild Tools to continue!"
            Throw
        }
    }
    else {
        $msbuild = "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
    }
    if (-not (Test-Path $msbuild)) {
        Write-Error "No MSBuild installed. Install Build Tools using 'Install-VsBuildTools2017', set -MSBuildVersionNumber to 15.0 and try again!"
        Throw
    }
    $arg1 = "/p:tv=$MSBuildVersionNumber"
    $arg2 = "/p:SSDTPath=$DataToolsFilePath"
    $arg3 = "/p:SQLDBExtensionsRefPath=$DataToolsFilePath"
    $arg4 = "/p:Configuration=Debug"

    Write-Host $msbuild $DatabaseSolutionFilePath $arg1 $arg2 $arg3 $arg4 -ForegroundColor White -BackgroundColor DarkGreen

    & $msbuild $DatabaseSolutionFilePath $arg1 $arg2 $arg3 $arg4 2>&1
}