|
5 | 5 | package cmd |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "encoding/json" |
8 | 9 | "fmt" |
9 | 10 | "os" |
10 | 11 | "strings" |
@@ -61,25 +62,28 @@ func runCheck(cmd *Command, args []string) { |
61 | 62 | return |
62 | 63 | } |
63 | 64 |
|
| 65 | + pkgConf := new(gopmConfig) |
64 | 66 | importsCache := make(map[string]bool) |
65 | 67 | uninstallList := make([]string, 0) |
66 | 68 | isInstalled := false |
67 | 69 | // Check if dependencies have been installed. |
68 | | - paths := utils.GetGOPATH() |
69 | 70 |
|
70 | 71 | for _, v := range imports { |
71 | 72 | // Make sure it doesn't belong to same project. |
72 | 73 | if utils.GetProjectPath(v) != utils.GetProjectPath(importPath) { |
73 | | - for _, p := range paths { |
74 | | - if checkIsExistWithVCS(p + "/src/" + v + "/") { |
| 74 | + if !importsCache[v] { |
| 75 | + importsCache[v] = true |
| 76 | + pkgConf.Deps = append(pkgConf.Deps, &node.Node{ |
| 77 | + ImportPath: v, |
| 78 | + }) |
| 79 | + |
| 80 | + if _, ok := utils.CheckIsExistInGOPATH(importPath); ok { |
75 | 81 | isInstalled = true |
76 | | - break |
77 | 82 | } |
78 | | - } |
79 | 83 |
|
80 | | - if !isInstalled && !importsCache[v] { |
81 | | - importsCache[v] = true |
82 | | - uninstallList = append(uninstallList, v) |
| 84 | + if !isInstalled { |
| 85 | + uninstallList = append(uninstallList, v) |
| 86 | + } |
83 | 87 | } |
84 | 88 | } |
85 | 89 | isInstalled = false |
@@ -121,8 +125,24 @@ func runCheck(cmd *Command, args []string) { |
121 | 125 | cmdArgs[1] = k |
122 | 126 | executeCommand("go", cmdArgs) |
123 | 127 | } |
| 128 | + } |
| 129 | + |
| 130 | + // Generate configure file. |
| 131 | + if !utils.IsExist("gopm.json") { |
| 132 | + fw, err := os.Create("gopm.json") |
| 133 | + if err != nil { |
| 134 | + utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] runCheck -> %s\n", PromptMsg["OpenFile"]), err)) |
| 135 | + return |
| 136 | + } |
| 137 | + defer fw.Close() |
124 | 138 |
|
125 | | - // Generate configure file. |
| 139 | + fbytes, err := json.MarshalIndent(&pkgConf, "", "\t") |
| 140 | + if err != nil { |
| 141 | + utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] runCheck -> %s\n", PromptMsg["ParseJSON"]), err)) |
| 142 | + return |
| 143 | + } |
| 144 | + fw.Write(fbytes) |
| 145 | + utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("<SUCCESS>$ %s\n", PromptMsg["GenerateConfig"]), importPath)) |
126 | 146 | } |
127 | 147 | } |
128 | 148 |
|
@@ -155,32 +175,3 @@ func checkImportsByRoot(rootPath, importPath string) (imports []string, err erro |
155 | 175 |
|
156 | 176 | return imports, err |
157 | 177 | } |
158 | | - |
159 | | -// checkIsExistWithVCS returns false if directory only has VCS folder, |
160 | | -// or doesn't exist. |
161 | | -func checkIsExistWithVCS(path string) bool { |
162 | | - // Check if directory exist. |
163 | | - if !utils.IsExist(path) { |
164 | | - return false |
165 | | - } |
166 | | - |
167 | | - // Check if only has VCS folder. |
168 | | - dirs, err := utils.GetDirsInfo(path) |
169 | | - if err != nil { |
170 | | - utils.ColorPrint(fmt.Sprintf("[ERROR] checkIsExistWithVCS -> [ %s ]", err)) |
171 | | - return false |
172 | | - } |
173 | | - |
174 | | - if len(dirs) > 1 { |
175 | | - return true |
176 | | - } else if len(dirs) == 0 { |
177 | | - return false |
178 | | - } |
179 | | - |
180 | | - switch dirs[0].Name() { |
181 | | - case ".git", ".hg", ".svn": |
182 | | - return false |
183 | | - } |
184 | | - |
185 | | - return true |
186 | | -} |
0 commit comments