mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Single-file extras (file + per-target as) already worked in the sync engine, but the only way to create one was editing config.yaml or the dashboard's AGENTS.md tab, so they looked like an AGENTS.md-only feature. Syncing a file such as Pi's APPEND_SYSTEM.md required hand-written config. - extras init gains --file and --as; the init TUI asks Folder or Single file. Validation runs before anything is written, and init neither creates the source file nor syncs. - extras list and add-target show the full source and target file paths. - POST /api/extras accepts file and per-target as, with the same connection checks as add-target. - The dashboard's first Extras tab becomes "Folders & files" and lists every extra except AGENTS.md files; Add extra can create a single file with a per-target file name. - Assigning shared AGENTS.md files now only attaches or detaches AGENTS.md extras. Before, saving on the AGENTS.md tab restored and removed any other single-file extra linked to the same instruction file. Refs #300
169 lines
5.4 KiB
Go
169 lines
5.4 KiB
Go
package instructions
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"skillshare/internal/config"
|
|
syncpkg "skillshare/internal/sync"
|
|
)
|
|
|
|
// Target is a target's global instruction file, the unit shared files attach to.
|
|
type Target struct {
|
|
Name string
|
|
File string // absolute path
|
|
Import bool // several shared files can attach, each as an @path line
|
|
}
|
|
|
|
// Resolver locates the files of extras: the source directory of an extra and
|
|
// the directory a configured target path means.
|
|
type Resolver struct {
|
|
SourceDir func(config.ExtraConfig) string
|
|
TargetDir func(string) string
|
|
}
|
|
|
|
// IsShared reports whether extra is a shared instruction file: a single-file
|
|
// extra whose file is AGENTS.md. Other single-file extras may target the same
|
|
// instruction files, but Assign and PlanAssign never attach or detach them.
|
|
func IsShared(extra config.ExtraConfig) bool { return extra.File == AgentsFile }
|
|
|
|
// Shared returns the shared instruction files among extras.
|
|
func Shared(extras []config.ExtraConfig) []config.ExtraConfig {
|
|
return slices.DeleteFunc(slices.Clone(extras), func(e config.ExtraConfig) bool { return !IsShared(e) })
|
|
}
|
|
|
|
// Assign makes want exactly the shared files attached to t, in that order of
|
|
// addition. Files no longer wanted are restored first (their link or import
|
|
// line removed, the replaced file put back); newly wanted ones are added to
|
|
// the extra's targets and synced. A target without import takes at most one.
|
|
// It returns the updated extras; on error the extras reflect the steps done.
|
|
func Assign(extras []config.ExtraConfig, t Target, want []string, r Resolver, warnings ...*[]syncpkg.FileWarning) ([]config.ExtraConfig, error) {
|
|
if _, err := PlanAssign(extras, t, want, r); err != nil {
|
|
return extras, err
|
|
}
|
|
|
|
var errs []string
|
|
for i := range extras {
|
|
if !IsShared(extras[i]) || slices.Contains(want, extras[i].Name) {
|
|
continue
|
|
}
|
|
j := targetIndex(extras[i], t.File, r)
|
|
if j == -1 {
|
|
continue
|
|
}
|
|
tc := extras[i].Targets[j]
|
|
f := syncpkg.NewExtraFile(r.SourceDir(extras[i]), extras[i].File, r.TargetDir(tc.Path), tc.As, tc.Mode)
|
|
if _, err := syncpkg.RestoreExtraTarget(f); err != nil {
|
|
errs = append(errs, err.Error())
|
|
continue
|
|
}
|
|
extras[i].Targets = slices.Delete(extras[i].Targets, j, j+1)
|
|
}
|
|
if len(errs) > 0 {
|
|
return extras, fmt.Errorf("could not restore %s: %s", t.Name, strings.Join(errs, "; "))
|
|
}
|
|
|
|
for _, name := range want {
|
|
i := indexOf(extras, name)
|
|
if targetIndex(extras[i], t.File, r) != -1 {
|
|
continue
|
|
}
|
|
tc := config.ExtraTargetConfig{Path: filepath.Dir(t.File), Mode: "symlink"}
|
|
if t.Import {
|
|
tc.Mode = "import"
|
|
}
|
|
if base := filepath.Base(t.File); base != extras[i].File {
|
|
tc.As = base
|
|
}
|
|
if err := config.ValidateExtraConfig(config.ExtraConfig{Name: name, File: extras[i].File, Targets: []config.ExtraTargetConfig{tc}}); err != nil {
|
|
return extras, err
|
|
}
|
|
f := syncpkg.NewExtraFile(r.SourceDir(extras[i]), extras[i].File, r.TargetDir(tc.Path), tc.As, tc.Mode)
|
|
result, err := syncpkg.SyncExtraFile(f, false, "")
|
|
if err != nil {
|
|
return extras, fmt.Errorf("attach %s to %s: %w", name, t.Name, err)
|
|
}
|
|
if len(warnings) > 0 {
|
|
*warnings[0] = append(*warnings[0], result.FileWarnings...)
|
|
}
|
|
if result.Skipped > 0 {
|
|
return extras, fmt.Errorf("%s", strings.Join(result.Warnings, "; "))
|
|
}
|
|
extras[i].Targets = append(extras[i].Targets, tc)
|
|
}
|
|
return extras, nil
|
|
}
|
|
|
|
// PlanAssign validates the complete desired ownership without changing files or
|
|
// the caller's config. API batches use it before making their first mutation.
|
|
func PlanAssign(extras []config.ExtraConfig, t Target, want []string, r Resolver) ([]config.ExtraConfig, error) {
|
|
if !t.Import && len(want) > 1 {
|
|
return nil, &config.ExtraTargetConflict{Name: want[0], Target: t.Name}
|
|
}
|
|
for _, name := range want {
|
|
if i := indexOf(extras, name); i == -1 || !IsShared(extras[i]) {
|
|
return nil, fmt.Errorf("shared instruction file %q not found", name)
|
|
}
|
|
}
|
|
next := slices.Clone(extras)
|
|
for i := range next {
|
|
next[i].Targets = slices.Clone(next[i].Targets)
|
|
if !IsShared(next[i]) {
|
|
continue
|
|
}
|
|
j := targetIndex(next[i], t.File, r)
|
|
if !slices.Contains(want, next[i].Name) {
|
|
if j != -1 {
|
|
next[i].Targets = slices.Delete(next[i].Targets, j, j+1)
|
|
}
|
|
} else if j == -1 {
|
|
mode := "symlink"
|
|
if t.Import {
|
|
mode = "import"
|
|
}
|
|
tc := config.ExtraTargetConfig{Path: filepath.Dir(t.File), As: filepath.Base(t.File), Mode: mode}
|
|
next[i].Targets = append(next[i].Targets, tc)
|
|
}
|
|
}
|
|
if err := config.ValidateExtraConnections(next, r.SourceDir, r.TargetDir); err != nil {
|
|
return nil, err
|
|
}
|
|
return next, nil
|
|
}
|
|
|
|
// Find returns the index of the named single-file extra's target that writes
|
|
// file, or -1.
|
|
func Find(extras []config.ExtraConfig, name, file string, r Resolver) (int, int) {
|
|
i := indexOf(extras, name)
|
|
if i == -1 || extras[i].File == "" {
|
|
return -1, -1
|
|
}
|
|
return i, targetIndex(extras[i], file, r)
|
|
}
|
|
|
|
// ExtraFile returns the sync view of an extra's j-th target.
|
|
func ExtraFile(extra config.ExtraConfig, j int, r Resolver) syncpkg.ExtraFile {
|
|
tc := extra.Targets[j]
|
|
return syncpkg.NewExtraFile(r.SourceDir(extra), extra.File, r.TargetDir(tc.Path), tc.As, tc.Mode)
|
|
}
|
|
|
|
func indexOf(extras []config.ExtraConfig, name string) int {
|
|
for i := range extras {
|
|
if extras[i].Name == name {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func targetIndex(extra config.ExtraConfig, file string, r Resolver) int {
|
|
for j := range extra.Targets {
|
|
if samePath(ExtraFile(extra, j, r).Target, file) {
|
|
return j
|
|
}
|
|
}
|
|
return -1
|
|
}
|