Permalink
Browse files

fixed ngorf issue, impl merge

  • Loading branch information...
1 parent d9dde83 commit ac6f2197765d997211f16461c630a28bd055681a @skelterjohn committed Apr 2, 2011
Showing with 124 additions and 3 deletions.
  1. +4 −1 field.go
  2. +1 −0 gorf.go
  3. +105 −1 merge.go
  4. +1 −1 scan.go
  5. +13 −0 util.go
View
@@ -29,7 +29,10 @@ func FieldCmd(args []string) (err os.Error) {
return MakeErr("Old name and new name are the same")
}
- ScanAllForImports(LocalRoot)
+ err = ScanAllForImports(LocalRoot)
+ if err != nil {
+ return
+ }
defer func() {
if err != nil {
View
@@ -43,6 +43,7 @@ func main() {
"pkg" : PkgCmd,
"rename" : RenameCmd,
"move" : MoveCmd,
+ "merge" : MergeCmd,
"scan" : ScanCmd,
}
View
106 merge.go
@@ -2,12 +2,116 @@ package main
import (
"os"
+ "fmt"
+ "go/ast"
+ "path/filepath"
)
func MergeCmd(args []string) (err os.Error) {
if len(args) != 2 {
return MakeErr("Usage: gorf [flags] merge <old path> <new path>")
}
- return MakeErr("not implemented yet")
+ oldpath, newpath := args[0], args[1]
+
+ defer func() {
+ if err != nil {
+ UndoCmd([]string{})
+ }
+ }()
+
+ err = ScanAllForImports(LocalRoot)
+ if err != nil {
+ return
+ }
+
+ if PackageTops[oldpath] == nil {
+ return MakeErr("Old path '%s' contains no package", oldpath)
+ }
+
+ if PackageTops[newpath] == nil {
+ return MakeErr("Old path '%s' contains no package", newpath)
+ }
+
+ oldpkg, newpkg := LocalImporter(oldpath), LocalImporter(newpath)
+
+ // check for conflicts
+ duplicates := false
+ for name, oldobj := range oldpkg.Scope.Objects {
+ if oldobj.Decl == nil {
+ continue
+ }
+ if newobj, ok := newpkg.Scope.Objects[name]; ok && newobj.Decl != nil {
+
+
+ duplicates = true
+
+ fmt.Printf("Duplicate name %s\n", name)
+ if oldNode, oldOk := oldobj.Decl.(ast.Node); oldOk {
+ fmt.Printf(" %s\n", AllSources.Position(oldNode.Pos()))
+ } else {
+ fmt.Printf("%T\n", oldobj.Decl)
+ }
+ if newNode, newOk := newobj.Decl.(ast.Node); newOk {
+ fmt.Printf(" %s\n", AllSources.Position(newNode.Pos()))
+ } else {
+ fmt.Printf("%T\n", newobj.Decl)
+ }
+ }
+ }
+ if duplicates {
+ return MakeErr("Packages in '%s' and '%s' contain duplicate names", oldpath, newpath)
+ }
+
+ //move source files
+ for fpath := range oldpkg.Files {
+ _, fname := filepath.Split(fpath)
+ npath := GetUniqueFilename(newpkg, filepath.Join(newpath, fname))
+
+ err = MoveSource(fpath, npath)
+ if err != nil {
+ return
+ }
+ }
+
+ //update imports
+ for _, ipath := range ImportedBy[QuotePath(oldpath)] {
+ pkg := LocalImporter(ipath)
+ for fpath, file := range pkg.Files {
+ ir := ImportRepath {
+ OldName : oldpkg.Name,
+ OldPath : oldpath,
+ NewPath : newpath,
+ }
+ ast.Walk(&ir, file)
+ if ir.Updated {
+ err = RewriteSource(fpath, file)
+ if err != nil {
+ return
+ }
+ }
+ }
+ }
+
+ return// MakeErr("not implemented yet")
+}
+
+type ImportRepath struct {
+ OldName string
+ OldPath, NewPath string
+ Updated bool
+}
+
+func (this *ImportRepath) Visit(node ast.Node) ast.Visitor {
+ if is, ok := node.(*ast.ImportSpec); ok {
+ if is.Path.Value == QuotePath(this.OldPath) {
+ is.Path.Value = QuotePath(this.NewPath)
+ if is.Name == nil {
+ is.Name = &ast.Ident{ Name : this.OldName }
+ }
+ this.Updated = true
+ return nil
+ }
+ }
+ return this
}
View
@@ -76,7 +76,7 @@ func (s *ScanWalker) VisitDir(path string, f *os.FileInfo) bool {
}
func (s *ScanWalker) VisitFile(fpath string, f *os.FileInfo) {
- if strings.HasSuffix(fpath, ".gorg") || strings.HasSuffix(fpath, ".ngorg") {
+ if strings.HasSuffix(fpath, ".gorf") || strings.HasSuffix(fpath, ".gorfn") {
os.Remove(fpath)
}
}
View
13 util.go
@@ -9,6 +9,7 @@ import (
"unicode"
"utf8"
"go/ast"
+ "path/filepath"
"fmt"
"rog-go.googlecode.com/hg/exp/go/types"
)
@@ -79,6 +80,18 @@ func GetUniqueIdent(files []*ast.File, candidate string) (id string) {
return
}
+func GetUniqueFilename(pkg *ast.Package, candidate string) (fname string) {
+ ext := filepath.Ext(candidate)
+ base := candidate[:len(candidate)-len(ext)]
+
+ fname = candidate
+ for i:=0; pkg.Files[fname] != nil; i++ {
+ fname = fmt.Sprintf("%s_%d%s", base, i, ext)
+ }
+
+ return
+}
+
type IdentCollector map[string]bool
func (this IdentCollector) Visit(node ast.Node) ast.Visitor {

0 comments on commit ac6f219

Please sign in to comment.