diff --git a/internal/serverless/serverless.go b/internal/serverless/serverless.go index 625a01b5..27f705c5 100644 --- a/internal/serverless/serverless.go +++ b/internal/serverless/serverless.go @@ -3,8 +3,6 @@ package serverless import ( "os" "path/filepath" - - "github.com/jzero-io/jzero-contrib/modx" ) type Plugin struct { @@ -13,8 +11,6 @@ type Plugin struct { } func GetPlugins() ([]Plugin, error) { - wd, _ := os.Getwd() - var plugins []Plugin dir, err := os.ReadDir("plugins") if err != nil { @@ -22,13 +18,8 @@ func GetPlugins() ([]Plugin, error) { } for _, p := range dir { if p.IsDir() { - goMod, err := modx.GetGoMod(filepath.Join(wd, "plugins", p.Name())) - if err != nil { - return nil, err - } plugins = append(plugins, Plugin{ - Path: filepath.ToSlash(filepath.Join("plugins", p.Name())), - Module: goMod.Path, + Path: filepath.ToSlash(filepath.Join("plugins", p.Name())), }) } } diff --git a/internal/serverless/serverlessbuild/run.go b/internal/serverless/serverlessbuild/run.go index a6c0580b..02ac0517 100644 --- a/internal/serverless/serverlessbuild/run.go +++ b/internal/serverless/serverlessbuild/run.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" + "github.com/jzero-io/jzero-contrib/modx" "github.com/jzero-io/jzero-contrib/templatex" "github.com/pkg/errors" "github.com/rinchsan/gosimports" @@ -31,7 +32,7 @@ func Run() error { return err } for _, p := range plugins { - if !strings.HasPrefix(p.Module, "./") { + if !strings.HasPrefix(p.Path, "./") { p.Path = "./" + p.Path } if err = work.DropUse(p.Path); err != nil { @@ -39,7 +40,7 @@ func Run() error { } } for _, p := range plugins { - if !strings.HasPrefix(p.Module, "./") { + if !strings.HasPrefix(p.Path, "./") { p.Path = "./" + p.Path } if err = work.AddUse(p.Path, ""); err != nil { @@ -67,6 +68,13 @@ func Run() error { if err != nil { return err } + for i := 0; i < len(plugins); i++ { + pluginGoMod, err := modx.GetGoMod(filepath.Join(wd, plugins[i].Path)) + if err != nil { + return err + } + plugins[i].Module = pluginGoMod.Path + } pluginsGoBytes, err := templatex.ParseTemplate(map[string]any{ "Plugins": plugins, "Module": goMod.Path, diff --git a/internal/serverless/serverlessdelete/run.go b/internal/serverless/serverlessdelete/run.go index 1f834741..1f336f6c 100644 --- a/internal/serverless/serverlessdelete/run.go +++ b/internal/serverless/serverlessdelete/run.go @@ -6,23 +6,32 @@ import ( "strings" "github.com/jzero-io/jzero-contrib/templatex" + "github.com/rinchsan/gosimports" "github.com/samber/lo" "golang.org/x/mod/modfile" "github.com/jzero-io/jzero/config" "github.com/jzero-io/jzero/embeded" "github.com/jzero-io/jzero/internal/serverless" - "github.com/jzero-io/jzero/internal/serverless/serverlessbuild" + "github.com/jzero-io/jzero/pkg/mod" ) func Run() error { + wd, _ := os.Getwd() + plugins, err := serverless.GetPlugins() if err != nil { return err } + deletePlugins := plugins + var remainingPlugins []serverless.Plugin + for _, p := range config.C.Serverless.Delete.Plugin { - plugins = lo.Reject(plugins, func(item serverless.Plugin, index int) bool { + deletePlugins = lo.Reject(plugins, func(item serverless.Plugin, index int) bool { + return item.Path != filepath.ToSlash(filepath.Join("plugins", p)) + }) + remainingPlugins = lo.Filter(plugins, func(item serverless.Plugin, index int) bool { return item.Path != filepath.ToSlash(filepath.Join("plugins", p)) }) } @@ -33,8 +42,8 @@ func Run() error { if err != nil { return err } - for _, p := range plugins { - if !strings.HasPrefix(p.Module, "./") { + for _, p := range deletePlugins { + if !strings.HasPrefix(p.Path, "./") { p.Path = "./" + p.Path } if err = work.DropUse(p.Path); err != nil { @@ -44,25 +53,37 @@ func Run() error { if err = os.WriteFile("go.work", modfile.Format(work.Syntax), 0o644); err != nil { return err } - } - - for _, p := range plugins { - if _, err := os.Stat(p.Path); err == nil { - if err = os.RemoveAll(p.Path); err != nil { - return err - } + // reread + goWork, _ = os.ReadFile("go.work") + work, err = modfile.ParseWork("", goWork, nil) + if err != nil { + return err + } + if (len(work.Use) == 0) || (len(work.Use) == 1 && work.Use[0].Path == ".") { + _ = os.Remove("go.work") + _ = os.Remove("go.work.sum") } } // write plugins/plugins.go + goMod, err := mod.GetGoMod(wd) + if err != nil { + return err + } + pluginsGoBytes, err := templatex.ParseTemplate(map[string]any{ - "Plugins": plugins, + "Plugins": remainingPlugins, + "Module": goMod.Path, }, embeded.ReadTemplateFile(filepath.ToSlash(filepath.Join("plugins", "api", "serverless_plugins.go.tpl")))) if err != nil { return err } - if err := os.WriteFile(filepath.Join("plugins", "plugins.go"), pluginsGoBytes, 0o644); err != nil { + formatBytes, err := gosimports.Process("", pluginsGoBytes, &gosimports.Options{Comments: true}) + if err != nil { + return err + } + if err := os.WriteFile(filepath.Join("plugins", "plugins.go"), formatBytes, 0o644); err != nil { return err } - return serverlessbuild.Run() + return nil }