Skip to content
Merged
139 changes: 139 additions & 0 deletions .github/scripts/check-template-versions.ps1
Comment thread
gurry marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/usr/bin/env pwsh
# Copyright (c) Microsoft Corporation
# License: MIT OR Apache-2.0

# This script verifies that the cargo-wdk template Cargo.toml files use the correct
# versions of WDK crates by reading from each crate's [package] section.

$ErrorActionPreference = "Stop"

# Function to extract version from a Cargo.toml [package] section
function Get-PackageVersion {
param (
[string]$CargoTomlPath
)

if (-not (Test-Path $CargoTomlPath)) {
Write-Error "Cargo.toml not found at: $CargoTomlPath"
exit 1
}

$content = Get-Content $CargoTomlPath -Raw

# Match version in [package] section
if ($content -match '(?ms)\[package\].*?version\s*=\s*"([^"]+)"') {
return $Matches[1]
}

Write-Error "Could not find version in [package] section of $CargoTomlPath"
exit 1
}

# Function to extract dependency versions from a template file
function Get-TemplateDependencies {
param (
[string]$TemplatePath
)

if (-not (Test-Path $TemplatePath)) {
Write-Error "Template not found at: $TemplatePath"
exit 1
}

$content = Get-Content $TemplatePath -Raw
$dependencies = @{}

# Extract wdk-* dependencies and their versions
$pattern = '(wdk[a-z-]*)\s*=\s*"([^"]+)"'
$matches = [regex]::Matches($content, $pattern)

foreach ($match in $matches) {
$depName = $match.Groups[1].Value
$depVersion = $match.Groups[2].Value
$dependencies[$depName] = $depVersion
}

return $dependencies
}

Write-Host "Checking template versions against crate package versions..." -ForegroundColor Cyan

# Get actual crate versions from [package] sections
$wdkVersion = Get-PackageVersion "crates/wdk/Cargo.toml"
$wdkAllocVersion = Get-PackageVersion "crates/wdk-alloc/Cargo.toml"
$wdkBuildVersion = Get-PackageVersion "crates/wdk-build/Cargo.toml"
$wdkPanicVersion = Get-PackageVersion "crates/wdk-panic/Cargo.toml"
$wdkSysVersion = Get-PackageVersion "crates/wdk-sys/Cargo.toml"

Write-Host ""
Write-Host "Current crate versions:" -ForegroundColor Green
Write-Host " wdk: $wdkVersion"
Write-Host " wdk-alloc: $wdkAllocVersion"
Write-Host " wdk-build: $wdkBuildVersion"
Write-Host " wdk-panic: $wdkPanicVersion"
Write-Host " wdk-sys: $wdkSysVersion"
Write-Host ""

$expectedVersions = @{
"wdk" = $wdkVersion
"wdk-alloc" = $wdkAllocVersion
"wdk-build" = $wdkBuildVersion
"wdk-panic" = $wdkPanicVersion
"wdk-sys" = $wdkSysVersion
}

$templates = @(
@{
Name = "KMDF"
Path = "crates/cargo-wdk/templates/kmdf/Cargo.toml.tmp"
ExpectedDeps = @("wdk", "wdk-alloc", "wdk-build", "wdk-panic", "wdk-sys")
},
@{
Name = "UMDF"
Path = "crates/cargo-wdk/templates/umdf/Cargo.toml.tmp"
ExpectedDeps = @("wdk", "wdk-build", "wdk-sys")
},
@{
Name = "WDM"
Path = "crates/cargo-wdk/templates/wdm/Cargo.toml.tmp"
ExpectedDeps = @("wdk", "wdk-alloc", "wdk-build", "wdk-panic", "wdk-sys")
}
)

$hasErrors = $false

foreach ($template in $templates) {
Write-Host "Checking $($template.Name) template..." -ForegroundColor Cyan

$templateDeps = Get-TemplateDependencies $template.Path

foreach ($depName in $template.ExpectedDeps) {
if (-not $templateDeps.ContainsKey($depName)) {
Write-Host " ERROR: $depName is missing from template" -ForegroundColor Red
$hasErrors = $true
continue
}

$templateVersion = $templateDeps[$depName]
$expectedVersion = $expectedVersions[$depName]

if ($templateVersion -ne $expectedVersion) {
Write-Host " ERROR: $depName version mismatch" -ForegroundColor Red
Write-Host " Template has: $templateVersion" -ForegroundColor Red
Write-Host " Expected: $expectedVersion" -ForegroundColor Red
$hasErrors = $true
} else {
Write-Host " OK: $depName = $templateVersion" -ForegroundColor Green
}
}

Write-Host ""
}

if ($hasErrors) {
Write-Host "Template version check FAILED!" -ForegroundColor Red
exit 1
} else {
Write-Host "All template versions are correct!" -ForegroundColor Green
exit 0
}
27 changes: 27 additions & 0 deletions .github/workflows/template-version-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Template Version Check
Comment thread
gurry marked this conversation as resolved.
Outdated

on:
push:
branches-ignore:
- 'gh-readonly-queue/**'
pull_request:
merge_group:
schedule: # Trigger a job on default branch at 4AM PST everyday
- cron: 0 11 * * *

concurrency:
group: ${{ github.workflow }}-${{ github.event.compare || github.head_ref || github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
check-template-versions:
name: Check Template Versions
runs-on: windows-2025

steps:
- name: Checkout Repository
uses: actions/checkout@v5

- name: Run Template Version Check
shell: pwsh
run: .github/scripts/check-template-versions.ps1
10 changes: 5 additions & 5 deletions crates/cargo-wdk/templates/kmdf/Cargo.toml.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ target-kmdf-version-minor = 33
crate-type = ["cdylib"]

[build-dependencies]
wdk-build = "0.3.0"
wdk-build = "0.5.0"

[dependencies]
wdk = "0.3.0"
wdk-alloc = "0.3.0"
wdk-panic = "0.3.0"
wdk-sys = "0.3.0"
wdk = "0.4.0"
wdk-alloc = "0.4.0"
wdk-panic = "0.4.0"
wdk-sys = "0.5.0"

[features]
default = []
Expand Down
6 changes: 3 additions & 3 deletions crates/cargo-wdk/templates/umdf/Cargo.toml.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ target-umdf-version-minor = 33
crate-type = ["cdylib"]

[build-dependencies]
wdk-build = "0.3.0"
wdk-build = "0.5.0"

[dependencies]
wdk = "0.3.0"
wdk-sys = "0.3.0"
wdk = "0.4.0"
wdk-sys = "0.5.0"

[features]
default = []
Expand Down
10 changes: 5 additions & 5 deletions crates/cargo-wdk/templates/wdm/Cargo.toml.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ driver-type = "WDM"
crate-type = ["cdylib"]

[build-dependencies]
wdk-build = "0.3.0"
wdk-build = "0.5.0"

[dependencies]
wdk = "0.3.0"
wdk-alloc = "0.3.0"
wdk-panic = "0.3.0"
wdk-sys = "0.3.0"
wdk = "0.4.0"
wdk-alloc = "0.4.0"
wdk-panic = "0.4.0"
wdk-sys = "0.5.0"

[features]
default = []
Expand Down
Loading