use std::path::{Path, PathBuf};
use super::super::manifest::{ManifestError, PluginManifest, PluginToolSpec, resolve_capabilities};
use super::super::registry::{PluginPin, ToolPin, hex_sha256};
use crate::script_tool::{
ScriptLawError, check_script_file_on_disk, pin_from_plugin_root, resolve_under_root,
script_file_index, script_file_is_program,
};
use super::types::*;
pub(super) fn build_plan(
staging_dir: PathBuf,
plugins_root: &Path,
) -> Result<InstallPlan, InstallError> {
let manifest_path = staging_dir.join("plugin.json");
let manifest_raw = std::fs::read(&manifest_path).map_err(InstallError::Io)?;
let manifest_text = String::from_utf8_lossy(&manifest_raw).into_owned();
let manifest = PluginManifest::from_json(&manifest_text).map_err(InstallError::Manifest)?;
let plugin_root = plugins_root.join(&manifest.name);
let digest = manifest
.review(&plugin_root)
.map_err(InstallError::Manifest)?;
let pin = build_pin(&manifest, &manifest_raw, &plugin_root, &staging_dir)?;
Ok(InstallPlan {
digest,
pin,
plugin_root,
staging: StagingLayout { dir: staging_dir },
plugins_root: plugins_root.to_path_buf(),
})
}
pub(super) fn build_pin(
manifest: &PluginManifest,
manifest_raw: &[u8],
plugin_root: &Path,
staging_dir: &Path,
) -> Result<PluginPin, InstallError> {
let mut tools = Vec::with_capacity(manifest.tools.len());
for tool in &manifest.tools {
let identity = tool.identity(&manifest.name).map_err(|source| {
InstallError::Manifest(ManifestError::BadName {
name: tool.name().to_string(),
source,
})
})?;
match tool {
PluginToolSpec::Wasm(t) => {
let module = resolve_under_root(plugin_root, &t.module)
.map_err(|source| InstallError::PathLaw { source })?;
let staged_module = staging_dir.join(&t.module);
let bytes = std::fs::read(&staged_module).map_err(InstallError::Io)?;
let capabilities = resolve_capabilities(&t.capabilities, plugin_root)
.map_err(|source| InstallError::PathLaw { source })?;
tools.push(ToolPin::Wasm {
identity,
module,
wasm_sha256: hex_sha256(&bytes),
capabilities,
});
}
PluginToolSpec::Script(t) => {
let run = pin_from_plugin_root(&t.run, plugin_root)
.map_err(|source| InstallError::PathLaw { source })?;
let index =
script_file_index(&t.run).map_err(|source| InstallError::PathLaw { source })?;
let require_executable = script_file_is_program(&t.run)
.map_err(|source| InstallError::PathLaw { source })?;
check_script_file_on_disk(&staging_dir.join(&t.run[index]), require_executable)
.map_err(|source| InstallError::PathLaw { source })?;
tools.push(ToolPin::Script { identity, run });
}
PluginToolSpec::Mcp(t) => {
let command = match &t.server.command {
Some(cmd) => Some(
pin_server_argv(cmd, plugin_root)
.map_err(|source| InstallError::PathLaw { source })?,
),
None => None,
};
tools.push(ToolPin::Mcp {
label: t.name.clone(),
command,
env: t.server.env.clone(),
url: t.server.url.clone(),
headers: t.server.headers.clone(),
});
}
}
}
Ok(PluginPin {
name: manifest.name.clone(),
version: manifest.version.clone(),
source: "installed".into(),
manifest_sha256: hex_sha256(manifest_raw),
tools,
})
}
pub(super) fn pin_server_argv(
argv: &[String],
plugin_root: &Path,
) -> Result<Vec<String>, ScriptLawError> {
argv.iter()
.map(|arg| {
let pathish = arg.contains('/') || arg.contains('\\') || arg.starts_with('.');
if !pathish || Path::new(arg).is_absolute() {
return Ok(arg.clone());
}
resolve_under_root(plugin_root, arg)
})
.collect()
}