diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-09 22:21:16 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-09 22:21:16 -0700 |
commit | 06c4fedf892d065d579b95e3416b567e19ff729b (patch) | |
tree | 61b04ebd45f0ff08ad3f302a3ce8fe046267e08c | |
parent | 5deea97f48a0c6205642dc79fd538b3a53919ef6 (diff) |
added a build and run cli command to package, vendor & compile code, and then run the built binary using the same command signature as $ ponze serve
-rw-r--r-- | cmd/ponzu/main.go | 33 | ||||
-rw-r--r-- | cmd/ponzu/options.go | 84 |
2 files changed, 114 insertions, 3 deletions
diff --git a/cmd/ponzu/main.go b/cmd/ponzu/main.go index ddc4b98..fcb6035 100644 --- a/cmd/ponzu/main.go +++ b/cmd/ponzu/main.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "os/exec" "strings" "github.com/bosssauce/ponzu/system/admin" @@ -116,6 +117,38 @@ func main() { fmt.Println(err) os.Exit(1) } + case "build": + err := buildPonzuServer(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + case "run": + if len(args) < 2 { + flag.PrintDefaults() + os.Exit(1) + } + + var addTLS string + if tls { + addTLS = "--tls" + } else { + addTLS = "--tls=false" + } + serve := exec.Command("./ponzu-server", + fmt.Sprintf("--port=%d", port), + addTLS, + "serve", + args[1], + ) + + err := serve.Run() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + case "serve", "s": db.Init() if len(args) > 1 { diff --git a/cmd/ponzu/options.go b/cmd/ponzu/options.go index ff58ae0..5b3a633 100644 --- a/cmd/ponzu/options.go +++ b/cmd/ponzu/options.go @@ -1,8 +1,11 @@ package main import ( + "errors" "fmt" "html/template" + "io" + "io/ioutil" "os" "os/exec" "path/filepath" @@ -190,9 +193,11 @@ func newProjectInDir(path string) error { return createProjInDir(path) } +var ponzuRepo = []string{"github.com", "bosssauce", "ponzu"} + func createProjInDir(path string) error { gopath := os.Getenv("GOPATH") - repo := []string{"github.com", "bosssauce", "ponzu"} + repo := ponzuRepo local := filepath.Join(gopath, "src", filepath.Join(repo...)) network := "https://" + strings.Join(repo, "/") + ".git" @@ -235,8 +240,9 @@ func createProjInDir(path string) error { // create a 'vendor' directory in $path/cmd/ponzu and move 'content', // 'management' and 'system' packages into it - vendorPath := filepath.Join(path, "cmd", "ponzu", "vendor") - err = os.Mkdir(vendorPath, os.ModeDir|os.ModePerm) + + vendorPath := filepath.Join(path, "cmd", "ponzu", "vendor", "github.com", "bosssauce", "ponzu") + err = os.MkdirAll(vendorPath, os.ModeDir|os.ModePerm) if err != nil { // TODO: rollback, remove ponzu project from path return err @@ -269,3 +275,75 @@ func createProjInDir(path string) error { fmt.Println("New ponzu project created at", path) return nil } + +func buildPonzuServer(args []string) error { + // copy all ./content .go files to $vendor/content + // check to see if any file exists, move on to next file if so, + // and report this conflict to user for them to fix & re-run build + pwd, err := os.Getwd() + if err != nil { + return err + } + + contentSrcPath := filepath.Join(pwd, "content") + contentDstPath := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "bosssauce", "ponzu", "content") + + srcFiles, err := ioutil.ReadDir(contentSrcPath) + if err != nil { + return err + } + + var conflictFiles = []string{"item.go", "types.go"} + var mustRenameFiles = []string{} + for _, srcFileInfo := range srcFiles { + // check srcFile exists in contentDstPath + for _, conflict := range conflictFiles { + if srcFileInfo.Name() == conflict { + mustRenameFiles = append(mustRenameFiles, conflict) + continue + } + } + + dstFile, err := os.Create(filepath.Join(contentDstPath, srcFileInfo.Name())) + if err != nil { + return err + } + + srcFile, err := os.Open(filepath.Join(contentSrcPath, srcFileInfo.Name())) + if err != nil { + return err + } + + _, err = io.Copy(dstFile, srcFile) + if err != nil { + return err + } + } + + if len(mustRenameFiles) > 1 { + fmt.Println("Ponzu couldn't fully build your project:") + fmt.Println("Some of your files in the content directory exist in the vendored directory.") + fmt.Println("You must rename the following files, as they conflict with Ponzu core:") + for _, file := range mustRenameFiles { + fmt.Println(file) + } + + fmt.Println("Once the files above have been renamed, run '$ ponzu build' to retry.") + return errors.New("Ponzu has very few internal conflicts, sorry for the inconvenience.") + } + + // execute go build -o ponzu-cms cmd/ponzu/*.go + build := exec.Command("go", "build", "-o", "ponzu-server", "cmd/ponzu/*.go") + err = build.Start() + if err != nil { + return errors.New("Ponzu build step failed. Please try again. " + "\n" + err.Error()) + + } + err = build.Wait() + if err != nil { + return errors.New("Ponzu build step failed. Please try again. " + "\n" + err.Error()) + + } + + return nil +} |