Skip to content

Commit

Permalink
Merge pull request #30 from bootun/win_support
Browse files Browse the repository at this point in the history
support windows
  • Loading branch information
GrantZheng committed Feb 6, 2022
2 parents dcf2c47 + 826069e commit f2f548d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 66 deletions.
14 changes: 9 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
"github.com/spf13/viper"
)

const version = "v1.1.2"

// RootCmd is the root command of kit
var RootCmd = &cobra.Command{
Use: "kit",
Short: "Go-Kit CLI",
Use: "kit",
Short: "Go-Kit CLI",
Version: version,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Expand Down Expand Up @@ -46,8 +49,8 @@ func checkProtoc() bool {
`Install proto3.
https://github.com/google/protobuf/releases
Update protoc Go bindings via
> go get -u github.com/golang/protobuf/proto
> go get -u github.com/golang/protobuf/protoc-gen-go
> go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
> go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
See also
https://github.com/grpc/grpc-go/tree/master/examples`,
Expand All @@ -60,7 +63,8 @@ https://github.com/grpc/grpc-go/tree/master/examples`,
> ./autogen.sh ; ./configure ; make ; make install
Update protoc Go bindings via
> go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
> go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
> go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
See also
https://github.com/grpc/grpc-go/tree/master/examples`,
Expand Down
70 changes: 11 additions & 59 deletions generator/add_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ package generator

import (
"bytes"
"errors"
"fmt"
"path"
"strings"

"os/exec"

"os"

"os/exec"
"path"
"runtime"
"strings"

"errors"

"github.com/dave/jennifer/jen"
"github.com/emicklei/proto"
"github.com/emicklei/proto-contrib/pkg/protofmt"
"github.com/GrantZheng/kit/fs"
grpcTemplate "github.com/GrantZheng/kit/generator/template/grpc"
"github.com/GrantZheng/kit/parser"
"github.com/GrantZheng/kit/utils"
"github.com/dave/jennifer/jen"
"github.com/emicklei/proto"
"github.com/emicklei/proto-contrib/pkg/protofmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -756,65 +753,20 @@ func (g *generateGRPCTransportProto) Generate() (err error) {
if runtime.GOOS == "windows" {
return g.fs.WriteFile(
g.compileFilePath,
fmt.Sprintf(`:: Install proto3.
:: https://github.com/google/protobuf/releases
:: Update protoc Go bindings via
:: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
:: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
::
:: See also
:: https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`, g.name),
grpcTemplate.WindowsScriptText(g.name),
false,
)
}
if runtime.GOOS == "darwin" {
return g.fs.WriteFile(
g.compileFilePath,
fmt.Sprintf(`#!/usr/bin/env sh
# Install proto3 from source macOS only.
# brew install autoconf automake libtool
# git clone https://github.com/google/protobuf
# ./autogen.sh ; ./configure ; make ; make install
#
# Update protoc Go bindings via
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# See also
# https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`, g.name),
grpcTemplate.DarwinScriptText(g.name),
false,
)
}
return g.fs.WriteFile(
g.compileFilePath,
fmt.Sprintf(`#!/usr/bin/env sh
# Install proto3
# sudo apt-get install -y git autoconf automake libtool curl make g++ unzip
# git clone https://github.com/google/protobuf.git
# cd protobuf/
# ./autogen.sh
# ./configure
# make
# make check
# sudo make install
# sudo ldconfig # refresh shared library cache.
#
# Update protoc Go bindings via
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
#
# See also
# https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`, g.name),
grpcTemplate.ScriptText(g.name),
false,
)
}
Expand Down
13 changes: 11 additions & 2 deletions generator/new_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"os/exec"
"path"
"runtime"
"strings"

"github.com/dave/jennifer/jen"
"github.com/GrantZheng/kit/fs"
"github.com/GrantZheng/kit/utils"
"github.com/dave/jennifer/jen"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -78,14 +79,22 @@ func (g *NewService) genModule() error {
moduleName = strings.Join(moduleNameSlice, "/")
}
cmdStr := "cd " + prjName + " && go mod init " + moduleName
cmd := exec.Command("sh", "-c", cmdStr)
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("sh", "-c", cmdStr)
}

var stderr bytes.Buffer
cmd.Stderr = &stderr

_, err := cmd.Output()
// return cmd.Stderr to debug (err here provides nothing useful, only `exit status 1`)
if err != nil {
if runtime.GOOS == "windows" {
return fmt.Errorf("genModule: cmd /C %s => err:%v", cmdStr, err.Error()+" , "+stderr.String())
}
return fmt.Errorf("genModule: sh -c %s => err:%v", cmdStr, err.Error()+" , "+stderr.String())
}
return nil
Expand Down
74 changes: 74 additions & 0 deletions generator/template/grpc/script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package grpc

import "fmt"

const (
scriptTemplate = `#!/usr/bin/env sh
# Install proto3
# sudo apt-get install -y git autoconf automake libtool curl make g++ unzip
# git clone https://github.com/google/protobuf.git
# cd protobuf/
# ./autogen.sh
# ./configure
# make
# make check
# sudo make install
# sudo ldconfig # refresh shared library cache.
#
# Update protoc Go bindings via
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
#
# See also
# https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`

windowsScriptTemplate = `:: Install proto3.
:: https://github.com/google/protobuf/releases
:: Update protoc Go bindings via
:: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
:: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
::
:: See also
:: https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`

darwinScriptText = `#!/usr/bin/env sh
# Install proto3 from source macOS only.
# brew install autoconf automake libtool
# git clone https://github.com/google/protobuf
# ./autogen.sh ; ./configure ; make ; make install
#
# Update protoc Go bindings via
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# See also
# https://github.com/grpc/grpc-go/tree/master/examples
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative %s`
)

// WindowsScriptText return gRPC compile script text for windows,
// it uses the syntax of windows batch
func WindowsScriptText(name string) string {
return fmt.Sprintf(windowsScriptTemplate, name)
}

// DarwinScriptText return gRPC compile script text for macOS, it
// uses the syntax of shell script
func DarwinScriptText(name string) string {
return fmt.Sprintf(darwinScriptText, name)
}

// ScriptText return gRPC compile script text, it uses the syntax
// of shell script
func ScriptText(name string) string {
return fmt.Sprintf(scriptTemplate, name)
}

0 comments on commit f2f548d

Please sign in to comment.