1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Registration-time DSL: evaluating a `ci.fnl` script with the
//! `(require :quire.ci)` module bound and collecting the jobs and
//! image it registers.
//!
//! The pipeline module calls [`register`] to drive evaluation; the
//! runtime module is not involved here. Per-job validation errors
//! collected during evaluation are returned as a single
//! `PipelineError`, not raised as Lua errors.

use std::cell::RefCell;
use std::rc::Rc;

use mlua::{IntoLua, Lua};

use miette::NamedSource;

use super::pipeline::{
    self, CompileResult, DefinitionError, Diagnostic, Job, PipelineError, RunFn,
};
use crate::fennel::Fennel;

/// Output of [`register`]: jobs and image successfully registered
/// from the script. Definition-time errors are returned via the `Err`
/// arm, not collected here.
#[derive(Debug)]
pub struct Registrations {
    pub jobs: Vec<Job>,
    pub image: Option<String>,
}

/// Evaluate `source` with the registration module bound and collect
/// what got registered.
///
/// Pre-graph rules run inside the callback, so a single bad job does
/// not abort the rest of the script — but if any rule fired, the
/// whole batch is returned as a `PipelineError` instead of partial
/// registrations.
pub fn register(fennel: &Fennel, source: &str, name: &str) -> CompileResult<Registrations> {
    let jobs: Rc<RefCell<Vec<Job>>> = Rc::new(RefCell::new(Vec::new()));
    let image = Rc::new(RefCell::new(None));
    let src = Rc::new(source.to_string());

    let errors = Rc::new(RefCell::new(Vec::new()));

    fennel.eval_raw(source, name, |lua| {
        lua.register_module(
            "quire.ci",
            Registration {
                jobs: jobs.clone(),
                errors: errors.clone(),
                image: image.clone(),
                source: src.clone(),
            },
        )?;
        Ok(())
    })?;

    // Remove the Registration app data so `ci.image`/`ci.job` calls at
    // runtime (inside run-fns) hit "registration not installed" instead of
    // silently pushing into the already-consumed sinks.
    fennel.lua().remove_app_data::<Registration>();

    let errors = errors.take();
    if !errors.is_empty() {
        return Err(PipelineError {
            src: NamedSource::new(name, source.to_string()),
            diagnostics: errors.into_iter().map(Diagnostic::Definition).collect(),
        }
        .into());
    }

    let image_name = image.borrow().as_ref().map(|i| i.name.clone());
    Ok(Registrations {
        jobs: jobs.take(),
        image: image_name,
    })
}

/// The registration-time module exposed to Fennel scripts via
/// `(require :quire.ci)`.
///
/// Converted into a Lua table via [`IntoLua`]: stows itself on the
/// VM as app data (so `register_job` can find the registration sink)
/// and returns a table whose only entry is `job`. Runtime primitives
/// (`sh`, `secret`) live on the per-execution `Runtime` handle, not
/// here.
///
/// ```fennel
/// (local ci (require :quire.ci))
/// (ci.job :build [:quire/push]
///   (fn [{: sh : secret}]
///     (sh ["echo" (secret :github_token)])))
/// ```
pub struct Registration {
    pub jobs: Rc<RefCell<Vec<Job>>>,
    pub errors: Rc<RefCell<Vec<DefinitionError>>>,
    image: Rc<RefCell<Option<ImageRegistration>>>,
    pub source: Rc<String>,
}

impl IntoLua for Registration {
    fn into_lua(self, lua: &Lua) -> mlua::Result<mlua::Value> {
        lua.set_app_data(self);
        let table = lua.create_table()?;
        table.set("job", lua.create_function(register_job)?)?;
        table.set("image", lua.create_function(register_image)?)?;
        // Carry forward the runtime stub from the placeholder
        // `quire.ci` table seeded by `Fennel::new`. `register_module`
        // overwrites `package.loaded["quire.ci"]` with the value we
        // return; reusing the existing stub keeps references captured
        // before registration (and any held by previously preloaded
        // modules like `quire.stdlib`) pointing at the same Lua table
        // that `RuntimeHandle::install` mutates.
        let package: mlua::Table = lua.globals().get("package")?;
        let loaded: mlua::Table = package.get("loaded")?;
        let placeholder: mlua::Table = loaded.get("quire.ci")?;
        let runtime: mlua::Table = placeholder.get("runtime")?;
        table.set("runtime", runtime)?;
        table.into_lua(lua)
    }
}

impl Registration {
    /// Push a registered job after enforcing id uniqueness. On
    /// collision, records `DuplicateJob` against the caller's source
    /// line and drops the new job; the first registration wins.
    pub fn add_job(&self, job: Job, line: u32) {
        let mut jobs = self.jobs.borrow_mut();
        if jobs.iter().any(|j| j.id == job.id) {
            let span = pipeline::span_for_line(&self.source, line);
            self.errors
                .borrow_mut()
                .push(DefinitionError::DuplicateJob {
                    job_id: job.id,
                    span,
                });
            return;
        }
        jobs.push(job);
    }
}

/// A pending image registration extracted from the Lua callback.
struct ImageRegistration {
    name: String,
    _line: u32,
}

/// Body of `(ci.image name)`. Records the image on the first call;
/// pushes a `DuplicateImage` error on subsequent calls.
fn register_image(lua: &Lua, (name,): (String,)) -> mlua::Result<()> {
    let r = lua
        .app_data_ref::<Registration>()
        .ok_or_else(|| mlua::Error::external("quire.ci registration not installed on Lua VM"))?;
    let line = lua
        .inspect_stack(1, |d| d.current_line())
        .flatten()
        .map(|l| l as u32)
        .unwrap_or(0);
    let mut img = r.image.borrow_mut();
    match &*img {
        Some(_) => {
            let span = pipeline::span_for_line(&r.source, line);
            r.errors
                .borrow_mut()
                .push(DefinitionError::DuplicateImage { span });
        }
        None => {
            *img = Some(ImageRegistration { name, _line: line });
        }
    }
    Ok(())
}

/// Body of `(ci.job id inputs run-fn)`. Captures the call-site line
/// from the Lua debug stack so per-job validation errors carry a span
/// pointing back at the user's source. Enforces the user-facing
/// reserved-slash rule: ids may not contain `/`, since the `quire/`
/// namespace is reserved for built-in helpers.
fn register_job(
    lua: &Lua,
    (id, inputs, run_fn): (String, Vec<String>, mlua::Function),
) -> mlua::Result<()> {
    let r = lua
        .app_data_ref::<Registration>()
        .ok_or_else(|| mlua::Error::external("quire.ci registration not installed on Lua VM"))?;
    let line = lua
        .inspect_stack(1, |d| d.current_line())
        .flatten()
        .map(|l| l as u32)
        .unwrap_or(0);

    if id.contains('/') {
        let span = pipeline::span_for_line(&r.source, line);
        r.errors
            .borrow_mut()
            .push(DefinitionError::ReservedSlash { job_id: id, span });
        return Ok(());
    }

    match Job::new(id, inputs, RunFn::Lua(run_fn), line, &r.source) {
        Ok(job) => r.add_job(job, line),
        Err(e) => r.errors.borrow_mut().push(e),
    }
    Ok(())
}