Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Apr 12, 2024
0 parents commit 7b119d1
Show file tree
Hide file tree
Showing 34 changed files with 617 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
35 changes: 35 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI
on:
push:
branches:
- main
tags:
- "*"
pull_request:
workflow_dispatch:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- macos-13
- macos-14
- ubuntu-22.04
- windows-2022
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v5
with:
go-version-file: go.work

- name: run checks
run: go run mage check

- name: build snapshot
run: go run mage snapshot
23 changes: 23 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Release"

on:
push:
tags:
- v*

jobs:
build:
name: Release
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v5
with:
go-version-file: go.work

- run: go run mage release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40 changes: 40 additions & 0 deletions .github/workflows/update-upstream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "Update Upstream"

on:
schedule:
- cron: "5 4 * * *"
workflow_dispatch:

jobs:
build:
name: Update Upstream
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v5
with:
go-version-file: go.work

- run: go run mage updateUpstream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: git diff --exit-code
id: check-diff
continue-on-error: true

- name: create PR
if: steps.check-diff.outcome == 'failure'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git checkout -b update-upstream-${{ github.run_id }}
git add .
git commit -m "Update to latest upstream"
git push -u origin -f update-upstream-${{ github.run_id }}
gh pr create --fill --head update-upstream-${{ github.run_id }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
.vscode
build
dist
go.work.sum
61 changes: 61 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
builds:
- main: ./cmd/protoc-gen-prost
id: es
binary: protoc-gen-prost
env:
- CGO_ENABLED=0
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- windows_amd64
- windows_arm64
- main: ./cmd/protoc-gen-prost-crate
id: es
binary: protoc-gen-prost-crate
env:
- CGO_ENABLED=0
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- windows_amd64
- windows_arm64
- main: ./cmd/protoc-gen-prost-serde
id: es
binary: protoc-gen-prost-serde
env:
- CGO_ENABLED=0
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- windows_amd64
- windows_arm64
- main: ./cmd/protoc-gen-tonic
id: es
binary: protoc-gen-tonic
env:
- CGO_ENABLED=0
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- windows_amd64
- windows_arm64
archives:
- format_overrides:
- goos: windows
format: zip
release:
mode: append
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
skip: true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) wasilibs authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# go-protoc-gen-prost

go-protoc-gen-prost is a distribution of [protoc-gen-prost][1].
It does not actually reimplement any functionality of prost in Go, instead compiling it
to WebAssembly, and executing with the pure Go Wasm runtime [wazero][2].
This means that `go install` or `go run` can be used to execute it, with no need to rely on external
package managers such as Homebrew, on any platform that Go supports.

## Installation

Precompiled binaries are available in the [releases](https://github.com/wasilibs/go-protoc-gen-prost/releases).
Alternatively, install the plugin using `go install`.

```bash
$ go install github.com/wasilibs/go-protoc-gen-prost/cmd/protoc-gen-prost@latest
```

As long as `$GOPATH/bin`, e.g. `~/go/bin` is on the `PATH`, you can use it with protoc as normal.

```bash
$ protoc --prost_out=out/rust -Iprotos protos/helloworld.proto
```

For [buf][3] users, to avoid installation entirely, it can be convenient to use `go run` in `buf.gen.yaml`.

```yaml
version: v1
plugins:
- plugin: prost
out: out/rust
path: ["go", "run", "github.com/wasilibs/go-protoc-gen-prost/cmd/protoc-gen-prost@latest"]
```
This makes it possible to have full protobuf/gRPC generation with no installation of tools,
besides Go itself, on any platform that Go supports. The above examples use `@latest`, but it is
recommended to specify a version, in which case all of the developers on your codebase will use the
same version of the tool with no special steps.

See a full [example][5] in `go-protoc-gen-builtins`. To generate protos, enter the directory and run
`go run github.com/bufbuild/buf/cmd/[email protected] generate`. As long as your machine has Go installed,
you will be able to generate protos. The first time using `go run` for a command, Go automatically builds
it making it slower, but subsequent invocations should be quite fast.

[1]: https://github.com/neoeinstein/protoc-gen-prost
[2]: https://wazero.io/
[3]: https://buf.build/
[4]: https://github.com/wasilibs/go-protoc-gen-builtins
[5]: https://github.com/wasilibs/go-protoc-gen-builtins/tree/main/example
17 changes: 17 additions & 0 deletions buildtools/wasm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM rust:1-slim

RUN apt-get update && apt-get install -y binaryen curl
RUN rustup target add wasm32-wasi

ADD buildtools/wasm/version.txt version.txt
RUN mkdir -p /workspace && curl -L https://github.com/neoeinstein/protoc-gen-prost/archive/$(cat version.txt).tar.gz | tar -xz --strip-components 1 -C /workspace
WORKDIR /workspace

# Temporarily force update to 0.12.4+ for https://github.com/tokio-rs/prost/pull/962
RUN cargo update
RUN cargo build --release --target wasm32-wasi

SHELL ["/bin/bash", "-c"]
RUN for f in target/wasm32-wasi/release/protoc-gen-*.wasm; do wasm-opt -o "target/$(basename $f)" --flatten --rereloop --converge -O3 "$f"; done

CMD ["bash", "-c", "cp target/protoc-gen-*.wasm /out/"]
1 change: 1 addition & 0 deletions buildtools/wasm/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protoc-gen-prost-v0.3.1
12 changes: 12 additions & 0 deletions cmd/protoc-gen-prost-crate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"

"github.com/wasilibs/go-protoc-gen-prost/internal/runner"
"github.com/wasilibs/go-protoc-gen-prost/internal/wasm"
)

