|
| 1 | +//! Entrypoints a provider finds in one source file (`buildProviders` 1). |
| 2 | +//! |
| 3 | +//! A run declared with `entrypoints: true` starts an entrypoint the provider |
| 4 | +//! names, such as a class with a `main` method, in one of its projects. Core |
| 5 | +//! never reads the source: the provider answers `lithe/build/runEntrypoints` |
| 6 | +//! for a file and Core checks that every answer is something the run can |
| 7 | +//! start, so the host can save and start it as an ordinary provider run. |
| 8 | +
|
| 9 | +use super::decode::{invalid_output, MAX_OUTPUT_BYTES}; |
| 10 | +use super::manifest::{BuildSystemDeclaration, TaskSource}; |
| 11 | +use super::plans::{self, OutputPlanRequest, PlanBase, SelectedEntity}; |
| 12 | +use lithe_plugin_model::Validation; |
| 13 | +use serde::Deserialize; |
| 14 | +use serde_json::{json, Value}; |
| 15 | + |
| 16 | +/// Version of the `litheRunEntrypoints` document a provider returns. |
| 17 | +const ENTRYPOINTS_VERSION: u64 = 0; |
| 18 | + |
| 19 | +/// Most entrypoints one file may name. |
| 20 | +const MAX_ENTRYPOINTS: usize = 64; |
| 21 | + |
| 22 | +/// Longest entrypoint name, in bytes. |
| 23 | +const MAX_NAME: usize = 256; |
| 24 | + |
| 25 | +/// Longest opaque entrypoint, in bytes, as `launchOptions.entrypoint` allows. |
| 26 | +const MAX_ENTRYPOINT: usize = 4096; |
| 27 | + |
| 28 | +/// The source file whose entrypoints the host asks for. |
| 29 | +#[derive(Debug, Deserialize)] |
| 30 | +#[serde(rename_all = "camelCase")] |
| 31 | +pub(crate) struct RunEntrypointsPlanRequest { |
| 32 | + #[serde(flatten)] |
| 33 | + pub(crate) base: OutputPlanRequest, |
| 34 | + /// Workspace-relative file inside the instance root. |
| 35 | + pub(crate) file: String, |
| 36 | +} |
| 37 | + |
| 38 | +/// The plan's configuration and the provider's answer. |
| 39 | +#[derive(Debug, Deserialize)] |
| 40 | +#[serde(rename_all = "camelCase")] |
| 41 | +pub(crate) struct DecodeRunEntrypointsRequest { |
| 42 | + #[serde(flatten)] |
| 43 | + pub(crate) base: PlanBase, |
| 44 | + pub(crate) result: Value, |
| 45 | +} |
| 46 | + |
| 47 | +/// One entrypoint as the provider names it. |
| 48 | +#[derive(Deserialize)] |
| 49 | +struct Entrypoint { |
| 50 | + run: String, |
| 51 | + name: String, |
| 52 | + entrypoint: String, |
| 53 | + project: SelectedEntity, |
| 54 | +} |
| 55 | + |
| 56 | +fn has_entrypoint_run(build_system: &BuildSystemDeclaration) -> bool { |
| 57 | + build_system |
| 58 | + .run |
| 59 | + .iter() |
| 60 | + .any(|run| run.entrypoints && run.source == TaskSource::Provider) |
| 61 | +} |
| 62 | + |
| 63 | +/// `build.runEntrypointsPlan`: asks the provider which entrypoints `file` holds. |
| 64 | +pub(crate) fn plan( |
| 65 | + build_system: &BuildSystemDeclaration, |
| 66 | + request: &RunEntrypointsPlanRequest, |
| 67 | +) -> Validation<Value> { |
| 68 | + if !has_entrypoint_run(build_system) { |
| 69 | + return Err(plans::unavailable(build_system, "run entrypoints")); |
| 70 | + } |
| 71 | + if request.base.project.is_some() || request.base.output_file.is_some() { |
| 72 | + return Err(plans::invalid_selection( |
| 73 | + "Run entrypoints are found by file, not by project.", |
| 74 | + )); |
| 75 | + } |
| 76 | + let context = plans::PlanContext::new(build_system, &request.base.base)?; |
| 77 | + let file = super::decode::request_path(&request.file, "file")?; |
| 78 | + if super::decode::relative_to(&context.root, &file).is_none_or(|relative| relative == ".") { |
| 79 | + return Err(plans::invalid_selection( |
| 80 | + "The file is outside the build-system root.", |
| 81 | + )); |
| 82 | + } |
| 83 | + let mut params = plans::provider_hints(&request.base)?; |
| 84 | + params.insert("file".to_string(), json!(file)); |
| 85 | + let provider = build_system |
| 86 | + .provider |
| 87 | + .as_ref() |
| 88 | + .ok_or_else(|| plans::unavailable(build_system, "a provider"))?; |
| 89 | + Ok(context.provider_plan( |
| 90 | + provider, |
| 91 | + "lithe/build/runEntrypoints", |
| 92 | + "litheRunEntrypoints", |
| 93 | + params, |
| 94 | + )) |
| 95 | +} |
| 96 | + |
| 97 | +/// `build.decodeRunEntrypoints`: the entrypoints the host may save and start, in |
| 98 | +/// the provider's order without duplicates. A malformed answer is refused whole. |
| 99 | +pub(crate) fn decode( |
| 100 | + build_system: &BuildSystemDeclaration, |
| 101 | + request: &DecodeRunEntrypointsRequest, |
| 102 | +) -> Validation<Value> { |
| 103 | + if !has_entrypoint_run(build_system) { |
| 104 | + return Err(plans::unavailable(build_system, "run entrypoints")); |
| 105 | + } |
| 106 | + let context = plans::PlanContext::new(build_system, &request.base)?; |
| 107 | + let result = &request.result; |
| 108 | + if serde_json::to_vec(result).map_or(usize::MAX, |bytes| bytes.len()) > MAX_OUTPUT_BYTES |
| 109 | + || result["format"] != "litheRunEntrypoints" |
| 110 | + || result["version"] != ENTRYPOINTS_VERSION |
| 111 | + { |
| 112 | + return Err(invalid_output("Invalid run entrypoints document.")); |
| 113 | + } |
| 114 | + let items = result["entrypoints"] |
| 115 | + .as_array() |
| 116 | + .filter(|items| items.len() <= MAX_ENTRYPOINTS) |
| 117 | + .ok_or_else(|| invalid_output("Run entrypoints hold at most 64 entries."))?; |
| 118 | + let mut entrypoints: Vec<Value> = Vec::new(); |
| 119 | + for item in items { |
| 120 | + let entry: Entrypoint = serde_json::from_value(item.clone()) |
| 121 | + .map_err(|_| invalid_output("Invalid run entrypoint."))?; |
| 122 | + let runnable = build_system.run.iter().any(|run| { |
| 123 | + run.id == entry.run && run.entrypoints && run.source == TaskSource::Provider |
| 124 | + }); |
| 125 | + let name = entry.name.trim(); |
| 126 | + if !runnable |
| 127 | + || name.is_empty() |
| 128 | + || name.len() > MAX_NAME |
| 129 | + || name.chars().any(char::is_control) |
| 130 | + || entry.entrypoint.is_empty() |
| 131 | + || entry.entrypoint.len() > MAX_ENTRYPOINT |
| 132 | + || entry.entrypoint.contains('\0') |
| 133 | + { |
| 134 | + return Err(invalid_output( |
| 135 | + "A run entrypoint names a run, a name or an entrypoint this build system cannot start.", |
| 136 | + )); |
| 137 | + } |
| 138 | + let project = |
| 139 | + plans::selection(&entry.project, &context.root, None, "project").map_err(|_| { |
| 140 | + invalid_output("A run entrypoint's project is outside the build-system root.") |
| 141 | + })?; |
| 142 | + let decoded = json!({ |
| 143 | + "run": entry.run, |
| 144 | + "name": name, |
| 145 | + "entrypoint": entry.entrypoint, |
| 146 | + "project": {"name": project.name, "path": project.path}, |
| 147 | + }); |
| 148 | + if !entrypoints.contains(&decoded) { |
| 149 | + entrypoints.push(decoded); |
| 150 | + } |
| 151 | + } |
| 152 | + Ok(json!({ "entrypoints": entrypoints })) |
| 153 | +} |
0 commit comments