Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.

Commit dbd5fd1

Browse files
committed
add feature auto-generate package config file for command check
1 parent 469738a commit dbd5fd1

File tree

11 files changed

+129
-48
lines changed

11 files changed

+129
-48
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ This application still in experiment, any change could happen, but it doesn't af
3535

3636
### v0.3.*
3737

38-
- Command `install` add flag `-u` for force update pakcages, and need to check if has downloaded same package with same version already.
39-
- Command `install` and `remove` give number to let user choose operate one package.
40-
- Command `check` add feature to update or generate gopack.json.
38+
- Command `check` add feature to update gopm.json.
4139
- Command `install` generates dependencies configuration file.
4240
- Command `install` save tarball add support for packages in code.google.com, bitbucket.org, launchpad.net, git.oschina.net, gitcafe.com, *.codeplex.com.
43-
- Command `build` use dependencies configuration file to build with specific versions of dependencies, if VCS tools are available, simply use `checkout`.
41+
- Command `build` use dependencies configuration file to build with specific versions of dependencies.
4442
- Command `clean` is for cleaning empty directories and backup.
4543
- Add gpm working principle design.
4644
- Complete documentation.
4745

4846
### Future
4947

48+
- Command `install` and `remove` give number to let user choose operate one package when using snapshot.
49+
- Figure out how to use tool chian directly in order to compile only with `.a` files.
5050
- Command `search` compares version.
5151
- Command `home` and `doc`.
5252
- Command `remove` add flag `-d` for removing dependencies at the same time.

cmd/check.go

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package cmd
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"os"
1011
"strings"
@@ -61,25 +62,28 @@ func runCheck(cmd *Command, args []string) {
6162
return
6263
}
6364

65+
pkgConf := new(gopmConfig)
6466
importsCache := make(map[string]bool)
6567
uninstallList := make([]string, 0)
6668
isInstalled := false
6769
// Check if dependencies have been installed.
68-
paths := utils.GetGOPATH()
6970

7071
for _, v := range imports {
7172
// Make sure it doesn't belong to same project.
7273
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 {
7581
isInstalled = true
76-
break
7782
}
78-
}
7983

80-
if !isInstalled && !importsCache[v] {
81-
importsCache[v] = true
82-
uninstallList = append(uninstallList, v)
84+
if !isInstalled {
85+
uninstallList = append(uninstallList, v)
86+
}
8387
}
8488
}
8589
isInstalled = false
@@ -121,8 +125,24 @@ func runCheck(cmd *Command, args []string) {
121125
cmdArgs[1] = k
122126
executeCommand("go", cmdArgs)
123127
}
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()
124138

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))
126146
}
127147
}
128148

@@ -155,32 +175,3 @@ func checkImportsByRoot(rootPath, importPath string) (imports []string, err erro
155175

156176
return imports, err
157177
}
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-
}

cmd/install.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/exec"
1414
"regexp"
15+
"strconv"
1516
"strings"
1617

