コード例 #1
0
ファイル: types.go プロジェクト: ewindfall/go-liteide-tools
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)
}
コード例 #2
0
ファイル: types.go プロジェクト: donseba/gotools
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
}