light_fw_cli_core/
build.rs1use std::path::{Path, PathBuf};
8
9use crate::error::{Error, Result};
10use crate::process::CommandRunner;
11
12#[derive(Debug, Clone, Default)]
14pub struct BuildOptions {
15 pub release: bool,
17 pub features: Vec<String>,
19 pub no_default_features: bool,
21 pub extra_cargo_args: Vec<String>,
23}
24
25impl BuildOptions {
26 pub fn default_release() -> Self {
28 Self {
29 release: true,
30 ..Self::default()
31 }
32 }
33}
34
35pub fn build_args(opts: &BuildOptions) -> Vec<String> {
37 let mut args = vec!["build".to_string()];
38 if opts.release {
39 args.push("--release".to_string());
40 }
41 if opts.no_default_features {
42 args.push("--no-default-features".to_string());
43 }
44 if !opts.features.is_empty() {
45 args.push("--features".to_string());
46 args.push(opts.features.join(","));
47 }
48 args.extend(opts.extra_cargo_args.iter().cloned());
49 args
50}
51
52pub fn run_build(
54 runner: &dyn CommandRunner,
55 cwd: &Path,
56 opts: &BuildOptions,
57) -> Result<crate::process::CommandOutput> {
58 let output = runner.run("cargo", &build_args(opts), Some(cwd))?;
59 if !output.success() {
60 return Err(Error::CommandFailed {
61 program: "cargo build".to_string(),
62 code: output.status,
63 stderr: output.stderr.trim().to_string(),
64 });
65 }
66 Ok(output)
67}
68
69pub fn locate_binary(target_dir: &Path, bin_name: &str, release: bool) -> Result<PathBuf> {
74 let profile = if release { "release" } else { "debug" };
75 let base = target_dir.join(profile).join(bin_name);
76 let candidates = [
77 format!("{}.exe", base.display()),
78 base.to_string_lossy().into_owned(),
79 ];
80 for candidate in &candidates {
81 let p = PathBuf::from(candidate);
82 if p.exists() {
83 return Ok(p);
84 }
85 }
86 Ok(candidates[0].clone().into())
89}
90
91pub fn format_size(bytes: u64) -> String {
93 if bytes < 1024 {
94 format!("{bytes} B")
95 } else if bytes < 1024 * 1024 {
96 format!("{:.1} KiB", bytes as f64 / 1024.0)
97 } else if bytes < 1024 * 1024 * 1024 {
98 format!("{:.1} MiB", bytes as f64 / (1024.0 * 1024.0))
99 } else {
100 format!("{:.1} GiB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn release_build_args() {
110 let opts = BuildOptions::default_release();
111 assert_eq!(build_args(&opts), vec!["build", "--release"]);
112 }
113
114 #[test]
115 fn debug_build_args() {
116 let opts = BuildOptions::default();
117 assert_eq!(build_args(&opts), vec!["build"]);
118 }
119
120 #[test]
121 fn build_args_with_features() {
122 let mut opts = BuildOptions::default_release();
123 opts.features = vec!["a".to_string(), "b".to_string()];
124 assert_eq!(
125 build_args(&opts),
126 vec!["build", "--release", "--features", "a,b"]
127 );
128 }
129
130 #[test]
131 fn build_args_no_default_features() {
132 let mut opts = BuildOptions::default_release();
133 opts.no_default_features = true;
134 assert_eq!(
135 build_args(&opts),
136 vec!["build", "--release", "--no-default-features"]
137 );
138 }
139
140 #[test]
141 fn build_args_with_extra() {
142 let mut opts = BuildOptions::default_release();
143 opts.extra_cargo_args = vec!["--jobs".to_string(), "4".to_string()];
144 assert_eq!(build_args(&opts), vec!["build", "--release", "--jobs", "4"]);
145 }
146
147 #[test]
148 fn format_size_bytes() {
149 assert_eq!(format_size(512), "512 B");
150 assert_eq!(format_size(1023), "1023 B");
151 }
152
153 #[test]
154 fn format_size_kib() {
155 assert_eq!(format_size(1024), "1.0 KiB");
156 assert_eq!(format_size(1536), "1.5 KiB");
157 }
158
159 #[test]
160 fn format_size_mib() {
161 assert_eq!(format_size(1024 * 1024), "1.0 MiB");
162 assert_eq!(format_size(2 * 1024 * 1024 + 512 * 1024), "2.5 MiB");
163 }
164
165 #[test]
166 fn format_size_gib() {
167 assert_eq!(format_size(1024 * 1024 * 1024), "1.0 GiB");
168 }
169
170 #[test]
171 fn locate_binary_existent() {
172 let dir = std::env::temp_dir().join(format!("light-build-test-{}", std::process::id()));
173 let release_dir = dir.join("release");
174 if let Err(e) = std::fs::create_dir_all(&release_dir) {
175 eprintln!("skip: cannot create temp dir: {e}");
176 return;
177 }
178 let bin = release_dir.join("myapp.exe");
179 std::fs::write(&bin, b"fake").unwrap();
180 let found = locate_binary(&dir, "myapp", true).unwrap();
181 assert_eq!(found, bin);
182 let _ = std::fs::remove_dir_all(&dir);
183 }
184
185 #[test]
186 fn locate_binary_nonexistent_returns_expected_path() {
187 let dir = std::env::temp_dir().join("light-build-missing");
188 let expected = dir.join("release").join("nothere.exe");
189 let found = locate_binary(&dir, "nothere", true).unwrap();
190 assert_eq!(found, expected);
191 }
192}