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

refactor: port certificate-generation condition script to Rust #259

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions crates/wdk-build/rust-driver-makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,23 @@ args = [

[tasks.generate-certificate]
private = true
condition_script_runner_args = [
"--base-path",
"${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}",
]
condition_script = '''
#!@duckscript
#!@rust

out = exec certmgr.exe -put -s WDRTestCertStore -c -n WDRLocalTestCert ${WDK_BUILD_OUTPUT_DIRECTORY}/WDRLocalTestCert.cer
if eq ${out.code} 0
echo WDRLocalTestCert found in WDRTestCertStore. Skipping certificate generation.
exit 1
else
echo WDRLocalTestCert not found in WDRTestCertStore. Generating new certificate.
exit 0
end
//! ```cargo
//! [dependencies]
//! wdk-build = { path = ".", version = "0.3.0" }
//! anyhow = "1"
//! ```
#![allow(unused_doc_comments)]

fn main() -> anyhow::Result<()> {
wdk_build::cargo_make::generate_certificate_condition_script()
}
'''
command = "makecert"
args = [
Expand Down
46 changes: 46 additions & 0 deletions crates/wdk-build/src/cargo_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
env,
panic::UnwindSafe,
path::{Path, PathBuf},
process::Command,
};

use anyhow::Context;
Expand Down Expand Up @@ -1007,6 +1008,51 @@ pub fn package_driver_flow_condition_script() -> anyhow::Result<()> {
})
}

/// `cargo-make` condition script for `generate-certificate` task in
/// [`rust-driver-makefile.toml`](../rust-driver-makefile.toml)
///
/// # Errors
///
/// This functions returns an error whenever it determines that the
/// `generate-certificate` `cargo-make` task should be skipped. This only
/// occurs when the `WdrLocalTestCert` cannot be added from the build directory
/// to `WDRTestCertStore`.
///
/// # Panics
///
/// Panics if certmgr is unable to be run.
pub fn generate_certificate_condition_script() -> anyhow::Result<()> {
condition_script(|| {
let output = Command::new("certmgr")
.args([
"-put",
"-s",
"WDRTestCertStore",
"-c",
"-n",
"WdrLocalTestCert",
])
.arg(get_wdk_build_output_directory().join("WDRLocalTestCert.cer"))
.output()
.expect("Failed to run certmgr.exe.");
Copy link
Preview

Copilot AI Jan 10, 2025

Choose a reason for hiding this comment

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

The error message 'Failed to run certmgr.exe.' could be more descriptive. Consider providing more context about what went wrong.

Suggested change
.expect("Failed to run certmgr.exe.");
.expect("Failed to run certmgr.exe with the provided arguments.");

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

match output.status.code() {
Some(0) => Err(anyhow::anyhow!(
"WDRLocalTestCert already in WDRTestCertStore. Skipping certificate generation."
)),
Some(1) => Ok(()),
Some(_) => {
eprintln!("Unknown status code found from certmgr. Generating certificate.");
Ok(())
}
None => {
eprintln!("No status code found from certmgr. Generating certificate.");
Ok(())
}
}
})
}

fn configure_wdf_build_output_dir(target_arg: Option<&String>, cargo_make_cargo_profile: &str) {
let cargo_make_crate_custom_triple_target_directory =
env::var(CARGO_MAKE_CRATE_CUSTOM_TRIPLE_TARGET_DIRECTORY_ENV_VAR).unwrap_or_else(|_| {
Expand Down
Loading