Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added build option before running a local application
- Loading branch information
Showing
15 changed files
with
442 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package build | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/eko/monday/pkg/build/command" | ||
"github.com/eko/monday/pkg/config" | ||
"github.com/eko/monday/pkg/helper" | ||
"github.com/eko/monday/pkg/ui" | ||
) | ||
|
||
// Builder represents a local application builder | ||
type Builder interface { | ||
BuildAll() | ||
Build(application *config.Application) | ||
} | ||
|
||
type builder struct { | ||
projectName string | ||
applications []*config.Application | ||
view ui.View | ||
} | ||
|
||
// NewBuilder instanciates a new builder instance | ||
func NewBuilder(view ui.View, project *config.Project) *builder { | ||
return &builder{ | ||
projectName: project.Name, | ||
applications: project.Applications, | ||
view: view, | ||
} | ||
} | ||
|
||
// BuildAll builds all local applications in separated goroutines | ||
func (b *builder) BuildAll() { | ||
var wg = sync.WaitGroup{} | ||
|
||
for _, application := range b.applications { | ||
wg.Add(1) | ||
go func(application *config.Application) { | ||
defer wg.Done() | ||
b.Build(application) | ||
}(application) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
// Build builds the application | ||
func (b *builder) Build(application *config.Application) { | ||
if application.Build == nil { | ||
return | ||
} | ||
|
||
if err := helper.CheckPathExists(application.GetPath()); err != nil { | ||
b.view.Writef("❌ %s\n", err.Error()) | ||
return | ||
} | ||
|
||
var build = application.Build | ||
var err error | ||
|
||
switch build.Type { | ||
case command.BuilderType: | ||
b.view.Writef("⚙️ Building local app '%s' via %s...\n", application.Name, build.Type) | ||
err = command.Build(application, b.view) | ||
|
||
default: | ||
b.view.Writef("❌ Unknown build type '%s' for application '%s'\n", build.Type, application.Name) | ||
} | ||
|
||
if err != nil { | ||
b.view.Writef("❌ Error while building application '%s': %v\n", application.Name, err) | ||
return | ||
} | ||
|
||
b.view.Writef("✅ Build of application '%s' complete!\n", application.Name) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package build | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/eko/monday/pkg/config" | ||
"github.com/eko/monday/pkg/log" | ||
"github.com/eko/monday/pkg/ui" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewBuilder(t *testing.T) { | ||
// Given | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
view := ui.NewMockView(ctrl) | ||
|
||
project := getMockedProjectWithApplication() | ||
|
||
// When | ||
b := NewBuilder(view, project) | ||
|
||
// Then | ||
assert.IsType(t, new(builder), b) | ||
assert.Implements(t, new(Builder), b) | ||
|
||
assert.Equal(t, view, b.view) | ||
assert.Equal(t, project.Name, b.projectName) | ||
assert.Equal(t, project.Applications, b.applications) | ||
} | ||
|
||
func TestBuildAll(t *testing.T) { | ||
// Given | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
view := ui.NewMockView(ctrl) | ||
view.EXPECT().Writef("⚙️ Building local app '%s' via %s...\n", "test-app", "command") | ||
view.EXPECT().Writef("👉 Running commands:\n%s\n", "echo 'ok it works'\necho yes it's ok") | ||
view.EXPECT().Write(log.ColorGreen + "test-app" + log.ColorWhite + " 'ok it works'\n") | ||
view.EXPECT().Write(log.ColorGreen + "test-app" + log.ColorWhite + " yes it's ok\n") | ||
view.EXPECT().Writef("✅ Build of application '%s' complete!\n", "test-app") | ||
|
||
project := getMockedProjectWithApplication() | ||
|
||
builder := NewBuilder(view, project) | ||
|
||
// When - Then | ||
builder.BuildAll() | ||
} | ||
|
||
func getMockedProjectWithApplication() *config.Project { | ||
return &config.Project{ | ||
Name: "My project name", | ||
Applications: []*config.Application{ | ||
{ | ||
Name: "test-app", | ||
Path: "/", | ||
|
||
Build: &config.Build{ | ||
Type: "command", | ||
Commands: []string{ | ||
"echo 'ok it works'", | ||
"echo yes it's ok", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.