add beego project recognition

This commit is contained in:
slene 2013-06-27 15:29:27 +08:00
parent 15c558d329
commit 1224787b47
2 changed files with 67 additions and 24 deletions

View File

@ -83,13 +83,8 @@ func main() {
if cmd.CustomFlags { if cmd.CustomFlags {
args = args[1:] args = args[1:]
} else { } else {
if len(args) > 2 { cmd.Flag.Parse(args[1:])
cmd.Flag.Parse(args[2:]) args = cmd.Flag.Args()
args = append([]string{args[1]}, cmd.Flag.Args()...)
} else {
cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args()
}
} }
cmd.Run(cmd, args) cmd.Run(cmd, args)
os.Exit(2) os.Exit(2)

82
pack.go
View File

@ -7,9 +7,11 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
path "path/filepath" path "path/filepath"
"regexp"
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
@ -19,13 +21,15 @@ import (
) )
var cmdPack = &Command{ var cmdPack = &Command{
UsageLine: "pack [appPath]", CustomFlags: true,
Short: "pack an beego project into one execute file", UsageLine: "pack",
Short: "compress an beego project",
Long: ` Long: `
compress an project compress an beego project
-p app path. default is current path
-b build specify platform app. default true -b build specify platform app. default true
-o compressed file output path. default use appname -o compressed file output dir. default use current path
-f format. [ tar.gz / zip ]. default tar.gz. note: zip doesn't support embed symlink, skip it -f format. [ tar.gz / zip ]. default tar.gz. note: zip doesn't support embed symlink, skip it
-exp path exclude prefix -exp path exclude prefix
-exs path exclude suffix. default: .go:.DS_Store:.tmp -exs path exclude suffix. default: .go:.DS_Store:.tmp
@ -38,6 +42,7 @@ compress an project
} }
var ( var (
appPath string
excludeP string excludeP string
excludeS string excludeS string
outputP string outputP string
@ -49,9 +54,10 @@ var (
) )
func init() { func init() {
fs := flag.NewFlagSet("pack", flag.ExitOnError) fs := flag.NewFlagSet("pack", flag.ContinueOnError)
fs.StringVar(&appPath, "p", "", "")
fs.StringVar(&excludeP, "exp", "", "") fs.StringVar(&excludeP, "exp", "", "")
fs.StringVar(&excludeS, "exs", ".go:.DS_Store", "") fs.StringVar(&excludeS, "exs", ".go:.DS_Store:.tmp", "")
fs.StringVar(&outputP, "o", "", "") fs.StringVar(&outputP, "o", "", "")
fs.BoolVar(&build, "b", true, "") fs.BoolVar(&build, "b", true, "")
fs.BoolVar(&fsym, "fs", false, "") fs.BoolVar(&fsym, "fs", false, "")
@ -163,6 +169,10 @@ func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error
return err return err
} }
if fpath == outputP {
return nil
}
if fi.IsDir() { if fi.IsDir() {
return nil return nil
} }
@ -363,20 +373,45 @@ func packDirectory(excludePrefix []string, excludeSuffix []string, includePath .
return return
} }
func packApp(cmd *Command, args []string) { func isBeegoProject(thePath string) bool {
if len(args) == 0 { fh, _ := os.Open(thePath)
fmt.Fprintln(os.Stderr, "need appPath") fis, _ := fh.Readdir(-1)
cmdPack.Usage() regex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?"github.com/astaxie/beego".*?\).*func main()`)
for _, fi := range fis {
if fi.IsDir() == false && strings.HasSuffix(fi.Name(), ".go") {
data, err := ioutil.ReadFile(path.Join(thePath, fi.Name()))
if err != nil {
continue
}
if len(regex.Find(data)) > 0 {
return true
}
}
} }
return false
}
func packApp(cmd *Command, args []string) {
curPath, _ := os.Getwd() curPath, _ := os.Getwd()
thePath := "" thePath := ""
appPath := args[0]
nArgs := []string{}
has := false
for _, a := range args {
if a != "" && a[0] == '-' {
has = true
}
if has {
nArgs = append(nArgs, a)
}
}
cmdPack.Flag.Parse(nArgs)
if path.IsAbs(appPath) == false { if path.IsAbs(appPath) == false {
thePath = path.Join(curPath, appPath) appPath = path.Join(curPath, appPath)
} }
thePath, err := path.Abs(thePath)
thePath, err := path.Abs(appPath)
if err != nil { if err != nil {
exitPrint(fmt.Sprintf("wrong app path: %s", thePath)) exitPrint(fmt.Sprintf("wrong app path: %s", thePath))
} }
@ -384,6 +419,12 @@ func packApp(cmd *Command, args []string) {
exitPrint(fmt.Sprintf("not exist app path: %s", thePath)) exitPrint(fmt.Sprintf("not exist app path: %s", thePath))
} }
if isBeegoProject(thePath) == false {
exitPrint(fmt.Sprintf("not support non beego project"))
}
fmt.Printf("app path: %s\n", thePath)
appName := path.Base(thePath) appName := path.Base(thePath)
goos := runtime.GOOS goos := runtime.GOOS
@ -427,14 +468,21 @@ func packApp(cmd *Command, args []string) {
format = "tar.gz" format = "tar.gz"
} }
if outputP == "" { outputN := appName + "." + format
outputP = path.Join(curPath, appName+"."+format)
if outputP == "" || path.IsAbs(outputP) == false {
outputP = path.Join(curPath, outputP)
} }
if stat, err := os.Stat(outputP); err == nil && stat.IsDir() { if _, err := os.Stat(outputP); err != nil {
outputP = path.Join(outputP, appName+"."+format) err = os.MkdirAll(outputP, 0755)
if err != nil {
exitPrint(err.Error())
}
} }
outputP = path.Join(outputP, outputN)
var exp, exs []string var exp, exs []string
for _, p := range strings.Split(excludeP, ":") { for _, p := range strings.Split(excludeP, ":") {
if len(p) > 0 { if len(p) > 0 {