1+ # Copyright (c) Microsoft Corporation.
2+ # Licensed under the MIT License.
3+
4+ <#
5+ . SYNOPSIS
6+ Generate the version info for the DSC project.
7+
8+ . DESCRIPTION
9+ This script inspects the git tags for the project and uses them to construct data representing
10+ those versions, saving it to `.versions.json` in the same directory as this script.
11+
12+ The data file contains every non-prerelease version tag as well as the latest major, minor, and
13+ patch version releases.
14+
15+ The versions are saved as:
16+
17+ - `V<Major>`, like `V3`, for every major version number.
18+ - `V<Major>_<Minor>`, like `V3_1`, for every minor version number.
19+ - `V<Major>_<Minor>_<Patch>`, like `V3_1_0`, for every non-prerelease version number.
20+
21+ This data is used by `build.rs` to generate the contents for the `RecognizedSchemaVersion`
22+ enum type definition and trait implementations.
23+ #>
24+
25+ [CmdletBinding ()]
26+ param ()
27+
28+ begin {
29+ function Get-DscProjectTagVersion {
30+ [cmdletbinding ()]
31+ [OutputType ([semver ])]
32+ param ()
33+
34+ process {
35+ $null = git fetch -- all -- tags
36+ git tag - l
37+ | Where-Object - FilterScript {$_ -match ' ^v\d+(\.\d+){2}$' }
38+ | ForEach-Object - Process { [semver ]($_.Substring (1 )) }
39+ }
40+ }
41+
42+ function ConvertTo-EnumName {
43+ [CmdletBinding ()]
44+ [OutputType ([string ])]
45+ param (
46+ [Parameter (ValueFromPipeline )]
47+ [semver []]$Version ,
48+ [switch ]$Major ,
49+ [switch ]$Minor
50+
51+ )
52+
53+ process {
54+ foreach ($v in $Version ) {
55+ if ($Major ) {
56+ ' V{0}' -f $v.Major
57+ } elseif ($Minor ) {
58+ ' V{0}_{1}' -f $v.Major , $v.Minor
59+ } else {
60+ ' V{0}_{1}_{2}' -f $v.Major , $v.Minor , $v.Patch
61+ }
62+ }
63+ }
64+ }
65+
66+ function Export-DscProjectTagVersion {
67+ [cmdletbinding ()]
68+ param ()
69+
70+ process {
71+ $publishedVersions = Get-DscProjectTagVersion
72+ | Sort-Object - Descending
73+
74+ [System.Collections.Generic.HashSet [semver ]]$majorVersions = @ ()
75+ [System.Collections.Generic.HashSet [semver ]]$minorVersions = @ ()
76+ [System.Collections.Generic.HashSet [semver ]]$patchVersions = @ ()
77+
78+ foreach ($version in $publishedVersions ) {
79+ $null = $majorVersions.Add ([semver ]" $ ( $version.Major ) " )
80+ $null = $minorVersions.Add ([semver ]" $ ( $version.Major ) .$ ( $version.Minor ) " )
81+ $null = $patchVersions.Add ($version )
82+ }
83+
84+ # Sort the versions with major version, then each child minor version and child patch versions.
85+ [System.Collections.Generic.HashSet [string ]]$allVersions = @ ()
86+ foreach ($major in ($majorVersions | Sort-Object - Descending)) {
87+ $null = $allVersions.Add (($major | ConvertTo-EnumName - Major))
88+
89+ $majorMinor = $minorVersions
90+ | Where-Object { $_.Major -eq $major.Major }
91+ | Sort-Object - Descending
92+
93+ foreach ($minor in $majorMinor ) {
94+ $null = $allVersions.Add (($minor | ConvertTo-EnumName - Minor))
95+
96+ $majorMinorPatch = $patchVersions
97+ | Where-Object { $_.Major -eq $minor.Major -and $_.Minor -eq $minor.Minor }
98+ | Sort-Object - Descending
99+
100+ foreach ($patch in $majorMinorPatch ) {
101+ $null = $allVersions.Add (($patch | ConvertTo-EnumName ))
102+ }
103+ }
104+ }
105+
106+ [string ]$latestMajorVersion = $majorVersions
107+ | Sort-Object - Descending
108+ | Select-Object - First 1
109+ | ConvertTo-EnumName - Major
110+ [string ]$latestMinorVersion = $minorVersions
111+ | Sort-Object - Descending
112+ | Select-Object - First 1
113+ | ConvertTo-EnumName - Minor
114+ [string ]$latestPatchVersion = $patchVersions
115+ | Sort-Object - Descending
116+ | Select-Object - First 1
117+ | ConvertTo-EnumName
118+
119+ $data = [ordered ]@ {
120+ latestMajor = $latestMajorVersion
121+ latestMinor = $latestMinorVersion
122+ latestPatch = $latestPatchVersion
123+ all = $allVersions
124+ }
125+
126+ $dataJson = $data
127+ | ConvertTo-Json
128+ | ForEach-Object - Process { $_ -replace " `r`n " , " `n " }
129+
130+ $dataPath = Join-Path - Path $PSScriptRoot - ChildPath ' .versions.json'
131+ $dataContent = Get-Content - Raw - Path $dataPath
132+
133+ if ($dataJson.Trim () -ne $dataContent.Trim ()) {
134+ $dataJson | Set-Content - Path $PSScriptRoot / .versions.json
135+ }
136+
137+ $dataJson
138+ }
139+ }
140+ }
141+
142+ process {
143+ Export-DscProjectTagVersion
144+ }
0 commit comments