diff --git a/generators/newapp/new.go b/generators/newapp/new.go index d0d4bd6c8..76c808553 100644 --- a/generators/newapp/new.go +++ b/generators/newapp/new.go @@ -42,72 +42,37 @@ func (a Generator) Run(root string, data makr.Data) error { } for _, f := range files { - if a.AsAPI { - if strings.Contains(f.WritePath, "locales") || strings.Contains(f.WritePath, "templates") || strings.Contains(f.WritePath, "public") { - continue - } - g.Add(makr.NewFile(f.WritePath, f.Body)) - } else { + if !a.AsAPI { g.Add(makr.NewFile(f.WritePath, f.Body)) + continue } + if strings.Contains(f.WritePath, "locales") || strings.Contains(f.WritePath, "templates") || strings.Contains(f.WritePath, "public") { + continue + } + + g.Add(makr.NewFile(f.WritePath, f.Body)) } + data["name"] = a.Name if err := refresh.Run(root, data); err != nil { return errors.WithStack(err) } - // Add CI configuration, if requested - if a.CIProvider == "travis" { - g.Add(makr.NewFile(".travis.yml", nTravis)) - } else if a.CIProvider == "gitlab-ci" { - if a.WithPop { - if a.DBType == "postgres" { - data["testDbUrl"] = "postgres://postgres:postgres@postgres:5432/" + a.Name.File() + "_test?sslmode=disable" - } else if a.DBType == "mysql" { - data["testDbUrl"] = "mysql://root:root@(mysql:3306)/" + a.Name.File() + "_test" - } else { - data["testDbUrl"] = "" - } - g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCi)) - } else { - g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCiNoPop)) - } - } + a.setupCI(g, data) - if !a.AsAPI { - if a.WithWebpack { - w := webpack.New() - w.App = a.App - w.Bootstrap = a.Bootstrap - if err := w.Run(root, data); err != nil { - return errors.WithStack(err) - } - } else { - if err := standard.Run(root, data); err != nil { - return errors.WithStack(err) - } - } + if err := a.setupWebpack(root, data); err != nil { + return errors.WithStack(err) } - if a.WithPop { - sg := soda.New() - sg.App = a.App - sg.Dialect = a.DBType - data["appPath"] = a.Root - data["name"] = a.Name.File() - if err := sg.Run(root, data); err != nil { - return errors.WithStack(err) - } + + if err := a.setupPop(root, data); err != nil { + return errors.WithStack(err) } - if a.Docker != "none" { - o := docker.New() - o.App = a.App - o.Version = a.Version - if err := o.Run(root, data); err != nil { - return errors.WithStack(err) - } + if err := a.setupDocker(root, data); err != nil { + return errors.WithStack(err) } + g.Add(makr.NewCommand(a.goGet())) g.Add(makr.Func{ @@ -117,19 +82,104 @@ func (a Generator) Run(root string, data makr.Data) error { }, }) - if a.VCS == "git" || a.VCS == "bzr" { - // Execute git or bzr case (same CLI API) - if _, err := exec.LookPath(a.VCS); err == nil { - g.Add(makr.NewCommand(exec.Command(a.VCS, "init"))) - g.Add(makr.NewCommand(exec.Command(a.VCS, "add", "."))) - g.Add(makr.NewCommand(exec.Command(a.VCS, "commit", "-m", "Initial Commit"))) - } - } + a.setupVCS(g) data["opts"] = a return g.Run(root, data) } +func (a Generator) setupVCS(g *makr.Generator) { + if a.VCS != "git" && a.VCS != "bzr" { + return + } + // Execute git or bzr case (same CLI API) + if _, err := exec.LookPath(a.VCS); err != nil { + return + } + + g.Add(makr.NewCommand(exec.Command(a.VCS, "init"))) + g.Add(makr.NewCommand(exec.Command(a.VCS, "add", "."))) + g.Add(makr.NewCommand(exec.Command(a.VCS, "commit", "-m", "Initial Commit"))) +} + +func (a Generator) setupDocker(root string, data makr.Data) error { + if a.Docker == "none" { + return nil + } + + o := docker.New() + o.App = a.App + o.Version = a.Version + if err := o.Run(root, data); err != nil { + return errors.WithStack(err) + } + + return nil +} + +func (a Generator) setupPop(root string, data makr.Data) error { + if !a.WithPop { + return nil + } + + sg := soda.New() + sg.App = a.App + sg.Dialect = a.DBType + data["appPath"] = a.Root + data["name"] = a.Name.File() + + if err := sg.Run(root, data); err != nil { + return errors.WithStack(err) + } + + return nil +} + +func (a Generator) setupWebpack(root string, data makr.Data) error { + if a.AsAPI { + return nil + } + + if a.WithWebpack { + w := webpack.New() + w.App = a.App + w.Bootstrap = a.Bootstrap + if err := w.Run(root, data); err != nil { + return errors.WithStack(err) + } + + return nil + } + + if err := standard.Run(root, data); err != nil { + return errors.WithStack(err) + } + + return nil +} + +func (a Generator) setupCI(g *makr.Generator, data makr.Data) { + + switch a.CIProvider { + case "travis": + g.Add(makr.NewFile(".travis.yml", nTravis)) + case "gitlab-ci": + if a.WithPop { + if a.DBType == "postgres" { + data["testDbUrl"] = "postgres://postgres:postgres@postgres:5432/" + a.Name.File() + "_test?sslmode=disable" + } else if a.DBType == "mysql" { + data["testDbUrl"] = "mysql://root:root@(mysql:3306)/" + a.Name.File() + "_test" + } else { + data["testDbUrl"] = "" + } + g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCi)) + break + } + + g.Add(makr.NewFile(".gitlab-ci.yml", nGitlabCiNoPop)) + } +} + func (a Generator) goGet() *exec.Cmd { cd, _ := os.Getwd() defer os.Chdir(cd)