Skip to content

Commit

Permalink
feat: add files updater plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Aug 11, 2020
1 parent 18e84ac commit 2fe7a31
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pkg/plugin/buildin/buildin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/go-semantic-release/semantic-release/pkg/provider"
"github.com/go-semantic-release/semantic-release/pkg/provider/github"
"github.com/go-semantic-release/semantic-release/pkg/provider/gitlab"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/go-semantic-release/semantic-release/pkg/updater/npm"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -71,6 +73,12 @@ func GetPluginCommands() []*cli.Command {
Hidden: true,
HideHelp: true,
},
{
Name: updater.FilesUpdaterPluginName + "_npm",
Action: npm.Main,
Hidden: true,
HideHelp: true,
},
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-semantic-release/semantic-release/pkg/condition"
"github.com/go-semantic-release/semantic-release/pkg/generator"
"github.com/go-semantic-release/semantic-release/pkg/provider"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
Expand Down Expand Up @@ -90,3 +91,11 @@ func StartProviderPlugin(opts *PluginOpts) (provider.Provider, error) {
}
return raw.(provider.Provider), nil
}

func StartFilesUpdaterPlugin(opts *PluginOpts) (updater.FilesUpdater, error) {
raw, err := startPlugin(opts)
if err != nil {
return nil, err
}
return raw.(updater.FilesUpdater), nil
}
10 changes: 10 additions & 0 deletions pkg/plugin/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-semantic-release/semantic-release/pkg/condition"
"github.com/go-semantic-release/semantic-release/pkg/generator"
"github.com/go-semantic-release/semantic-release/pkg/provider"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -36,7 +37,12 @@ func (p *GRPCWrapper) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) erro
provider.RegisterProviderPluginServer(s, &provider.Server{
Impl: p.Impl.(provider.Provider),
})
case updater.FilesUpdaterPluginName:
updater.RegisterFilesUpdaterPluginServer(s, &updater.FilesUpdaterServer{
Impl: p.Impl.(updater.FilesUpdater),
})
}

return nil
}

Expand All @@ -58,6 +64,10 @@ func (p *GRPCWrapper) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker,
return &provider.Client{
Impl: provider.NewProviderPluginClient(c),
}, nil
case updater.FilesUpdaterPluginName:
return &updater.FilesUpdaterClient{
Impl: updater.NewFilesUpdaterPluginClient(c),
}, nil
}
return nil, errors.New("unknown plugin type")
}
14 changes: 6 additions & 8 deletions pkg/plugin/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/go-semantic-release/semantic-release/pkg/plugin/buildin"
"github.com/go-semantic-release/semantic-release/pkg/provider"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/go-semantic-release/semantic-release/pkg/updater/npm"
)

type PluginManager struct {
Expand Down Expand Up @@ -55,29 +54,28 @@ func (m *PluginManager) GetProvider() (provider.Provider, error) {

func (m *PluginManager) GetCommitAnalyzer() (analyzer.CommitAnalyzer, error) {
ca, err := plugin.StartCommitAnalyzerPlugin(buildin.GetPluginOpts(analyzer.CommitAnalyzerPluginName))

if err != nil {
return nil, err
}

return ca, nil
}

func (m *PluginManager) GetChangelogGenerator() (generator.ChangelogGenerator, error) {
cg, err := plugin.StartChangelogGeneratorPlugin(buildin.GetPluginOpts(generator.ChangelogGeneratorPluginName))

if err != nil {
return nil, err
}

return cg, nil
}

func (m *PluginManager) GetUpdater() (updater.Updater, error) {
npmUpdater, err := plugin.StartFilesUpdaterPlugin(buildin.GetPluginOpts(updater.FilesUpdaterPluginName, "npm"))
if err != nil {
return nil, err
}

updater := &updater.ChainedUpdater{
Updaters: []updater.FilesUpdater{
&npm.Updater{},
},
Updaters: []updater.FilesUpdater{npmUpdater},
}
return updater, nil
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/go-semantic-release/semantic-release/pkg/condition"
"github.com/go-semantic-release/semantic-release/pkg/generator"
"github.com/go-semantic-release/semantic-release/pkg/provider"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/hashicorp/go-plugin"
)

Expand All @@ -17,12 +18,14 @@ type CommitAnalyzerFunc func() analyzer.CommitAnalyzer
type CIConditionFunc func() condition.CICondition
type ChangelogGeneratorFunc func() generator.ChangelogGenerator
type ProviderFunc func() provider.Provider
type FilesUpdaterFunc func() updater.FilesUpdater

type ServeOpts struct {
CommitAnalyzer CommitAnalyzerFunc
CICondition CIConditionFunc
ChangelogGenerator ChangelogGeneratorFunc
Provider ProviderFunc
FilesUpdater FilesUpdaterFunc
}

func Serve(opts *ServeOpts) {
Expand Down Expand Up @@ -56,6 +59,13 @@ func Serve(opts *ServeOpts) {
}
}

if opts.FilesUpdater != nil {
pluginSet[updater.FilesUpdaterPluginName] = &GRPCWrapper{
Type: updater.FilesUpdaterPluginName,
Impl: opts.FilesUpdater(),
}
}

plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: Handshake,
VersionedPlugins: map[int]plugin.PluginSet{
Expand Down
13 changes: 13 additions & 0 deletions pkg/updater/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"io/ioutil"
"os"
"path"

"github.com/go-semantic-release/semantic-release/pkg/plugin"
"github.com/go-semantic-release/semantic-release/pkg/updater"
"github.com/urfave/cli/v2"
)

const npmrc = "//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n"
Expand Down Expand Up @@ -65,3 +69,12 @@ func (u *Updater) Apply(file, newVersion string) error {

return err
}

func Main(c *cli.Context) error {
plugin.Serve(&plugin.ServeOpts{
FilesUpdater: func() updater.FilesUpdater {
return &Updater{}
},
})
return nil
}
Loading

0 comments on commit 2fe7a31

Please sign in to comment.