Example #1
0
File: scan.go Project: newblue/gorf
func LocalImporter(path string) (pkg *ast.Package) {
	path = filepath.Clean(path)

	//fmt.Printf("Importing %s\n", path)
	var ok bool
	var pkgtop *ast.Package
	if pkgtop, ok = PackageTops[path]; !ok {
		pkg = types.DefaultImporter(path)
		return
	}
	if pkg, ok = Packages[path]; ok {
		return
	}
	var sourcefiles []string
	for srcfile := range pkgtop.Files {
		sourcefiles = append(sourcefiles, srcfile)
	}
	//fmt.Printf("Parsing %v\n", sourcefiles)
	dirpkgs, err := parser.ParseFiles(AllSources, sourcefiles, parser.ParseComments)
	if err != nil {
		fmt.Println(err)
		return
	}

	pkg = dirpkgs[pkgtop.Name]

	Packages[path] = pkg

	//fmt.Printf("nil: %v name: %s\n", pkg == nil, pkgtop.Name)

	return
}
Example #2
0
File: scan.go Project: newblue/gorf
//Look at the imports, and build up ImportedBy
func ScanForImports(path string) (err os.Error) {
	sourcefiles, _ := filepath.Glob(filepath.Join(path, "*.go"))
	dirpkgs, err := parser.ParseFiles(AllSourceTops, sourcefiles, parser.ImportsOnly)

	if err != nil {
		fmt.Println(err)
	}

	//take the first non-main. otherwise, main is ok.
	var prime *ast.Package
	for name, pkg := range dirpkgs {
		prime = pkg
		if name != "main" {
			break
		}
	}

	if prime == nil {
		return
	}

	PackageTops[path] = prime

	is := make(ImportScanner)

	ast.Walk(is, prime)

	if v, ok := is["."]; !v && ok {
		return MakeErr("gorf can not deal with unnamed import in '%s'", path)
	}

	for path, _ := range is {
		if strings.HasPrefix(path, ".") {
			return MakeErr("gorf can not deal with relative import in '%s'", path)
		}
	}

	for imp := range is {
		ImportedBy[imp] = append(ImportedBy[imp], path)
	}

	return
}