From 77b767aa0e58ab6ef3426e664ca10c54dd025916 Mon Sep 17 00:00:00 2001 From: MDietermann Date: Tue, 14 Apr 2026 22:07:48 +0200 Subject: [PATCH] feat(themes): implement PlanForce --- internal/modules/themes/themes.go | 43 ++++++++++++++++++++++-- internal/modules/themes/themes_test.go | 45 ++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/internal/modules/themes/themes.go b/internal/modules/themes/themes.go index 283f358..5f6bd80 100644 --- a/internal/modules/themes/themes.go +++ b/internal/modules/themes/themes.go @@ -4,7 +4,6 @@ package themes import ( "bytes" "context" - "errors" "fmt" "io/fs" "os" @@ -116,5 +115,45 @@ func (m Module) Verify(ctx context.Context) error { } func (m Module) PlanForce(ctx context.Context) (module.Plan, error) { - return module.Plan{}, errors.New("PlanForce not implemented") + dst, err := destRoot() + if err != nil { + return module.Plan{}, err + } + var pairs []filePair + err = filepath.WalkDir(m.cfg.Source, func(p string, d fs.DirEntry, werr error) error { + if werr != nil { + return werr + } + if d.IsDir() { + return nil + } + rel, _ := filepath.Rel(m.cfg.Source, p) + pairs = append(pairs, filePair{src: p, dst: filepath.Join(dst, rel)}) + return nil + }) + if err != nil && !os.IsNotExist(err) { + return module.Plan{}, err + } + if len(pairs) == 0 { + return module.Plan{}, nil + } + return module.Plan{Actions: []module.Action{{ + Description: fmt.Sprintf("rewrite %d theme files (force)", len(pairs)), + Kind: module.ActionFileWrite, + Exec: func(ctx context.Context) error { + for _, fp := range pairs { + if err := os.MkdirAll(filepath.Dir(fp.dst), 0o755); err != nil { + return err + } + data, err := os.ReadFile(fp.src) + if err != nil { + return err + } + if err := os.WriteFile(fp.dst, data, 0o644); err != nil { + return err + } + } + return nil + }, + }}}, nil } diff --git a/internal/modules/themes/themes_test.go b/internal/modules/themes/themes_test.go index d5a7e2c..ff8548a 100644 --- a/internal/modules/themes/themes_test.go +++ b/internal/modules/themes/themes_test.go @@ -63,4 +63,49 @@ func applyOnce(t *testing.T, m Module) error { return m.Apply(context.Background(), p, nop{}) } +func TestPlanForceRewritesAllThemes(t *testing.T) { + src := t.TempDir() + os.WriteFile(filepath.Join(src, "one.toml"), []byte("FRESH-A"), 0o644) + os.MkdirAll(filepath.Join(src, "sub"), 0o755) + os.WriteFile(filepath.Join(src, "sub", "two.toml"), []byte("FRESH-B"), 0o644) + + home := t.TempDir() + t.Setenv("HOME", home) + m := New(config.ThemesConfig{Source: src}) + dst := filepath.Join(home, ".config", "archduino", "themes") + os.MkdirAll(filepath.Join(dst, "sub"), 0o755) + os.WriteFile(filepath.Join(dst, "one.toml"), []byte("FRESH-A"), 0o644) + os.WriteFile(filepath.Join(dst, "sub", "two.toml"), []byte("FRESH-B"), 0o644) + + st, _ := m.Detect(context.Background()) + p, _ := m.Plan(context.Background(), st) + if !p.IsEmpty() { + t.Fatalf("precondition: plan should be empty, got %+v", p.Actions) + } + + os.WriteFile(filepath.Join(dst, "one.toml"), []byte("STALE"), 0o644) + os.WriteFile(filepath.Join(dst, "sub", "two.toml"), []byte("STALE"), 0o644) + + plan, err := m.PlanForce(context.Background()) + if err != nil { + t.Fatalf("PlanForce: %v", err) + } + if plan.IsEmpty() { + t.Fatal("force plan must not be empty") + } + for _, a := range plan.Actions { + if err := a.Exec(context.Background()); err != nil { + t.Fatalf("Exec: %v", err) + } + } + got, _ := os.ReadFile(filepath.Join(dst, "one.toml")) + if string(got) != "FRESH-A" { + t.Fatalf("one.toml not rewritten: %q", got) + } + got, _ = os.ReadFile(filepath.Join(dst, "sub", "two.toml")) + if string(got) != "FRESH-B" { + t.Fatalf("two.toml not rewritten: %q", got) + } +} + var _ module.Module = Module{}