Skip to content

Commit

Permalink
refactor: add ZipRelative to zip files relative to the current package (
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Nov 3, 2023
1 parent 425e5bc commit 3839bbb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion internal/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func UnzipDir(zipReader *zip.Reader, destDir string) error {
}
for _, file := range zipReader.File {
destPath := filepath.Clean(filepath.Join(destDir, file.Name)) //nolint:gosec
if !strings.HasPrefix(destPath, destDir) {
if destDir != "." && !strings.HasPrefix(destPath, destDir) {
return errors.Errorf("invalid file path: %q", destPath)
}
// Create directory if it doesn't exist
Expand Down
42 changes: 42 additions & 0 deletions internal/zip_relative.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build !release

package internal

import (
"archive/zip"
"os"
"path/filepath"
"runtime"
)

// ZipRelativeToCaller creates a temporary zip file from a path relative to the caller.
//
// This function will leak a file descriptor and thus can only be used in development.
func ZipRelativeToCaller(relativePath string) *zip.Reader {
_, file, _, _ := runtime.Caller(1)
dir := filepath.Join(filepath.Dir(file), relativePath)
w, err := os.CreateTemp("", "")
if err != nil {
panic(err)
}
defer os.Remove(w.Name()) // This is okay because the zip.Reader will keep it open.
if err != nil {
panic(err)
}

err = ZipDir(dir, w.Name())
if err != nil {
panic(err)
}

info, err := w.Stat()
if err != nil {
panic(err)
}
_, _ = w.Seek(0, 0)
zr, err := zip.NewReader(w, info.Size())
if err != nil {
panic(err)
}
return zr
}
39 changes: 1 addition & 38 deletions kotlin-runtime/devel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,8 @@
package kotlinruntime

import (
"archive/zip"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/TBD54566975/ftl/internal"
)

// Files is the FTL Kotlin runtime scaffolding files.
var Files = func() *zip.Reader {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
dir := filepath.Join(strings.TrimSpace(string(out)), "kotlin-runtime", "scaffolding")
w, err := os.CreateTemp("", "")
if err != nil {
panic(err)
}
defer os.Remove(w.Name()) // This is okay because the zip.Reader will keep it open.
if err != nil {
panic(err)
}

err = internal.ZipDir(dir, w.Name())
if err != nil {
panic(err)
}

info, err := w.Stat()
if err != nil {
panic(err)
}
_, _ = w.Seek(0, 0)
zr, err := zip.NewReader(w, info.Size())
if err != nil {
panic(err)
}
return zr
}()
var Files = internal.ZipRelativeToCaller("scaffolding")
4 changes: 2 additions & 2 deletions scripts/ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
set -euo pipefail
ftldir="$(dirname "$0")/.."
name="$(basename "$0")"
dest="${TMPDIR:-/tmp}/${name}"
dest="${ftldir}/build/devel"
mkdir -p "$dest"
(cd "${ftldir}" && ./bin/go build -trimpath -ldflags="-s -w -buildid=" -o "$dest/${name}" "./cmd/${name}") && exec "$dest/${name}" "$@"
(cd "${ftldir}" && ./bin/go build -ldflags="-s -w -buildid=" -o "$dest/${name}" "./cmd/${name}") && exec "$dest/${name}" "$@"

0 comments on commit 3839bbb

Please sign in to comment.