You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

11 years ago
// 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.
12 years ago
package cmd
12 years ago
11 years ago
import (
"os"
11 years ago
"strings"
11 years ago
"github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
"github.com/codegangsta/cli"
11 years ago
11 years ago
"github.com/gpmgo/gopm/doc"
"github.com/gpmgo/gopm/log"
)
11 years ago
11 years ago
var CmdGen = cli.Command{
Name: "gen",
11 years ago
Usage: "generate a gopmfile according current Go project",
11 years ago
Description: `Command gen gets dependencies and generates a gopmfile
11 years ago
11 years ago
gopm gen
11 years ago
11 years ago
Make sure you run this command in the root path of a go project.`,
Action: runGen,
Flags: []cli.Flag{
11 years ago
cli.BoolFlag{"example, e", "check dependencies for example(s)"},
11 years ago
cli.BoolFlag{"verbose, v", "show process details"},
11 years ago
},
}
var commonRes = []string{"views", "templates", "static", "public", "conf"}
11 years ago
func runGen(ctx *cli.Context) {
setup(ctx)
11 years ago
11 years ago
if !com.IsExist(".gopmfile") {
os.Create(".gopmfile")
}
11 years ago
11 years ago
gf, err := goconfig.LoadConfigFile(".gopmfile")
if err != nil {
log.Error("gen", "Cannot load gopmfile:")
log.Fatal("", "\t"+err.Error())
11 years ago
}
targetPath := parseTarget(gf.MustValue("target", "path"))
// Get and set dependencies.
11 years ago
imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false)
11 years ago
for _, p := range imports {
11 years ago
p = doc.GetProjectPath(p)
// Skip subpackage(s) of current project.
11 years ago
if strings.HasSuffix(strings.Replace(workDir, "\\", "/", -1), p) ||
strings.HasPrefix(p, targetPath) {
11 years ago
continue
}
// Check if user specified the version.
11 years ago
if value := gf.MustValue("deps", p); len(value) == 0 {
gf.SetValue("deps", p, "")
11 years ago
}
11 years ago
}
// 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, "|"))
11 years ago
err = goconfig.SaveConfigFile(gf, ".gopmfile")
11 years ago
if err != nil {
log.Error("gen", "Fail to save gopmfile:")
log.Fatal("", "\t"+err.Error())
11 years ago
}
12 years ago
log.Success("SUCC", "gen", "Generate gopmfile successfully!")
12 years ago
}