pub struct TemplateContextBuilder { /* private fields */ }Expand description
Template context builder for AGPM resource installation.
This struct is responsible for building the template context that will be available to Markdown templates during rendering. It collects data from the manifest, lockfile, and current resource being processed.
§Context Structure
The built context follows this structure:
{
"agpm": {
"resource": {
"type": "agent",
"name": "example-agent",
"install_path": ".claude/agents/example.md",
"source": "community",
"version": "v1.0.0",
"resolved_commit": "abc123...",
"checksum": "sha256:...",
"path": "agents/example.md"
},
"deps": {
"agents": {
"helper": {
"install_path": ".claude/agents/helper.md",
"version": "v1.0.0",
"resolved_commit": "def456...",
"checksum": "sha256:...",
"source": "community",
"path": "agents/helper.md"
}
},
"snippets": { ... },
"commands": { ... }
}
}
}Implementations§
Source§impl TemplateContextBuilder
impl TemplateContextBuilder
Sourcepub fn new(
lockfile: Arc<LockFile>,
project_config: Option<ProjectConfig>,
cache: Arc<Cache>,
project_dir: PathBuf,
) -> Self
pub fn new( lockfile: Arc<LockFile>, project_config: Option<ProjectConfig>, cache: Arc<Cache>, project_dir: PathBuf, ) -> Self
Create a new template context builder.
§Arguments
lockfile- The resolved lockfile, wrapped in Arc for efficient sharingproject_config- Optional project-specific template variables from the manifestcache- Cache instance for reading source files during content extractionproject_dir- Project root directory for resolving local file paths
Sourcepub async fn build_context(
&self,
resource_name: &str,
resource_type: ResourceType,
) -> Result<TeraContext>
pub async fn build_context( &self, resource_name: &str, resource_type: ResourceType, ) -> Result<TeraContext>
Sourcepub fn compute_context_digest(&self) -> Result<String>
pub fn compute_context_digest(&self) -> Result<String>
Compute a stable digest of the template context data.
This method creates a deterministic hash of all lockfile metadata that could affect template rendering. The digest is used as part of the cache key to ensure that changes to dependency versions or metadata properly invalidate the cache.
§Returns
Returns a hex-encoded string containing the first 16 characters of the SHA-256 hash of the serialized template context data. This is sufficient to uniquely identify context changes while keeping the digest compact.
§What’s Included
The digest includes all lockfile metadata that affects rendering:
- Resource names, types, and installation paths
- Dependency versions and resolved commits
- Checksums and source information
§Determinism
The hash is stable across runs because:
- Resources are sorted by type and name before hashing
- JSON serialization uses consistent ordering (BTreeMap)
- Only metadata fields that affect rendering are included
§Examples
use agpm_cli::templating::TemplateContextBuilder;
use agpm_cli::lockfile::LockFile;
use std::path::{Path, PathBuf};
use std::sync::Arc;
let lockfile = LockFile::load(Path::new("agpm.lock"))?;
let cache = Arc::new(agpm_cli::cache::Cache::new()?);
let project_dir = std::env::current_dir()?;
let builder = TemplateContextBuilder::new(
Arc::new(lockfile),
None,
cache,
project_dir
);
let digest = builder.compute_context_digest()?;
println!("Template context digest: {}", digest);