|
| 1 | +use std::fs; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +use handlebars::Handlebars; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use tempfile::TempDir; |
| 7 | + |
| 8 | +use crate::builder::{BenchmarkPackage, GoBenchmark}; |
| 9 | +use crate::utils; |
| 10 | +use crate::{builder::patcher, prelude::*}; |
| 11 | + |
| 12 | +#[derive(Debug, Serialize, Deserialize)] |
| 13 | +struct TemplateData { |
| 14 | + benchmarks: Vec<GoBenchmark>, |
| 15 | + module_name: String, |
| 16 | +} |
| 17 | + |
| 18 | +pub fn run(package: &BenchmarkPackage) -> anyhow::Result<(TempDir, PathBuf)> { |
| 19 | + // 1. Copy the whole module to a build directory |
| 20 | + let target_dir = TempDir::new()?; |
| 21 | + std::fs::create_dir_all(&target_dir).context("Failed to create target directory")?; |
| 22 | + utils::copy_dir_recursively(&package.module.dir, &target_dir)?; |
| 23 | + |
| 24 | + // Get files that need to be renamed first |
| 25 | + let files = package |
| 26 | + .test_go_files |
| 27 | + .as_ref() |
| 28 | + .ok_or_else(|| anyhow::anyhow!("No test files found for package: {}", package.name))?; |
| 29 | + |
| 30 | + // Calculate the relative path from module root to package directory |
| 31 | + let package_dir = Path::new(&package.dir); |
| 32 | + let module_dir = Path::new(&package.module.dir); |
| 33 | + let relative_package_path = package_dir.strip_prefix(module_dir).context(format!( |
| 34 | + "Package dir {:?} is not within module dir {:?}", |
| 35 | + package.dir, package.module.dir |
| 36 | + ))?; |
| 37 | + debug!("Relative package path: {relative_package_path:?}"); |
| 38 | + |
| 39 | + // 2. Find benchmark files and patch their imports |
| 40 | + let files_to_patch = package |
| 41 | + .benchmarks |
| 42 | + .iter() |
| 43 | + .map(|bench| target_dir.path().join(&bench.file_path)) |
| 44 | + .collect::<Vec<_>>(); |
| 45 | + patcher::patch_imports(&target_dir, files_to_patch)?; |
| 46 | + |
| 47 | + // 3. Rename the _test.go files to _codspeed.go |
| 48 | + for file in files { |
| 49 | + let old_path = target_dir.path().join(relative_package_path).join(file); |
| 50 | + let new_path = old_path.with_file_name( |
| 51 | + old_path |
| 52 | + .file_name() |
| 53 | + .unwrap() |
| 54 | + .to_string_lossy() |
| 55 | + .replace("_test", "_codspeed"), |
| 56 | + ); |
| 57 | + |
| 58 | + fs::rename(&old_path, &new_path) |
| 59 | + .context(format!("Failed to rename {old_path:?} to {new_path:?}"))?; |
| 60 | + } |
| 61 | + |
| 62 | + // 4. Generate the codspeed/runner.go file using the template |
| 63 | + let mut handlebars = Handlebars::new(); |
| 64 | + let template_content = include_str!("template.go"); |
| 65 | + handlebars.register_template_string("main", template_content)?; |
| 66 | + |
| 67 | + // import <alias> <mod_path> |
| 68 | + // { "<name>", <qualified_path> }, |
| 69 | + let data = TemplateData { |
| 70 | + benchmarks: package.benchmarks.clone(), |
| 71 | + module_name: "codspeed_runner".into(), |
| 72 | + }; |
| 73 | + let rendered = handlebars.render("main", &data)?; |
| 74 | + |
| 75 | + let runner_path = target_dir |
| 76 | + .path() |
| 77 | + .join(relative_package_path) |
| 78 | + .join("codspeed/runner.go"); |
| 79 | + fs::create_dir_all( |
| 80 | + target_dir |
| 81 | + .path() |
| 82 | + .join(relative_package_path) |
| 83 | + .join("codspeed"), |
| 84 | + ) |
| 85 | + .context("Failed to create codspeed directory")?; |
| 86 | + fs::write(&runner_path, rendered).context("Failed to write runner.go file")?; |
| 87 | + |
| 88 | + Ok((target_dir, runner_path)) |
| 89 | +} |
0 commit comments