Exemplo n.º 1
0
// build gets imports from source files.
func (w *walker) build(srcs []*source) ([]string, error) {
	// Add source files to walker, I skipped references here.
	w.srcs = make(map[string]*source)
	for _, src := range srcs {
		w.srcs[src.name] = src
	}

	w.fset = token.NewFileSet()

	// Find the package and associated files.
	ctxt := build.Context{
		GOOS:          runtime.GOOS,
		GOARCH:        runtime.GOARCH,
		CgoEnabled:    true,
		JoinPath:      path.Join,
		IsAbsPath:     path.IsAbs,
		SplitPathList: func(list string) []string { return strings.Split(list, ":") },
		IsDir:         func(path string) bool { panic("unexpected") },
		HasSubdir:     func(root, dir string) (rel string, ok bool) { panic("unexpected") },
		ReadDir:       func(dir string) (fi []os.FileInfo, err error) { return w.readDir(dir) },
		OpenFile:      func(path string) (r io.ReadCloser, err error) { return w.openFile(path) },
		Compiler:      "gc",
	}

	bpkg, err := ctxt.ImportDir(w.ImportPath, 0)
	// Continue if there are no Go source files; we still want the directory info.
	_, nogo := err.(*build.NoGoError)
	if err != nil {
		if nogo {
			err = nil
		} else {
			return nil, errors.New("doc.walker.build(): " + err.Error())
		}
	}

	// Parse the Go files

	files := make(map[string]*ast.File)
	for _, name := range append(bpkg.GoFiles, bpkg.CgoFiles...) {
		file, err := parser.ParseFile(w.fset, name, w.srcs[name].data, parser.ParseComments)
		if err != nil {
			//beego.Error("doc.walker.build():", err)
			continue
		}
		files[name] = file
	}

	var imports []string
	for _, v := range bpkg.Imports {
		// Skip strandard library.
		if !utils.IsGoRepoPath(v) &&
			(utils.GetProjectPath(v) != utils.GetProjectPath(w.ImportPath)) {
			imports = append(imports, v)
		}
	}

	return imports, err
}
Exemplo n.º 2
0
// removePackage removes package from local file system.
func removePackage(node *doc.Node) (*doc.Node, []string) {
	// Find package in GOPATH.
	paths := utils.GetGOPATH()
	for _, p := range paths {
		absPath := p + "/src/" + utils.GetProjectPath(node.ImportPath) + "/"
		if utils.IsExist(absPath) {
			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["RemovePackage"]), node.ImportPath)
			// Remove files.
			os.RemoveAll(absPath)
			// Remove file in GOPATH/bin
			proName := utils.GetExecuteName(node.ImportPath)
			paths := utils.GetGOPATH()
			var gopath string

			for _, v := range paths {
				if utils.IsExist(v + "/bin/" + proName) {
					gopath = v // Don't need to find again.
					os.Remove(v + "/bin/" + proName)
				}
			}

			pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/" + node.ImportPath
			// Remove file in GOPATH/pkg
			if len(gopath) == 0 {
				for _, v := range paths {
					if utils.IsExist(v + pkgPath + "/") {
						gopath = v
					}
				}
			}

			os.RemoveAll(gopath + pkgPath + "/")
			os.Remove(gopath + pkgPath + ".a")
			return node, nil
		}
	}

	// Cannot find package.
	fmt.Printf(fmt.Sprintf("%s\n", promptMsg["PackageNotFound"]), node.ImportPath)
	return nil, nil
}
Exemplo n.º 3
0
func runCheck(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, config.AutoEnable.Check, args, printCheckPrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	wd, _ := os.Getwd()
	// Guess import path.
	gopath := utils.GetBestMatchGOPATH(wd) + "/src/"
	if len(wd) <= len(gopath) {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InvalidPath"]))
		return
	}

	importPath := wd[len(gopath):]
	imports, err := doc.CheckImports(wd+"/", importPath)
	if err != nil {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["CheckImports"]), err)
		return
	}

	if len(imports) == 0 {
		return
	}

	uninstallList := make([]string, 0)
	isInstalled := false
	// Check if dependencies have been installed.
	paths := utils.GetGOPATH()

	for _, v := range imports {
		// Make sure it doesn't belong to same project.
		if utils.GetProjectPath(v) != utils.GetProjectPath(importPath) {
			for _, p := range paths {
				if utils.IsExist(p + "/src/" + v + "/") {
					isInstalled = true
					break
				}
			}

			if !isInstalled {
				uninstallList = append(uninstallList, v)
			}
		}
	}

	// Check if need to install packages.
	if len(uninstallList) > 0 {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MissingImports"]))
		for _, v := range uninstallList {
			fmt.Printf("%s\n", v)
		}
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["ContinueDownload"]))
		var option string
		fmt.Fscan(os.Stdin, &option)
		if strings.ToLower(option) != "y" {
			os.Exit(0)
		}

		installGOPATH = utils.GetBestMatchGOPATH(appPath)
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["DownloadPath"]), installGOPATH)
		// Generate temporary nodes.
		nodes := make([]*doc.Node, len(uninstallList))
		for i := range nodes {
			nodes[i] = new(doc.Node)
			nodes[i].ImportPath = uninstallList[i]
		}
		// Download packages.
		downloadPackages(nodes)

		// Install packages all together.
		var cmdArgs []string
		cmdArgs = append(cmdArgs, "install")
		cmdArgs = append(cmdArgs, "<blank>")

		paths := utils.GetGOPATH()
		pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/"
		for _, k := range uninstallList {
			// Delete old packages.
			for _, p := range paths {
				os.RemoveAll(p + pkgPath + k + "/")
				os.Remove(p + pkgPath + k + ".a")
			}
		}

		for _, k := range uninstallList {
			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InstallStatus"]), k)
			cmdArgs[1] = k
			executeCommand("go", cmdArgs)
		}

		// Generate configure file.
	}
}