Skip to content

Commit

Permalink
feat: support partly vendored
Browse files Browse the repository at this point in the history
  • Loading branch information
kulti committed Nov 26, 2024
1 parent c6d6c6d commit c1274e7
Show file tree
Hide file tree
Showing 36 changed files with 4,740 additions and 15 deletions.
1 change: 1 addition & 0 deletions api/rockamalg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ message AmalgRequest {
bool isolate = 5;
bool disable_debug = 6;
bool allow_dev_dependencies = 7;
bytes vendor = 8;
}

message AmalgResponse {
Expand Down
40 changes: 25 additions & 15 deletions internal/api/rockamalgrpc/rockamalg.pb.go

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

27 changes: 27 additions & 0 deletions internal/rockamalg/rockamalg.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ func (a *amalg) Do(ctx context.Context) error {
}
}

if a.p.Vendor != "" {
if empty, err := isFileEmpty(a.p.Vendor); err != nil {
return fmt.Errorf("check vendor file is empty: %w", err)
} else if !empty {
if err := a.wrapWithMsg(a.extractVendorArchive, "Extracting vendor archive")(ctx); err != nil {
return fmt.Errorf("extract vendor archive: %w", err)
}
}
}

if a.p.Rockspec != "" {
if err := a.wrapWithMsg(a.installDependencies, "Installing dependencies")(ctx); err != nil {
return fmt.Errorf("install dependencies: %w", err)
Expand Down Expand Up @@ -248,6 +258,10 @@ func (a *amalg) installDependencies(ctx context.Context) error {
return nil
}

func (a *amalg) extractVendorArchive(_ context.Context) error {
return archive.UnzipFileToDir(a.p.Vendor, a.tree)
}

func (a *amalg) buildVendorArchive(_ context.Context) error {
return archive.ZipDirToFile(a.tree, a.p.Vendor)
}
Expand Down Expand Up @@ -444,3 +458,16 @@ func isDirectory(path string) (bool, error) {

return fileInfo.IsDir(), err
}

func isFileEmpty(path string) (bool, error) {
fi, err := os.Stat(path)
if err == nil {
return fi.Size() == 0, nil
}

if os.IsNotExist(err) {
return true, nil
}

return false, err
}
7 changes: 7 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ func (s *Server) prepareAmalgParams(
AllowDevDeps: req.GetAllowDevDependencies(),
}

if len(req.GetVendor()) != 0 {
if err := os.WriteFile(amalgParams.Vendor, req.GetVendor(), 0o600); err != nil {

Check failure on line 104 in internal/server/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

mnd: Magic number: 0o600, in <argument> detected (gomnd)
return zero, status.Newf(codes.Internal, "create vendor.zip file: %v", err)
}
}

if len(req.GetDependencies()) != 0 {
amalgParams.Dependencies = filepath.Join(amalgDir, "deps")
if err := s.writeDependenciesFile(req.GetDependencies(), amalgParams.Dependencies); err != nil {
return zero, status.Newf(codes.Internal, "create dependencies file: %v", err)
}
}

var err error
amalgParams.Rockspec, err = s.writeRockspec(ctx, req.GetRockspec(), amalgDir)
if err != nil {
return zero, status.Newf(codes.Internal, "create rockspec file: %v", err)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/amalg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
publicRocks rockstype = iota
privateRocks
devRocksTest
vendoredRocks
)

func TestAmalgCommandPublicRocks(t *testing.T) {
Expand All @@ -41,6 +42,12 @@ func TestAmalgCommandPrivateRocks(t *testing.T) {
testAmalg(t, "testdata/amalg-private", privateRocks)
}

func TestAmalgCommandPartlyVendored(t *testing.T) {
t.Parallel()

testAmalg(t, "testdata/amalg-partly-vendor", vendoredRocks)
}

func TestAmalgDevDeps(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -197,6 +204,11 @@ func buildTestOpts(
t.Cleanup(func() { os.Remove(vendorFile.Name()) })
require.NoError(t, vendorFile.Close())

if rt == vendoredRocks {
vendorInPath := filepath.Join(testdataPath, "vendor_in")
require.NoError(t, archive.ZipDirToFile(vendorInPath, vendorFile.Name()))
}

luaNameBytes, err := os.ReadFile(filepath.Join(testdataPath, "lua"))
require.NoError(t, err)

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func TestServerPrivateRocks(t *testing.T) {
testServer(t, "testdata/amalg-private", port, privateRocks)
}

func TestServerCommandPartlyVendored(t *testing.T) {
t.Parallel()

testAmalg(t, "testdata/amalg-partly-vendor", vendoredRocks)
}

func TestServerDevDeps(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -129,6 +135,10 @@ func buildReq(t *testing.T, opts testOpts, isolate bool) *rockamalgrpc.AmalgRequ
req.Dependencies = readDependencies(t, opts.depsFileName)
}

if !fileIsEmpty(t, opts.vendorFileName) {
req.Vendor = zipDir(t, opts.vendorFileName)
}

return req
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lua-string == 1.2.0
inspect ~> 3.1.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "lua-string"
inspect = require "inspect"

local v = ("Hello world!"):trimend("!"):sub(6):trim():totable()
print(inspect(v))
Loading

0 comments on commit c1274e7

Please sign in to comment.