refactor/rename: attempt to fix windows build...
...by using buildutil.ContainingPackage instead of guessImportPath. The former should be more portable. (I meant to do this earlier, so this is also a nice cleanup.) LGTM=gri R=gri, sameer CC=golang-codereviews https://golang.org/cl/148050043
This commit is contained in:
parent
717118b6c8
commit
a69ece7231
|
|
@ -115,12 +115,12 @@ func parseFromFlag(ctxt *build.Context, fromFlag string) (*spec, error) {
|
||||||
if !buildutil.FileExists(ctxt, spec.filename) {
|
if !buildutil.FileExists(ctxt, spec.filename) {
|
||||||
return nil, fmt.Errorf("no such file: %s", spec.filename)
|
return nil, fmt.Errorf("no such file: %s", spec.filename)
|
||||||
}
|
}
|
||||||
// Guess the default package.
|
|
||||||
var err error
|
bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename)
|
||||||
if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("-from: couldn't guess package from filename: %s: %s",
|
return nil, err
|
||||||
spec.filename, err)
|
|
||||||
}
|
}
|
||||||
|
spec.pkg = bp.ImportPath
|
||||||
|
|
||||||
} else if a, b := splitAtLastDot(main); b == "" {
|
} else if a, b := splitAtLastDot(main); b == "" {
|
||||||
// importpath e.g. "encoding/json"
|
// importpath e.g. "encoding/json"
|
||||||
|
|
@ -194,12 +194,12 @@ func parseOffsetFlag(ctxt *build.Context, offsetFlag string) (*spec, error) {
|
||||||
if !buildutil.FileExists(ctxt, spec.filename) {
|
if !buildutil.FileExists(ctxt, spec.filename) {
|
||||||
return nil, fmt.Errorf("no such file: %s", spec.filename)
|
return nil, fmt.Errorf("no such file: %s", spec.filename)
|
||||||
}
|
}
|
||||||
// Guess the default package.
|
|
||||||
var err error
|
bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename)
|
||||||
if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("couldn't guess package from filename: %s: %s",
|
return nil, err
|
||||||
spec.filename, err)
|
|
||||||
}
|
}
|
||||||
|
spec.pkg = bp.ImportPath
|
||||||
|
|
||||||
for _, r := range parts[1] {
|
for _, r := range parts[1] {
|
||||||
if !isDigit(r) {
|
if !isDigit(r) {
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@
|
||||||
package rename
|
package rename
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"go/build"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
@ -79,61 +77,6 @@ func isDigit(ch rune) bool {
|
||||||
|
|
||||||
// -- Plundered from code.google.com/p/go.tools/oracle -----------------
|
// -- Plundered from code.google.com/p/go.tools/oracle -----------------
|
||||||
|
|
||||||
// guessImportPath finds the package containing filename, and returns
|
|
||||||
// its import path relative to it.
|
|
||||||
func guessImportPath(filename string, ctxt *build.Context) (importPath string, err error) {
|
|
||||||
// TODO(adonovan): move this to package "buildutil"; factor in common with oracle.
|
|
||||||
// bp, err := buildutil.ContainingPackage(ctxt, wd, filename)
|
|
||||||
// if err != nil {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// return bp.ImportPath, nil
|
|
||||||
|
|
||||||
absFile, err := filepath.Abs(filename)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("can't form absolute path of %s", filename)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
absFileDir := segments(filepath.Dir(absFile))
|
|
||||||
|
|
||||||
// Find the innermost directory in $GOPATH that encloses filename.
|
|
||||||
minD := 1024
|
|
||||||
for _, gopathDir := range ctxt.SrcDirs() {
|
|
||||||
// We can assume $GOPATH and $GOROOT dirs are absolute,
|
|
||||||
// thus gopathDir too, and that it exists.
|
|
||||||
d := prefixLen(segments(gopathDir), absFileDir)
|
|
||||||
// If there are multiple matches,
|
|
||||||
// prefer the innermost enclosing directory
|
|
||||||
// (smallest d).
|
|
||||||
if d >= 0 && d < minD {
|
|
||||||
minD = d
|
|
||||||
importPath = strings.Join(absFileDir[len(absFileDir)-minD:], string(os.PathSeparator))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if importPath == "" {
|
|
||||||
err = fmt.Errorf("can't find package for file %s", filename)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func segments(path string) []string {
|
|
||||||
return strings.Split(path, string(os.PathSeparator))
|
|
||||||
}
|
|
||||||
|
|
||||||
// prefixLen returns the length of the remainder of y if x is a prefix
|
|
||||||
// of y, a negative number otherwise.
|
|
||||||
func prefixLen(x, y []string) int {
|
|
||||||
d := len(y) - len(x)
|
|
||||||
if d >= 0 {
|
|
||||||
for i := range x {
|
|
||||||
if y[i] != x[i] {
|
|
||||||
return -1 // not a prefix
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
// sameFile returns true if x and y have the same basename and denote
|
// sameFile returns true if x and y have the same basename and denote
|
||||||
// the same file.
|
// the same file.
|
||||||
//
|
//
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue