Skip to content

Commit 46d1fb6

Browse files
tmp
1 parent b9fbe81 commit 46d1fb6

File tree

9 files changed

+199
-0
lines changed

9 files changed

+199
-0
lines changed

ruby-wasm-poc/Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
3+
gem "ruby_wasm", "~> 2.6"
4+
gem "ruby-next", "~> 1.0"
5+
gem "google-protobuf", "~> 3.25"
6+
gem "grpc", "~> 1.63"

ruby-wasm-poc/Gemfile.lock

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
ast (2.4.2)
5+
diff-lcs (1.5.1)
6+
google-protobuf (3.25.3-arm64-darwin)
7+
googleapis-common-protos-types (1.14.0)
8+
google-protobuf (~> 3.18)
9+
grpc (1.63.0-arm64-darwin)
10+
google-protobuf (~> 3.25)
11+
googleapis-common-protos-types (~> 1.0)
12+
paco (0.2.3)
13+
parser (3.3.1.0)
14+
ast (~> 2.4.1)
15+
racc
16+
racc (1.7.3)
17+
require-hooks (0.2.2)
18+
ruby-next (1.0.3)
19+
paco (~> 0.2)
20+
require-hooks (~> 0.2)
21+
ruby-next-core (= 1.0.3)
22+
ruby-next-parser (>= 3.2.2.0)
23+
unparser (~> 0.6.0)
24+
ruby-next-core (1.0.3)
25+
ruby-next-parser (3.2.2.0)
26+
parser (>= 3.0.3.1)
27+
ruby_wasm (2.6.1-arm64-darwin)
28+
unparser (0.6.13)
29+
diff-lcs (~> 1.3)
30+
parser (>= 3.3.0)
31+
32+
PLATFORMS
33+
arm64-darwin
34+
35+
DEPENDENCIES
36+
google-protobuf (~> 3.25)
37+
grpc (~> 1.63)
38+
ruby-next (~> 1.0)
39+
ruby_wasm (~> 2.6)
40+
41+
BUNDLED WITH
42+
2.5.10

ruby-wasm-poc/Makefile

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
SHELL := /bin/bash
2+
PWD := $(shell pwd)
3+
RUBY_WASM := ${HOME}/bin/ruby-wasm
4+
5+
protobuf-generate:
6+
./scripts/generate_protobuf.sh
7+
8+
tebako-build:
9+
tebako press \
10+
--root='.' \
11+
--entry=bin/plugin.rb \
12+
--output=dist/plugin.tebako \
13+
--Ruby=3.2.3 \
14+
--log-level=debug # TODO if removed - fails on segmentation fault
15+
16+
wasm-build:
17+
rbwasm build --remake --reconfigure --ruby-version 3.2 -o dist/plugin.wasm
18+
echo "WASM filesize:" && du -sh dist/plugin.wasm
19+
20+
sync-wasm-sha: protobuf-generate wasm-build
21+
./scripts/sync_wasm_sha.sh
22+
23+
sqlc-generate-process: protobuf-generate tebako-build
24+
sqlc -f sqlc.process.yaml generate
25+
26+
sqlc-generate-wasm: sync-wasm-sha
27+
SQLCCACHE=./; sqlc -f sqlc.wasm.yaml generate

ruby-wasm-poc/buf.gen.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: v1
2+
plugins:
3+
- plugin: buf.build/protocolbuffers/ruby
4+
out: lib/gen

ruby-wasm-poc/lib/gen/plugin/codegen_pb.rb

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby-wasm-poc/lib/plugin.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require_relative 'gen/plugin/codegen_pb'
2+
require 'json'
3+
4+
module Plugin
5+
class Runner
6+
@options = {}
7+
8+
def run
9+
request_bytes = $stdin.read.bytes
10+
generate_request = Plugin::GenerateRequest.decode(request_bytes)
11+
generate_response = generate(generate_request)
12+
$stdout.write(generate_response.encode)
13+
end
14+
15+
private
16+
17+
def generate(generate_request)
18+
@options = parse_options(generate_request['PluginOptions'])
19+
files = generate_files_from_queries(generate_request)
20+
generate_response = Plugin::GenerateResponse.new(:Files => files)
21+
generate_response.encode
22+
end
23+
24+
def parse_options(plugin_options)
25+
plugin_options_str = plugin_options
26+
.bytes.pack('C*')
27+
.force_encoding('UTF-8')
28+
JSON.parse(plugin_options_str)
29+
end
30+
31+
def generate_files_from_queries(generate_request)
32+
generate_request['Queries']
33+
.group_by { |query| query['Filename'] }
34+
.map { |filename, queries| generate_file(filename, queries) }
35+
end
36+
37+
def generate_file(filename, queries)
38+
Plugin.OutputFile.new(:Name => filename, :Contents => "".bytes)
39+
end
40+
end
41+
end
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
replace_in_file () {
6+
text_to_replace=$1
7+
replace_with=$2
8+
filename=$3
9+
10+
if [[ "$(uname -s)" == "Linux" ]]; then
11+
sed -i "s!${text_to_replace}!${replace_with}!g" "${filename}"
12+
else
13+
sed -i '' "s!${text_to_replace}!${replace_with}!g" "${filename}"
14+
fi
15+
}
16+
17+
if wget -q --spider http://google.com; then
18+
buf generate --template buf.gen.yaml buf.build/sqlc/sqlc --path plugin/
19+
20+
# File is a builtin class library in Ruby, and it's a bad practice to name a class as a builtin class.
21+
# Since we have limited direct control on the generated protobuf code, we fix it post-generate.
22+
replace_in_file 'File[[:blank:]]' 'OutputFile ' lib/gen/plugin/codegen_pb.rb
23+
else
24+
echo "No internet connection - using pre-existing protobuf files.."
25+
fi
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
# uninstall
6+
gem uninstall ruby_wasm --all --executables
7+
asdf uninstall ruby 3.2.2 || echo "ruby not installed"
8+
9+
# install
10+
gem install ruby_wasm
11+
brew install libyaml
12+
gem install psych -- --with-libyaml-dir="$(brew --prefix libyaml)"
13+
RUBY_CONFIGURE_OPTS="--with-libyaml-dir=$(brew --prefix libyaml)"
14+
export RUBY_CONFIGURE_OPTS
15+
asdf install ruby 3.2.2 && asdf global ruby 3.2.2
16+
17+
cd /tmp && curl -LO --silent \
18+
https://github.com/ruby/ruby.wasm/releases/download/2.6.1/ruby-3.2-wasm32-unknown-wasip1-full.tar.gz
19+
tar xvf ruby-3.2-wasm32-unknown-wasip1-full.tar.gz
20+
mv /tmp/ruby-3.2-wasm32-unknown-wasip1-full/usr/local/bin/ruby ~/bin/ruby.wasm
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
PLUGIN_SHA=$(shasum -a 256 dist/plugin.wasm | awk '{ print $1 }')
4+
yq -i ".plugins[0].wasm.sha256 = \"${PLUGIN_SHA}\"" sqlc.wasm.yaml

0 commit comments

Comments
 (0)