Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to update WDK version #848

Closed
wants to merge 3 commits into from
Closed

Script to update WDK version #848

wants to merge 3 commits into from

Conversation

Alan-Jowett
Copy link
Owner

@Alan-Jowett Alan-Jowett commented Dec 9, 2024

Description

Testing

Do any existing tests cover this change? Are new tests needed?

Documentation

Is there any documentation impact for this change?

Installation

Is there any installer impact for this change?

Summary by CodeRabbit

  • New Features

    • Introduced enhanced functionality in a PowerShell script for automating the update of the Windows Driver Kit (WDK) version in Visual Studio project files.
    • Added a new XML configuration file defining package dependencies for a Windows development environment.
  • Improvements

    • Enhanced error handling in the PowerShell script for better robustness during version updates.
    • Updated project files to utilize a centralized WDK version property, improving maintainability and simplifying version management.

Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Copy link

coderabbitai bot commented Dec 9, 2024

Walkthrough

The pull request introduces enhancements to the PowerShell script Update-WdkVersion.ps1, which automates the updating of the Windows Driver Kit (WDK) version in Visual Studio project files and a configuration file. It adds a new function for updating template files and improves error handling in existing functions. Additionally, a new XML configuration file, packages.config.template, is created to define package dependencies for the WDK and Windows SDK. Changes to Visual Studio project files replace hardcoded WDK version numbers with a dynamic variable, improving maintainability.

Changes

File Path Change Summary
scripts/Update-WdkVersion.ps1 Enhanced with new function Update-TemplateFile, improved error handling in Get-PackageVersion and Update-VersionInVsFile.
scripts/setup_build/packages.config.template New file added containing package declarations for WDK and Windows SDK dependencies.
tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj Added <WDKVersion> property and updated multiple <Import> statements to use $(WDKVersion).
tools/bpf2c/templates/user_mode_bpf2c.vcxproj Added <WDKVersion> property and updated multiple <Import> statements to use $(WDKVersion).

Possibly related PRs

🐇 In the code we hop and play,
Updating WDK in a clever way.
With scripts and templates, all in line,
Our projects will shine, oh so fine!
Version numbers now dance and sway,
Thanks to the changes made today! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (1)
scripts/Update-WdkVersion.ps1 (1)

30-35: Consider using configuration file for file paths

The hardcoded file paths make the script less maintainable. Consider moving them to a configuration file.

+# Read paths from config file
+$config_path = Join-Path $PSScriptRoot "update_wdk_config.json"
+$config = Get-Content $config_path | ConvertFrom-Json
+$vs_files_to_update = $config.files_to_update
-$vs_files_to_update = @(
-    "wdk.props",
-    "tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj",
-    "tools\bpf2c\templates\user_mode_bpf2c.vcxproj"
-)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4290611 and 48d253b.

📒 Files selected for processing (4)
  • scripts/Update-WdkVersion.ps1 (1 hunks)
  • scripts/setup_build/packages.config.template (1 hunks)
  • tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj (2 hunks)
  • tools/bpf2c/templates/user_mode_bpf2c.vcxproj (2 hunks)
✅ Files skipped from review due to trivial changes (2)
  • scripts/setup_build/packages.config.template
  • tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj
🔇 Additional comments (1)
tools/bpf2c/templates/user_mode_bpf2c.vcxproj (1)

51-55: LGTM: Import statements updated correctly

The import statements have been correctly updated to use the WDKVersion variable.

scripts/Update-WdkVersion.ps1 Outdated Show resolved Hide resolved
scripts/Update-WdkVersion.ps1 Outdated Show resolved Hide resolved
scripts/Update-WdkVersion.ps1 Outdated Show resolved Hide resolved
scripts/Update-WdkVersion.ps1 Outdated Show resolved Hide resolved
tools/bpf2c/templates/user_mode_bpf2c.vcxproj Show resolved Hide resolved
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
scripts/Update-WdkVersion.ps1 (1)

1-111: Consider adding advanced features for better maintainability and user experience

The script could benefit from the following architectural improvements:

  1. Add a logging mechanism for better troubleshooting
  2. Implement a dry-run option to preview changes
  3. Add version comparison to skip unnecessary updates
  4. Add a progress bar for multiple file updates
  5. Consider adding a configuration file for file paths and patterns

These improvements would make the script more maintainable and user-friendly.

Would you like me to provide implementation details for any of these improvements?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 48d253b and c93ecc9.

📒 Files selected for processing (1)
  • scripts/Update-WdkVersion.ps1 (1 hunks)
🔇 Additional comments (4)
scripts/Update-WdkVersion.ps1 (4)

1-3: LGTM: License header is appropriate

The MIT license header with copyright notice is correctly formatted.


4-33: LGTM: Robust implementation of package version retrieval

The implementation includes comprehensive error handling and validation as previously suggested.


35-67: Consider including original error message in the throw statement

While the implementation is robust and follows best practices, the error message could be more informative.

-        throw "Failed to update version in file: $vs_file_path"
+        throw "Failed to update version in file $vs_file_path`: $_"

88-111: ⚠️ Potential issue

Add overall error handling and rollback mechanism

The main execution flow needs error handling to ensure all files are either updated successfully or rolled back to their original state.

+# Track updated files for rollback
+$updated_files = @()
+
 # Paths relative to the root of the repository
 $vs_files_to_update = @(
     "wdk.props",
     "tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj",
     "tools\bpf2c\templates\user_mode_bpf2c.vcxproj"
 )

