// 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) } } pkgList := []string{node.ImportPath} removePackageFiles(gopath, pkgList) return node, nil } } // Cannot find package. fmt.Printf(fmt.Sprintf("%s\n", promptMsg["PackageNotFound"]), node.ImportPath) return nil, nil }
// getAppPath returns application execute path for current process. func getAppPath() bool { // Look up executable in PATH variable. appPath, _ = exec.LookPath(path.Base(os.Args[0])) // Check if run under $GOPATH/bin if !utils.IsExist(appPath + "conf/") { paths := utils.GetGOPATH() for _, p := range paths { if utils.IsExist(p + "/src/github.com/GPMGo/gopm/") { appPath = p + "/src/github.com/GPMGo/gopm/" break } } } if len(appPath) == 0 { fmt.Printf("ERROR: getAppPath -> Unable to indicate current execute path.\n") return false } appPath = filepath.Dir(appPath) + "/" if runtime.GOOS == "windows" { // Replace all '\' to '/'. appPath = strings.Replace(appPath, "\\", "/", -1) } doc.SetAppConfig(appPath, config.AutoBackup) return true }
// removePackageFiles removes package files in $GOPATH/pkg. func removePackageFiles(gopath string, pkgList []string) { var paths []string // Check if need to find GOPATH. if len(gopath) == 0 { paths = utils.GetGOPATH() } else { paths = append(paths, gopath) } pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/" for _, p := range pkgList { for _, g := range paths { os.RemoveAll(g + pkgPath + p + "/") os.Remove(g + pkgPath + p + ".a") } } }
func runBuild(cmd *Command, args []string) { // Check flags. num := checkFlags(cmd.Flags, config.AutoEnable.Build, args, printBuildPrompt) if num == -1 { return } args = args[num:] var cmdArgs []string cmdArgs = append(cmdArgs, "install") if cmdBuild.Flags["-v"] { cmdArgs = append(cmdArgs, "-v") } executeCommand("go", cmdArgs) // Find executable in GOPATH and copy to current directory. wd, _ := os.Getwd() proName := utils.GetExecuteName(wd) paths := utils.GetGOPATH() for _, v := range paths { if utils.IsExist(v + "/bin/" + proName) { if utils.IsExist(wd + "/" + proName) { err := os.Remove(wd + "/" + proName) if err != nil { fmt.Printf(fmt.Sprintf("ERROR: %s\n", promptMsg["RemoveFile"]), err) return } } err := os.Rename(v+"/bin/"+proName, wd+"/"+proName) if err == nil { fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MovedFile"]), v, wd) // Check if need to run program. if cmdBuild.Flags["-r"] { cmdArgs = make([]string, 0) executeCommand(proName, cmdArgs) } return } fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MoveFile"]), v, wd) break } } }
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("runCheck -> %s\n", promptMsg["InvalidPath"])) return } importPath := wd[len(gopath):] imports, err := checkImportsByRoot(wd+"/", importPath) if err != nil { fmt.Printf(fmt.Sprintf("runCheck -> %s\n", promptMsg["CheckImports"]), err) return } if len(imports) == 0 { return } importsCache := make(map[string]bool) 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 checkIsExistWithVCS(p + "/src/" + v + "/") { isInstalled = true break } } if !isInstalled && !importsCache[v] { importsCache[v] = true uninstallList = append(uninstallList, v) } } isInstalled = false } // 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) removePackageFiles("", uninstallList) // Install packages all together. var cmdArgs []string cmdArgs = append(cmdArgs, "install") cmdArgs = append(cmdArgs, "<blank>") for _, k := range uninstallList { fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InstallStatus"]), k) cmdArgs[1] = k executeCommand("go", cmdArgs) } // Generate configure file. } }