Skip to main content

ai_agent/utils/plugins/
plugin_versioning.rs

1// Source: ~/claudecode/openclaudecode/src/utils/plugins/pluginVersioning.ts
2#![allow(dead_code)]
3
4use std::path::Path;
5
6use super::schemas::PluginManifest;
7
8/// Stub for git filesystem head retrieval.
9fn _git_head_stub(_dir_path: &str) -> Option<String> {
10    None
11}
12
13/// Calculate the version for a plugin based on its source.
14pub async fn calculate_plugin_version(
15    plugin_id: &str,
16    _source: &super::schemas::PluginSource,
17    manifest: Option<&PluginManifest>,
18    install_path: Option<&str>,
19    provided_version: Option<&str>,
20    git_commit_sha: Option<&str>,
21) -> String {
22    // 1. Use explicit version from plugin.json if available
23    if let Some(m) = manifest {
24        if let Some(ref v) = m.version {
25            log::debug!("Using manifest version for {}: {}", plugin_id, v);
26            return v.clone();
27        }
28    }
29
30    // 2. Use provided version (typically from marketplace entry)
31    if let Some(v) = provided_version {
32        log::debug!("Using provided version for {}: {}", plugin_id, v);
33        return v.to_string();
34    }
35
36    // 3. Use pre-resolved git SHA
37    if let Some(sha) = git_commit_sha {
38        let short_sha = &sha[..sha.len().min(12)];
39        log::debug!(
40            "Using pre-resolved git SHA for {}: {}",
41            plugin_id,
42            short_sha
43        );
44        return short_sha.to_string();
45    }
46
47    // 4. Try to get git SHA from install path
48    if let Some(path) = install_path {
49        if let Ok(Some(sha)) = get_git_commit_sha(path) {
50            let short_sha = &sha[..sha.len().min(12)];
51            log::debug!("Using git SHA for {}: {}", plugin_id, short_sha);
52            return short_sha.to_string();
53        }
54    }
55
56    // 5. Return 'unknown' as last resort
57    log::debug!("No version found for {}, using 'unknown'", plugin_id);
58    "unknown".to_string()
59}
60
61/// Get the git commit SHA for a directory.
62pub fn get_git_commit_sha(
63    _dir_path: &str,
64) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
65    Ok(None)
66}
67
68/// Extract version from a versioned cache path.
69pub fn get_version_from_path(install_path: &str) -> Option<String> {
70    let parts: Vec<&str> = install_path.split('/').filter(|p| !p.is_empty()).collect();
71
72    let cache_index = parts.iter().position(|&p| p == "cache");
73
74    if let Some(idx) = cache_index {
75        let components_after_cache = &parts[idx + 1..];
76        if components_after_cache.len() >= 3 {
77            return Some(components_after_cache[2].to_string());
78        }
79    }
80
81    None
82}
83
84/// Check if a path is a versioned plugin path.
85pub fn is_versioned_path(path: &str) -> bool {
86    get_version_from_path(path).is_some()
87}