Skip to content

Commit

Permalink
Merge pull request #5 from zouyuxuan/main
Browse files Browse the repository at this point in the history
Implement the Build interface to generate the /bin/build program
  • Loading branch information
wangeguo authored Jan 15, 2024
2 parents 042a01e + cd24944 commit 4e2aa6e
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/*
.idea
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# `ghcr.io/amp-buildpacks/scarb`

A Cloud Native Buildpack for scarb

## Usage

### 1. To use this buildpack, simply run:

```shell
pack build <image-name> \
--path <cairo-path> \
--buildpack ghcr.io/amp-buildpacks/scarb \
--builder paketobuildpacks/builder-jammy-full
```

For example:

```shell
pack build scarb \
--path ./samples/hello_scarb \
--buildpack ghcr.io/amp-buildpacks/scarb \
--builder paketobuildpacks/builder-jammy-full
```

### 2. To run the image, simply run:

```shell
docker run -u <uid>:<gid> -it <image-name>
```

For example:

```shell
docker run -u 1001:cnb -it scarb
```

## Contributing

If anything feels off, or if you feel that some functionality is missing, please
check out the [contributing
page](https://docs.amphitheatre.app/contributing/). There you will find
instructions for sharing your feedback, building the tool locally, and
submitting pull requests to the project.

## License

Copyright (c) The Amphitheatre Authors. All rights reserved.

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.

## Credits

Heavily inspired by https://github.com/paketo-community/rustup
62 changes: 62 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Indicates compatibility version with lifecycle
api = "0.8"

[buildpack]
description = "A Cloud Native Buildpack that installs scarb"
homepage = "https://github.com/amp-buildpacks/scarb"
id = "amp-buildpacks/scarb"
keywords = ["cairo", "scarb"]
name = "AMP Buildpack for Scarb"
sbom-formats = ["application/vnd.cyclonedx+json", "application/vnd.syft+json"]
version = "0.1.0"

[[buildpack.licenses]]
type = "Apache-2.0"
uri = "https://github.com/amp-buildpacks/scarb/blob/main/LICENSE"

[metadata]
include-files = ["LICENSE", "README.md", "bin/build", "bin/detect", "bin/main", "buildpack.toml"]
pre-package = "scripts/build.sh"

[[metadata.configurations]]
build = true
default = "false"
description = "enable the scarb run process"
name = "BP_ENABLE_SCARB_PROCESS"

[[metadata.configurations]]
build = true
default = "2.4.3"
description = "the scarb version"
name = "BP_SCARB_VERSION"

[[metadata.configurations]]
build = true
default = "gnu"
description = "libc implementation: gnu or musl"
name = "BP_SCARB_LIBC"

[[metadata.dependencies]]
id = "scarb-musl"
name = "Scarb (musl libc)"
purl = "pkg:generic/[email protected]"
sha256 = "0c80edd932b21308fd024a2474f2db4647c25124557737fa0facfd902fd39f53"
stacks = ["io.buildpacks.stacks.bionic", "io.paketo.stacks.tiny", "*"]
uri = "https://github.com/software-mansion/scarb/releases/download/v2.4.3/scarb-v2.4.3-x86_64-unknown-linux-musl.tar.gz"
version = "2.4.3"

[[metadata.dependencies]]
id = "scarb-gnu"
name = "Scarb (gnu libc)"
purl = "pkg:generic/[email protected]"
sha256 = "bae801283f7fa3822079f86fbccc882f557e316305ee0ea01c8867796db52d3c"
stacks = ["io.buildpacks.stacks.bionic", "io.paketo.stacks.tiny", "*"]
uri = "https://github.com/software-mansion/scarb/releases/download/v2.4.3/scarb-v2.4.3-x86_64-unknown-linux-gnu.tar.gz"
version = "2.4.3"

[[metadata.dependencies.licenses]]
type = "MIT"
uri = "https://github.com/software-mansion/scarb/blob/main/LICENSE"

[[stacks]]
id = "*"
30 changes: 30 additions & 0 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// 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.

package main

import (
"os"

"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak/bard"
"scarb/scarb"
)

func main() {
libcnb.Main(
scarb.Detect{},
scarb.Build{Logger: bard.NewLogger(os.Stdout)},
)
}
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module scarb

go 1.20

require (
github.com/buildpacks/libcnb v1.30.1
github.com/mattn/go-shellwords v1.0.12
github.com/paketo-buildpacks/libpak v1.68.0
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/creack/pty v1.1.20 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/heroku/color v0.0.6 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/sys v0.14.0 // indirect
)
49 changes: 49 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/buildpacks/libcnb v1.30.1 h1:airpkZzh8Mlt+SQEMTj0B1r5agDHZvgfpwzpKVMEgZQ=
github.com/buildpacks/libcnb v1.30.1/go.mod h1:JU2fam/ECXw6I5iRnZbfKgjWsrZbzbgvZHR6WBSVHRc=
github.com/creack/pty v1.1.20 h1:VIPb/a2s17qNeQgDnkfZC35RScx+blkKF8GV68n80J4=
github.com/creack/pty v1.1.20/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/heroku/color v0.0.6 h1:UTFFMrmMLFcL3OweqP1lAdp8i1y/9oHqkeHjQ/b/Ny0=
github.com/heroku/color v0.0.6/go.mod h1:ZBvOcx7cTF2QKOv4LbmoBtNl5uB17qWxGuzZrsi1wLU=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/paketo-buildpacks/libpak v1.68.0 h1:unfJscVujFT8WZgUONMh4vp64x+LvAz6kwpPdagYibM=
github.com/paketo-buildpacks/libpak v1.68.0/go.mod h1:Np94HbeUNYcMmaJEnaP5VgqGgaz86RhWA9nONRCN96U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
59 changes: 59 additions & 0 deletions scarb/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// 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.

package scarb

import (
"fmt"
"log"

"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
)

type Build struct {
Logger bard.Logger
}

func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
b.Logger.Title(context.Buildpack)
result := libcnb.NewBuildResult()
config, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
buildScarb, _ := config.Resolve("BP_ENABLE_SCARB_PROCESS")
dependency, err := libpak.NewDependencyResolver(context)
if err != nil {
return libcnb.BuildResult{}, err
}
libc, _ := config.Resolve("BP_SCARB_LIBC")

version, _ := config.Resolve("BP_LEO_VERSION")
buildDependency, _ := dependency.Resolve(fmt.Sprintf("scarb-%s", libc), version)
log.Printf("scarb dependency = %+v", buildDependency)

dc, err := libpak.NewDependencyCache(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err)
}
dc.Logger = b.Logger

scarb := NewScarb(buildDependency, dc)
scarb.Logger = b.Logger
result.Processes, err = scarb.BuildProcessTypes(buildScarb)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to build list of process types\n%w", err)
}
result.Layers = append(result.Layers, scarb)
return result, nil
}
61 changes: 61 additions & 0 deletions scarb/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// 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.

package scarb

import (
"fmt"
"github.com/buildpacks/libcnb"
"os"
"path/filepath"
)

type Detect struct {
}

const PlanEntryScarb = "scarb"

func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) {
found, err := d.CairoProject(context.Application.Path)
if err != nil {
return libcnb.DetectResult{}, fmt.Errorf("unable to detect scarb requirements\n%w", err)

}
if !found {
return libcnb.DetectResult{Pass: false}, nil
}
return libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
Provides: []libcnb.BuildPlanProvide{
{Name: PlanEntryScarb},
},
Requires: []libcnb.BuildPlanRequire{
{Name: PlanEntryScarb},
},
},
},
}, nil
}

func (d Detect) CairoProject(appDir string) (bool, error) {
_, err := os.Stat(filepath.Join(appDir, "Scarb.toml"))
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, fmt.Errorf("unable to determine if Scarb.toml exists\n%w", err)
}
return true, nil
}
Loading

0 comments on commit 4e2aa6e

Please sign in to comment.