Skip to content

Commit

Permalink
reducing complexity on the app generator
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni authored and EC2 Default User committed Jan 21, 2018
1 parent 302182e commit 9054e87
Showing 1 changed file with 110 additions and 60 deletions.
170 changes: 110 additions & 60 deletions generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down

0 comments on commit 9054e87

Please sign in to comment.