1use std::path::{Path, PathBuf};
4use std::process::Command;
5
6use serde_json::{json, Value};
7use superfluid_adapter_kit::install::{Candidate, Host};
8
9use crate::installs::{self, Install};
10use crate::runtime_pick::RuntimeId;
11use crate::runtimes::Catalog;
12
13pub struct Listing {
14 pub id: RuntimeId,
15 pub host: String,
16 pub candidates: Vec<Candidate>,
18 pub installs: Vec<Install>,
19 pub default: Option<String>,
20 pub pinned: Option<String>,
21}
22
23pub fn of(catalog: &Catalog, id: RuntimeId) -> Result<Listing, String> {
26 let named = std::env::var_os(crate::runtimes::worker_env(id.base())).map(PathBuf::from);
27 of_with(catalog, id, named.as_deref())
28}
29
30pub fn of_with(catalog: &Catalog, id: RuntimeId, named: Option<&Path>) -> Result<Listing, String> {
31 let id = id.base();
32 let (host, candidates) = match (named, crate::linked::get(id)) {
33 (Some(worker), _) => asked(worker)?,
34 (None, Some(rt)) => {
35 let host = Host::detect();
36 (host.describe(), rt.versions(&host))
37 }
38 (None, None) => asked(&installs::installer(catalog, id)?)?,
39 };
40 Ok(Listing {
41 id,
42 host,
43 candidates,
44 installs: installs::list(catalog, id),
45 default: installs::default(catalog, id).map(|i| i.name),
46 pinned: installs::pinned(catalog, id),
47 })
48}
49
50fn asked(worker: &Path) -> Result<(String, Vec<Candidate>), String> {
51 let mut cmd = Command::new(worker);
52 crate::exec::without_secrets(&mut cmd).arg("versions");
53 let out = crate::exec::output_within(&mut cmd, crate::exec::ANSWER_WITHIN)
54 .map_err(|e| format!("{} could not start: {e}", worker.display()))?
55 .ok_or_else(|| format!("{} versions did not answer within {}s", worker.display(), crate::exec::ANSWER_WITHIN.as_secs()))?;
56 match out.status.code() {
57 Some(0) => {}
58 Some(2) => return Err(format!("{} lists no versions ({}): an adapter from before the listing existed", worker.display(), out.status)),
60 _ => return Err(format!("{} versions failed ({}){}", worker.display(), out.status, crate::exec::stderr_tail(&out.stderr))),
61 }
62 let v: Value = serde_json::from_slice(&out.stdout).map_err(|e| format!("{} versions printed no listing: {e}", worker.display()))?;
63 let listed = v["versions"].as_array().ok_or_else(|| format!("{} versions printed no listing", worker.display()))?;
64 let candidates = listed.iter().map(Candidate::from_json).collect::<Result<Vec<_>, _>>()?;
65 Ok((v["describe"].as_str().unwrap_or_default().to_string(), candidates))
66}
67
68pub fn here(listing: &Listing) -> Vec<&str> {
70 listing.candidates.iter().filter(|c| c.here.is_ok()).map(|c| c.version.as_str()).collect()
71}
72
73pub fn cell(listing: &Result<Listing, String>) -> String {
75 let Ok(l) = listing else { return "-".into() };
76 match l.candidates.iter().find(|c| c.here.is_ok()) {
77 None => "none here".into(),
78 Some(c) if l.installs.iter().any(|i| i.manifest.version == c.version) => format!("{} (installed)", c.version),
79 Some(c) => c.version.clone(),
80 }
81}
82
83struct Row<'a> {
85 version: &'a str,
86 listed: Option<&'a Candidate>,
87 installed: Vec<&'a Install>,
88}
89
90impl Listing {
91 fn rows(&self) -> Vec<Row<'_>> {
92 let of_version = |v: &str| self.installs.iter().filter(|i| i.manifest.version == v).collect::<Vec<_>>();
93 let mut rows: Vec<Row> =
94 self.candidates.iter().map(|c| Row { version: &c.version, listed: Some(c), installed: of_version(&c.version) }).collect();
95 for i in &self.installs {
96 if !rows.iter().any(|r| r.version == i.manifest.version) {
97 rows.push(Row { version: &i.manifest.version, listed: None, installed: of_version(&i.manifest.version) });
98 }
99 }
100 rows
101 }
102
103 fn mark(&self, name: &str) -> String {
104 if self.pinned.as_deref() == Some(name) {
105 format!("{name} (pinned)")
106 } else if self.default.as_deref() == Some(name) {
107 format!("{name} (default)")
108 } else {
109 name.to_string()
110 }
111 }
112
113 pub fn text(&self) -> String {
114 let mut out = format!("{}: the versions this adapter installs on its own, newest first; this machine is {}\n", self.id, self.host);
115 let rows = self.rows();
116 if rows.is_empty() {
117 out.push_str(" none: the adapter lists no version, and nothing is installed\n");
118 } else {
119 let mut table = vec![["VERSION", "INSTALLS HERE AS", "INSTALLED"].map(String::from).to_vec()];
120 for r in &rows {
121 let here = match r.listed.map(|c| (c, &c.here)) {
122 Some((c, Ok(p))) => match c.size() {
123 0 => p.install.clone(),
124 size => format!("{} ({:.1} MB)", p.install, size as f64 / 1e6),
125 },
126 Some((_, Err(why))) => format!("no: {why}"),
127 None => "(not a version this adapter lists)".to_string(),
128 };
129 let installed = match r.installed.as_slice() {
130 [] => "-".to_string(),
131 some => some.iter().map(|i| self.mark(&i.name)).collect::<Vec<_>>().join(", "),
132 };
133 table.push(vec![r.version.to_string(), here, installed]);
134 }
135 out.push_str(&crate::runtimes_report::columns(&table));
136 }
137 out.push_str(&format!(
138 "`superfluid runtime install {} --version <v>` installs one; a version not listed installs with --untested\n",
139 self.id
140 ));
141 out
142 }
143
144 pub fn json(&self) -> Value {
145 let versions: Vec<Value> = self
146 .rows()
147 .iter()
148 .map(|r| {
149 let mut v = json!({
150 "version": r.version,
151 "listed": r.listed.is_some(),
152 "installed": r.installed.iter().map(|i| i.name.as_str()).collect::<Vec<_>>(),
153 });
154 match r.listed.map(|c| (c, &c.here)) {
155 Some((c, Ok(p))) => {
156 v["installable"] = true.into();
157 v["install"] = json!(p.install);
158 v["backend"] = json!(p.backend);
159 v["size"] = json!(c.size());
160 }
161 Some((_, Err(why))) => v["installable"] = json!(why),
162 None => {}
163 }
164 v
165 })
166 .collect();
167 json!({"id": self.id.name(), "host": self.host, "default": self.default, "pinned": self.pinned, "versions": versions})
168 }
169}
170
171#[cfg(test)]
172pub(crate) mod tests {
173 use super::*;
174 use std::os::unix::fs::PermissionsExt;
175 use std::path::PathBuf;
176
177 pub(crate) fn scratch(tag: &str) -> PathBuf {
178 let d = std::env::temp_dir().join(format!("superfluid-versions-{tag}-{}", std::process::id()));
179 let _ = std::fs::remove_dir_all(&d);
180 std::fs::create_dir_all(&d).unwrap();
181 d
182 }
183
184 pub(crate) fn fake_worker_listing(dir: &Path, name: &str, report: &str, listing: &str) -> PathBuf {
186 std::fs::create_dir_all(dir).unwrap();
187 let p = dir.join(name);
188 std::fs::write(&p, format!("#!/bin/sh\nif [ \"$1\" = versions ]; then cat <<'EOF'\n{listing}\nEOF\nexit 0; fi\ncat <<'EOF'\n{report}\nEOF\nexit 0\n"))
189 .unwrap();
190 std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
191 p
192 }
193
194 pub(crate) fn manifest(home: &Path, id: &str, name: &str, version: &str, backend: &str, tested: bool) -> PathBuf {
196 let dir = home.join("runtimes").join(id).join(name);
197 std::fs::create_dir_all(dir.join("bin")).unwrap();
198 let m = json!({"id": id, "install": name, "version": version, "backend": backend, "worker": format!("bin/superfluid-worker-{id}"), "tested": tested});
199 std::fs::write(dir.join("runtime.json"), format!("{m:#}\n")).unwrap();
200 dir
201 }
202
203 pub(crate) const LISTING: &str = r#"{"describe":"macOS arm64","versions":[
204 {"version":"t2","error":"toyrt t2 publishes no build for macos-arm64"},
205 {"version":"t1","plan":{"id":"toyrt","version":"t1","backend":"metal","install":"t1-metal","assets":[{"name":"a.tgz","url":"https://x/a.tgz","sha256":"ab","size":2500000}],"tested":true,"why":"w","extra":{"wheels_size":500000}}}]}"#;
206
207 #[test]
208 fn a_listing_says_which_versions_install_here_and_which_are_installed() {
209 let root = scratch("listing");
210 let (beside, libexec, home) = (root.join("bin"), root.join("libexec").join("superfluid"), root.join("home"));
211 std::fs::create_dir_all(&beside).unwrap();
212 std::fs::create_dir_all(&libexec).unwrap();
213 fake_worker_listing(&libexec, "superfluid-worker-toyrt", "{}", LISTING);
214 manifest(&home, "toyrt", "t1-metal", "t1", "metal", true);
215 manifest(&home, "toyrt", "t9-cpu", "t9", "cpu", false);
216 let cat = Catalog::new(home.clone(), Some(beside.clone()), beside.join("superfluid-workerd"));
217 let id = RuntimeId::new("toyrt");
218
219 let l = of(&cat, id).unwrap();
220 assert_eq!((l.host.as_str(), l.default.as_deref(), l.pinned.as_deref()), ("macOS arm64", Some("t1-metal"), None));
221 let text = l.text();
222 let squeezed: Vec<String> = text.lines().map(|l| l.split_whitespace().collect::<Vec<_>>().join(" ")).collect();
224 assert_eq!(squeezed[0], "toyrt: the versions this adapter installs on its own, newest first; this machine is macOS arm64");
225 assert_eq!(squeezed[1], "VERSION INSTALLS HERE AS INSTALLED");
226 assert_eq!(squeezed[2], "t2 no: toyrt t2 publishes no build for macos-arm64 -");
227 assert_eq!(squeezed[3], "t1 t1-metal (3.0 MB) t1-metal (default)");
228 assert_eq!(squeezed[4], "t9 (not a version this adapter lists) t9-cpu");
229 assert_eq!(squeezed[5], "`superfluid runtime install toyrt --version <v>` installs one; a version not listed installs with --untested");
230 assert_eq!(squeezed.len(), 6);
231 let lines: Vec<&str> = text.lines().collect();
232 assert!(lines[1].starts_with("VERSION INSTALLS HERE AS") && lines[2].starts_with("t2 no: "), "aligned: {text}");
233 assert_eq!(lines[1].find("INSTALLED"), lines[3].find("t1-metal (default)"), "aligned: {text}");
234 let j = l.json();
235 assert_eq!(j["id"], "toyrt");
236 assert_eq!(j["versions"][0]["installable"], "toyrt t2 publishes no build for macos-arm64");
237 assert_eq!(j["versions"][0]["installed"], json!([]));
238 assert_eq!((j["versions"][1]["installable"].as_bool(), j["versions"][1]["install"].as_str()), (Some(true), Some("t1-metal")));
239 assert_eq!((j["versions"][1]["size"].as_u64(), j["versions"][1]["installed"][0].as_str()), (Some(3_000_000), Some("t1-metal")));
240 assert_eq!((j["versions"][2]["listed"].as_bool(), j["versions"][2]["installable"].is_null()), (Some(false), true));
241 assert_eq!(here(&l), ["t1"]);
242 assert_eq!(cell(&Ok(l)), "t1 (installed)");
243
244 std::fs::write(home.join("runtimes").join("toyrt").join("pinned"), "t9-cpu\n").unwrap();
245 let l = of(&cat, id).unwrap();
246 let text = l.text();
247 let squeezed: Vec<String> = text.lines().map(|l| l.split_whitespace().collect::<Vec<_>>().join(" ")).collect();
248 assert_eq!(squeezed[3], "t1 t1-metal (3.0 MB) t1-metal", "{text}");
249 assert_eq!(squeezed[4], "t9 (not a version this adapter lists) t9-cpu (pinned)", "{text}");
250 assert_eq!(l.json()["pinned"], "t9-cpu");
251
252 let bare = Listing { installs: vec![], default: None, pinned: None, candidates: vec![Candidate { version: "t2".into(), here: Err("no".into()) }], ..l };
253 assert_eq!(cell(&Ok(bare)), "none here");
254
255 crate::runtimes::tests::fake_worker(&libexec, "superfluid-worker-oldrt", "{}", 2);
256 let Err(e) = of(&cat, RuntimeId::new("oldrt")) else { panic!("an old worker listed versions") };
257 assert!(e.contains("lists no versions"), "{e}");
258 assert_eq!(cell(&Err(e)), "-");
259 let fails = libexec.join("superfluid-worker-failrt");
260 std::fs::write(&fails, "#!/bin/sh\necho 'superfluid-worker-failrt: recipe.json is not JSON' >&2\nexit 1\n").unwrap();
261 std::fs::set_permissions(&fails, std::fs::Permissions::from_mode(0o755)).unwrap();
262 let Err(e) = of(&cat, RuntimeId::new("failrt")) else { panic!("a failing worker listed versions") };
263 assert!(e.contains("versions failed") && e.ends_with(": superfluid-worker-failrt: recipe.json is not JSON"), "{e}");
264 assert!(!e.contains("before the listing existed"), "a new adapter that fails is not an old one: {e}");
265 let named = fake_worker_listing(&root.join("elsewhere"), "superfluid-worker-named", "{}", r#"{"describe":"here","versions":[{"version":"n1","error":"not here"}]}"#);
266 for id in crate::linked::ids().into_iter().chain([RuntimeId::new("toyrt")]) {
267 let l = of_with(&cat, id, Some(&named)).unwrap();
268 assert_eq!((l.host.as_str(), l.candidates.len(), l.candidates[0].version.as_str()), ("here", 1, "n1"), "{}", id.name());
269 }
270 let _ = std::fs::remove_dir_all(&root);
271 }
272}