Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify carton package to work with targets #323

Merged
merged 1 commit into from
Apr 4, 2024
Merged
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
30 changes: 29 additions & 1 deletion carton/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -97,10 +98,37 @@ func (p Package) Create(options ...Option) {
return
}

logger.Debugf("IncludeFiles: %+v", metadata.IncludeFiles)

supportedTargets := []string{}
for _, i := range metadata.IncludeFiles {
if strings.HasPrefix(i, "linux/") {
parts := strings.SplitN(i, "/", 3)
if len(parts) < 3 {
// this shouldn't happen, but if it does for some reason just ignore it
// this entry is not a properly formatted target
continue
}
supportedTargets = append(supportedTargets, fmt.Sprintf("%s/%s", parts[0], parts[1]))
}
}

if len(supportedTargets) == 0 {
logger.Info("No supported targets found, defaulting to old format")
}

logger.Debugf("Supported targets: %+v", supportedTargets)

entries := map[string]string{}

for _, i := range metadata.IncludeFiles {
entries[i] = filepath.Join(p.Source, i)
if len(supportedTargets) == 0 || strings.HasPrefix(i, "linux/") || i == "buildpack.toml" {
entries[i] = filepath.Join(p.Source, i)
} else {
for _, target := range supportedTargets {
entries[fmt.Sprintf("%s/%s", target, i)] = filepath.Join(p.Source, i)
}
}
}
logger.Debugf("Include files: %+v", entries)

Expand Down
63 changes: 62 additions & 1 deletion carton/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,68 @@ include-files = [
Expect(e.Dir).To(Equal(path))
})

it("includes include_files", func() {
context("has a buildpack.toml with target specific include files", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(path, "buildpack.toml"), []byte(`
api = "0.0.0"

[buildpack]
name = "test-name"
version = "{{.version}}"

[[metadata.dependencies]]
id = "test-id"
name = "test-name"
version = "1.1.1"
uri = "test-uri"
sha256 = "test-sha256"
stacks = [ "test-stack" ]

[[metadata.dependencies.licenses]]
type = "test-type"
uri = "test-uri"

[metadata]
pre-package = "test-pre-package"
include-files = [
"buildpack.toml",
"README",
"LICENSE",
"linux/amd64/bin/just-once",
"linux/arm64/bin/also-just-once"
]
`), 0644)).To(Succeed())
})

it("includes include_files using the original format", func() {
carton.Package{
Source: path,
Destination: "test-destination",
}.Create(
carton.WithEntryWriter(entryWriter),
carton.WithExecutor(executor),
carton.WithExitHandler(exitHandler))

Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml")))
Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml")))

Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "LICENSE")))
Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/amd64/LICENSE")))
Expect(entryWriter.Calls[2].Arguments[0]).To(Equal(filepath.Join(path, "README")))
Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/amd64/README")))
Expect(entryWriter.Calls[3].Arguments[0]).To(Equal(filepath.Join(path, "linux/amd64/bin/just-once")))
Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/amd64/bin/just-once")))

Expect(entryWriter.Calls[4].Arguments[0]).To(Equal(filepath.Join(path, "LICENSE")))
Expect(entryWriter.Calls[4].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/arm64/LICENSE")))
Expect(entryWriter.Calls[5].Arguments[0]).To(Equal(filepath.Join(path, "README")))
Expect(entryWriter.Calls[5].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/arm64/README")))
Expect(entryWriter.Calls[6].Arguments[0]).To(Equal(filepath.Join(path, "linux/arm64/bin/also-just-once")))
Expect(entryWriter.Calls[6].Arguments[1]).To(Equal(filepath.Join("test-destination", "linux/arm64/bin/also-just-once")))
})
})

it("includes include_files using the target format", func() {
carton.Package{
Source: path,
Destination: "test-destination",
Expand Down