Files
homeagent-sdk/tools/hmapdev/replace.go
JianFeeeee b237787c90 refactor(toolchain)!: 工具链 plugindev 更名为 hmapdev,module path 改回 gitcode
- 目录 tools/plugindev → tools/hmapdev,可执行文件名/平台产物名同步
  (hmapdev_linux_amd64 等;包格式仍叫 .hmap)
- module path github.com/JianFeeeee/homeagent-sdk/tools/... → gitcode.com/...
  (与仓库实际托管一致;核心仓不依赖该 path,改动无外部影响)
- SDK 存储目录 ~/.homeagent/plugindev/sdk → ~/.homeagent/hmapdev/sdk
  新目录不存在而旧目录存在时沿用旧目录 → 已装 SDK 版本不会丢失
- 命令表/usage/--help/生成项目 README/示例 README/NSIS 安装器/
  package/build.sh/build-examples.sh 全部同步;PLUGINDEV 环境变量保留兼容
- sdk/ 目录零改动(公开接口不变)

验证:
- go build ./... ok;go test ./tools/hmapdev/ ok(含模板接线守卫 TestProcTemplate_CoversAllCoreMethods)
- bash -n package/{build,build-examples}.sh ok
- 端到端:hmapdev init demo && hmapdev build → dist/demo_bundle.hmap(linux+darwin)
- 本机安装 /usr/local/bin/hmapdev,旧名以软链保留;sdk list/current 正常
2026-09-12 12:30:20 +08:00

78 lines
1.7 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
type GoModPatcher struct {
dir string
replaces []string
backup string
}
func NewGoModPatcher(dir string, replaces []string) *GoModPatcher {
return &GoModPatcher{dir: dir, replaces: replaces}
}
func (p *GoModPatcher) Apply() (func(), error) {
if len(p.replaces) == 0 {
return func() {}, nil
}
gomodPath := filepath.Join(p.dir, "go.mod")
data, err := os.ReadFile(gomodPath)
if err != nil {
return func() {}, fmt.Errorf("read go.mod: %w", err)
}
p.backup = string(data)
var sb strings.Builder
sb.WriteString(strings.TrimRight(string(data), "\r\n"))
sb.WriteString("\n")
for _, r := range p.replaces {
from, to, found := strings.Cut(r, "=")
if !found {
fmt.Printf(" warn: invalid replace %q, skipping\n", r)
continue
}
from = strings.TrimSpace(from)
to = strings.TrimSpace(to)
absTo, err := filepath.Abs(to)
if err != nil {
fmt.Printf(" warn: resolve path %q: %v, skipping\n", to, err)
continue
}
absTo = strings.ReplaceAll(absTo, "\\", "/")
sb.WriteString(fmt.Sprintf("replace %s => %s\n", from, absTo))
}
if err := os.WriteFile(gomodPath, []byte(sb.String()), 0644); err != nil {
return func() {}, fmt.Errorf("write go.mod: %w", err)
}
return p.restore, nil
}
func (p *GoModPatcher) restore() {
if p.backup == "" {
return
}
gomodPath := filepath.Join(p.dir, "go.mod")
os.WriteFile(gomodPath, []byte(p.backup), 0644)
p.backup = ""
}
func (p *GoModPatcher) ReplaceDirs() []string {
var dirs []string
for _, r := range p.replaces {
_, to, found := strings.Cut(r, "=")
if found {
dirs = append(dirs, strings.TrimSpace(to))
}
}
return dirs
}