Skip to content

Commit

Permalink
feat: bundle sqlc binaries in ftl release binary
Browse files Browse the repository at this point in the history
also implements the Go API for invoking generate
  • Loading branch information
worstell committed Jan 7, 2025
1 parent d22c2a5 commit cbd49cf
Show file tree
Hide file tree
Showing 16 changed files with 497 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ jobs:
just errtrace
just build ftl # Ensure all the prerequisites are built before we use goreleaser
just build-language-plugins
just build-sqlc-gen-ftl
goreleaser release --skip=validate
env:
GITHUB_TOKEN: ${{ github.token }}
Expand Down
12 changes: 8 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ZIP_DIRS := "go-runtime/compile/build-template " + \
CONSOLE_ROOT := "frontend/console"
FRONTEND_OUT := CONSOLE_ROOT + "/dist/index.html"
EXTENSION_OUT := "frontend/vscode/dist/extension.js"
SQLC_GEN_FTL_OUT := "sqlc-gen-ftl/target/wasm32-wasip1/release/sqlc-gen-ftl.wasm"
SQLC_GEN_FTL_OUT := "internal/sqlc/resources/sqlc-gen-ftl.wasm"
PROTOS_IN := "common/protos backend/protos"
PROTOS_OUT := "backend/protos/xyz/block/ftl/console/v1/console.pb.go " + \
"backend/protos/xyz/block/ftl//v1/ftl.pb.go " + \
Expand Down Expand Up @@ -194,14 +194,18 @@ build-extension: pnpm-install
@mk {{EXTENSION_OUT}} : frontend/vscode/src frontend/vscode/package.json -- "cd frontend/vscode && rm -f ftl-*.vsix && pnpm run compile"

# Build the sqlc-ftl-gen plugin, used to generate FTL schema from SQL
build-sqlc-gen-ftl: build-rust-protos
build-sqlc-gen-ftl: build-rust-protos download-sqlc
@mk {{SQLC_GEN_FTL_OUT}} : sqlc-gen-ftl/src -- \
"cd sqlc-gen-ftl && \
cargo build --target wasm32-wasip1 --release"
"cargo build --manifest-path sqlc-gen-ftl/Cargo.toml --target wasm32-wasip1 --release && \
cp sqlc-gen-ftl/target/wasm32-wasip1/release/sqlc-gen-ftl.wasm internal/sqlc/resources"

test-sqlc-gen-ftl:
@cargo test --manifest-path sqlc-gen-ftl/Cargo.toml --features ci --test sqlc_gen_ftl_test -- --nocapture

# Download SQLC binaries, embedded in the FTL binary as resources
download-sqlc:
@bash scripts/provide-sqlc-resources

# Generate Rust protos
build-rust-protos:
@mk sqlc-gen-ftl/src/protos : backend/protos -- \
Expand Down
13 changes: 13 additions & 0 deletions internal/sqlc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM alpine:latest

RUN apk add --no-cache bash curl tar

WORKDIR /app

COPY scripts/download-sqlc /app/download-sqlc

RUN chmod +x /app/download-sqlc

ENV SQLC_VERSION=""

CMD ["/app/download-sqlc"]
36 changes: 36 additions & 0 deletions internal/sqlc/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sqlc

import (
"embed"
"fmt"
"io"
"os"
"path/filepath"
)

//go:embed resources/*
var embeddedResources embed.FS

func extractEmbeddedFile(resourceName, destPath string) error {
if err := os.MkdirAll(filepath.Dir(destPath), 0750); err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}

srcFile, err := embeddedResources.Open(filepath.Join("resources", resourceName))
if err != nil {
return fmt.Errorf("failed to open embedded resource %s: %w", resourceName, err)
}
defer srcFile.Close()

destFile, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer destFile.Close()

if _, err := io.Copy(destFile, srcFile); err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
}

return nil
}
Loading

0 comments on commit cbd49cf

Please sign in to comment.