use std::path::PathBuf;
use tempfile::TempDir;
use tuff_adapter_claude::plugin::ClaudePlugin;
use tuff_core::bundle::{self, BundleLocation, BundleReader, ImportedPack, LocalBundle};
use tuff_core::error::{Result, TuffError};
use tuff_core::lockfile::PackGitSource;
use tuff_core::pack::{PackImport, PackUnsupported};
use crate::adapter::{AdapterKind, AgentAdapter};
use crate::git::{self as pack_git, CheckedOut, CheckoutRequest, GitPackCheckout};
pub fn import_plugin(
bundle: &LocalBundle,
version_hint: Option<&str>,
origin: Origin,
) -> Result<ImportedPack> {
let mut imported = ClaudePlugin.read(bundle, version_hint)?;
let mut targets = Vec::new();
for adapter in AdapterKind::all() {
match first_unwritable(adapter, &imported) {
None => targets.push(adapter.id().to_string()),
Some(reason) => imported.notes.push(format!(
"the pack is not built for {}: {reason}",
adapter.display_name()
)),
}
}
imported.manifest.build.targets = targets;
if let Some(import) = imported.manifest.import.as_mut() {
import.url = origin.url;
import.path = origin.path;
import.plugin = origin.plugin;
import.commit = origin.commit;
}
Ok(imported)
}
fn first_unwritable(adapter: AdapterKind, imported: &ImportedPack) -> Option<String> {
for member in &imported.members {
if !adapter.supports(member.capability_type) {
return Some(format!(
"it does not take {} '{}'",
member.capability_type, member.id
));
}
if let Some(server) = member.manifest.get("server").cloned()
&& let Ok(server) = server.try_into::<tuff_core::manifest::McpServerConfig>()
&& let Err(error) = adapter.mcp_server_entry_checked(&server)
{
return Some(error.to_string());
}
}
None
}
#[derive(Default)]
pub struct Origin {
pub url: Option<String>,
pub path: Option<String>,
pub plugin: Option<String>,
pub commit: Option<String>,
}
impl Origin {
pub fn of_git(source: &PackGitSource) -> Self {
Self {
url: Some(source.url.clone()),
path: (!source.path.is_empty()).then(|| source.path.clone()),
plugin: source.plugin.clone(),
commit: Some(source.git_ref.clone()),
}
}
}
pub struct SourcePack {
pub directory: Option<TempDir>,
pub root: PathBuf,
pub notes: Vec<String>,
}
pub fn source_pack_of(checkout: &GitPackCheckout) -> Result<SourcePack> {
match &checkout.found {
CheckedOut::Tuff => Ok(SourcePack {
directory: None,
root: checkout.pack_root.clone(),
notes: Vec::new(),
}),
CheckedOut::ClaudePlugin(bundle) => {
let imported = import_plugin(
bundle,
checkout.version_hint.as_deref(),
Origin::of_git(&checkout.source),
)?;
let directory = tempfile::tempdir()?;
let output = directory.path().join("pack");
bundle::write_source_pack(&output, &imported)?;
Ok(SourcePack {
directory: Some(directory),
root: output,
notes: imported.notes,
})
}
}
}
pub struct ImportRequest<'a> {
pub source: &'a str,
pub plugin: Option<&'a str>,
pub git_ref: Option<&'a str>,
pub path: Option<&'a str>,
}
pub fn import(request: ImportRequest<'_>) -> Result<ImportedPack> {
let ImportRequest {
source,
plugin,
git_ref,
path,
} = request;
let imported = if tuff_core::git::is_git_url(source) {
let checkout = pack_git::checkout(CheckoutRequest {
url: source,
path,
git_ref,
plugin,
allow_remote: true,
})?;
let CheckedOut::ClaudePlugin(bundle) = &checkout.found else {
return Err(TuffError::usage(format!(
"{source} holds a Tuff pack already; install it with 'tuff add pack {source}'"
)));
};
let imported = import_plugin(
bundle,
checkout.version_hint.as_deref(),
Origin::of_git(&checkout.source),
)?;
drop(checkout);
imported
} else {
if git_ref.is_some() {
return Err(TuffError::usage(
"--ref applies to an import from a git address",
));
}
let mut root = PathBuf::from(source);
if let Some(path) = path {
root.push(bundle::bundle_path(path)?);
}
if !ClaudePlugin.detect(&root) {
return Err(TuffError::not_found(format!(
"no Claude Code plugin or marketplace at {}",
root.display()
))
.with_hint("a plugin has .claude-plugin/plugin.json; a marketplace has .claude-plugin/marketplace.json"));
}
match ClaudePlugin.locate(&root, plugin)? {
BundleLocation::Local(bundle) => import_plugin(
&bundle,
None,
Origin {
url: Some(root.display().to_string()),
plugin: bundle.name.clone(),
..Origin::default()
},
)?,
BundleLocation::Remote(remote) => {
let (guard, bundle, git) = pack_git::checkout_remote(&remote)?;
let imported = import_plugin(&bundle, None, Origin::of_git(&git))?;
drop(guard);
imported
}
}
};
Ok(imported)
}
pub fn unsupported_of(import: Option<&PackImport>) -> &[PackUnsupported] {
import
.map(|import| import.unsupported.as_slice())
.unwrap_or_default()
}