Files
Willieandsim 51107fb1ac fix(projects): save new projects in config.yaml again (#793)
* fix(projects): save new projects in config.yaml again

v0.29.0 wrote every project add, edit and remove to a machine-local
registry, so a project added on one machine never reached config.yaml
or the other machines that share it (#792). Most people share one
config.yaml and expect a new project to follow it.

config.yaml is the default again. Machine-local storage stays as an
explicit choice: --machine-local on add, or projects_storage:
machine-local for every new project, with --shared to override it.
The new share command moves a local project into config.yaml, and
localize still copies one to this machine. Edits and removals stay in
the layer that owns the project; removing a local copy of a
config.yaml project keeps config.yaml's version unless --shared asks
to remove both, so one machine cannot silently drop a project for
the others. Exclusions written by v0.29.0 are left as they are.

The dashboard picks storage once, in the Add project dialog, and
offers the two removal choices only where both copies exist. Moving a
project between layers stays a CLI command, so the detail page keeps
one job.

Closes #792

* fix(projects): recheck both layers after a confirmation, sync the saved key

remove and share ask before taking the config lock, then compared only
the merged entry after locking. A share or localize from another
terminal moves a project between layers without changing that entry, so
a remove confirmed for this machine's copy could then delete config.yaml's
declaration for every machine, and a confirmed share could overwrite
config.yaml settings changed meanwhile. The recheck now compares the
owning layer and config.yaml's declaration too (ProjectLayerState).

Re-adding a project that v0.29.0 hid restores config.yaml's own key,
which can be spelled differently from the folder given to add (~/app
versus an absolute path). add --sync looked the project up by the given
spelling and failed with "project not found" after saving it; it now
syncs the key that was saved.

* test(projects): make the layer tests hold on Windows

The mutation-sequence test looked the shared project up by
filepath.Join, but the fixture spells config.yaml's key $ROOT/app;
on Windows the two spellings differ, so the lookup found nothing.
It now asks the config for the key it uses.

The remove save-failure test blocked the local registry, but removing a
config.yaml project now writes config.yaml. On Linux the registry check
still failed with ENOTDIR first, which hid the mismatch; on Windows that
reads as a missing file and the save went through. The test now blocks
config.yaml, as the edit save-failure test does, and its hook no longer
shadows the config path with the target path.

---------

Co-authored-by: sim <sim@example.invalid>
2026-11-06 23:49:30 +08:00

134 lines
4.1 KiB
Go

package projects
import (
"errors"
"fmt"
"slices"
"skillshare/internal/config"
)
// Origins of a project, as Config.ProjectOrigin reports them. They also name
// the layer a new project is saved in.
const (
OriginShared = "shared"
OriginLocal = "local"
)
// ErrSharedDiffers stops Share from replacing different config.yaml settings
// unless the caller confirmed it.
var ErrSharedDiffers = errors.New("config.yaml declares this project with different settings")
// PrepareLayers validates an edit of both project layers. Affected lists the
// roots whose effective settings or origin change.
func PrepareLayers(path string, cfg *config.Config, edit func(*config.ProjectLayers) error) (*Candidate, error) {
c := &Candidate{}
if cfg == nil {
c.Err = fmt.Errorf("cannot prepare projects for a nil config")
return c, c.Err
}
c.Save, c.Err = config.PrepareProjectLayers(path, cfg, edit)
if c.Err != nil {
return c, c.Err
}
if c.Err = validateProjectFilters(c.Save.Expanded.Projects); c.Err != nil {
c.Save.Err = c.Err
return c, c.Err
}
after := c.Save.Expanded
c.Affected = affectedProjects(cfg.Projects, after.Projects)
for root := range cfg.Projects {
if _, kept := after.Projects[root]; !kept || slices.Contains(c.Affected, root) {
continue
}
before, _ := cfg.ProjectOrigin(root)
if now, _ := after.ProjectOrigin(root); now != before {
c.Affected = append(c.Affected, root)
}
}
slices.Sort(c.Affected)
return c, nil
}
// commit writes a prepared change unless it is a preview or changes no file.
func commit(c *Candidate, dryRun bool, result *Result, registration string) error {
if dryRun || !c.Save.Changed() {
return nil
}
if err := c.Save.Write(); err != nil {
return fmt.Errorf("%w: %w", ErrSave, err)
}
result.Registration = registration
return nil
}
func configItem(cfg *config.Config, root string, p config.ManagedProject) Item {
v := item(root, p)
v.Origin, v.OriginPath = cfg.ProjectOrigin(root)
v.SharedCopy = v.Origin == OriginLocal && cfg.HasSharedProject(root)
return v
}
// resultItem reports root after a change that may respell its key.
func resultItem(cfg *config.Config, root string) Item {
if key, ok := cfg.ProjectKey(root); ok {
root = key
}
return configItem(cfg, root, cfg.Projects[root])
}
// Localize copies a config.yaml project to this machine without changing
// config.yaml, detaching destinations or synchronizing files. Repeats are noops.
func (s *Service) Localize(key string, dryRun bool) (Result, error) {
cfg, err := s.load()
if err != nil {
return Result{}, err
}
root, err := lookup(cfg, key, false)
if err != nil {
return Result{}, err
}
c, err := PrepareLayers(s.path, cfg, func(l *config.ProjectLayers) error { l.SetLocal(root, cfg.Projects[root]); return nil })
if err != nil {
return Result{}, err
}
v := resultItem(c.Save.Expanded, root)
result := Result{Item: &v, Affected: c.Affected, Expanded: c.Save.Expanded, Registration: "kept"}
return result, commit(c, dryRun, &result, "updated")
}
// ShareOptions previews a move or confirms replacing different shared settings.
type ShareOptions struct{ DryRun, Replace bool }
// Share moves a project from this machine into config.yaml, the reverse of
// Localize. Its settings stay the same, so nothing is detached or synced.
func (s *Service) Share(key string, opts ShareOptions) (Result, error) {
cfg, err := s.load()
if err != nil {
return Result{}, err
}
root, err := lookup(cfg, key, false)
if err != nil {
return Result{}, err
}
entry := cfg.Projects[root]
origin, _ := cfg.ProjectOrigin(root)
c, err := PrepareLayers(s.path, cfg, func(l *config.ProjectLayers) error {
if origin != OriginLocal {
return nil
}
if shared, ok := l.SharedKey(root); ok && !opts.Replace && !config.SameManagedProject(l.Shared[shared], entry) {
return fmt.Errorf("%w: %s", ErrSharedDiffers, root)
}
l.SetShared(root, entry)
l.ClearLocal(root)
return nil
})
if err != nil {
return Result{}, err
}
v := resultItem(c.Save.Expanded, root)
result := Result{Item: &v, Affected: c.Affected, Expanded: c.Save.Expanded, Registration: "kept"}
return result, commit(c, opts.DryRun, &result, "updated")
}