@@ -52,74 +52,85 @@ function Initialize-CommonBuildEnvironment
5252 [string ]$repoRoot ,
5353 [switch ]$FullInit
5454 )
55-
56- # Script code should ALWAYS use the global CurrentBuildKind
57- $currentBuildKind = Get-CurrentBuildKind
58-
59- # set/reset legacy environment vars for non-script tools (i.e. msbuild.exe)
60- $env: IsAutomatedBuild = $currentBuildKind -ne [BuildKind ]::LocalBuild
61- $env: IsPullRequestBuild = $currentBuildKind -eq [BuildKind ]::PullRequestBuild
62- $env: IsReleaseBuild = $currentBuildKind -eq [BuildKind ]::ReleaseBuild
63-
64- switch ($currentBuildKind )
55+ try
6556 {
66- ([BuildKind ]::LocalBuild) { $env: CiBuildName = ' ZZZ' }
67- ([BuildKind ]::PullRequestBuild) { $env: CiBuildName = ' PRQ' }
68- ([BuildKind ]::CiBuild) { $env: CiBuildName = ' BLD' }
69- ([BuildKind ]::ReleaseBuild) { $env: CiBuildName = ' ' }
70- default { throw " Invalid build kind" }
71- }
57+ # Script code should ALWAYS use the global CurrentBuildKind
58+ $currentBuildKind = Get-CurrentBuildKind
7259
73- # get the ISO-8601 formatted time stamp of the HEAD commit or the current UTC time for local builds
74- # for FullInit force a re-capture of the time stamp.
75- if (! $env: BuildTime -or $FullInit )
76- {
77- # for any automated builds use the ISO-8601 time stamp of the current commit
78- if ($currentBuildKind -ne [BuildKind ]::LocalBuild)
79- {
80- $env: BuildTime = (git show - s -- format=% cI)
81- }
82- else
60+ # set/reset legacy environment vars for non-script tools (i.e. msbuild.exe)
61+ $env: IsAutomatedBuild = $currentBuildKind -ne [BuildKind ]::LocalBuild
62+ $env: IsPullRequestBuild = $currentBuildKind -eq [BuildKind ]::PullRequestBuild
63+ $env: IsReleaseBuild = $currentBuildKind -eq [BuildKind ]::ReleaseBuild
64+
65+ switch ($currentBuildKind )
8366 {
84- $env: BuildTime = ([System.DateTime ]::UtcNow.ToString(" o" ))
67+ ([BuildKind ]::LocalBuild) { $env: CiBuildName = ' ZZZ' }
68+ ([BuildKind ]::PullRequestBuild) { $env: CiBuildName = ' PRQ' }
69+ ([BuildKind ]::CiBuild) { $env: CiBuildName = ' BLD' }
70+ ([BuildKind ]::ReleaseBuild) { $env: CiBuildName = ' ' }
71+ default { throw " Invalid build kind" }
8572 }
86- }
87-
88- # On Windows setup the equivalent of a Developer prompt.
89- #
90- # Other platform runners may have different defaulted paths etc...
91- # to account for here.
92- if ($IsWindows )
93- {
94- Write-Verbose " Configuring VS tools support"
9573
96- # For windows force a common VS tools prompt;
97- # Sadly most of this is undocumented and found from various sites
98- # spelunking the process. This isn't as bad as it might seem as the
99- # installer will create persistent use of this as a Windows Terminal
100- # "profile" and the actual command is exposed.
101- if ($null -eq (Find-OnPath vswhere))
74+ # get the ISO-8601 formatted time stamp of the HEAD commit or the current UTC time for local builds
75+ # for FullInit force a re-capture of the time stamp.
76+ if (! $env: BuildTime -or $FullInit )
10277 {
103- # NOTE: automated builds in Github do NOT include WinGet (for reasons unknown)
104- # However, they do contain VSWHERE so should not hit this.
105- winget install Microsoft.VisualStudio.Locator | Out-Null
78+ # for any automated builds use the ISO-8601 time stamp of the current commit
79+ if ($currentBuildKind -ne [BuildKind ]::LocalBuild)
80+ {
81+ $env: BuildTime = (git show - s -- format=% cI)
82+ }
83+ else
84+ {
85+ $env: BuildTime = ([System.DateTime ]::UtcNow.ToString(" o" ))
86+ }
10687 }
10788
108- $vsShellModulePath = vswhere - find ** \Microsoft.VisualStudio.DevShell.dll
109- $vsToolsArch = Get-VsArchitecture
110- if (! $vsShellModulePath )
89+ # On Windows setup the equivalent of a Developer prompt.
90+ #
91+ # Other platform runners may have different defaulted paths etc...
92+ # to account for here.
93+ if ($IsWindows )
11194 {
112- throw " VS shell module not found!"
95+ Write-Verbose " Configuring VS tools support"
96+
97+ # For windows force a common VS tools prompt;
98+ # Sadly most of this is undocumented and found from various sites
99+ # spelunking the process. This isn't as bad as it might seem as the
100+ # installer will create persistent use of this as a Windows Terminal
101+ # "profile" and the actual command is exposed.
102+ if ($null -eq (Find-OnPath vswhere))
103+ {
104+ # NOTE: automated builds in Github do NOT include WinGet (for reasons unknown)
105+ # However, they do contain VSWHERE so should not hit this.
106+ winget install Microsoft.VisualStudio.Locator | Out-Null
107+ }
108+
109+ $vsShellModulePath = vswhere - latest - find ** \Microsoft.VisualStudio.DevShell.dll
110+ $vsToolsArch = Get-VsArchitecture
111+ if (! $vsShellModulePath )
112+ {
113+ throw " VS shell module not found!"
114+ }
115+
116+ Import-Module $vsShellModulePath | Out-Null
117+ $vsInstanceId = vswhere - latest - format value - property InstanceId
118+ Enter-VsDevShell $vsInstanceId - SkipAutomaticLocation - DevCmdArguments " -arch=$vsToolsArch -host_arch=$vsToolsArch " | Out-Null
113119 }
114120
115- Import-Module $vsShellModulePath | Out-Null
116- $vsInstanceId = vswhere - format value - property InstanceId
117- Enter-VsDevShell $vsInstanceId - SkipAutomaticLocation - DevCmdArguments " -arch=$vsToolsArch -host_arch=$vsToolsArch " | Out-Null
121+ # Start with standard build paths then add additional values to the hashtable
122+ $buildInfo = Get-DefaultBuildPaths $repoRoot
123+ $buildInfo [' CurrentBuildKind' ] = $currentBuildKind
124+ $buildInfo [' VersionTag' ] = Get-BuildVersionTag $buildInfo
125+ return $buildInfo
126+ }
127+ catch
128+ {
129+ # everything from the official docs to the various articles in the blog-sphere says this isn't needed
130+ # and in fact it is redundant - They're all WRONG! By re-throwing the exception the original location
131+ # information is retained and the error reported will include the correct source file and line number
132+ # data for the error. Without this, only the error message is retained and the location information is
133+ # Line 1, Column 1, of the outer most script file, or the calling location neither of which is useful.
134+ throw
118135 }
119-
120- # Start with standard build paths then add additional values to the hashtable
121- $buildInfo = Get-DefaultBuildPaths $repoRoot
122- $buildInfo [' CurrentBuildKind' ] = $currentBuildKind
123- $buildInfo [' VersionTag' ] = Get-BuildVersionTag $buildInfo
124- return $buildInfo
125136}
0 commit comments