func main() {
os.Exit(runner.Run("protoc-gen-prost-crate", os.Args[1:], wasm.ProtocGenProstCrate, os.Stdin, os.Stdout, os.Stderr, "."))
}
12 changes: 12 additions & 0 deletions cmd/protoc-gen-prost-serde/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"

"github.com/wasilibs/go-protoc-gen-prost/internal/runner"
"github.com/wasilibs/go-protoc-gen-prost/internal/wasm"
)

func main() {
os.Exit(runner.Run("protoc-gen-prost-serde", os.Args[1:], wasm.ProtocGenProstSerde, os.Stdin, os.Stdout, os.Stderr, "."))
}
12 changes: 12 additions & 0 deletions cmd/protoc-gen-prost/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"

"github.com/wasilibs/go-protoc-gen-prost/internal/runner"
"github.com/wasilibs/go-protoc-gen-prost/internal/wasm"
)

func main() {
os.Exit(runner.Run("protoc-gen-prost", os.Args[1:], wasm.ProtocGenProst, os.Stdin, os.Stdout, os.Stderr, "."))
}
12 changes: 12 additions & 0 deletions cmd/protoc-gen-tonic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"

"github.com/wasilibs/go-protoc-gen-prost/internal/runner"
"github.com/wasilibs/go-protoc-gen-prost/internal/wasm"
)

func main() {
os.Exit(runner.Run("protoc-gen-prost-tonic", os.Args[1:], wasm.ProtocGenTonic, os.Stdin, os.Stdout, os.Stderr, "."))
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/wasilibs/go-protoc-gen-prost

go 1.22

require github.com/tetratelabs/wazero v1.7.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/tetratelabs/wazero v1.7.0 h1:jg5qPydno59wqjpGrHph81lbtHzTrWzwwtD4cD88+hQ=
github.com/tetratelabs/wazero v1.7.0/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
9 changes: 9 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
go 1.22

toolchain go1.22.2

use (
.
./mage
./magefiles
)
51 changes: 51 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package runner

import (
"context"
"crypto/rand"
"io"
"log"
"os"
"strings"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/sysfs"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/sys"
)

func Run(name string, args []string, wasm []byte, stdin io.Reader, stdout io.Writer, stderr io.Writer, cwd string) int {
ctx := context.Background()

rt := wazero.NewRuntime(ctx)

wasi_snapshot_preview1.MustInstantiate(ctx, rt)

args = append([]string{name}, args...)

root := sysfs.DirFS(cwd)

cfg := wazero.NewModuleConfig().
WithSysNanosleep().
WithSysNanotime().
WithSysWalltime().
WithStderr(stderr).
WithStdout(stdout).
WithStdin(stdin).
WithRandSource(rand.Reader).
WithArgs(args...).
WithFSConfig(wazero.NewFSConfig().(sysfs.FSConfig).WithSysFSMount(root, "/"))
for _, env := range os.Environ() {
k, v, _ := strings.Cut(env, "=")
cfg = cfg.WithEnv(k, v)
}

_, err := rt.InstantiateWithConfig(ctx, wasm, cfg)
if err != nil {
if sErr, ok := err.(*sys.ExitError); ok {
return int(sErr.ExitCode())
}
log.Fatal(err)
}
return 0
}
Binary file added internal/wasm/protoc-gen-prost-crate.wasm
Binary file not shown.
Binary file added internal/wasm/protoc-gen-prost-serde.wasm
Binary file not shown.
Binary file added internal/wasm/protoc-gen-prost.wasm
Binary file not shown.
Binary file added internal/wasm/protoc-gen-tonic.wasm
Binary file not shown.
Loading

0 comments on commit 7b119d1

Please sign in to comment.