feat(neovim): implement PlanForce

This commit is contained in:
MDietermann
2026-04-14 22:15:32 +02:00
parent 77b767aa0e
commit 25b4eefecd
2 changed files with 198 additions and 2 deletions

View File

@@ -6,7 +6,6 @@ package neovim
import (
"bytes"
"context"
"errors"
"embed"
"fmt"
"os"
@@ -463,6 +462,114 @@ func (m Module) Verify(ctx context.Context) error {
return nil
}
// PlanForce rewrites chadrc.lua and reinstalls packages but does NOT
// delete an existing NvChad clone, to preserve any user modifications.
// If NvChad is missing, it's cloned fresh.
func (m Module) PlanForce(ctx context.Context) (module.Plan, error) {
return module.Plan{}, errors.New("PlanForce not implemented")
cfg, err := nvimCfg()
if err != nil {
return module.Plan{}, err
}
rendered, err := m.renderChadrc()
if err != nil {
return module.Plan{}, err
}
var actions []module.Action
if m.pm != nil {
actions = append(actions, module.Action{
Description: "pacman -S (force) neovim",
Kind: module.ActionPackageInstall,
Exec: func(ctx context.Context) error { return m.pm.Reinstall(ctx, []string{"neovim"}) },
})
}
// Clone NvChad only if absent; preserve any user modifications otherwise.
repo := m.cfg.NvChadStarterRepo
actions = append(actions, module.Action{
Description: "ensure NvChad starter cloned (skip if present)",
Kind: module.ActionGitClone,
Exec: func(ctx context.Context) error {
if _, err := os.Stat(filepath.Join(cfg, "lua", "plugins", "init.lua")); err == nil {
return nil
}
if err := os.MkdirAll(filepath.Dir(cfg), 0o755); err != nil {
return err
}
if err := m.git.Clone(ctx, repo, cfg); err != nil {
return err
}
return os.RemoveAll(filepath.Join(cfg, ".git"))
},
})
actions = append(actions, module.Action{
Description: "strip nvim-lspconfig block from starter init.lua (force)",
Kind: module.ActionFileWrite,
Exec: func(ctx context.Context) error {
p := filepath.Join(cfg, "lua", "plugins", "init.lua")
data, err := os.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return platform.WriteUserFile(p, stripLspconfigBlock(data), 0o644)
},
})
actions = append(actions, module.Action{
Description: "remove starter lspconfig.lua (force)",
Kind: module.ActionFileWrite,
Exec: func(ctx context.Context) error {
err := os.Remove(filepath.Join(cfg, "lua", "configs", "lspconfig.lua"))
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
},
})
renderedCopy := append([]byte{}, rendered...)
themeName := m.resolvedTheme()
actions = append(actions, module.Action{
Description: "rewrite chadrc.lua with theme " + themeName + " (force)",
Kind: module.ActionFileWrite,
Exec: func(ctx context.Context) error {
if err := os.MkdirAll(filepath.Join(cfg, "lua"), 0o755); err != nil {
return err
}
return platform.WriteUserFile(filepath.Join(cfg, "lua", "chadrc.lua"), renderedCopy, 0o644)
},
})
for _, a := range assetMap {
a := a
actions = append(actions, module.Action{
Description: "rewrite asset " + a.DstRel + " (force)",
Kind: module.ActionFileWrite,
Exec: func(ctx context.Context) error {
data, err := assets.ReadFile(a.SrcPath)
if err != nil {
return err
}
return platform.WriteUserFile(filepath.Join(cfg, a.DstRel), data, 0o644)
},
})
}
actions = append(actions, module.Action{
Description: "clear NvChad base46 theme cache (force)",
Kind: module.ActionFileWrite,
Exec: func(ctx context.Context) error {
home, _ := os.UserHomeDir()
_ = os.RemoveAll(filepath.Join(home, ".local", "share", "nvim", "lazy", "base46", "lua", "base46", "themes"))
_ = os.RemoveAll(filepath.Join(home, ".cache", "nvim", "base46"))
return nil
},
})
return module.Plan{Actions: actions}, nil
}