Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
create prebuilder struct and add import to annotated generation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 8, 2019
1 parent 7910497 commit 2b27242
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 70 deletions.
82 changes: 12 additions & 70 deletions EXPERIMENTAL/typicmd/internal/prebuilder/prebuild.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package prebuilder

import (
"time"

log "github.com/sirupsen/logrus"
"github.com/typical-go/runn"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/bash"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/internal/prebuilder/golang"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/internal/prebuilder/walker"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typienv"
Expand All @@ -21,8 +16,8 @@ var (
// PreBuild process to build the typical project
func PreBuild(ctx *typictx.Context) (err error) {
root := typienv.AppName
projPkgs, filenames, _ := projectFiles(root)
conf := createConfiguration(ctx)
packages, filenames, _ := projectFiles(root)

projFiles, err := walker.WalkProject(filenames)
if err != nil {
return
Expand All @@ -31,70 +26,17 @@ func PreBuild(ctx *typictx.Context) (err error) {
if err != nil {
return
}
return runn.Execute(
typienv.WriteEnvIfNotExist(ctx),
prepareTestTargets(projPkgs),
generateAnnotated(projFiles),
generateConfiguration(conf, ctxFile),
)
}

func prepareTestTargets(projPkgs []string) (err error) {
name := "test_targets"
// err = writeCache(name, projPkgs)
// if err != nil {
// return
// }
return generateTestTargets(name, projPkgs)
}

func generateTestTargets(name string, testTargets []string) error {
defer elapsed("Generate TestTargets")()
pkg := typienv.Dependency.Package
src := golang.NewSourceCode(pkg)
src.AddTestTargets(testTargets...)
target := dependency + "/" + name + ".go"
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
)
}

func generateAnnotated(files *walker.ProjectFiles) error {
defer elapsed("Generate Annotated")()
pkg := typienv.Dependency.Package
name := "annotateds.go"
src := golang.NewSourceCode(pkg)
src.AddConstructors(files.Autowires()...)
src.AddMockTargets(files.Automocks()...)
target := dependency + "/" + name
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
)
}

func generateConfiguration(conf Configuration, ctxFile *walker.ContextFile) error {
defer elapsed("Generate Configuration")()
// TODO: try if manual import can improve goimport execution
pkg := typienv.Dependency.Package
name := "configurations.go"
src := golang.NewSourceCode(pkg).AddStruct(conf.Struct)
src.AddImport("", "github.com/kelseyhightower/envconfig")
for _, imp := range ctxFile.Imports {
src.AddImport(imp.Name, imp.Path)
prebuilder := PreBuilder{
Context: ctx,
Filenames: filenames,
Packages: packages,
ProjectFiles: projFiles,
ContextFile: ctxFile,
}
src.AddConstructors(conf.Constructors...)
target := dependency + "/" + name
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
typienv.WriteEnvIfNotExist(ctx),
prebuilder.TestTargets(),
prebuilder.Annotated(),
prebuilder.Configuration(),
)
}

func elapsed(what string) func() {
start := time.Now()
return func() {
log.Infof("%s took %v\n", what, time.Since(start))
}
}
78 changes: 78 additions & 0 deletions EXPERIMENTAL/typicmd/internal/prebuilder/prebuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package prebuilder

import (
log "github.com/sirupsen/logrus"

"time"

"github.com/typical-go/runn"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/bash"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/internal/prebuilder/golang"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/internal/prebuilder/walker"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typienv"
)

// PreBuilder responsible to prebuild process
type PreBuilder struct {
*typictx.Context
*walker.ProjectFiles
*walker.ContextFile
Filenames []string
Packages []string
}

// TestTargets generate test target
func (p *PreBuilder) TestTargets() error {
defer elapsed("Generate TestTargets")()
pkg := typienv.Dependency.Package
src := golang.NewSourceCode(pkg)
src.AddTestTargets(p.Packages...)
target := dependency + "/test_targets.go"
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
)
}

// Annotated to generate annotated
func (p *PreBuilder) Annotated() error {
defer elapsed("Generate Annotated")()
pkg := typienv.Dependency.Package
src := golang.NewSourceCode(pkg)
for _, pkg := range p.Packages {
src.AddImport("", p.Context.Root+"/"+pkg)
}
src.AddConstructors(p.ProjectFiles.Autowires()...)
src.AddMockTargets(p.ProjectFiles.Automocks()...)
target := dependency + "/annotateds.go"
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
)
}

// Configuration to generate configuration
func (p *PreBuilder) Configuration() error {
defer elapsed("Generate Configuration")()
conf := createConfiguration(p.Context)
pkg := typienv.Dependency.Package
src := golang.NewSourceCode(pkg).AddStruct(conf.Struct)
src.AddImport("", "github.com/kelseyhightower/envconfig")
for _, imp := range p.ContextFile.Imports {
src.AddImport(imp.Name, imp.Path)
}
src.AddConstructors(conf.Constructors...)
target := dependency + "/configurations.go"
return runn.Execute(
src.Cook(target),
bash.GoImports(target),
)
}

func elapsed(what string) func() {
start := time.Now()
return func() {
log.Infof("%s took %v\n", what, time.Since(start))
}
}

0 comments on commit 2b27242

Please sign in to comment.