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