Skip to content

Commit

Permalink
fix(serverless): fix serverless feature (#182)
Browse files Browse the repository at this point in the history
fix(serverless): fix serverless feature
  • Loading branch information
jaronnie committed Jan 9, 2025
1 parent 1479b92 commit c99ebef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
11 changes: 1 addition & 10 deletions internal/serverless/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package serverless
import (
"os"
"path/filepath"

"github.com/jzero-io/jzero-contrib/modx"
)

type Plugin struct {
Expand All @@ -13,22 +11,15 @@ type Plugin struct {
}

func GetPlugins() ([]Plugin, error) {
wd, _ := os.Getwd()

var plugins []Plugin
dir, err := os.ReadDir("plugins")
if err != nil {
return nil, err
}
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())),
})
}
}
Expand Down
12 changes: 10 additions & 2 deletions internal/serverless/serverlessbuild/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,15 +32,15 @@ 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 {
return err
}
}
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 {
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 43 additions & 13 deletions internal/serverless/serverlessdelete/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import (
"path/filepath"
"strings"

"github.com/jzero-io/jzero-contrib/modx"
"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))
})
}
Expand All @@ -33,8 +43,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 {
Expand All @@ -44,25 +54,45 @@ func Run() error {
if err = os.WriteFile("go.work", modfile.Format(work.Syntax), 0o644); 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")
}
}

for _, p := range plugins {
if _, err := os.Stat(p.Path); err == nil {
if err = os.RemoveAll(p.Path); err != nil {
return err
}
// write plugins/plugins.go
goMod, err := mod.GetGoMod(wd)
if err != nil {
return err
}

for i := 0; i < len(remainingPlugins); i++ {
pluginGoMod, err := modx.GetGoMod(filepath.Join(wd, remainingPlugins[i].Path))
if err != nil {
return err
}
remainingPlugins[i].Module = pluginGoMod.Path
}

// write plugins/plugins.go
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
}

0 comments on commit c99ebef

Please sign in to comment.