From 8efccb9a5325525196ae8adad08f1d5eb29dc403 Mon Sep 17 00:00:00 2001 From: James Fargher Date: Tue, 14 Mar 2023 15:21:46 +1300 Subject: module-updater: Extract individual file import updating Previously there was a lot of file iteration logic mixed with error handling. Here we extract a helper so that the logic is clearly separated. --- tools/module-updater/main.go | 58 +++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 28 deletions(-) (limited to 'tools/module-updater/main.go') diff --git a/tools/module-updater/main.go b/tools/module-updater/main.go index d8cfbda1e..b7c897fc2 100644 --- a/tools/module-updater/main.go +++ b/tools/module-updater/main.go @@ -161,48 +161,50 @@ func rewriteImports(moduleAbsRootPath, prev, next string) error { } seen[file.Name()] = struct{}{} - astFile, err := parser.ParseFile(fileSet, file.Name(), nil, parser.ParseComments) - if err != nil { + if err := rewriteFileImports(fileSet, file.Name(), prev, next); err != nil { rerr = err return false } - for _, imprt := range astFile.Imports { - oldImport, err := strconv.Unquote(imprt.Path.Value) - if err != nil { - rerr = fmt.Errorf("unquote import: %s :%w", imprt.Path.Value, err) - return false - } + return true + }) - if newImport := strings.Replace(oldImport, prev, next, 1); newImport != oldImport { - imprt.EndPos = imprt.End() - imprt.Path.Value = strconv.Quote(newImport) - } - } + return rerr +} + +func rewriteFileImports(fileSet *token.FileSet, filename, prev, next string) (retErr error) { + astFile, err := parser.ParseFile(fileSet, filename, nil, parser.ParseComments) + if err != nil { + return err + } - f, err := os.Create(file.Name()) + for _, imprt := range astFile.Imports { + oldImport, err := strconv.Unquote(imprt.Path.Value) if err != nil { - rerr = fmt.Errorf("open file %q: %w", file.Name(), err) - return false + return fmt.Errorf("unquote import: %s :%w", imprt.Path.Value, err) } - ferr := format.Node(f, fileSet, astFile) - cerr := f.Close() - - if ferr != nil { - rerr = fmt.Errorf("rewrite file %q: %w", file.Name(), err) - return false + if newImport := strings.Replace(oldImport, prev, next, 1); newImport != oldImport { + imprt.EndPos = imprt.End() + imprt.Path.Value = strconv.Quote(newImport) } + } - if cerr != nil { - rerr = fmt.Errorf("close file %q: %w", file.Name(), err) - return false + f, err := os.Create(filename) + if err != nil { + return fmt.Errorf("open file %q: %w", filename, err) + } + defer func() { + if err := f.Close(); retErr == nil && err != nil { + retErr = fmt.Errorf("close file %q: %w", filename, err) } + }() - return true - }) + if err := format.Node(f, fileSet, astFile); err != nil { + return fmt.Errorf("rewrite file %q: %w", filename, err) + } - return rerr + return nil } func normaliseDesiredImportReplacement(module, from, to string) (string, string, error) { -- cgit v1.2.3 From 857860476afcd862a11f55c5da81d33e44e9e67b Mon Sep 17 00:00:00 2001 From: James Fargher Date: Wed, 15 Mar 2023 11:11:41 +1300 Subject: module-updater: Relax proto package matcher to catch test protos --- tools/module-updater/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/module-updater/main.go') diff --git a/tools/module-updater/main.go b/tools/module-updater/main.go index b7c897fc2..4366fcebc 100644 --- a/tools/module-updater/main.go +++ b/tools/module-updater/main.go @@ -291,7 +291,7 @@ func rewriteProto(moduleAbsRootPath, prev, next string) error { for _, line := range lines { tokens := bytes.Fields(line) pckg := bytes.Join(tokens, []byte{'~'}) - if !bytes.HasPrefix(pckg, []byte(`option~go_package~=~"`+prev+`/proto/go/gitalypb";`)) { + if !bytes.HasPrefix(pckg, []byte(`option~go_package~=~"`+prev+`/proto/`)) { modified = append(modified, line) continue } -- cgit v1.2.3