Files
skillshare/internal/sync/legacy_project.go
T
Willie 46b23cd2ad feat(targets): let a target keep its instructions with skills switched off
Some tools only need skillshare for AGENTS.md, MCP or agents, while their
skills folder is managed elsewhere or shared through another target.
Syncing skills into those folders duplicated what the tool already reads,
so a target can now turn skills off (`target <name> --no-skills`, the
dashboard switch) and sync, diff, status and doctor skip its skills.
Switching off warns about the other tools that read the same folder.

The target list also gains tools that were missing, with colored vendor
logos where their licenses allow it, and zencoder/replit default paths
follow their current documentation.

The dashboard's target pages get the matching UI: universal is pinned as
the shared folder when adding a target, the add dialog lists what will be
written, and the AGENTS.md tab shows where the file comes from in one card
with an in-place switch between shared files.
2026-09-29 04:51:24 +08:00

137 lines
4.4 KiB
Go

package sync
import (
"fmt"
"os"
"path/filepath"
"strings"
"skillshare/internal/config"
"skillshare/internal/utils"
)
// MovedTarget describes one configured project target for legacy-directory cleanup.
type MovedTarget struct {
Name string // canonical target name, used for the also_scans lookup
Path string // resolved skills path this target writes to
Pinned bool // the project config pinned an explicit path
TargetNaming string
// Disabled marks a target with skills off: its path stays protected from
// other targets' sweeps, but it starts none of its own.
Disabled bool
}
// MovedProjectTargets pairs each project config entry with its resolved target,
// which is how CleanMovedProjectDirs learns both the path a target writes to and
// whether the config pinned it.
func MovedProjectTargets(cfg *config.ProjectConfig, resolved map[string]config.TargetConfig) []MovedTarget {
if cfg == nil {
return nil
}
targets := make([]MovedTarget, 0, len(cfg.Targets))
for _, entry := range cfg.Targets {
target, ok := resolved[entry.Name]
if !ok {
continue
}
sc := target.SkillsConfig()
targets = append(targets, MovedTarget{
Name: entry.Name,
Path: sc.Path,
Pinned: strings.TrimSpace(entry.SkillsConfig().Path) != "",
TargetNaming: sc.TargetNaming,
Disabled: !sc.IsEnabled(),
})
}
return targets
}
// CleanMovedProjectDirs removes skillshare-managed entries that stayed behind in
// a target's also_scans directories after its default project path moved (e.g.
// goose from .goose/skills to .agents/skills). Without this the runtime reads
// both directories and shows every skill twice.
//
// Only directories no configured target writes to are considered, and a target
// with a pinned path is skipped entirely — the user chose that path, so nothing
// moved under them. Inside a directory the prune functions run with an empty
// skills list, so they remove exactly the entries skillshare owns (symlinks into
// the project source and manifest-tracked copies) and leave local folders and
// external symlinks alone.
//
// Returns one message per cleaned directory, for the caller to render.
func CleanMovedProjectDirs(root, sourcePath string, targets []MovedTarget, dryRun bool) []string {
owned := make(map[string]bool, len(targets))
for _, t := range targets {
if t.Path != "" {
owned[filepath.Clean(t.Path)] = true
}
}
var messages []string
cleaned := make(map[string]bool)
for _, t := range targets {
if t.Pinned || t.Disabled {
continue
}
for _, rel := range config.AlsoScansProject(t.Name) {
dir := filepath.Clean(filepath.Join(root, filepath.FromSlash(rel)))
if owned[dir] || cleaned[dir] {
continue
}
// Lstat, never Stat: if the directory is itself a symlink, symlink
// mode pointed it at the source and pruning would walk into the
// source skills.
// ponytail: symlink-mode leftovers are left in place; neither prune
// function can remove a whole-directory link safely.
info, err := os.Lstat(dir)
if err != nil || !info.IsDir() || utils.IsSymlinkOrJunction(dir) {
continue
}
cleaned[dir] = true
// A dry run leaves both passes reporting the same entry, so count names.
removed := make(map[string]bool)
// ManagedOnly: this directory belongs to another tool, so a folder or
// link is ours only if it points into the source or is in the manifest.
if r, err := PruneOrphanLinksWithSkills(PruneOptions{
TargetPath: dir,
SourcePath: sourcePath,
TargetNaming: t.TargetNaming,
TargetName: t.Name,
DryRun: dryRun,
ManagedOnly: true,
}); err == nil {
for _, name := range r.Removed {
removed[name] = true
}
}
// Sweeps manifest entries whose directory is already gone.
if r, err := PruneOrphanCopiesWithSkills(dir, nil, nil, nil, t.Name, t.TargetNaming, dryRun); err == nil {
for _, name := range r.Removed {
removed[name] = true
}
}
if len(removed) > 0 {
verb := "Cleaned"
if dryRun {
verb = "Would clean"
}
messages = append(messages, fmt.Sprintf(
"%s %d leftover skill(s) from %s: the default path for '%s' moved to %s",
verb, len(removed), rel, t.Name, defaultProjectSkillsPath(t.Name)))
}
}
}
return messages
}
func defaultProjectSkillsPath(name string) string {
if known, ok := config.LookupProjectTarget(name); ok {
return known.Path
}
return ""
}