Skip to content

Commit

Permalink
move idl into project dir on generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritesh-Patel committed Oct 5, 2019
1 parent d92dcae commit eba25fe
Show file tree
Hide file tree
Showing 26 changed files with 197 additions and 94 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
@@ -0,0 +1,17 @@
FROM golang:1.12.6@sha256:83e8267be041b3ddf6a5792c7e464528408f75c446745642db08cfe4e8d58d18 AS build
WORKDIR /cache
COPY go.mod .
COPY go.sum .
RUN go mod tidy && go mod download
COPY . .
RUN apt-get update && apt-get -y install sudo && apt-get -y install unzip
RUN make deps-linux && mv /go/bin/protoc-* /usr/local/bin/
RUN make install-go && \
mv /go/bin/sprout /usr/local/bin/


FROM alpine
WORKDIR project
RUN apk add --update make
COPY --from=build /usr/local/bin /usr/local/bin
COPY --from=build /usr/local/include /usr/include
17 changes: 9 additions & 8 deletions Makefile
Expand Up @@ -14,8 +14,8 @@ deps-linux: deps-go deps-protoc-linux deps-grpc-web-linux
deps-protoc-linux:
curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3
sudo mv protoc3/bin/* /usr/local/bin/
sudo mv protoc3/include/* /usr/local/include/
sudo mv protoc3/bin/* /usr/local/bin
sudo mv protoc3/include/* /usr/local/include
rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip

deps-grpc-web-linux:
Expand All @@ -36,19 +36,20 @@ run:
go run main.go

build:
packr2 build -o sprout
CGO_ENABLED=0 packr2 build -o sprout
packr2 clean

build-example: build clean-example
mkdir -p example
cd example && ../sprout create "hello-world"
cd example/hello-world && ../../sprout generate -l go

build-docker-local:
docker build . -t sprout:v0

clean-example:
rm -rf example

install-linux: build
mkdir -p ${HOME}/bin
cp sprout ${HOME}/bin/sprout
chmod +x ${HOME}/bin/sprout
@printf "\n Please run 'source ~/.profile' to add this installation to the path."
install-go:
CGO_ENABLED=0 packr2 build -o ${GOPATH}/bin/sprout
packr2 clean
30 changes: 18 additions & 12 deletions README.md
Expand Up @@ -18,24 +18,30 @@ It will also live with your project, when you add a new service to the config it

## What does it generate?

The generation will create 2 folders.
The generation will create project folder, within this there will be your implementation and an IDL folder

* A repo for the IDL's, this folder will also contain generated artifacts from the IDL under 'gen'
* A repo that implements the interfaces of the generated artifacts
* A parrent directory that implements a skeleton and sets up your service implementation of the generated artifacts
* A child directory for the IDL's, this folder will also contain generated artifacts from the IDL under 'gen'

`NOTE: It only creates the folders for these repos, you will still need to create the git repos on your respected platform. Aswell as initialise each folder as a git repo and push when there have been changes. (if there is a strong desire we can look at how to make this process easier.)`

`NOTE: You`

## The development cycle

1) Make folder with the name of your project and within that folder execute `sprout create [PROJECT_NAME]`
1) To create a project run `sprout create [PROJECT_NAME]`
2) A folder will be created and within that update the `sprout.yml` and then run `sprout generate -l=[LANGUAGE OF CHOICE] eg. go`
3) Move back to the root folder and you will see that there is now an idl folder created.
4) Modify the the protobuf services generated with your desired methods
5) Either run `make generate` or return to the application folder and re run `sprout generate`
6) Push up the IDL repo
6) Implement these methods on the main application repo
7) Push up the IDL repo & remove the replace function in the go.mod in the implementation project (this will swap it over to using the live IDL pushed up to git instead of your local one)
8) When you feel the need to add more services add them to the sprout config and re-run `sprout generate` and repeat steps 4 - 7.
3) You will see that there is now an idl folder created.
4) Within the idl folder modify the the protobuf services generated with your desired methods
5) Go up to the parrent directory and re run `sprout generate -l=[LANGUAGE OF CHOICE]`
6) Return back to the parent directory and implement the methods
7) Once you have tested your implementation and are happy with it return to the idl repo push that directory up to git
8) Return to the parent directory and check the depency file, for go it will be the go.mod file remove the lines that point it to your local directory, this will now point it to the version on git that was pushed up previously
10) Test and push up your implementation!
9) When you feel the need to add more services add them to the sprout config and re-run `sprout generate` and repeat steps 4 - 7.

## Usage & installation

As there alot of dependencies it will be easier to use this tool within the provided image, clone the repo and then run `make build-docker-local`. The best way then to use this is to alias `docker run -v "$(pwd):/project" sprout:local` as sprout from then you can use the CLI as if it was installed as usual on your machine.

## Dependencies

Expand Down
18 changes: 16 additions & 2 deletions cmd/create.go
Expand Up @@ -24,6 +24,7 @@ var createCmd = &cobra.Command{
projectName := args[0]

rootDir := fmt.Sprintf("./%v", projectName)
idlDir := fmt.Sprintf("./%v-idl", rootDir)

log.Printf("Creating project %s.", projectName)

Expand All @@ -34,13 +35,26 @@ var createCmd = &cobra.Command{
log.Fatalf("Error creating root: %v ", err)
}

sproutConfigPath := fmt.Sprintf("%v/sprout.yml", projectName)
err = os.Mkdir(idlDir, os.ModePerm)
if os.IsExist(err) {
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}

sproutConfigPath := fmt.Sprintf("%v/sprout.yml", rootDir)

f, err := os.Create(sproutConfigPath)
if err != nil {
log.Printf("Error creating sprout config: %v", err)
}

Templator.Sprout.Execute(f, projectName)

gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir)
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating sprout config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)
},
}
1 change: 0 additions & 1 deletion cmd/generate.go
Expand Up @@ -39,7 +39,6 @@ var generateCmd = &cobra.Command{
cfg.Print()

proto.Generate(Templator, cfg)

switch language {
case Go:
golang.Generate(Templator, cfg)
Expand Down
1 change: 1 addition & 0 deletions example/hello-world/.gitignore
@@ -0,0 +1 @@
/
2 changes: 1 addition & 1 deletion example/hello-world/go.mod
Expand Up @@ -2,7 +2,7 @@ module github.com/yourrepo/hello-world

go 1.12

replace github.com/yourrepo/hello-world-idl => ../hello-world-idl
replace github.com/yourrepo/hello-world-idl => hello-world-idl

require (
github.com/yourrepo/hello-world-idl v0.0.0
Expand Down
@@ -1,13 +1,11 @@
PROTOC_VERSION := 3.9.2
PROTOC_WEB_VERSION := 1.0.6

PROTO_SOURCES := -I /usr/local/include
PROTO_SOURCES += -I .
PROTO_SOURCES += -I ${GOPATH}/src
PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway

deps-linux: deps-protoc-linux deps-grpc-web-linux deps-go
deps-linux: deps-protoc-linux deps-grpc-web-linux

deps-protoc-linux:
curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
Expand All @@ -21,14 +19,9 @@ deps-grpc-web-linux:
sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
chmod +x /usr/local/bin/protoc-gen-grpc-web

deps-go:
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

generate: generate-grpc generate-web generate-http
generate: generate-grpc generate-web

generate-grpc:
generate-grpc:
mkdir -p gen/go
protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/health/*.proto
protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/helloworld/*.proto
Expand All @@ -40,11 +33,4 @@ generate-web:
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/helloworld/*.proto
cp -f -rv gen/web/proto/* gen/web
rm -rf gen/web/Proto gen/web/proto
generate-http:
mkdir -p gen/http gen/swagger
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/health/*.proto
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/helloworld/*.proto
cp -f -rv gen/http/proto/* gen/http
cp -f -rv gen/swagger/proto/* gen/swagger
rm -rf gen/swagger/proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,8 +1,5 @@
import * as jspb from "google-protobuf"

import * as google_api_annotations_pb from '../../google/api/annotations_pb';
import * as protoc$gen$swagger_options_annotations_pb from '../../protoc-gen-swagger/options/annotations_pb';

export class HealthCheckRequest extends jspb.Message {
getService(): string;
setService(value: string): void;
Expand Down
File renamed without changes.
@@ -1,15 +1,6 @@
syntax = "proto3";

package mycompany.health;
import "google/api/annotations.proto";
import "protoc-gen-swagger/options/annotations.proto";

option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = {
info: {
title: "Health Checks";
version: "1.0";
}
};

message HealthCheckRequest {
string service = 1;
Expand All @@ -26,11 +17,7 @@ message HealthCheckResponse {
}

service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse){
option (google.api.http) = {
get: "/v1/health"
};
}
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);

rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}
4 changes: 0 additions & 4 deletions example/hello-world/sprout.yml
Expand Up @@ -14,10 +14,6 @@ network:
http:
enabled: true
port: 8080
web:
enabled: true
port: 8090


services:
- name: helloworld
Expand Down
10 changes: 5 additions & 5 deletions generate/proto/generate.go
Expand Up @@ -20,7 +20,7 @@ func Generate(templator *templator.Templator, config *config.SproutConfig) {
}

func GenerateGoModIDL(templator *templator.Templator, config *config.SproutConfig) {
idlPath := fmt.Sprintf("../%s-idl", config.Name)
idlPath := fmt.Sprintf("%s-idl", config.Name)
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)

f, err := os.Create(idlOutput)
Expand All @@ -34,7 +34,7 @@ func GenerateGoModIDL(templator *templator.Templator, config *config.SproutConfi
}

func GenerateIDLMakefile(templator *templator.Templator, config *config.SproutConfig) {
makeFilePath := fmt.Sprintf("../%s-idl", config.Name)
makeFilePath := fmt.Sprintf("%s-idl", config.Name)
makeFileOutput := fmt.Sprintf("%s/Makefile", makeFilePath)

err := util.CreateDirIfDoesNotExist(makeFilePath)
Expand All @@ -50,7 +50,7 @@ func GenerateIDLMakefile(templator *templator.Templator, config *config.SproutCo
}

func GenerateProtoHealth(templator *templator.Templator, config *config.SproutConfig) {
protoHealthPath := fmt.Sprintf("../%s-idl/proto/health", config.Name)
protoHealthPath := fmt.Sprintf("%s-idl/proto/health", config.Name)
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)

err := util.CreateDirIfDoesNotExist(protoHealthPath)
Expand All @@ -67,7 +67,7 @@ func GenerateProtoHealth(templator *templator.Templator, config *config.SproutCo
}

func GenerateServiceProtobufFiles(templator *templator.Templator, config *config.SproutConfig) {
protoPath := fmt.Sprintf("../%s-idl/proto", config.Name)
protoPath := fmt.Sprintf("%s-idl/proto", config.Name)
for _, s := range config.Services {
serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name)
err := os.Mkdir(serviceProtoDir, os.ModePerm)
Expand All @@ -91,7 +91,7 @@ func GenerateServiceProtobufFiles(templator *templator.Templator, config *config
}

func GenerateProtoServiceLibs(config *config.SproutConfig) {
idlRoot := fmt.Sprintf("../%s-idl", config.Name)
idlRoot := fmt.Sprintf("%s-idl", config.Name)
cmd := exec.Command("make", "generate")
cmd.Dir = idlRoot
bytes, err := cmd.Output()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -15,7 +15,7 @@ require (
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.3.1 // indirect
github.com/google/pprof v0.0.0-20190908185732-236ed259b199 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.11.2 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.11.2
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/kisielk/errcheck v1.2.0 // indirect
Expand Down Expand Up @@ -51,7 +51,7 @@ require (
golang.org/x/tools v0.0.0-20190923195354-c85f9fa9581e // indirect
google.golang.org/api v0.10.0 // indirect
google.golang.org/appengine v1.6.3 // indirect
google.golang.org/genproto v0.0.0-20190916214212-f660b8655731 // indirect
google.golang.org/genproto v0.0.0-20190916214212-f660b8655731
google.golang.org/grpc v1.23.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.2
Expand Down
2 changes: 1 addition & 1 deletion templates/golang/go_mod.tmpl
Expand Up @@ -2,7 +2,7 @@ module {{ .GitRepo }}/{{ .Name }}

go 1.12

replace {{ .GitRepo }}/{{ .Name }}-idl => ../{{ .Name }}-idl
replace {{ .GitRepo }}/{{ .Name }}-idl => {{ .Name }}-idl

require (
{{ .GitRepo }}/{{ .Name }}-idl v0.0.0
Expand Down
1 change: 1 addition & 0 deletions templates/util/gitignore.tmpl
@@ -0,0 +1 @@
/{{ .Name }}-idl

0 comments on commit eba25fe

Please sign in to comment.