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 7 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
63 changes: 63 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,68 @@ 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 `WdrLocalTestCert` already exists in `WDRTestCertStore`.
///
/// # Panics
///
/// Panics if `CARGO_MAKE_CURRENT_TASK_NAME` is not set in the environment.
pub fn generate_certificate_condition_script() -> anyhow::Result<()> {
condition_script(|| {
let cert_save_location = get_wdk_build_output_directory().join("WDRLocalTestCert.cer");

let mut command = Command::new("certmgr");

command
.args([
"-put",
"-s",
"WDRTestCertStore",
"-c",
"-n",
"WdrLocalTestCert",
])
.arg(cert_save_location);

let output = command.output().unwrap_or_else(|err| {
panic!(
"Failed to run certmgr.exe {} due to error: {}",
command
.get_args()
.map(|arg| arg.to_string_lossy())
.collect::<Vec<_>>()
.join(" "),
err
)
});

match output.status.code() {
Some(0) => Err(anyhow::anyhow!(
"WDRLocalTestCert found in WDRTestCertStore. Skipping certificate generation."
)),
Some(1) => {
println!(
"WDRLocalTestCert not found in WDRTestCertStore. Generating new certificate."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this print show up properly? I think I used eprintln for all the other prints for some reason to do with how cargo-make normally outputs things

);
Ok(())
}
Some(_) => {
eprintln!("Unknown status code found from certmgr. Generating new certificate.");
Ok(())
}
None => {
unreachable!("Unreachable, no status code found from certmgr.");
}
}
})
}

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