1use std::{
2 cmp::Ordering,
3 fmt::Write as _,
4 fs,
5 path::{Path, PathBuf},
6 process::Stdio,
7};
8
9use crate::util::{glob_walker, workspace_dir};
10
11pub fn find_about(metadata: Option<&Path>) -> zng_env::About {
12 if let Some(m) = metadata {
13 println_v!("parsing `{}`", m.display());
14
15 let cargo_toml = fs::read_to_string(m).unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", m.display()));
16 return zng_env::About::parse_manifest(&cargo_toml).unwrap_or_else(|e| fatal!("cannot parse `{}`, {e}", m.display()));
17 }
18
19 let mut options = Vec::with_capacity(1);
20
21 let workspace_manifest =
22 workspace_dir().unwrap_or_else(|| fatal!("cannot locate workspace, use --metadata if source is not in a cargo project"));
23 println_v!("workspace `{}`", workspace_manifest.display());
24
25 for manifest in glob_walker(
26 &format!(
27 "{}/**/Cargo.toml",
28 workspace_manifest.display().to_string().replace("\\", "/").trim_end_matches('/')
29 ),
30 false,
31 )
32 .unwrap_or_else(|e| fatal!("cannot search metadata, {e}"))
33 {
34 let manifest = manifest.unwrap_or_else(|e| fatal!("error searching metadata, {e}")).into_path();
35 let _empty = PathBuf::new();
36 let manifest_dir = manifest.parent().unwrap_or(&_empty);
37 if manifest_dir.as_os_str().is_empty() {
38 continue;
39 }
40
41 let output = std::process::Command::new("cargo")
42 .arg("locate-project")
43 .arg("--workspace")
44 .arg("--message-format=plain")
45 .current_dir(manifest_dir)
46 .stderr(Stdio::inherit())
47 .output()
48 .unwrap_or_else(|e| fatal!("cannot locate workspace in `{}`, {e}", manifest_dir.display()));
49 if !output.status.success() {
50 continue;
51 }
52 let w2 = Path::new(std::str::from_utf8(&output.stdout).unwrap().trim()).parent().unwrap();
53 if w2 != workspace_manifest {
54 println_v!("skip `{}`, not a workspace member", manifest.display());
55 continue;
56 }
57
58 let cargo_toml = fs::read_to_string(&manifest).unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", manifest.display()));
59 let about = match zng_env::About::parse_manifest(&cargo_toml) {
60 Ok(a) => a,
61 Err(e) => {
62 if e.message().contains("missing field `package`") {
63 println_v!("skip `{}`, no package metadata", manifest.display());
64 } else {
65 error!("cannot parse `{}`, {e}", manifest.display());
66 }
67 continue;
68 }
69 };
70
71 if about.has_about || manifest_dir.join("src/main.rs").exists() {
72 options.push(about);
73 } else {
74 println_v!(
75 "skip `{}` cause it has no zng metadata and/or it is not a bin crate",
76 manifest.display()
77 );
78 }
79 }
80
81 match options.len().cmp(&1) {
82 Ordering::Less => fatal!("cannot find main crate metadata, workspace has no bin crate, use --metadata to select a source"),
83 Ordering::Equal => options.remove(0),
84 Ordering::Greater => {
85 let mut main_options = Vec::with_capacity(1);
86 for (i, o) in options.iter().enumerate() {
87 if o.has_about {
88 main_options.push(i);
89 }
90 }
91 match main_options.len().cmp(&1) {
92 Ordering::Equal => options.remove(main_options[0]),
93 Ordering::Less => {
94 let mut msg = "cannot find main crate metadata, workspace has multiple bin crates\n".to_owned();
95 for o in &options {
96 writeln!(&mut msg, " {}", o.pkg_name).unwrap();
97 }
98 writeln!(
99 &mut msg,
100 "set [package.metadata.zng.about]app=\"Display Name\" in one of the crates\nor use --metadata to select the source"
101 )
102 .unwrap();
103 fatal!("{msg}");
104 }
105 Ordering::Greater => {
106 let mut msg = "cannot find main crate metadata, workspace has multiple metadata sources\n".to_owned();
107 for i in main_options {
108 writeln!(&mut msg, " {}", options[i].pkg_name).unwrap();
109 }
110 writeln!(
111 &mut msg,
112 "set [package.metadata.zng.about] in only one crate\nor use --metadata to select the source"
113 )
114 .unwrap();
115 fatal!("{msg}");
116 }
117 }
118 }
119 }
120}