Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automatic migrate the buf configs to v2 #4494

Merged
merged 8 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [#4289](https://github.com/ignite/cli/pull/4289), [#4423](https://github.com/ignite/cli/pull/4423), [#4432](https://github.com/ignite/cli/pull/4432) Cosmos SDK v0.52 support
- [#4477](https://github.com/ignite/cli/pull/4477) IBC v10 support
- [#4166](https://github.com/ignite/cli/issues/4166) Migrate buf config files to v2
- [#4494](https://github.com/ignite/cli/pull/4494) Automatic migrate the buf configs to v2

### Changes

Expand Down
23 changes: 21 additions & 2 deletions ignite/cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@ package ignitecmd
import (
"github.com/spf13/cobra"

chainconfig "github.com/ignite/cli/v29/ignite/config/chain"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/services/doctor"
)

func NewDoctor() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: "doctor",
Short: "Fix chain configuration",
Hidden: true,
RunE: func(cmd *cobra.Command, _ []string) error {
session := cliui.New()
defer session.End()
appPath := flagGetPath(cmd)

doc := doctor.New(doctor.CollectEvents(session.EventBus()))

if err := doc.MigrateConfig(cmd.Context()); err != nil {
cacheStorage, err := newCache(cmd)
if err != nil {
return err
}

configPath, err := chainconfig.LocateDefault(appPath)
if err != nil {
return err
}

if err := doc.MigrateChainConfig(configPath); err != nil {
return err
}

if err := doc.MigrateBufConfig(cmd.Context(), cacheStorage, appPath, configPath); err != nil {
return err
}

Expand All @@ -29,4 +45,7 @@ func NewDoctor() *cobra.Command {
return doc.FixDependencyTools(cmd.Context())
},
}

flagSetPath(c)
return c
}
17 changes: 17 additions & 0 deletions ignite/config/chain/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cosmos/cosmos-sdk/types/bech32"

"github.com/ignite/cli/v29/ignite/config/chain/defaults"
"github.com/ignite/cli/v29/ignite/config/chain/version"
"github.com/ignite/cli/v29/ignite/pkg/errors"
)
Expand Down Expand Up @@ -103,6 +104,22 @@ func ReadConfigVersion(configFile io.Reader) (version.Version, error) {
return c.Version, err
}

// ReadProtoPath reads the proto path.
func ReadProtoPath(configFile io.Reader) (string, error) {
c := struct {
Build struct {
Proto struct {
Path string `yaml:"path"`
} `yaml:"proto"`
} `yaml:"build"`
}{}

c.Build.Proto.Path = defaults.ProtoDir
err := yaml.NewDecoder(configFile).Decode(&c)

return c.Build.Proto.Path, err
}

func decodeConfig(r io.Reader, version version.Version) (version.Converter, error) {
c, ok := Versions[version]
if !ok {
Expand Down
37 changes: 34 additions & 3 deletions ignite/pkg/cosmosbuf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ const (
flagOutput = "output"
flagErrorFormat = "error-format"
flagLogFormat = "log-format"
flagWorkspace = "workspace"
flagBufGenYaml = "buf-gen-yaml"
flagIncludeImports = "include-imports"
flagIncludeWellKnownTypes = "include-wkt"
flagPath = "path"
fmtJSON = "json"
bufGenPrefix = "buf.gen."

// CMDGenerate generate command.
CMDGenerate Command = "generate"
CMDExport Command = "export"
CMDConfig Command = "config"
CMDDep Command = "dep"

specCacheNamespace = "generate.buf"
Expand All @@ -43,6 +47,7 @@ var (
commands = map[Command]struct{}{
CMDGenerate: {},
CMDExport: {},
CMDConfig: {},
CMDDep: {},
}

Expand Down Expand Up @@ -152,7 +157,7 @@ func (c Command) String() string {
// Update updates module dependencies.
// By default updates all dependencies unless one or more dependencies are specified.
func (b Buf) Update(ctx context.Context, modDir string) error {
files, err := xos.FindFilesExtension(modDir, xos.ProtoFile)
files, err := xos.FindFiles(modDir, xos.WithExtension(xos.ProtoFile))
if err != nil {
return err
}
Expand All @@ -167,9 +172,35 @@ func (b Buf) Update(ctx context.Context, modDir string) error {
return b.runCommand(ctx, cmd...)
}

// Migrate runs the buf Migrate command for the files in the app directory.
Pantani marked this conversation as resolved.
Show resolved Hide resolved
func (b Buf) Migrate(ctx context.Context, protoDir string) error {
yamlFiles, err := xos.FindFiles(protoDir, xos.WithExtension(xos.YAMLFile), xos.WithPrefix(bufGenPrefix))
if err != nil {
return err
}
ymlfiles, err := xos.FindFiles(protoDir, xos.WithExtension(xos.YMLFile), xos.WithPrefix(bufGenPrefix))
if err != nil {
return err
}
yamlFiles = append(yamlFiles, ymlfiles...)

flags := map[string]string{
flagWorkspace: ".",
}
if len(yamlFiles) > 0 {
flags[flagBufGenYaml] = strings.Join(yamlFiles, ",")
}

cmd, err := b.command(CMDConfig, flags, "migrate")
if err != nil {
return err
}
return b.runCommand(ctx, cmd...)
}

// Export runs the buf Export command for the files in the proto directory.
func (b Buf) Export(ctx context.Context, protoDir, output string) error {
files, err := xos.FindFilesExtension(protoDir, xos.ProtoFile)
files, err := xos.FindFiles(protoDir, xos.WithExtension(xos.ProtoFile))
if err != nil {
return err
}
Expand Down Expand Up @@ -202,7 +233,7 @@ func (b Buf) Generate(
}

// find all proto files into the path.
foundFiles, err := xos.FindFilesExtension(protoPath, xos.ProtoFile)
foundFiles, err := xos.FindFiles(protoPath, xos.WithExtension(xos.ProtoFile))
if err != nil || len(foundFiles) == 0 {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions ignite/pkg/cosmosgen/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/otiai10/copy"

"github.com/ignite/cli/v29/ignite/pkg/cosmosbuf"
"github.com/ignite/cli/v29/ignite/pkg/errors"
)

Expand All @@ -20,10 +19,6 @@ func (g *generator) protoPath() string {
}

func (g *generator) generateGoGo(ctx context.Context) error {
return g.generate(ctx, g.gogoTemplate(), g.goModPath)
}

func (g *generator) generate(ctx context.Context, template, fromPath string, excluded ...string) error {
// create a temporary dir to locate generated code under which later only some of them will be moved to the
// app's source code. this also prevents having leftover files in the app's source code or its parent dir - when
// command executed directly there - in case of an interrupt.
Expand All @@ -38,14 +33,13 @@ func (g *generator) generate(ctx context.Context, template, fromPath string, exc
ctx,
g.protoPath(),
tmp,
template,
cosmosbuf.ExcludeFiles(excluded...),
g.gogoTemplate(),
); err != nil {
return err
}

// move generated code for the app under the relative locations in its source code.
path := filepath.Join(tmp, fromPath)
path := filepath.Join(tmp, g.goModPath)
if _, err := os.Stat(path); err == nil {
err = copy.Copy(path, g.appPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/cosmosgen/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (g *generator) generateOpenAPISpec(ctx context.Context) error {
return errors.Wrapf(err, "failed to generate openapi spec %s, probally you need to exclude some proto files", protoPath)
}

specs, err := xos.FindFilesExtension(dir, xos.JSONFile)
specs, err := xos.FindFiles(dir, xos.WithExtension(xos.JSONFile))
if err != nil {
return err
}
Expand Down
53 changes: 39 additions & 14 deletions ignite/pkg/xos/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,64 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

const (
JSONFile = "json"
ProtoFile = "proto"
YAMLFile = "yaml"
YMLFile = "yml"
)

func FindFiles(directory string) ([]string, error) {
type findFileOptions struct {
extension string
prefix string
}

type FindFileOptions func(o *findFileOptions)

func WithExtension(extension string) FindFileOptions {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
return func(o *findFileOptions) {
o.extension = extension
}
}

func WithPrefix(prefix string) FindFileOptions {
return func(o *findFileOptions) {
o.prefix = prefix
}
}

// FindFiles searches for files in the specified directory based on the given options.
// It supports filtering files by extension and prefix. Returns a list of matching files or an error.
func FindFiles(directory string, options ...FindFileOptions) ([]string, error) {
opts := findFileOptions{}
for _, apply := range options {
apply(&opts)
}

files := make([]string, 0)
return files, filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
files = append(files, path)
// Filter by file extension if provided
if opts.extension != "" && filepath.Ext(path) != fmt.Sprintf(".%s", opts.extension) {
return nil // Skip files that don't match the extension
}
return nil
})
}

func FindFilesExtension(directory, extension string) ([]string, error) {
files := make([]string, 0)
return files, filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
// Filter by file prefix if provided
if opts.prefix != "" && !strings.HasPrefix(filepath.Base(path), opts.prefix) {
return nil // Skip files that don't match the prefix
}

// Add file to the result list if it is not a directory
if !info.IsDir() {
if filepath.Ext(path) == fmt.Sprintf(".%s", extension) {
files = append(files, path)
}
files = append(files, path)
}

return nil
})
}
Expand Down
Loading
Loading