1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#![deny(clippy::disallowed_methods)]
mod config;
mod git;
mod lockfile;
mod marketplace;
mod paths;
use clap::{Parser, Subcommand};
use color_eyre::eyre::{Result, WrapErr, bail};
use tracing::info;
#[derive(Parser)]
#[command(
name = "pinch",
about = "A plugin manager for Claude Code and pi",
version = option_env!("PINCH_VERSION").unwrap_or("dev")
)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Install plugins from the lockfile
Install {
/// Run update before installing
#[arg(long)]
update: bool,
},
/// Update the lockfile from config
Update {
/// Install after updating
#[arg(long)]
install: bool,
/// Update only the named plugin(s)
names: Vec<String>,
},
}
enum Action {
Update(Vec<String>),
Install,
}
fn main() -> Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt::init();
let cli = Cli::parse();
let actions = match cli.command {
Command::Install { update } => {
let mut actions = vec![];
if update {
actions.push(Action::Update(vec![]));
}
actions.push(Action::Install);
actions
}
Command::Update { install, names } => {
let mut actions = vec![Action::Update(names)];
if install {
actions.push(Action::Install);
}
actions
}
};
for action in actions {
match action {
Action::Update(names) => do_update(&names)?,
Action::Install => do_install()?,
}
}
Ok(())
}
fn do_update(names: &[String]) -> Result<()> {
let all_plugins = config::load().wrap_err("failed to load config")?;
let (plugins, mut lockfile) = if names.is_empty() {
(
all_plugins,
lockfile::Lockfile {
plugins: Default::default(),
},
)
} else {
let known: std::collections::HashSet<&str> =
all_plugins.iter().map(|p| p.name.as_str()).collect();
let mut unknown: Vec<_> = names
.iter()
.filter(|n| !known.contains(n.as_str()))
.collect();
if !unknown.is_empty() {
unknown.sort();
bail!(
"unknown plugin(s): {}",
unknown
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(", ")
);
}
let requested: std::collections::HashSet<&str> = names.iter().map(|s| s.as_str()).collect();
let plugins = all_plugins
.into_iter()
.filter(|p| requested.contains(p.name.as_str()))
.collect();
let lockfile = lockfile::load()?;
(plugins, lockfile)
};
let repos_dir = paths::repos_dir()?;
for plugin in &plugins {
info!("updating {}...", plugin.name);
let repo_path = git::repo_path(&repos_dir, &plugin.src);
git::clone_or_fetch(&repo_path, &plugin.src)?;
let rev = git::resolve_ref(&repo_path, &plugin.version)?;
info!(" {} -> {}", plugin.version, &rev[..12]);
// Create worktree to read marketplace.json and discover skill path
let wt_path = git::ensure_worktree(&repo_path, &rev)?;
let source =
marketplace::find_plugin_source(&wt_path, &plugin.name, plugin.manifest.as_deref())?;
let path = source.trim_start_matches("./").to_string();
info!(" found at {}", path);
lockfile.plugins.insert(
plugin.name.clone(),
lockfile::LockedPlugin {
src: plugin.src.clone(),
path,
rev,
},
);
}
lockfile::save(&lockfile).wrap_err("failed to save lockfile")?;
info!("lockfile updated");
Ok(())
}
fn do_install() -> Result<()> {
let lockfile = lockfile::load().wrap_err("no lockfile found — run `pinch update` first")?;
let repos_dir = paths::repos_dir()?;
let skills_dir = paths::skills_dir()?;
let commands_dir = paths::commands_dir()?;
let cache_dir = paths::cache_dir();
let mut installed_skills = std::collections::HashSet::new();
let mut installed_commands = std::collections::HashSet::new();
for (name, plugin) in &lockfile.plugins {
let repo_path = git::repo_path(&repos_dir, &plugin.src);
let wt_path = git::ensure_worktree(&repo_path, &plugin.rev)?;
let plugin_root = wt_path.join(&plugin.path);
if !plugin_root.is_dir() {
bail!(
"plugin '{}': path '{}' does not exist in repo at rev {}",
name,
plugin.path,
&plugin.rev[..12]
);
}
reject_unsupported(name, &plugin_root)?;
let has_skills = symlink_entries(
&plugin_root,
"skills",
&skills_dir,
true,
&mut installed_skills,
)?;
let has_commands = symlink_entries(
&plugin_root,
"commands",
&commands_dir,
false,
&mut installed_commands,
)?;
if !has_skills && !has_commands {
bail!(
"plugin '{}': no skills/ or commands/ directory in '{}'",
name,
plugin.path
);
}
}
remove_stale_symlinks(&skills_dir, &cache_dir, &installed_skills, "skill")?;
remove_stale_symlinks(&commands_dir, &cache_dir, &installed_commands, "command")?;
git::prune(&repos_dir, &lockfile)?;
info!("install complete");
Ok(())
}
/// Bail if the plugin contains types we don't support yet.
fn reject_unsupported(name: &str, plugin_root: &std::path::Path) -> Result<()> {
for unsupported in ["agents", "hooks"] {
if plugin_root.join(unsupported).is_dir() {
bail!(
"plugin '{}': contains '{}/' — only skills and commands are supported",
name,
unsupported
);
}
}
for unsupported in [".mcp.json", ".lsp.json"] {
if plugin_root.join(unsupported).is_file() {
bail!(
"plugin '{}': contains '{}' — only skills and commands are supported",
name,
unsupported
);
}
}
Ok(())
}
/// Symlink entries from `plugin_root/<subdir>/` into `target_dir`.
/// When `dirs_only` is true, non-directory entries are skipped (skills).
/// Returns whether the source directory existed.
// TODO: maybe re-use frork-lib for symlink management in the future
fn symlink_entries(
plugin_root: &std::path::Path,
subdir: &str,
target_dir: &std::path::Path,
dirs_only: bool,
installed: &mut std::collections::HashSet<std::ffi::OsString>,
) -> Result<bool> {
let source = plugin_root.join(subdir);
if !source.is_dir() {
return Ok(false);
}
for entry in fs_err::read_dir(&source)? {
let entry = entry?;
if dirs_only && !entry.path().is_dir() {
continue;
}
let name = entry.file_name();
let symlink_path = target_dir.join(&name);
// Skip if the symlink already points to the right place
if let Ok(existing) = fs_err::read_link(&symlink_path) {
if existing == entry.path() {
info!("up-to-date {subdir} {}", name.to_string_lossy());
installed.insert(name);
continue;
}
fs_err::remove_file(&symlink_path)?;
}
std::os::unix::fs::symlink(entry.path(), &symlink_path).wrap_err_with(|| {
format!(
"failed to create symlink: {} -> {}",
symlink_path.display(),
entry.path().display()
)
})?;
info!(
"installed {subdir} {} -> {}",
name.to_string_lossy(),
entry.path().display()
);
installed.insert(name);
}
Ok(true)
}
/// Remove symlinks in `dir` that point into `cache_dir` but aren't in `keep`.
fn remove_stale_symlinks(
dir: &std::path::Path,
cache_dir: &std::path::Path,
keep: &std::collections::HashSet<std::ffi::OsString>,
kind: &str,
) -> Result<()> {
for entry in fs_err::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if !path.symlink_metadata()?.file_type().is_symlink() {
continue;
}
let name = entry.file_name();
if let Ok(target) = fs_err::read_link(&path)
&& target.starts_with(cache_dir)
&& !keep.contains(&name)
{
fs_err::remove_file(&path)?;
info!("removed stale {kind} symlink: {}", name.to_string_lossy());
}
}
Ok(())
}