1718
"github.com/GPMGo/gopm/doc"
@@ -131,12 +132,19 @@ func runInstall(cmd *Command, args []string) {
131132
if len(nodes) > 0 {
132133
// Check with users if continue.
133134
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("%s\n", PromptMsg["BundleInfo"]), bundle))
134-
for _, n := range nodes {
135-
fmt.Printf("[%s] -> %s: %s.\n", n.ImportPath, n.Type, n.Value)
135+
for i, n := range nodes {
136+
fmt.Printf("%d.[%s] -> %s: %s.\n", i+1, n.ImportPath, n.Type, n.Value)
136137
}
137138
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["ContinueDownload"]))
138139
var option string
139140
fmt.Fscan(os.Stdin, &option)
141+
// Chekc if it is a index.
142+
num, err := strconv.Atoi(option)
143+
if err == nil && num > 0 && num <= len(nodes) {
144+
nodes = nodes[num-1 : num]
145+
break
146+
}
147+
140148
if strings.ToLower(option) != "y" {
141149
os.Exit(0)
142150
return
@@ -229,6 +237,14 @@ func downloadPackages(nodes []*node.Node) {
229237
for _, n := range nodes {
230238
// Check if it is a valid remote path.
231239
if utils.IsValidRemotePath(n.ImportPath) {
240+
if !CmdInstall.Flags["-u"] {
241+
// Check if package has been downloaded.
242+
if _, ok := utils.CheckIsExistInGOPATH(n.ImportPath); ok {
243+
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["SkipInstalled"]), n.ImportPath)
244+
continue
245+
}
246+
}
247+
232248
if !downloadCache[n.ImportPath] {
233249
// Download package.
234250
nod, imports := downloadPackage(n)

cmd/remove.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"runtime"
12+
"strconv"
1213
"strings"
1314

1415
"github.com/GPMGo/gopm/utils"
@@ -51,6 +52,13 @@ func runRemove(cmd *Command, args []string) {
5152
fmt.Printf(fmt.Sprintf("%s\n", PromptMsg["ContinueRemove"]))
5253
var option string
5354
fmt.Fscan(os.Stdin, &option)
55+
// Chekc if it is a index.
56+
num, err := strconv.Atoi(option)
57+
if err == nil && num > 0 && num <= len(nodes) {
58+
nodes = nodes[num-1 : num]
59+
break
60+
}
61+
5462
if strings.ToLower(option) != "y" {
5563
os.Exit(0)
5664
return

cmd/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func runSearch(cmd *Command, args []string) {
8484
buf.WriteString("-> " + k)
8585
// Check if has been installed.
8686
for _, path := range paths {
87-
if checkIsExistWithVCS(path + "/src/" + k + "/") {
87+
if utils.CheckIsExistWithVCS(path + "/src/" + k + "/") {
8888
installStr := " [Installed]"
8989
if !isWindws {
9090
installStr = strings.Replace(installStr, "[",

cmd/struct.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ var (
2525
LocalBundles []*doc.Bundle
2626
)
2727

28+
// gopmConfig represents the package config file in correspoding directory.
29+
type gopmConfig struct {
30+
Deps []*node.Node
31+
}
32+
2833
type tomlConfig struct {
2934
Title, Version string
3035
Lang string `toml:"user_language"`

conf/gopm.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is a configuration file for gpm with toml format.
22

33
title = "gpm(Go Package Manager)"
4-
version = "v0.2.4 Build 0525"
4+
version = "v0.2.6 Build 0527"
55
user_language = "en-US"
66
#user_language = "zh-CN"
77
auto_backup = true

gopm.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Deps": [
3+
{
4+
"import_path": "github.com/BurntSushi/toml",
5+
"type": "",
6+
"value": "",
7+
"deps": null
8+
},
9+
{
10+
"import_path": "github.com/GPMGo/node",
11+
"type": "",
12+
"value": "",
13+
"deps": null
14+
}
15+
]
16+
}

i18n/en-US/prompt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ NoPackage=Please list at least one package/bundle/snapshot.
2121
DownloadPath=Packages will be downloaded to GOPATH(%s).
2222
InstallStatus=Installing package: %s.
2323
BundleInfo=Bundle(%s) contains following nodes:
24-
ContinueDownload=Continue to download?(Y/n).
24+
ContinueDownload=Continue to download?(Y/n or index).
25+
SkipInstalled=Skipped installed pakcage: %s.
2526
SkipDownloaded=Skipped downloaded package: %s.
2627
SkipInvalidPath=Skipped invalid import path: %s.
2728
DownloadStatus=Downloading package: %s.
@@ -30,5 +31,6 @@ NoKeyword=Cannot search without a keyword.
3031
ContinueRemove=Continue to remove?(Y/n).
3132
InvalidPath=Cannot find package in current path.
3233
MissingImports=Following packages are missing:
34+
GenerateConfig=Auto-generated configuration file for package: %s.
3335
CheckExDeps=You enabled check dependencies in example.
3436
SearchResult=search results

i18n/zh-CN/prompt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ NoPackage=请列出至少一个包、集合或快照.
2121
DownloadPath=所有包将会被下载至 GOPATH(%s).
2222
InstallStatus=正在安装包: %s.
2323
BundleInfo=集合 (%s) 包含以下结点:
24-
ContinueDownload=是否继续下载?(Y/n).
24+
ContinueDownload=是否继续下载?(Y/n 或索引).
25+
SkipInstalled=忽略已安装包: %s.
2526
SkipDownloaded=忽略已下载包: %s.
2627
SkipInvalidPath=忽略无效的导入路径: %s.
2728
DownloadStatus=正在下载包: %s.
@@ -30,5 +31,6 @@ NoKeyword=没有关键字,无法搜索.
3031
ContinueRemove=是否继续删除?(Y/n).
3132
InvalidPath=无法在当前目录中找到包.
3233
MissingImports=下列依赖包未找到:
34+
GenerateConfig=已自动生成包配置文件: %s.
3335
CheckExDeps=已激活示例代码依赖检查.
3436
SearchResult=搜索结果

0 commit comments

Comments
 (0)