-# Get the current WDK version
-$wdk_version_number = Get-PackageVersion "Microsoft.Windows.WDK.x64"
+try {
+    # Verify all paths exist before starting
+    foreach ($vs_file in $vs_files_to_update) {
+        $full_path = Join-Path $PSScriptRoot "..\$vs_file"
+        if (-not (Test-Path $full_path)) {
+            throw "Required file not found: $vs_file"
+        }
+    }
 
-# Print the version number
-Write-Output "Found WDK version: $wdk_version_number"
+    # Get the current WDK version
+    $wdk_version_number = Get-PackageVersion "Microsoft.Windows.WDK.x64"
+    Write-Output "Found WDK version: $wdk_version_number"
 
-# Replace version in each VS file
-foreach ($vs_file in $vs_files_to_update) {
-    Write-Host "Updating WDK version in $vs_file"
-    $vs_file = $PSScriptRoot + "\..\" + $vs_file
-    Update-VersionInVsFile $vs_file $wdk_version_number
-}
+    # Replace version in each VS file
+    foreach ($vs_file in $vs_files_to_update) {
+        Write-Host "Updating WDK version in $vs_file"
+        $full_path = Join-Path $PSScriptRoot "..\$vs_file"
+        Update-VersionInVsFile $full_path $wdk_version_number
+        $updated_files += $full_path
+    }
 
-Update-TemplateFile -template_file_path "$PSScriptRoot\..\scripts\setup_build\packages.config.template" -output_file_path "$PSScriptRoot\..\scripts\setup_build\packages.config" -version_number $wdk_version_number
+    $template_path = Join-Path $PSScriptRoot "..\scripts\setup_build\packages.config.template"
+    $output_path = Join-Path $PSScriptRoot "..\scripts\setup_build\packages.config"
+    Update-TemplateFile -template_file_path $template_path -output_file_path $output_path -version_number $wdk_version_number
+    $updated_files += $output_path
 
-# Print success message
-Write-Output "Updated WDK version in all files"
+    # Print success message
+    Write-Output "Updated WDK version in all files"
+} catch {
+    Write-Error "Failed to update WDK version: $_"
+    # Attempt to restore backups
+    foreach ($file in $updated_files) {
+        $backup_path = "$file.bak"
+        if (Test-Path $backup_path) {
+            Copy-Item $backup_path $file -Force
+            Remove-Item $backup_path
+            Write-Warning "Rolled back changes to $file"
+        }
+    }
+    exit 1
+}

Likely invalid or redundant comment.

scripts/Update-WdkVersion.ps1 Outdated Show resolved Hide resolved
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
scripts/Update-WdkVersion.ps1 (3)

24-27: Consider using System.Version for version comparison

While the regex validation is good, using System.Version would provide more robust version comparison capabilities.

-if (-not ($packageVersion -match '^\d+\.\d+\.\d+\.\d+$')) {
+[System.Version]$parsedVersion = $null
+if (-not [System.Version]::TryParse($packageVersion, [ref]$parsedVersion)) {
     throw "Invalid version format: $packageVersion"
 }

113-117: Verify file paths before starting updates

Add validation for all file paths before starting any updates to fail fast.

 $vs_files_to_update = @(
     "wdk.props",
     "tools\bpf2c\templates\kernel_mode_bpf2c.vcxproj",
     "tools\bpf2c\templates\user_mode_bpf2c.vcxproj"
 )
+# Verify all paths exist before starting updates
+foreach ($vs_file in $vs_files_to_update) {
+    $full_path = Join-Path $PSScriptRoot "..\$vs_file"
+    if (-not (Test-Path $full_path)) {
+        throw "Required file not found: $vs_file"
+    }
+}

142-145: Add logging of rollback operations

Enhance rollback logging to include success/failure status.

-Write-Host "Rolling back changes in $file"
+Write-Host "Rolling back changes in $file..."
 Copy-Item "$file.bak" $file -Force
 Remove-Item "$file.bak"
+Write-Host "Successfully rolled back changes in $file"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c93ecc9 and 0af4449.

📒 Files selected for processing (1)
  • scripts/Update-WdkVersion.ps1 (1 hunks)
🔇 Additional comments (4)
scripts/Update-WdkVersion.ps1 (4)

1-2: LGTM: License header is properly formatted

The copyright notice and SPDX license identifier are correctly specified.


1-154: Overall implementation is robust and production-ready

The script demonstrates good practices with:

  • Comprehensive error handling
  • Backup and rollback mechanisms
  • Input validation
  • Clear logging

The suggested improvements are minor enhancements to an already solid implementation.


95-96: 🛠️ Refactor suggestion

Add verification after template replacement

The template replacement should be verified before writing back to the file.

 $template_file_content = $template_file_content -replace "\$\(WDKVersion\)", $version_number
+if ($template_file_content -notmatch [regex]::Escape($version_number)) {
+    throw "Failed to update version in template. Pattern not found."
+}

Likely invalid or redundant comment.


55-56: 🛠️ Refactor suggestion

Add verification after version replacement

The version replacement should be verified before writing back to the file.

 $vs_file_content = $vs_file_content -replace "<WDKVersion>.*</WDKVersion>", "<WDKVersion>$version_number</WDKVersion>"
+if ($vs_file_content -notmatch [regex]::Escape("<WDKVersion>$version_number</WDKVersion>")) {
+    throw "Failed to update version in file. Pattern not found."
+}

Likely invalid or redundant comment.

@Alan-Jowett Alan-Jowett closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant