-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Swift provider and Vapor support
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package swift | ||
|
||
import ( | ||
"github.com/spf13/afero" | ||
|
||
"github.com/zeabur/zbpack/internal/utils" | ||
"github.com/zeabur/zbpack/pkg/plan" | ||
"github.com/zeabur/zbpack/pkg/types" | ||
) | ||
|
||
type identify struct{} | ||
|
||
// NewIdentifier returns a new Python identifier. | ||
func NewIdentifier() plan.Identifier { | ||
return &identify{} | ||
} | ||
|
||
func (i *identify) PlanType() types.PlanType { | ||
return types.PlanTypeSwift | ||
} | ||
|
||
func (i *identify) Match(fs afero.Fs) bool { | ||
return utils.HasFile(fs, "Package.swift") | ||
} | ||
|
||
func (i *identify) PlanMeta(options plan.NewPlannerOptions) types.PlanMeta { | ||
return GetMeta(GetMetaOptions{Src: options.Source, Config: options.Config}) | ||
} | ||
|
||
var _ plan.Identifier = (*identify)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package swift | ||
|
||
import ( | ||
"github.com/moznion/go-optional" | ||
"github.com/spf13/afero" | ||
"github.com/zeabur/zbpack/internal/utils" | ||
"github.com/zeabur/zbpack/pkg/plan" | ||
"github.com/zeabur/zbpack/pkg/types" | ||
) | ||
|
||
type swiftPlanContext struct { | ||
Src afero.Fs | ||
Config plan.ImmutableProjectConfiguration | ||
Framework optional.Option[types.SwiftFramework] | ||
} | ||
|
||
// DetermineFramework determines the framework of the Swift project. | ||
func DetermineFramework(ctx *swiftPlanContext) types.SwiftFramework { | ||
src := ctx.Src | ||
fw := &ctx.Framework | ||
|
||
if framework, err := fw.Take(); err == nil { | ||
return framework | ||
} | ||
|
||
content, err := afero.ReadFile(src, "Package.swift") | ||
if err != nil { | ||
*fw = optional.Some(types.SwiftFrameworkNone) | ||
return fw.Unwrap() | ||
} | ||
|
||
if utils.WeakContains(string(content), "https://github.com/vapor/vapor.git") { | ||
*fw = optional.Some(types.SwiftFrameworkVapor) | ||
return fw.Unwrap() | ||
} | ||
|
||
*fw = optional.Some(types.SwiftFrameworkNone) | ||
return fw.Unwrap() | ||
} | ||
|
||
// GetMetaOptions is the options for GetMeta. | ||
type GetMetaOptions struct { | ||
Src afero.Fs | ||
Config plan.ImmutableProjectConfiguration | ||
} | ||
|
||
// GetMeta returns the metadata of a Swift project. | ||
func GetMeta(opt GetMetaOptions) types.PlanMeta { | ||
meta := types.PlanMeta{} | ||
|
||
ctx := &swiftPlanContext{ | ||
Src: opt.Src, | ||
Config: opt.Config, | ||
} | ||
|
||
framework := DetermineFramework(ctx) | ||
if framework != types.SwiftFrameworkNone { | ||
meta["framework"] = string(framework) | ||
} | ||
|
||
return meta | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package swift | ||
|
||
import ( | ||
"github.com/zeabur/zbpack/pkg/packer" | ||
"github.com/zeabur/zbpack/pkg/types" | ||
) | ||
|
||
type pack struct { | ||
*identify | ||
} | ||
|
||
func NewPacker() packer.Packer { | ||
return &pack{ | ||
identify: &identify{}, | ||
} | ||
} | ||
|
||
func GenerateDockerfile(meta types.PlanMeta) (string, error) { | ||
|
||
// TODO: following dockerfile is copied from Vapor's template, need to be modified to support other Swift use cases | ||
return `FROM swift:5.9-jammy as build | ||
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ | ||
&& apt-get -q update \ | ||
&& apt-get -q dist-upgrade -y \ | ||
&& apt-get install -y libjemalloc-dev | ||
WORKDIR /build | ||
COPY ./Package.* ./ | ||
RUN swift package resolve --skip-update \ | ||
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true) | ||
COPY . . | ||
RUN swift build -c release \ | ||
--static-swift-stdlib \ | ||
-Xlinker -ljemalloc | ||
WORKDIR /staging | ||
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./ | ||
RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./ | ||
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \; | ||
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true | ||
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true | ||
FROM ubuntu:jammy | ||
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ | ||
&& apt-get -q update \ | ||
&& apt-get -q dist-upgrade -y \ | ||
&& apt-get -q install -y \ | ||
libjemalloc2 \ | ||
ca-certificates \ | ||
tzdata \ | ||
libcurl4 \ | ||
libxml2 \ | ||
&& rm -r /var/lib/apt/lists/* | ||
WORKDIR /app | ||
COPY --from=build --chown=vapor:vapor /staging /app | ||
ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static | ||
ENTRYPOINT ["./App"] | ||
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"] | ||
`, nil | ||
} | ||
|
||
func (p *pack) GenerateDockerfile(meta types.PlanMeta) (string, error) { | ||
return GenerateDockerfile(meta) | ||
} | ||
|
||
var _ packer.Packer = (*pack)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters