github.com/bketelsen/buffalo@v0.9.5/generators/newapp/new.go (about) 1 package newapp 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 8 "github.com/gobuffalo/buffalo/generators" 9 "github.com/gobuffalo/buffalo/generators/assets/standard" 10 "github.com/gobuffalo/buffalo/generators/assets/webpack" 11 "github.com/gobuffalo/buffalo/generators/docker" 12 "github.com/gobuffalo/buffalo/generators/refresh" 13 "github.com/gobuffalo/envy" 14 "github.com/gobuffalo/makr" 15 "github.com/pkg/errors" 16 ) 17 18 // App is the representation of a new Buffalo application 19 type App struct { 20 RootPath string 21 Name string 22 Force bool 23 Verbose bool 24 SkipPop bool 25 SkipWebpack bool 26 SkipYarn bool 27 DBType string 28 CIProvider string 29 API bool 30 WithDep bool 31 Docker string 32 } 33 34 // Generator returns a generator to create a new application 35 func (a *App) Generator(data makr.Data) (*makr.Generator, error) { 36 g := makr.New() 37 g.Add(makr.NewCommand(makr.GoGet("golang.org/x/tools/cmd/goimports", "-u"))) 38 g.Add(makr.NewCommand(makr.GoGet("github.com/golang/dep/cmd/dep", "-u"))) 39 g.Add(makr.NewCommand(makr.GoGet("github.com/motemen/gore", "-u"))) 40 41 files, err := generators.Find(filepath.Join(generators.TemplatesPath, "newapp")) 42 if err != nil { 43 return nil, errors.WithStack(err) 44 } 45 46 for _, f := range files { 47 g.Add(makr.NewFile(f.WritePath, f.Body)) 48 } 49 rr, err := refresh.New() 50 if err != nil { 51 return nil, err 52 } 53 g.Add(rr) 54 55 if data["ciProvider"] == "travis" { 56 g.Add(makr.NewFile(".travis.yml", nTravis)) 57 } else if data["ciProvider"] == "gitlab-ci" { 58 if _, ok := data["withPop"]; ok { 59 if data["dbType"] == "postgres" { 60 data["testDbUrl"] = "postgres://postgres:postgres@postgres:5432/" + data["name"].(string) + "_test?sslmode=disable" 61 } else if data["dbType"] == "mysql" { 62 data["testDbUrl"] = "mysql://root:root@mysql:3306/" + data["name"].(string) + "_test" 63 } else { 64 data["testDbUrl"] = "" 65 } 66 g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCi)) 67 } else { 68 g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCiNoPop)) 69 } 70 } 71 72 if !a.API { 73 if a.SkipWebpack { 74 wg, err := standard.New(data) 75 if err != nil { 76 return g, errors.WithStack(err) 77 } 78 g.Add(wg) 79 } else { 80 wg, err := webpack.New(data) 81 if err != nil { 82 return g, errors.WithStack(err) 83 } 84 g.Add(wg) 85 } 86 } 87 if !a.SkipPop { 88 g.Add(newSodaGenerator()) 89 } 90 if a.API { 91 g.Add(makr.Func{ 92 Runner: func(path string, data makr.Data) error { 93 return os.RemoveAll(filepath.Join(path, "templates")) 94 }, 95 }) 96 g.Add(makr.Func{ 97 Runner: func(path string, data makr.Data) error { 98 return os.RemoveAll(filepath.Join(path, "locales")) 99 }, 100 }) 101 } 102 if a.Docker != "none" { 103 dg, err := docker.New() 104 if err != nil { 105 return g, errors.WithStack(err) 106 } 107 g.Add(dg) 108 } 109 g.Add(makr.NewCommand(a.goGet())) 110 111 if _, err := exec.LookPath("git"); err == nil { 112 g.Add(makr.NewCommand(exec.Command("git", "init"))) 113 g.Add(makr.NewCommand(exec.Command("git", "add", "."))) 114 g.Add(makr.NewCommand(exec.Command("git", "commit", "-m", "Initial Commit"))) 115 } 116 117 return g, nil 118 } 119 120 func (a App) goGet() *exec.Cmd { 121 if a.WithDep { 122 if _, err := exec.LookPath("dep"); err == nil { 123 return exec.Command("dep", "init") 124 } 125 } 126 appArgs := []string{"get", "-t"} 127 if a.Verbose { 128 appArgs = append(appArgs, "-v") 129 } 130 appArgs = append(appArgs, "./...") 131 return exec.Command(envy.Get("GO_BIN", "go"), appArgs...) 132 } 133 134 const nTravis = ` 135 language: go 136 137 go: 138 - 1.8.x 139 140 env: 141 - GO_ENV=test 142 143 {{ if eq .dbType "postgres" -}} 144 services: 145 - postgresql 146 {{ end -}} 147 148 before_script: 149 {{ if eq .dbType "postgres" -}} 150 - psql -c 'create database {{.name}}_test;' -U postgres 151 {{ end -}} 152 - mkdir -p $TRAVIS_BUILD_DIR/public/assets 153 154 go_import_path: {{.packagePath}} 155 156 install: 157 - go get github.com/gobuffalo/buffalo/buffalo 158 {{ if .withDep -}} 159 - go get github.com/golang/dep/cmd/dep 160 - dep ensure 161 {{ else -}} 162 - go get $(go list ./... | grep -v /vendor/) 163 {{ end -}} 164 165 script: buffalo test 166 ` 167 168 const nGitlabCi = `before_script: 169 - apt-get update && apt-get install -y postgresql-client mysql-client 170 - ln -s /builds /go/src/$(echo "{{.packagePath}}" | cut -d "/" -f1) 171 - cd /go/src/{{.packagePath}} 172 - mkdir -p public/assets 173 - go get -u github.com/gobuffalo/buffalo/buffalo 174 - go get -t -v ./... 175 - export PATH="$PATH:$GOPATH/bin" 176 177 stages: 178 - test 179 180 .test-vars: &test-vars 181 variables: 182 GO_ENV: "test" 183 POSTGRES_DB: "{{.name}}_test" 184 MYSQL_DATABASE: "{{.name}}_test" 185 MYSQL_ROOT_PASSWORD: "root" 186 TEST_DATABASE_URL: "{{.testDbUrl}}" 187 188 # Golang version choice helper 189 .use-golang-image: &use-golang-latest 190 image: golang:latest 191 192 .use-golang-image: &use-golang-1-8 193 image: golang:1.8 194 195 test:latest: 196 <<: *use-golang-latest 197 <<: *test-vars 198 stage: test 199 services: 200 - mysql:latest 201 - postgres:latest 202 script: 203 - buffalo test 204 205 test:1.8: 206 <<: *use-golang-1-8 207 <<: *test-vars 208 stage: test 209 services: 210 - mysql:latest 211 - postgres:latest 212 script: 213 - buffalo test 214 ` 215 216 const nGitlabCiNoPop = `before_script: 217 - ln -s /builds /go/src/$(echo "{{.packagePath}}" | cut -d "/" -f1) 218 - cd /go/src/{{.packagePath}} 219 - mkdir -p public/assets 220 - go get -u github.com/gobuffalo/buffalo/buffalo 221 - go get -t -v ./... 222 - export PATH="$PATH:$GOPATH/bin" 223 224 stages: 225 - test 226 227 .test-vars: &test-vars 228 variables: 229 GO_ENV: "test" 230 231 # Golang version choice helper 232 .use-golang-image: &use-golang-latest 233 image: golang:latest 234 235 .use-golang-image: &use-golang-1-8 236 image: golang:1.8 237 238 test:latest: 239 <<: *use-golang-latest 240 <<: *test-vars 241 stage: test 242 script: 243 - buffalo test 244 245 test:1.8: 246 <<: *use-golang-1-8 247 <<: *test-vars 248 stage: test 249 script: 250 - buffalo test 251 `