Scripts/Set-BuildVariable.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
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
<#
.SYNOPSIS
    Normalize build system and project details into variables

.FUNCTIONALITY
    CI/CD

.DESCRIPTION
    Normalize build system and project details into variables

    Creates the following variables:
        BHProjectPath via Get-BuildVariables
        BHBranchName via Get-BuildVariables
        BHCommitMessage via Get-BuildVariables
        BHBuildNumber via Get-BuildVariables
        BHProjectName via Get-ProjectName
        BHPSModuleManifest via Get-PSModuleManifest
        BHPSModulePath via Split-Path on BHPSModuleManifest

.PARAMETER Path
    Path to project root. Defaults to the current working path

.PARAMETER Scope
    Determines the scope of the variables. Valid values are "Global", "Local", or "Script", or a number
    relative to the current scope (0 through the number of scopes, where 0 is the current scope and 1 is its
    parent). For more information, see about_Scopes.

    Defaults to the calling scope, 0 if it is dot-sourced, 1 if it is invoked normally.

    The scope value Script may only be used with dot-sourced Set-BuildVariable.

.NOTES
    We assume you are in the project root, for several of the fallback options

.EXAMPLE
    Set-BuildVariable
    Get-Variable BH* -Scope 0

    # Set build variables in the current scope, read them

.EXAMPLE
    . Set-BuildVariable -Scope Script
    Get-Variable BH* -Scope Script

    # Set build variables in the script scope (mind the .), read them

.LINK
    https://github.com/RamblingCookieMonster/BuildHelpers

.LINK
    Get-BuildVariables

.LINK
    Get-ProjectName

.LINK
    about_BuildHelpers
#>

[cmdletbinding()]
param(
    $Path = $PWD.Path,

    [validatescript({
        if(-not ('Global', 'Local', 'Script', 'Current' -contains $_ -or (($_ -as [int]) -ge 0)))
        {
            throw "'$_' is an invalid Scope. For more information, run Get-Help Set-BuildVariable -Parameter Scope"
        }
        $true
    })]
    [string]
    $Scope,

    [ValidatePattern('\w*')]
    [String]
    $VariableNamePrefix = 'BH'
)

if($MyInvocation.InvocationName -eq '.')
{
    if(-not $Scope)
    {
        $Scope = '0'
    }
}
else
{
    if($Scope -eq 'Script')
    {
        throw 'The script scope may only be used with dot-sourced Set-BuildVariable.'
    }
    if(-not $Scope)
    {
        $Scope = '1'
    }
}

${Build.Vars} = Get-BuildVariables -Path $Path
${Build.ProjectName} = Get-ProjectName -Path $Path
${Build.ManifestPath} = Get-PSModuleManifest -Path $Path
$BuildHelpersVariables = @{
    BuildSystem = ${Build.Vars}.BuildSystem
    ProjectPath = ${Build.Vars}.ProjectPath
    BranchName  = ${Build.Vars}.BranchName
    CommitMessage = ${Build.Vars}.CommitMessage
    BuildNumber = ${Build.Vars}.BuildNumber
    ProjectName = ${Build.ProjectName}
    PSModuleManifest = ${Build.ManifestPath}
    PSModulePath = $(Split-Path -Path ${Build.ManifestPath} -Parent)
}
foreach ($VarName in $BuildHelpersVariables.Keys) {
    Set-Variable -Scope $Scope -Name ('{0}{1}' -f $VariableNamePrefix,$VarName) -Value $BuildHelpersVariables[$VarName]
}