|
1 | 1 | //! Patches the imports to use codspeed rather than the official "testing" package. |
2 | 2 |
|
3 | 3 | use crate::prelude::*; |
| 4 | +use rayon::prelude::*; |
4 | 5 | use std::fs; |
5 | 6 | use std::path::Path; |
6 | 7 | use std::process::Command; |
| 8 | +use std::sync::atomic::AtomicU64; |
7 | 9 |
|
8 | 10 | pub fn replace_pkg<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> { |
9 | 11 | let codspeed_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); |
@@ -34,28 +36,37 @@ pub fn patch_imports<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> { |
34 | 36 | debug!("Patching imports in folder: {folder:?}"); |
35 | 37 |
|
36 | 38 | // 1. Find all imports that match "testing" and replace them with codspeed equivalent |
37 | | - let mut patched_files = 0; |
38 | | - |
| 39 | + let patched_files = AtomicU64::new(0); |
39 | 40 | let pattern = folder.join("**/*.go"); |
40 | | - for go_file in glob::glob(pattern.to_str().unwrap())?.filter_map(Result::ok) { |
41 | | - // Skip directories - glob can match directories ending in .go (e.g., vendor/github.com/nats-io/nats.go) |
42 | | - if !go_file.is_file() { |
43 | | - continue; |
44 | | - } |
45 | | - |
46 | | - let content = |
47 | | - fs::read_to_string(&go_file).context(format!("Failed to read Go file: {go_file:?}"))?; |
48 | | - |
49 | | - let patched_content = patch_imports_for_source(&content); |
50 | | - if patched_content != content { |
51 | | - fs::write(&go_file, patched_content) |
52 | | - .context(format!("Failed to write patched Go file: {go_file:?}"))?; |
53 | | - |
54 | | - debug!("Patched imports in: {go_file:?}"); |
55 | | - patched_files += 1; |
56 | | - } |
57 | | - } |
58 | | - debug!("Patched {patched_files} files"); |
| 41 | + glob::glob(pattern.to_str().unwrap())? |
| 42 | + .filter_map(Result::ok) |
| 43 | + .par_bridge() |
| 44 | + .for_each(|go_file| { |
| 45 | + // Skip directories - glob can match directories ending in .go (e.g., vendor/github.com/nats-io/nats.go) |
| 46 | + if !go_file.is_file() { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + let Ok(content) = fs::read_to_string(&go_file) else { |
| 51 | + error!("Failed to read Go file: {go_file:?}"); |
| 52 | + return; |
| 53 | + }; |
| 54 | + |
| 55 | + let patched_content = patch_imports_for_source(&content); |
| 56 | + if patched_content != content { |
| 57 | + let Ok(_) = fs::write(&go_file, patched_content) else { |
| 58 | + error!("Failed to write patched Go file: {go_file:?}"); |
| 59 | + return; |
| 60 | + }; |
| 61 | + |
| 62 | + debug!("Patched imports in: {go_file:?}"); |
| 63 | + patched_files.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 64 | + } |
| 65 | + }); |
| 66 | + debug!( |
| 67 | + "Patched {} files", |
| 68 | + patched_files.load(std::sync::atomic::Ordering::SeqCst) |
| 69 | + ); |
59 | 70 |
|
60 | 71 | Ok(()) |
61 | 72 | } |
|
0 commit comments