1use std::path::Path;
12
13use anyhow::{Context, Result};
14use serde::Serialize;
15
16use crate::config::Config;
17use crate::{env, runners};
18
19const DEFAULT_JOB: &str = "verify";
20const DEFAULT_MACHINE: &str = "ubuntu-26.04";
21const DEFAULT_LABELS: &[&str] = &["ubuntu-26.04"];
22
23#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
25pub struct CiJob {
26 pub id: String,
28 pub name: String,
30 pub machine: String,
32 pub labels: Vec<String>,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
38pub struct Matrix {
39 pub include: Vec<CiJob>,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
45pub struct CiPlan {
46 pub matrix: Matrix,
48}
49
50pub fn plan(config: &Config) -> Result<CiPlan> {
58 runners::declared(config)?;
59 let mut include = Vec::new();
60 if config.ci.is_empty() {
61 let resolved = machines(config, DEFAULT_JOB, None)?;
62 push(&mut include, DEFAULT_JOB, &resolved);
63 return Ok(CiPlan {
64 matrix: Matrix { include },
65 });
66 }
67 for (id, job) in &config.ci {
68 let resolved = machines(config, id, job.runners.as_ref())?;
69 push(&mut include, id, &resolved);
70 }
71 Ok(CiPlan {
72 matrix: Matrix { include },
73 })
74}
75
76fn machines(
77 config: &Config,
78 id: &str,
79 named: Option<&Vec<String>>,
80) -> Result<Vec<runners::Machine>> {
81 runners::of(config, "ci job", id, named, DEFAULT_MACHINE, DEFAULT_LABELS)
82}
83
84fn push(include: &mut Vec<CiJob>, id: &str, resolved: &[runners::Machine]) {
85 for machine in resolved {
86 include.push(CiJob {
87 id: id.to_owned(),
88 name: runners::check_name(id, &machine.name, resolved),
89 machine: machine.name.clone(),
90 labels: machine.labels.clone(),
91 });
92 }
93}
94
95pub fn write_github_output(plan: &CiPlan, path: &Path) -> Result<()> {
102 let encoded = serde_json::to_string(&plan.matrix).context("encode matrix")?;
103 env::write_github_output(path, &format!("matrix={encoded}\n"))
104}
105
106#[cfg(test)]
107mod tests;