summaryrefslogtreecommitdiff
path: root/scripts/build.ps1
blob: 91d803d7b5bbd780178ee46629a1ef19f4204fe6 (plain)
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
[CmdletBinding()]
param (
    [Parameter()]
    [switch]
    $SkipTests,

    [Parameter()]
    [switch]
    $Install,

    [Parameter()]
    [switch]
    $DebugBuild,

    [Parameter()]
    [ValidateSet('', 'readonly', 'vendor')]
    [string]
    $GoMod = 'vendor'
)

$script:PSDefaultParameterValues = @{
    '*:Confirm'           = $false
    '*:ErrorAction'       = 'Stop'
}

. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)

function clean() {
    Write-Host "Cleaning $BUILD_DIR"
    if (Test-Path -Path $BUILD_DIR) {
        Remove-Item -Recurse -Force -Path $BUILD_DIR
    }
    $null = New-Item -ItemType Container -Path $BUILD_DIR
}

function compile() {
    $NAME = Get-Content -Raw -Path $PROVIDER_NAME_FILE
    $VERSION = Get-Content -Raw -Path $PROVIDER_VERSION_FILE

    $BUILD_ARTIFACT="terraform-provider-${NAME}_v${VERSION}"
    if ($env:OS -like '*Windows*') {
        $BUILD_ARTIFACT += '.exe'
    }
    Write-Host "Attempting to build $BUILD_ARTIFACT"
    Push-Location -Path $SOURCE_DIR
    try {
        $env:GO111MODULE='on'

        go mod download
        if ($LASTEXITCODE) {
            throw "Failed to download modules"
        }

        $argv = @(
            'build',
            "-mod=$(if ('' -ne $GoMod) { $GoMod } else { $null })",
            '-o',
            "$BUILD_DIR/$BUILD_ARTIFACT"
        )
        if ($DebugBuild) {
            $argv += @( '-gcflags="all=-N -l"' )
        }
        go @argv 
        if ($LASTEXITCODE) {
            throw "Build failed"
        }
    }
    finally {
        'GO111MODULE' `
        | ForEach-Object -Process {Remove-Item -Path "Env:$_" }    
        Pop-Location
    }
}

function clean_and_build() {
    clean
    compile
    if (-not $SkipTests) {
        & (Join-Path -Path $PSScriptRoot -ChildPath 'unittest.ps1' -Resolve) -GoMod $GoMod
    }
    Write-Host "Build finished successfully"
    if ($Install) {
        & (Join-Path -Path $PSScriptRoot -ChildPath 'local-install.ps1' -Resolve)
    }
}

clean_and_build