Files
skillshare/internal/sync/legacy_project.go
T
Willie 449fe40d6f fix(targets): move goose and openhands to .agents/skills
Both tools now document ~/.agents/skills and <project>/.agents/skills as
their skills locations and keep the old directories only for backward
compatibility. Writing to the old path next to `universal` makes them read
every skill from two places.

Global configs pin absolute paths, so they are unaffected and keep their
overlap warning. Project configs store target names only, so an existing
project follows the new default on its next sync while the links it created
in .goose/skills or .openhands/skills would stay behind and show up as
duplicates. Project sync now removes those leftovers from also_scans
directories that no configured target writes to.

That directory belongs to another tool, so the cleanup runs the prune with
ManagedOnly: only symlinks into the source and manifest-tracked folders are
removed. The flat-name heuristic would otherwise delete hand-made folders
such as `_drafts`. Targets with an explicit path are skipped, and --dry-run
only previews.

Refs #135
2026-09-21 02:55:18 +08:00

133 lines
4.2 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
}
// 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,
})
}
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 {
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 ""
}