Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/buildpack/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var packs = map[string]func(context.Context, Sys, yb.BuildpackSpec) (biome.Envir
"r": installR,
"ruby": installRuby,
"rust": installRust,
"sbt": installSBT,
"yarn": installYarn,
}

Expand Down Expand Up @@ -101,14 +102,18 @@ func extract(ctx context.Context, sys Sys, dstDir, url string, extractMode bool)
zipExt = ".zip"
tarXZExt = ".tar.xz"
tarGZExt = ".tar.gz"
tgzExt = ".tgz"
tarBZ2Ext = ".tar.bz2"
tbz2Ext = ".tbz2"
)
const cleanupTimeout = 10 * time.Second
exts := []string{
zipExt,
tarXZExt,
tarGZExt,
tgzExt,
tarBZ2Ext,
tbz2Ext,
}
var ext string
for _, testExt := range exts {
Expand Down Expand Up @@ -183,7 +188,7 @@ func extract(ctx context.Context, sys Sys, dstDir, url string, extractMode bool)
if extractMode == stripTopDirectory {
invoke.Argv = append(invoke.Argv, "--strip-components", "1")
}
case tarGZExt:
case tarGZExt, tgzExt:
invoke.Argv = []string{
"tar",
"-x", // extract
Expand All @@ -193,7 +198,7 @@ func extract(ctx context.Context, sys Sys, dstDir, url string, extractMode bool)
if extractMode == stripTopDirectory {
invoke.Argv = append(invoke.Argv, "--strip-components", "1")
}
case tarBZ2Ext:
case tarBZ2Ext, tbz2Ext:
invoke.Argv = []string{
"tar",
"-x", // extract
Expand Down
111 changes: 111 additions & 0 deletions internal/buildpack/sbt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2021 YourBase Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package buildpack

import (
"context"
"fmt"
"strings"
"os"
"io/ioutil"

"github.com/yourbase/yb"
"github.com/yourbase/yb/internal/biome"
"zombiezen.com/go/log"
)

func installSBT(ctx context.Context, sys Sys, spec yb.BuildpackSpec) (_ biome.Environment, err error) {
installDir := sys.Biome.JoinPath(sys.Biome.Dirs().Tools, "sbt", spec.Version())
desc := sys.Biome.Describe()
parentDir := os.TempDir()
socketDir, err := ioutil.TempDir(parentDir, "*-ybsbt")
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately won't work on Docker-based builds, since it creates the directory on the host instead of inside the biome.

Would it be appropriate to use a directory inside the biome home? Perhaps sys.Biome.JoinPath(sys.Biome.Dirs().Tools, "sbt-server")?

if err != nil {
log.Errorf(ctx, "Error creating directory for SBT sockets: %v", err)
}
log.Infof(ctx, "SBT will use %s as the socket directory", socketDir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the average user needs to know this information unless something went wrong, so I suggest debug-level log.

Suggested change
log.Infof(ctx, "SBT will use %s as the socket directory", socketDir)
log.Debugf(ctx, "SBT will use %s as the socket directory", socketDir)

// defer os.RemoveAll(socketDir) // clean up

env := biome.Environment{
Vars: map[string]string{
"SBT_GLOBAL_SERVER_DIR": socketDir,
},
PrependPath: []string{sys.Biome.JoinPath(installDir)},
}

// If directory already exists, then use it.
if _, err := biome.EvalSymlinks(ctx, sys.Biome, installDir); err == nil {
log.Infof(ctx, "SBT v%s located in %s", spec.Version(), installDir)
return env, nil
}

log.Infof(ctx, "Installing SBT v%s in %s", spec.Version(), installDir)
downloadURL, err := sbtDownloadURL(spec.Version(), desc)
if err != nil {
return biome.Environment{}, err
}
if err := extract(ctx, sys, installDir, downloadURL, stripTopDirectory); err != nil {
return biome.Environment{}, err
}
return env, nil
}

func sbtDownloadURL(version string, desc *biome.Descriptor) (string, error) {
vparts := strings.SplitN(version, "+", 2)
subVersion := ""
if len(vparts) > 1 {
subVersion = vparts[1]
version = vparts[0]
}

parts := strings.Split(version, ".")

majorVersion, err := convertVersionPiece(parts, 0)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: major: %w", version, err)
}
minorVersion, err := convertVersionPiece(parts, 1)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: minor: %w", version, err)
}
patchVersion, err := convertVersionPiece(parts, 2)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: patch: %w", version, err)
}

urlPattern := "https://github.com/sbt/sbt/releases/download/v{{ .MajorVersion }}.{{ .MinorVersion }}.{{ .PatchVersion }}/sbt-{{ .MajorVersion }}.{{ .MinorVersion }}.{{ .PatchVersion }}.tgz"

var data struct {
OS string
Arch string
MajorVersion int64
MinorVersion int64
PatchVersion int64
SubVersion string // not always an int, sometimes a float
}
data.OS = map[string]string{
biome.Linux: "linux",
biome.MacOS: "mac",
}[desc.OS]
if data.OS == "" {
return "", fmt.Errorf("unsupported os %s", desc.OS)
}
data.MajorVersion = majorVersion
data.MinorVersion = minorVersion
data.PatchVersion = patchVersion
data.SubVersion = subVersion
return templateToString(urlPattern, data)
}
Comment on lines +66 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the URL is only dependent on version, right? I think this function can be simplified to pass the version string through verbatim.

Suggested change
func sbtDownloadURL(version string, desc *biome.Descriptor) (string, error) {
vparts := strings.SplitN(version, "+", 2)
subVersion := ""
if len(vparts) > 1 {
subVersion = vparts[1]
version = vparts[0]
}
parts := strings.Split(version, ".")
majorVersion, err := convertVersionPiece(parts, 0)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: major: %w", version, err)
}
minorVersion, err := convertVersionPiece(parts, 1)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: minor: %w", version, err)
}
patchVersion, err := convertVersionPiece(parts, 2)
if err != nil {
return "", fmt.Errorf("parse jdk version %q: patch: %w", version, err)
}
urlPattern := "https://github.com/sbt/sbt/releases/download/v{{ .MajorVersion }}.{{ .MinorVersion }}.{{ .PatchVersion }}/sbt-{{ .MajorVersion }}.{{ .MinorVersion }}.{{ .PatchVersion }}.tgz"
var data struct {
OS string
Arch string
MajorVersion int64
MinorVersion int64
PatchVersion int64
SubVersion string // not always an int, sometimes a float
}
data.OS = map[string]string{
biome.Linux: "linux",
biome.MacOS: "mac",
}[desc.OS]
if data.OS == "" {
return "", fmt.Errorf("unsupported os %s", desc.OS)
}
data.MajorVersion = majorVersion
data.MinorVersion = minorVersion
data.PatchVersion = patchVersion
data.SubVersion = subVersion
return templateToString(urlPattern, data)
}
func sbtDownloadURL(version string) (string, error) {
const urlPattern = "https://github.com/sbt/sbt/releases/download/v{{ .Version }}/sbt-{{ .Version }}.tgz"
var data struct {
Version string
}
data.Version = url.PathEscape(version)
return templateToString(urlPattern, data)
}

