github.com/travisturner/buffalo@v0.11.1/generators/newapp/new.go (about)

     1  package newapp
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/gobuffalo/buffalo/generators"
    11  	"github.com/gobuffalo/buffalo/generators/assets/standard"
    12  	"github.com/gobuffalo/buffalo/generators/assets/webpack"
    13  	"github.com/gobuffalo/buffalo/generators/docker"
    14  	"github.com/gobuffalo/buffalo/generators/refresh"
    15  	"github.com/gobuffalo/buffalo/generators/soda"
    16  	"github.com/gobuffalo/envy"
    17  	"github.com/gobuffalo/makr"
    18  	"github.com/gobuffalo/packr"
    19  	"github.com/pkg/errors"
    20  )
    21  
    22  // Run returns a generator to create a new application
    23  func (a Generator) Run(root string, data makr.Data) error {
    24  	g := makr.New()
    25  
    26  	if a.AsAPI {
    27  		defer os.RemoveAll(filepath.Join(a.Root, "templates"))
    28  		defer os.RemoveAll(filepath.Join(a.Root, "locales"))
    29  		defer os.RemoveAll(filepath.Join(a.Root, "public"))
    30  	}
    31  	if a.Force {
    32  		os.RemoveAll(a.Root)
    33  	}
    34  
    35  	g.Add(makr.NewCommand(makr.GoGet("golang.org/x/tools/cmd/goimports", "-u")))
    36  	if a.WithDep {
    37  		g.Add(makr.NewCommand(makr.GoGet("github.com/golang/dep/cmd/dep", "-u")))
    38  	}
    39  
    40  	files, err := generators.FindByBox(packr.NewBox("../newapp/templates"))
    41  	if err != nil {
    42  		return errors.WithStack(err)
    43  	}
    44  
    45  	for _, f := range files {
    46  		if !a.AsAPI {
    47  			g.Add(makr.NewFile(f.WritePath, f.Body))
    48  			continue
    49  		}
    50  
    51  		if strings.Contains(f.WritePath, "locales") || strings.Contains(f.WritePath, "templates") || strings.Contains(f.WritePath, "public") {
    52  			continue
    53  		}
    54  
    55  		g.Add(makr.NewFile(f.WritePath, f.Body))
    56  	}
    57  
    58  	data["name"] = a.Name
    59  	if err := refresh.Run(root, data); err != nil {
    60  		return errors.WithStack(err)
    61  	}
    62  
    63  	a.setupCI(g, data)
    64  
    65  	if err := a.setupWebpack(root, data); err != nil {
    66  		return errors.WithStack(err)
    67  	}
    68  
    69  	if err := a.setupPop(root, data); err != nil {
    70  		return errors.WithStack(err)
    71  	}
    72  
    73  	if err := a.setupDocker(root, data); err != nil {
    74  		return errors.WithStack(err)
    75  	}
    76  
    77  	g.Add(makr.NewCommand(a.goGet()))
    78  
    79  	g.Add(makr.Func{
    80  		Runner: func(root string, data makr.Data) error {
    81  			g.Fmt(root)
    82  			return nil
    83  		},
    84  	})
    85  
    86  	a.setupVCS(g)
    87  
    88  	data["opts"] = a
    89  	return g.Run(root, data)
    90  }
    91  
    92  func (a Generator) setupVCS(g *makr.Generator) {
    93  	if a.VCS != "git" && a.VCS != "bzr" {
    94  		return
    95  	}
    96  	// Execute git or bzr case (same CLI API)
    97  	if _, err := exec.LookPath(a.VCS); err != nil {
    98  		return
    99  	}
   100  
   101  	// Create .gitignore or .bzrignore
   102  	g.Add(makr.NewFile(fmt.Sprintf(".%signore", a.VCS), nVCSIgnore))
   103  	g.Add(makr.NewCommand(exec.Command(a.VCS, "init")))
   104  	args := []string{"add", "."}
   105  	if a.VCS == "bzr" {
   106  		// Ensure Bazaar is as quiet as Git
   107  		args = append(args, "-q")
   108  	}
   109  	g.Add(makr.NewCommand(exec.Command(a.VCS, args...)))
   110  	g.Add(makr.NewCommand(exec.Command(a.VCS, "commit", "-q", "-m", "Initial Commit")))
   111  }
   112  
   113  func (a Generator) setupDocker(root string, data makr.Data) error {
   114  	if a.Docker == "none" {
   115  		return nil
   116  	}
   117  
   118  	o := docker.New()
   119  	o.App = a.App
   120  	o.Version = a.Version
   121  	if err := o.Run(root, data); err != nil {
   122  		return errors.WithStack(err)
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (a Generator) setupPop(root string, data makr.Data) error {
   129  	if !a.WithPop {
   130  		return nil
   131  	}
   132  
   133  	sg := soda.New()
   134  	sg.App = a.App
   135  	sg.Dialect = a.DBType
   136  	data["appPath"] = a.Root
   137  	data["name"] = a.Name.File()
   138  
   139  	if err := sg.Run(root, data); err != nil {
   140  		return errors.WithStack(err)
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  func (a Generator) setupWebpack(root string, data makr.Data) error {
   147  	if a.AsAPI {
   148  		return nil
   149  	}
   150  
   151  	if a.WithWebpack {
   152  		w := webpack.New()
   153  		w.App = a.App
   154  		w.Bootstrap = a.Bootstrap
   155  		if err := w.Run(root, data); err != nil {
   156  			return errors.WithStack(err)
   157  		}
   158  
   159  		return nil
   160  	}
   161  
   162  	if err := standard.Run(root, data); err != nil {
   163  		return errors.WithStack(err)
   164  	}
   165  
   166  	return nil
   167  }
   168  
   169  func (a Generator) setupCI(g *makr.Generator, data makr.Data) {
   170  
   171  	switch a.CIProvider {
   172  	case "travis":
   173  		g.Add(makr.NewFile(".travis.yml", nTravis))
   174  	case "gitlab-ci":
   175  		if a.WithPop {
   176  			if a.DBType == "postgres" {
   177  				data["testDbUrl"] = "postgres://postgres:postgres@postgres:5432/" + a.Name.File() + "_test?sslmode=disable"
   178  			} else if a.DBType == "mysql" {
   179  				data["testDbUrl"] = "mysql://root:root@(mysql:3306)/" + a.Name.File() + "_test"
   180  			} else {
   181  				data["testDbUrl"] = ""
   182  			}
   183  			g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCi))
   184  			break
   185  		}
   186  
   187  		g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCiNoPop))
   188  	}
   189  }
   190  
   191  func (a Generator) goGet() *exec.Cmd {
   192  	cd, _ := os.Getwd()
   193  	defer os.Chdir(cd)
   194  	os.Chdir(a.Root)
   195  	if a.WithDep {
   196  		if _, err := exec.LookPath("dep"); err == nil {
   197  			return exec.Command("dep", "init")
   198  		}
   199  	}
   200  	appArgs := []string{"get", "-t"}
   201  	if a.Verbose {
   202  		appArgs = append(appArgs, "-v")
   203  	}
   204  	appArgs = append(appArgs, "./...")
   205  	return exec.Command(envy.Get("GO_BIN", "go"), appArgs...)
   206  }
   207  
   208  const nTravis = `language: go
   209  
   210  go:
   211    - 1.8.x
   212  
   213  env:
   214    - GO_ENV=test
   215  
   216  {{ if eq .opts.DBType "postgres" -}}
   217  services:
   218    - postgresql
   219  {{- end }}
   220  
   221  before_script:
   222  {{- if eq .opts.DBType "postgres" }}
   223    - psql -c 'create database {{.opts.Name.File}}_test;' -U postgres
   224  {{- end }}
   225    - mkdir -p $TRAVIS_BUILD_DIR/public/assets
   226  
   227  go_import_path: {{.opts.PackagePkg}}
   228  
   229  install:
   230    - go get github.com/gobuffalo/buffalo/buffalo
   231  {{- if .opts.WithDep }}
   232    - go get github.com/golang/dep/cmd/dep
   233    - dep ensure
   234  {{- else }}
   235    - go get $(go list ./... | grep -v /vendor/)
   236  {{- end }}
   237  
   238  script: buffalo test
   239  `
   240  
   241  const nGitlabCi = `before_script:
   242    - ln -s /builds /go/src/$(echo "{{.opts.PackagePkg}}" | cut -d "/" -f1)
   243    - cd /go/src/{{.opts.PackagePkg}}
   244    - mkdir -p public/assets
   245    - go get -u github.com/gobuffalo/buffalo/buffalo
   246  {{- if .opts.WithDep }}
   247    - go get github.com/golang/dep/cmd/dep
   248    - dep ensure
   249  {{- else }}
   250    - go get -t -v ./...
   251  {{- end }}
   252    - export PATH="$PATH:$GOPATH/bin"
   253  
   254  stages:
   255    - test
   256  
   257  .test-vars: &test-vars
   258    variables:
   259      GO_ENV: "test"
   260  {{- if eq .opts.DBType "postgres" }}
   261      POSTGRES_DB: "{{.opts.Name.File}}_test"
   262  {{- else if eq .opts.DBType "mysql" }}
   263      MYSQL_DATABASE: "{{.opts.Name.File}}_test"
   264      MYSQL_ROOT_PASSWORD: "root"
   265  {{- end }}
   266      TEST_DATABASE_URL: "{{.testDbUrl}}"
   267  
   268  # Golang version choice helper
   269  .use-golang-image: &use-golang-latest
   270    image: golang:latest
   271  
   272  .use-golang-image: &use-golang-1-8
   273    image: golang:1.8
   274  
   275  test:latest:
   276    <<: *use-golang-latest
   277    <<: *test-vars
   278    stage: test
   279    services:
   280  {{- if eq .opts.DBType "mysql" }}
   281      - mysql:latest
   282  {{- else if eq .opts.DBType "postgres" }}
   283      - postgres:latest
   284  {{- end }}
   285    script:
   286      - buffalo test
   287  
   288  test:1.8:
   289    <<: *use-golang-1-8
   290    <<: *test-vars
   291    stage: test
   292    services:
   293  {{- if eq .opts.DBType "mysql" }}
   294      - mysql:latest
   295  {{- else if eq .opts.DBType "postgres" }}
   296      - postgres:latest
   297  {{- end }}
   298    script:
   299      - buffalo test
   300  `
   301  
   302  const nGitlabCiNoPop = `before_script:
   303    - ln -s /builds /go/src/$(echo "{{.opts.PackagePkg}}" | cut -d "/" -f1)
   304    - cd /go/src/{{.opts.PackagePkg}}
   305    - mkdir -p public/assets
   306    - go get -u github.com/gobuffalo/buffalo/buffalo
   307  {{- if .opts.WithDep }}
   308    - go get github.com/golang/dep/cmd/dep
   309    - dep ensure
   310  {{- else }}
   311    - go get -t -v ./...
   312  {{- end }}
   313    - export PATH="$PATH:$GOPATH/bin"
   314  
   315  stages:
   316    - test
   317  
   318  .test-vars: &test-vars
   319    variables:
   320      GO_ENV: "test"
   321  
   322  # Golang version choice helper
   323  .use-golang-image: &use-golang-latest
   324    image: golang:latest
   325  
   326  .use-golang-image: &use-golang-1-8
   327    image: golang:1.8
   328  
   329  test:latest:
   330    <<: *use-golang-latest
   331    <<: *test-vars
   332    stage: test
   333    script:
   334      - buffalo test
   335  
   336  test:1.8:
   337    <<: *use-golang-1-8
   338    <<: *test-vars
   339    stage: test
   340    script:
   341      - buffalo test
   342  `
   343  
   344  const nVCSIgnore = `vendor/
   345  **/*.log
   346  **/*.sqlite
   347  .idea/
   348  bin/
   349  tmp/
   350  node_modules/
   351  .sass-cache/
   352  *-packr.go
   353  public/assets/
   354  {{ .opts.Name.File }}
   355  .vscode/
   356  .grifter/
   357  .env
   358  `