Example #1
0
func (w *PkgWalker) importPath(path string, mode build.ImportMode) (*build.Package, error) {
	if filepath.IsAbs(path) {
		return w.context.ImportDir(path, 0)
	}
	if stdlib.IsStdPkg(path) {
		return stdlib.ImportStdPkg(w.context, path, mode)
	}
	return w.context.Import(path, "", mode)
}
Example #2
0
// importPathToNameGoPath finds out the actual package name, as declared in its .go files.
// If there's a problem, it falls back to using importPathToNameBasic.
func importPathToNameGoPath(importPath string) (packageName string) {
	if stdlib.IsStdPkg(importPath) {
		return path.Base(importPath)
	}
	if buildPkg, err := build.Import(importPath, "", 0); err == nil {
		return buildPkg.Name
	} else {
		return importPathToNameBasic(importPath)
	}
}
Example #3
0
func (w *PkgWalker) importPath(path string, mode build.ImportMode) (*build.Package, error) {

	if filepath.IsAbs(path) {
		return w.context.ImportDir(path, 0)
	}

	if stdlib.IsStdPkg(path) {
		return stdlib.ImportStdPkg(w.context, path, mode)
	}

	// first we check in the package exists in the GoPath
	imp, err := w.context.Import(path, "", mode)

	// if it doesnt exists in the import path
	// we start digging in the current package.
	if err != nil {

		fs := string(filepath.Separator)

		pkgPath := w.CurrPackage

		// strip the GOPATH from the package path if it
		// exists. We do not need to look beyond this part
		goPath := filepath.Join(w.context.GOPATH, "src") + fs
		if strings.Contains(pkgPath, goPath) {
			pkgPath = strings.Replace(pkgPath, goPath, "", 1)
		}

		parts := []string{}

		if strings.Contains(pkgPath, fs) {
			parts = strings.Split(pkgPath, fs)
		} else {
			parts = []string{pkgPath}
		}

		// find the nearest vendor map.
		// we start looking in the curent directory and move one directory
		// up from there, until we find something.
		// example project:
		// c:\GoPath\src\project1\
		// |-vendor
		// |-main
		// |--main.go
		// |-subpackage
		// |--subpackage.go
		// |---subSUBpackage
		// |----subSUBpackage.go

		i := 0
		tot := len(parts)
		searchPath := ""
		for i < tot {
			searchPath = strings.Join(parts[:(tot-i)], "/") + "/vendor/"

			imp, err = w.context.Import(searchPath+path, "", mode)
			if err != nil {
				i++
				continue
			}

			return imp, nil

		}
	}

	return imp, err
}
Example #4
0
func (w *PkgWalker) isBinaryPkg(pkg string) bool {
	return stdlib.IsStdPkg(pkg)
}