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 label = if opts.release { "Сборка (cargo build --release)" } else { "Сборка (cargo build)" };
59 let output = runner.run_with_progress("cargo", &build_args(opts), Some(cwd), label)?;
60 if !output.success() {
61 return Err(Error::CommandFailed {
62 program: "cargo build".to_string(),
63 code: output.status,
64 stderr: output.stderr.trim().to_string(),
65 });
66 }
67 Ok(output)
68}
69
70pub fn locate_binary(target_dir: &Path, bin_name: &str, release: bool) -> Result<PathBuf> {
75 let profile = if release { "release" } else { "debug" };
76 let base = target_dir.join(profile).join(bin_name);
77 let candidates = [
78 format!("{}.exe", base.display()),
79 base.to_string_lossy().into_owned(),
80 ];
81 for candidate in &candidates {
82 let p = PathBuf::from(candidate);
83 if p.exists() {
84 return Ok(p);
85 }
86 }
87 Ok(candidates[0].clone().into())
90}
91
92pub fn format_size(bytes: u64) -> String {
94 if bytes < 1024 {
95 format!("{bytes} B")
96 } else if bytes < 1024 * 1024 {
97 format!("{:.1} KiB", bytes as f64 / 1024.0)
98 } else if bytes < 1024 * 1024 * 1024 {
99 format!("{:.1} MiB", bytes as f64 / (1024.0 * 1024.0))
100 } else {
101 format!("{:.1} GiB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn release_build_args() {
111 let opts = BuildOptions::default_release();
112 assert_eq!(build_args(&opts), vec!["build", "--release"]);
113 }
114
115 #[test]
116 fn debug_build_args() {
117 let opts = BuildOptions::default();
118 assert_eq!(build_args(&opts), vec!["build"]);
119 }
120
121 #[test]
122 fn build_args_with_features() {
123 let mut opts = BuildOptions::default_release();
124 opts.features = vec!["a".to_string(), "b".to_string()];
125 assert_eq!(
126 build_args(&opts),
127 vec!["build", "--release", "--features", "a,b"]
128 );
129 }
130
131 #[test]
132 fn build_args_no_default_features() {
133 let mut opts = BuildOptions::default_release();
134 opts.no_default_features = true;
135 assert_eq!(
136 build_args(&opts),
137 vec!["build", "--release", "--no-default-features"]
138 );
139 }
140
141 #[test]
142 fn build_args_with_extra() {
143 let mut opts = BuildOptions::default_release();
144 opts.extra_cargo_args = vec!["--jobs".to_string(), "4".to_string()];
145 assert_eq!(build_args(&opts), vec!["build", "--release", "--jobs", "4"]);
146 }
147
148 #[test]
149 fn format_size_bytes() {
150 assert_eq!(format_size(512), "512 B");
151 assert_eq!(format_size(1023), "1023 B");
152 }
153
154 #[test]
155 fn format_size_kib() {
156 assert_eq!(format_size(1024), "1.0 KiB");
157 assert_eq!(format_size(1536), "1.5 KiB");
158 }
159
160 #[test]
161 fn format_size_mib() {
162 assert_eq!(format_size(1024 * 1024), "1.0 MiB");
163 assert_eq!(format_size(2 * 1024 * 1024 + 512 * 1024), "2.5 MiB");
164 }
165
166 #[test]
167 fn format_size_gib() {
168 assert_eq!(format_size(1024 * 1024 * 1024), "1.0 GiB");
169 }
170
171 #[test]
172 fn locate_binary_existent() {
173 let dir = std::env::temp_dir().join(format!("light-build-test-{}", std::process::id()));
174 let release_dir = dir.join("release");
175 if let Err(e) = std::fs::create_dir_all(&release_dir) {
176 eprintln!("skip: cannot create temp dir: {e}");
177 return;
178 }
179 let bin = release_dir.join("myapp.exe");
180 std::fs::write(&bin, b"fake").unwrap();
181 let found = locate_binary(&dir, "myapp", true).unwrap();
182 assert_eq!(found, bin);
183 let _ = std::fs::remove_dir_all(&dir);
184 }
185
186 #[test]
187 fn locate_binary_nonexistent_returns_expected_path() {
188 let dir = std::env::temp_dir().join("light-build-missing");
189 let expected = dir.join("release").join("nothere.exe");
190 let found = locate_binary(&dir, "nothere", true).unwrap();
191 assert_eq!(found, expected);
192 }
193}