forked from gpmgo/gopm
/
gen.go
109 lines (93 loc) · 2.86 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2013-2014 gopm authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package cmd
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
"github.com/codegangsta/cli"
"github.com/gpmgo/gopm/doc"
"github.com/gpmgo/gopm/log"
)
var CmdGen = cli.Command{
Name: "gen",
Usage: "generate a gopmfile according current Go project",
Description: `Command gen gets dependencies and generates a gopmfile
gopm gen
Make sure you run this command in the root path of a go project.`,
Action: runGen,
Flags: []cli.Flag{
cli.BoolFlag{"example, e", "check dependencies for example(s)"},
cli.BoolFlag{"verbose, v", "show process details"},
cli.BoolFlag{"local,l", "gen local .gopmfile"},
},
}
var commonRes = []string{"views", "templates", "static", "public", "conf"}
func runGen(ctx *cli.Context) {
setup(ctx)
if !com.IsExist(".gopmfile") {
os.Create(".gopmfile")
}
gf, err := goconfig.LoadConfigFile(".gopmfile")
if err != nil {
log.Error("gen", "Cannot load gopmfile:")
log.Fatal("", "\t"+err.Error())
}
targetPath := parseTarget(gf.MustValue("target", "path"))
// Get and set dependencies.
imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false)
sort.Strings(imports)
for _, p := range imports {
p = doc.GetProjectPath(p)
// Skip subpackage(s) of current project.
if isSubpackage(p, targetPath) {
continue
}
// Check if user specified the version.
if value := gf.MustValue("deps", p); len(value) == 0 {
gf.SetValue("deps", p, "")
}
}
// Get and set resources.
res := make([]string, 0, len(commonRes))
for _, cr := range commonRes {
if com.IsExist(cr) {
res = append(res, cr)
}
}
gf.SetValue("res", "include", strings.Join(res, "|"))
//Set local path and init src bin pkg directories
if ctx.Bool("local") {
path, _ := filepath.Abs(".")
gf.SetValue("project", "localPath", path)
if !com.IsDir(path + "/src") {
os.Mkdir(path+"/src", os.ModeDir|os.ModePerm)
}
if !com.IsDir(path + "/bin") {
os.Mkdir(path+"/bin", os.ModeDir|os.ModePerm)
}
if !com.IsDir(path + "pkg") {
os.Mkdir(path+"/pkg", os.ModeDir|os.ModePerm)
}
}
err = goconfig.SaveConfigFile(gf, ".gopmfile")
if err != nil {
log.Error("gen", "Fail to save gopmfile:")
log.Fatal("", "\t"+err.Error())
}
log.Success("SUCC", "gen", "Generate gopmfile successfully!")
}