Skip to content

Commit f5668ce

Browse files
(GH-538) Move DscRepoSchema into dsc-lib-jsonschema
Prior to this change, types in `dsc-lib` depended on the`DscRepoSchema` trait defined in the same crate for generating the set of recognized schema URIs and validating them. This change migrates this JSON Schema specific code into the `dsc-lib-jsonschema` crate for better organization and reuse, since this is actually required for any schema published from the DSC repository, even if those crates don't depend on `dsc-lib`. This migration also takes advantage of some improvements in code organization for compilation and testing, migrating the separate related types, like `SchemaForm` and `SchemaUriPrefix`, into their own private modules with the parent module re-exporting those types. Finally, this migration also incorporates a build script, which checks the git tags for the project to generate the variants for the `RecognizedSchemaVersion` enum. Prior to this implementation, the developers were required to manually update the enum definition prior to every release. This change does _not_ update any other crates to use the new implementation. A separate, future change will modify the other crates.
1 parent ccf3628 commit f5668ce

File tree

15 files changed

+1320
-1
lines changed

15 files changed

+1320
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
doc-valid-idents = ["IntelliSense", ".."]
1+
doc-valid-idents = ["IntelliSense", "PowerShell", ".."]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"latestMajor": "V3",
3+
"latestMinor": "V3_1",
4+
"latestPatch": "V3_1_2",
5+
"all": [
6+
"V3",
7+
"V3_1",
8+
"V3_1_2",
9+
"V3_1_1",
10+
"V3_1_0",
11+
"V3_0",
12+
"V3_0_2",
13+
"V3_0_1",
14+
"V3_0_0"
15+
]
16+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

lib/dsc-lib-jsonschema/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rust-i18n = { workspace = true }
1313
schemars = { workspace = true }
1414
serde = { workspace = true }
1515
serde_json = { workspace = true }
16+
thiserror = { workspace = true }
1617
tracing = { workspace = true }
1718
url = { workspace = true }
1819
urlencoding = { workspace = true }
@@ -21,5 +22,9 @@ urlencoding = { workspace = true }
2122
# Helps review complex comparisons, like schemas
2223
pretty_assertions = { workspace = true }
2324

25+
[build-dependencies]
26+
serde = { workspace = true }
27+
serde_json = { workspace = true }
28+
2429
[lints.clippy]
2530
pedantic = { level = "deny" }

0 commit comments

Comments
 (0)