Example #1
0
// checkImportsByRoot checks imports of packages from root path,
// and recursion checks all sub-directories.
func checkImportsByRoot(rootPath, importPath string) (imports []string, err error) {
	// Check imports of root path.
	importPkgs, err := doc.CheckImports(rootPath, importPath)
	if err != nil {
		return nil, err
	}
	imports = append(imports, importPkgs...)

	// Check sub-directories.
	dirs, err := utils.GetDirsInfo(rootPath)
	if err != nil {
		return nil, err
	}

	for _, d := range dirs {
		if d.IsDir() &&
			!(!cmdCheck.Flags["-e"] && strings.Contains(d.Name(), "example")) {
			importPkgs, err := checkImportsByRoot(rootPath+d.Name()+"/", importPath)
			if err != nil {
				return nil, err
			}
			imports = append(imports, importPkgs...)
		}
	}

	return imports, err
}
Example #2
0
// checkIsExistWithVCS returns false if directory only has VCS folder,
// or doesn't exist.
func checkIsExistWithVCS(path string) bool {
	// Check if directory exist.
	if !utils.IsExist(path) {
		return false
	}

	// Check if only has VCS folder.
	dirs, err := utils.GetDirsInfo(path)
	if err != nil {
		fmt.Printf("checkIsExistWithVCS -> [ %s ]", err)
		return false
	}

	if len(dirs) > 1 {
		return true
	} else if len(dirs) == 0 {
		return false
	}

	switch dirs[0].Name() {
	case ".git", ".hg", ".svn":
		return false
	}

	return true
}
Example #3
0
// GetGoogleDoc downloads raw files from code.google.com.
func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH string, node *Node, cmdFlags map[string]bool) ([]string, error) {
	setupGoogleMatch(match)
	// Check version control.
	if m := googleEtagRe.FindStringSubmatch(node.Value); m != nil {
		match["vcs"] = m[1]
	} else if err := getGoogleVCS(client, match); err != nil {
		return nil, err
	}

	// bundle and snapshot will have commit 'B' and 'S',
	// but does not need to download dependencies.
	isCheckImport := len(node.Value) == 0
	if len(node.Value) == 1 {
		node.Value = ""
	}

	rootPath := expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/", match)

	// Scrape the repo browser to find the project revision and individual Go files.
	p, err := HttpGetBytes(client, rootPath+"?r="+node.Value, nil)
	if err != nil {
		return nil, err
	}

	// Check revision tag.
	if m := googleRevisionRe.FindSubmatch(p); m == nil {
		return nil,
			errors.New("doc.GetGoogleDoc(): Could not find revision for " + match["importPath"])
	} else {
		node.Type = "commit"
		node.Value = string(m[1])
	}

	projectPath := expand("code.google.com/p/{repo}{dot}{subrepo}{dir}", match)
	installPath := installGOPATH + "/src/" + projectPath
	node.ImportPath = projectPath

	// Remove old files.
	os.RemoveAll(installPath + "/")
	// Create destination directory.
	os.MkdirAll(installPath+"/", os.ModePerm)

	isCodeOnly := cmdFlags["-c"]
	// Get source files in root path.
	files := make([]*source, 0, 5)
	for _, m := range googleFileRe.FindAllSubmatch(p, -1) {
		fname := strings.Split(string(m[1]), "?")[0]
		if isCodeOnly && !utils.IsDocFile(fname) {
			continue
		} else if strings.HasPrefix(fname, ".") {
			continue
		}

		files = append(files, &source{
			name:   fname,
			rawURL: expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/{0}", match, fname) + "?r=" + node.Value,
		})
	}

	// Fetch files from VCS.
	if err := fetchFiles(client, files, nil); err != nil {
		return nil, err
	}

	// Save files.
	for _, f := range files {
		absPath := installPath + "/"

		// Create diretory before create file.
		os.MkdirAll(path.Dir(absPath), os.ModePerm)

		// Write data to file
		fw, err := os.Create(absPath + f.name)
		if err != nil {
			return nil, err
		}

		_, err = fw.Write(f.data)
		fw.Close()
		if err != nil {
			return nil, err
		}
	}

	dirs := make([]string, 0, 3)
	// Get subdirectories.
	for _, m := range googleDirRe.FindAllSubmatch(p, -1) {
		dirName := strings.Split(string(m[1]), "?")[0]
		if strings.HasSuffix(dirName, "/") {
			dirs = append(dirs, dirName)
		}
	}

	err = downloadFiles(client, match, rootPath, installPath+"/", node.Value, dirs)
	if err != nil {
		return nil, err
	}

	var imports []string

	// Check if need to check imports.
	if isCheckImport {
		dirs, err := utils.GetDirsInfo(installPath + "/")
		if err != nil {
			return nil, err
		}

		for _, d := range dirs {
			if d.IsDir() && !(!cmdFlags["-e"] && strings.Contains(d.Name(), "example")) {
				absPath := installPath + "/" + d.Name() + "/"
				importPkgs, err := CheckImports(absPath, match["importPath"])
				if err != nil {
					return nil, err
				}
				imports = append(imports, importPkgs...)
			}
		}
	}

	return imports, err
}