|
|
|
@ -7,7 +7,6 @@ package main
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"runtime" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"github.com/GPMGo/gpm/doc" |
|
|
|
@ -20,13 +19,17 @@ var cmdCheck = &Command{
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
cmdCheck.Run = runCheck |
|
|
|
|
cmdCheck.Flags = map[string]bool{ |
|
|
|
|
"-e": false, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// printCheckPrompt prints prompt information to users to
|
|
|
|
|
// let them know what's going on.
|
|
|
|
|
func printCheckPrompt(flag string) { |
|
|
|
|
switch flag { |
|
|
|
|
|
|
|
|
|
case "-e": |
|
|
|
|
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["CheckExDeps"])) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -42,14 +45,14 @@ func runCheck(cmd *Command, args []string) {
|
|
|
|
|
// Guess import path.
|
|
|
|
|
gopath := utils.GetBestMatchGOPATH(wd) + "/src/" |
|
|
|
|
if len(wd) <= len(gopath) { |
|
|
|
|
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InvalidPath"])) |
|
|
|
|
fmt.Printf(fmt.Sprintf("runCheck -> %s\n", promptMsg["InvalidPath"])) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
importPath := wd[len(gopath):] |
|
|
|
|
imports, err := doc.CheckImports(wd+"/", importPath) |
|
|
|
|
imports, err := checkImportsByRoot(wd+"/", importPath) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["CheckImports"]), err) |
|
|
|
|
fmt.Printf(fmt.Sprintf("runCheck -> %s\n", promptMsg["CheckImports"]), err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -57,6 +60,7 @@ func runCheck(cmd *Command, args []string) {
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
importsCache := make(map[string]bool) |
|
|
|
|
uninstallList := make([]string, 0) |
|
|
|
|
isInstalled := false |
|
|
|
|
// Check if dependencies have been installed.
|
|
|
|
@ -66,16 +70,18 @@ func runCheck(cmd *Command, args []string) {
|
|
|
|
|
// 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 + "/") { |
|
|
|
|
if checkIsExistWithVCS(p + "/src/" + v + "/") { |
|
|
|
|
isInstalled = true |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !isInstalled { |
|
|
|
|
if !isInstalled && !importsCache[v] { |
|
|
|
|
importsCache[v] = true |
|
|
|
|
uninstallList = append(uninstallList, v) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
isInstalled = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Check if need to install packages.
|
|
|
|
@ -102,21 +108,13 @@ func runCheck(cmd *Command, args []string) {
|
|
|
|
|
// Download packages.
|
|
|
|
|
downloadPackages(nodes) |
|
|
|
|
|
|
|
|
|
removePackageFiles("", uninstallList) |
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
@ -126,3 +124,60 @@ func runCheck(cmd *Command, args []string) {
|
|
|
|
|
// Generate configure file.
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch dirs[0].Name() { |
|
|
|
|
case ".git", ".hg", ".svn": |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|