Skip to content

Commit

Permalink
Infer webrpc version from Go module or git history, if not set (#263)
Browse files Browse the repository at this point in the history
* Infer webrpc version if imported from another Go module

Useful if you import webrpc into your Go project and invoke it via
go run github.com/webrpc/webrpc/cmd/webrpc-gen.

* Infer version from git as a fallback

* make build: Inject version into the Go binary

* Replace webrpc version in all generated files to avoid git conflicts

* Update to actions/checkout@v4; Use 20 commit depth for git tags

* Improve make generate diff
  • Loading branch information
VojtechVitek committed Mar 13, 2024
1 parent b42a1c8 commit 8c823bf
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 41 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ jobs:
go-version: ["1.20", "1.19", "1.16"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 20
fetch-tags: true
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Set up webrpc cache folder
uses: actions/cache@v3
uses: actions/cache@v4
with:
key: webrpc-cache
path: /tmp/webrpc-cache
Expand All @@ -31,18 +34,21 @@ jobs:
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 20
fetch-tags: true
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Set up webrpc cache folder
uses: actions/cache@v3
uses: actions/cache@v4
with:
key: webrpc-cache
path: /tmp/webrpc-cache
- name: Build
run: go install ./cmd/webrpc-gen
run: make install
- name: Regenerate examples
run: make generate
- name: Git diff of regenerated examples
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: 20
fetch-tags: true
- name: Set up Go
uses: actions/setup-go@v3
with:
Expand All @@ -22,18 +22,18 @@ jobs:
with:
distribution: goreleaser
version: latest
args: release -f .github/.goreleaser.yml --rm-dist
args: release -f .github/workflows/.goreleaser.yml --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}

docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: 20
fetch-tags: true
- name: Log into Github registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Docker build
Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ all:
@echo ""

build:
go build -o ./bin/webrpc-gen ./cmd/webrpc-gen
go build -ldflags="-s -w -X github.com/webrpc/webrpc.VERSION=$$(git describe)" -o ./bin/webrpc-gen ./cmd/webrpc-gen

build-test:
go build -o ./bin/webrpc-test ./cmd/webrpc-test
go build -ldflags="-s -w -X github.com/webrpc/webrpc.VERSION=$$(git describe)" -o ./bin/webrpc-test ./cmd/webrpc-test

clean:
rm -rf ./bin

install:
go install ./cmd/webrpc-gen
go install ./cmd/webrpc-test
go install -ldflags="-s -w -X github.com/webrpc/webrpc.VERSION=$$(git describe)" ./cmd/webrpc-gen
go install -ldflags="-s -w -X github.com/webrpc/webrpc.VERSION=$$(git describe)" ./cmd/webrpc-test

generate: build
go generate -v -x ./...
Expand All @@ -46,6 +46,9 @@ generate: build
make generate || exit 1; \
cd ../../; \
done
# Replace webrpc version in all generated files to avoid git conflicts.
git grep -l "$$(git describe)" | xargs sed -i -e "s/@$$(git describe)//g"
sed -i "/$$(git describe)/d" tests/schema/test.debug.gen.txt

dep:
go mod tidy
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@ For more information please see the [schema readme](./schema/README.md).
## Building from source

1. Install Go 1.16+
2. $ `go get -u github.com/webrpc/webrpc/...`
3. $ `make build`
4. $ `make test`
5. $ `go install ./cmd/webrpc-gen`
2. $ `make build`
3. $ `make test`
4. $ `make install`


## Writing your own code-generator
Expand Down
2 changes: 1 addition & 1 deletion _examples/golang-basics/example.gen.go

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

2 changes: 1 addition & 1 deletion _examples/golang-nodejs/client/client.gen.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// example v0.0.1 adf488c1d88ea2996ff4c6f6dd21916cc816c480
// --
// Code generated by webrpc-gen@v0.15.2-dev with javascript generator. DO NOT EDIT.
// Code generated by webrpc-gen with javascript generator. DO NOT EDIT.
//
// webrpc-gen -schema=example.webrpc.json -target=javascript -client -out=./client/client.gen.mjs

Expand Down
2 changes: 1 addition & 1 deletion _examples/golang-nodejs/server/server.gen.go

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

2 changes: 1 addition & 1 deletion _examples/hello-webrpc-ts/server/hello_api.gen.go

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

2 changes: 1 addition & 1 deletion _examples/hello-webrpc-ts/webapp/src/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
// hello-webrpc v1.0.0 77ec2ae70d47ccdbeccf18cded85519e0ad547dd
// --
// Code generated by webrpc-gen@v0.15.2-dev with typescript generator. DO NOT EDIT.
// Code generated by webrpc-gen with typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=hello-api.ridl -target=typescript -client -out=./webapp/src/client.gen.ts

Expand Down
2 changes: 1 addition & 1 deletion _examples/hello-webrpc/server/hello_api.gen.go

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

2 changes: 1 addition & 1 deletion _examples/hello-webrpc/webapp/client.gen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// hello-webrpc v1.0.0 4fc01b139e8de5a1eb11b0581cd706326a434708
// --
// Code generated by webrpc-gen@v0.15.2-dev with javascript generator. DO NOT EDIT.
// Code generated by webrpc-gen with javascript generator. DO NOT EDIT.
//
// webrpc-gen -schema=hello-api.ridl -target=javascript -exports=false -client -out=./webapp/client.gen.js

Expand Down
2 changes: 1 addition & 1 deletion _examples/node-ts/server/server.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
// node-ts v1.0.0 bd572b349e330d81cc30b1ff3cf69d7ab59f1619
// --
// Code generated by webrpc-gen@v0.15.2-dev with typescript generator. DO NOT EDIT.
// Code generated by webrpc-gen with typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=typescript -server -out=./server/server.gen.ts

Expand Down
2 changes: 1 addition & 1 deletion _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
// node-ts v1.0.0 bd572b349e330d81cc30b1ff3cf69d7ab59f1619
// --
// Code generated by webrpc-gen@v0.15.2-dev with typescript generator. DO NOT EDIT.
// Code generated by webrpc-gen with typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=typescript -client -out=./webapp/client.gen.ts

Expand Down
2 changes: 1 addition & 1 deletion tests/client/client.gen.go

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

1 change: 0 additions & 1 deletion tests/schema/test.debug.gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2901,7 +2901,6 @@
Deprecated_Messages: ([]interface {}) <nil>
}),
SchemaHash: (string) (len=40) "1e00d2fe909b6626e4156ac2a40ff08223d75224",
WebrpcGenVersion: (string) (len=11) "v0.15.2-dev",
WebrpcGenCommand: (string) (len=84) "webrpc-gen -schema=./schema/test.ridl -target=debug -out=./schema/test.debug.gen.txt",
WebrpcTarget: (string) (len=5) "debug",
WebrpcErrors: ([]*schema.Error) (len=11 cap=11) {
Expand Down
2 changes: 1 addition & 1 deletion tests/server/server.gen.go

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

45 changes: 38 additions & 7 deletions version.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
package webrpc

// VERSION defines version of webrpc tools, such as webrpc-gen.
//
// Please update major/minor version when making any incompatible changes
// to the webrpc schema or webrpc-gen Template Functions API.
// The patch value is automatically updated with the latest git tag in CI.
import (
"os/exec"
"strings"
)

// VERSION of webrpc tooling and webrpc-gen Template Functions API.
// Available as {{.WebrpcGenVersion}} variable in Go templates.
//
// Version is available as {{.WebrpcGenVersion}} in generator templates.
var VERSION = "v0.15.2-dev"
// The value is injected during `go build' in the release CI step.
var VERSION = ""

func init() {
if VERSION == "" {
VERSION = getRuntimeVersion()
}
}

// getRuntimeVersion tries to infer webrpc version
// 1. from the current go.mod file, which is useful when running webrpc-gen from
// another Go module using `go run github.com/webrpc/webrpc/cmd/webrpc-gen'.
// 2. from the current git history.
func getRuntimeVersion() string {
// $ go list -m github.com/webrpc/webrpc
// github.com/webrpc/webrpc v0.15.1\n
if out, _ := exec.Command("go", "list", "-m", "github.com/webrpc/webrpc").Output(); len(out) > 0 {
parts := strings.Split(strings.TrimSpace(string(out)), " ")
if len(parts) >= 2 {
return parts[1]
}
}

// $ git describe
// v0.15.1-6-g550333d\n
if out, _ := exec.Command("git", "describe").Output(); len(out) > 0 {
return strings.TrimSpace(string(out))
}

return "unknown"
}

0 comments on commit 8c823bf

Please sign in to comment.