Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add build command #422

Merged
merged 13 commits into from
Feb 26, 2024
103 changes: 103 additions & 0 deletions console/console/build_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package console

import (
"fmt"
"golang.org/x/exp/slices"
"os"
"os/exec"

"github.com/gookit/color"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
)

type BuildCommand struct {
config config.Config
}

func NewBuildCommand(config config.Config) *BuildCommand {
return &BuildCommand{
config: config,
}
}

// Signature The name and signature of the console command.
func (receiver *BuildCommand) Signature() string {
return "build"

Check warning on line 28 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

// Description The console command description.
func (receiver *BuildCommand) Description() string {
return "Build the application"

Check warning on line 33 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}

// Extend The console command extend.
func (receiver *BuildCommand) Extend() command.Extend {
return command.Extend{
Category: "build",
Flags: []command.Flag{
&command.StringFlag{
Name: "system",
Aliases: []string{"s"},
Value: "",
Usage: "target system os",
},
},

Check warning on line 47 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L37-L47

Added lines #L37 - L47 were not covered by tests
}
}

// Handle Execute the console command.
func (receiver *BuildCommand) Handle(ctx console.Context) error {
if receiver.config.GetString("app.env") == "production" {
color.Yellowln("**************************************")
color.Yellowln("* Application In Production! *")
color.Yellowln("**************************************")
color.Println(color.New(color.Green).Sprintf("Do you really wish to run this command? (yes/no) ") + "[" + color.New(color.Yellow).Sprintf("no") + "]" + ":")

var result string
_, err := fmt.Scanln(&result)
if err != nil {
color.Redln(err.Error())

Check warning on line 62 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L62

Added line #L62 was not covered by tests

return nil

Check warning on line 64 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L64

Added line #L64 was not covered by tests
}

if result != "yes" {
color.Yellowln("Command Canceled")

return nil
}
}

system := ctx.Option("system")
if !slices.Contains([]string{"linux", "windows", "darwin"}, system) {
color.Redln("The value of the --system flag is invalid.")
return nil

Check warning on line 77 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

if err := receiver.buildTheApplication(system); err != nil {
color.Redln(err.Error())

Check warning on line 81 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L81

Added line #L81 was not covered by tests

return nil

Check warning on line 83 in console/console/build_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/build_command.go#L83

Added line #L83 was not covered by tests
}

color.Greenln("Built successfully.")

return nil
}

// buildTheApplication Build the application executable.
func (receiver *BuildCommand) buildTheApplication(system string) error {
os.Setenv("CGO_ENABLED", "0")
os.Setenv("GOOS", system)
os.Setenv("GOARCH", "amd64")
cmd := exec.Command(
"go",
"build",
".",
)
_, err := cmd.Output()
return err
}
54 changes: 54 additions & 0 deletions console/console/build_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package console

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

mocksconfig "github.com/goravel/framework/mocks/config"
mocksconsole "github.com/goravel/framework/mocks/console"
)

func TestBuildCommand(t *testing.T) {
mockConfig := &mocksconfig.Config{}
mockConfig.On("GetString", "app.env").Return("local").Once()

newBuildCommand := NewBuildCommand(mockConfig)
mockContext := &mocksconsole.Context{}
mockContext.On("Option", "system").Return("linux").Once()

assert.Nil(t, newBuildCommand.Handle(mockContext))

mockConfig.On("GetString", "app.env").Return("production").Once()
mockContext.On("Option", "system").Return("linux").Once()

reader, writer, err := os.Pipe()
assert.Nil(t, err)
originalStdin := os.Stdin
defer func() { os.Stdin = originalStdin }()
os.Stdin = reader
go func() {
defer writer.Close()
_, err = writer.Write([]byte("yes\n"))
assert.Nil(t, err)
}()

assert.Nil(t, newBuildCommand.Handle(mockContext))

mockConfig.On("GetString", "app.env").Return("production").Once()
mockContext.On("Option", "system").Return("linux").Once()

reader, writer, err = os.Pipe()
assert.Nil(t, err)
originalStdin = os.Stdin
defer func() { os.Stdin = originalStdin }()
os.Stdin = reader
go func() {
defer writer.Close()
_, err = writer.Write([]byte("no\n"))
assert.Nil(t, err)
}()

assert.Nil(t, newBuildCommand.Handle(mockContext))
}
1 change: 1 addition & 0 deletions console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
console.NewListCommand(artisan),
console.NewKeyGenerateCommand(config),
console.NewMakeCommand(),
console.NewBuildCommand(config),

Check warning on line 31 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L31

Added line #L31 was not covered by tests
})
}
Loading