46 changes: 46 additions & 0 deletions internal/buildpack/sbt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 YourBase Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package buildpack

import (
"context"
"strings"
"testing"

"github.com/yourbase/yb/internal/biome"
"zombiezen.com/go/log/testlog"
)

func TestSBT(t *testing.T) {
const version = "1.5.2"
ctx := testlog.WithTB(context.Background(), t)
// TODO(johnewart): SBT depends on Java -- see Ross's comment in sbt tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO(johnewart): SBT depends on Java -- see Ross's comment in sbt tests
// TODO(johnewart): SBT depends on Java -- see Ross's comment in Ant tests

sbtBiome, _ := testInstall(ctx, t, "java:8.252.10", "sbt:"+version)
versionOutput := new(strings.Builder)
err := sbtBiome.Run(ctx, &biome.Invocation{
Argv: []string{"sbt", "--version"},
Stdout: versionOutput,
Stderr: versionOutput,
})
t.Logf("sbt --version output:\n%s", versionOutput)
if err != nil {
t.Errorf("sbt --version: %v", err)
}
if got := versionOutput.String(); !strings.Contains(got, version) {
t.Errorf("sbt --version output does not include %q", version)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have some unit tests for the sbtDownloadURL function, like for the Java buildpack (but doesn't need to be nearly as extensive):

func TestJavaDownloadURL(t *testing.T) {
tests := []struct {
version string
want string
}{
{
version: "9+181",
want: "https://github.com/AdoptOpenJDK/openjdk9-binaries/releases/download/jdk-9%2B181/OpenJDK9U-jdk_x64_linux_hotspot_9_181.tar.gz",
},
{
version: "8.252+09",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u252-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u252b09.tar.gz",
},
{
version: "8.252.9",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u252-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u252b09.tar.gz",
},
{
version: "8.252.09",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u252-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u252b09.tar.gz",
},
{
version: "8.242+08",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz",
},
{
version: "8.242.8",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz",
},
{
version: "8.242.08",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz",
},
{
version: "8.232+09",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u232-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u232b09.tar.gz",
},
{
version: "8.232.09",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u232-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u232b09.tar.gz",
},
{
version: "8.232.9",
want: "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u232-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u232b09.tar.gz",
},
{
version: "11.0.6",
want: "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.6_10.tar.gz",
},
{
version: "11.0.6+10",
want: "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.6_10.tar.gz",
},
{
version: "12.0.2+10",
want: "https://github.com/AdoptOpenJDK/openjdk12-binaries/releases/download/jdk-12.0.2%2B10/OpenJDK12U-jdk_x64_linux_hotspot_12.0.2_10.tar.gz",
},
{
version: "13.0.2+8",
want: "https://github.com/AdoptOpenJDK/openjdk13-binaries/releases/download/jdk-13.0.2%2B8/OpenJDK13U-jdk_x64_linux_hotspot_13.0.2_8.tar.gz",
},
{
version: "13.0.1+9",
want: "https://github.com/AdoptOpenJDK/openjdk13-binaries/releases/download/jdk-13.0.1%2B9/OpenJDK13U-jdk_x64_linux_hotspot_13.0.1_9.tar.gz",
},
{
version: "14",
want: "https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14%2B36/OpenJDK14U-jdk_x64_linux_hotspot_14_36.tar.gz",
},
{
version: "14+36",
want: "https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14%2B36/OpenJDK14U-jdk_x64_linux_hotspot_14_36.tar.gz",
},
}
for _, test := range tests {
desc := &biome.Descriptor{
OS: biome.Linux,
Arch: biome.Intel64,
}
got, err := javaDownloadURL(test.version, desc)
if got != test.want || err != nil {
t.Errorf("javaDownloadURL(%q, %+v) =\n%q, %v; want\n%q, <nil>", test.version, desc, got, err, test.want)
}
}
t.Run("Existence", func(t *testing.T) {
if testing.Short() {
t.Skip("Skipping due to -short")
}
for _, test := range tests {
verifyURLExists(t, http.MethodGet, test.want)
}
})
}