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

Jm/ext Support for golang extensions #105

Closed
wants to merge 15 commits into from
Closed
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
5 changes: 4 additions & 1 deletion build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"path/filepath"

"github.com/loopholelabs/scale/compile/golang"
"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/scalefile"
"github.com/loopholelabs/scale/scalefunc"
"github.com/loopholelabs/scale/signature"
Expand Down Expand Up @@ -68,6 +69,8 @@ type LocalGolangOptions struct {

// Args are the optional arguments to pass to the compiler
Args []string

Extensions []extension.ExtensionInfo
}

func LocalGolang(options *LocalGolangOptions) (*scalefunc.Schema, error) {
Expand Down Expand Up @@ -156,7 +159,7 @@ func LocalGolang(options *LocalGolangOptions) (*scalefunc.Schema, error) {
_ = options.Storage.Delete(build)
}()

modfile, err := golang.GenerateGoModfile(options.Scalefile, signatureDependencyPath, signatureDependencyVersion, options.SourceDirectory)
modfile, err := golang.GenerateGoModfile(options.Scalefile, signatureDependencyPath, signatureDependencyVersion, options.SourceDirectory, options.Extensions)
if err != nil {
return nil, fmt.Errorf("unable to generate go.mod file: %w", err)
}
Expand Down
8 changes: 5 additions & 3 deletions compile/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
"github.com/loopholelabs/scale/version"

"github.com/loopholelabs/scale/compile/golang/templates"
"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/scalefile"
"github.com/loopholelabs/scale/signature"
)

var generator *Generator

func GenerateGoModfile(packageSchema *scalefile.Schema, signatureImport string, signatureVersion string, functionImport string) ([]byte, error) {
return generator.GenerateGoModfile(packageSchema, signatureImport, signatureVersion, functionImport)
func GenerateGoModfile(packageSchema *scalefile.Schema, signatureImport string, signatureVersion string, functionImport string, extensions []extension.ExtensionInfo) ([]byte, error) {
return generator.GenerateGoModfile(packageSchema, signatureImport, signatureVersion, functionImport, extensions)
}

func GenerateGoMain(packageSchema *scalefile.Schema, signatureSchema *signature.Schema) ([]byte, error) {
Expand All @@ -50,7 +51,7 @@ func New() *Generator {
}
}

func (g *Generator) GenerateGoModfile(packageSchema *scalefile.Schema, signatureImport string, signatureVersion string, functionImport string) ([]byte, error) {
func (g *Generator) GenerateGoModfile(packageSchema *scalefile.Schema, signatureImport string, signatureVersion string, functionImport string, extensions []extension.ExtensionInfo) ([]byte, error) {
if signatureVersion == "" && !strings.HasPrefix(signatureImport, "/") && !strings.HasPrefix(signatureImport, "./") && !strings.HasPrefix(signatureImport, "../") {
signatureImport = "./" + signatureImport
}
Expand All @@ -62,6 +63,7 @@ func (g *Generator) GenerateGoModfile(packageSchema *scalefile.Schema, signature
buf := new(bytes.Buffer)
err := g.template.ExecuteTemplate(buf, "mod.go.templ", map[string]interface{}{
"package_schema": packageSchema,
"extensions": extensions,
"signature_import": signatureImport,
"signature_version": signatureVersion,
"function_import": functionImport,
Expand Down
8 changes: 7 additions & 1 deletion compile/golang/templates/mod.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ replace {{ .package_schema.Name }} v0.1.0 => {{ .function_import }}
require (
signature v0.1.0
{{ .package_schema.Name }} v0.1.0
)
)

{{ range $extension := .extensions -}}
replace {{ $extension.Name }} => {{ $extension.Path }}

require {{ $extension.Name }} {{ $extension.Version }}
{{end -}}
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"regexp"

extension "github.com/loopholelabs/scale-extension-interfaces"
interfaces "github.com/loopholelabs/scale-signature-interfaces"
"github.com/loopholelabs/scale/scalefunc"
)
Expand Down Expand Up @@ -50,6 +51,7 @@ type Config[T interfaces.Signature] struct {
stdout io.Writer
stderr io.Writer
rawOutput bool
extensions []extension.Extension
}

// NewConfig returns a new Scale Runtime Config
Expand Down Expand Up @@ -85,6 +87,11 @@ func (c *Config[T]) validate() error {
return nil
}

func (c *Config[T]) WithExtension(e extension.Extension) *Config[T] {
c.extensions = append(c.extensions, e)
return c
}

func (c *Config[T]) WithSignature(newSignature interfaces.New[T]) *Config[T] {
c.newSignature = newSignature
return c
Expand Down
24 changes: 24 additions & 0 deletions extension/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2023 Loophole Labs

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

http://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 extension

type FunctionSchema struct {
Name string `hcl:"name,label"`
Description string `hcl:"description,optional"`
Params string `hcl:"params,optional"`
Return string `hcl:"return,optional"`
}
88 changes: 88 additions & 0 deletions extension/generator/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2023 Loophole Labs

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

http://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 generator

import (
"bytes"
"io"
"io/fs"
"os"
"time"

"golang.org/x/mod/zip"
)

var _ zip.File = (*File)(nil)
var _ os.FileInfo = (*File)(nil)

type File struct {
name string
path string
content []byte
reader *bytes.Reader
size int64
}

func NewFile(name string, path string, content []byte) File {
return File{
name: name,
path: path,
content: content,
reader: bytes.NewReader(content),
size: int64(len(content)),
}
}

func (g File) Name() string {
return g.name
}

func (g File) Size() int64 {
return g.size
}

func (g File) Mode() fs.FileMode {
return 0700
}

func (g File) ModTime() time.Time {
return time.Now()
}

func (g File) IsDir() bool {
return false
}

func (g File) Sys() any {
return g.content
}

func (g File) Path() string {
return g.path
}

func (g File) Lstat() (os.FileInfo, error) {
return g, nil
}

func (g File) Open() (io.ReadCloser, error) {
return io.NopCloser(g.reader), nil
}

func (g File) Data() []byte {
return g.content
}
158 changes: 158 additions & 0 deletions extension/generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
Copyright 2023 Loophole Labs
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
http://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 generator

import (
"bytes"
"encoding/hex"

"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/extension/generator/golang"
"github.com/loopholelabs/scale/extension/generator/rust"
)

type GuestRegistryPackage struct {
GolangModule *bytes.Buffer
GolangModfile []byte
RustCrate *bytes.Buffer
RustCargofile []byte
TypescriptPackage *bytes.Buffer
TypescriptPackageJSON []byte
}

type GuestLocalPackage struct {
GolangFiles []File
RustFiles []File
TypescriptFiles []File
}

type HostRegistryPackage struct {
GolangModule *bytes.Buffer
GolangModfile []byte
TypescriptPackage *bytes.Buffer
TypescriptPackageJSON []byte
}

type HostLocalPackage struct {
GolangFiles []File
TypescriptFiles []File
}

type Options struct {
Extension *extension.Schema

GolangPackageImportPath string
GolangPackageName string
GolangPackageVersion string

RustPackageName string
RustPackageVersion string

TypescriptPackageName string
TypescriptPackageVersion string
}

func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
hash, err := options.Extension.Hash()
if err != nil {
return nil, err
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

golangGuest, err := golang.GenerateGuest(options.Extension, options.GolangPackageName, options.GolangPackageVersion)
if err != nil {
return nil, err
}

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName, options.GolangPackageVersion)
if err != nil {
return nil, err
}

modfile, err := golang.GenerateModfile(options.GolangPackageName)
if err != nil {
return nil, err
}

golangFiles := []File{
NewFile("types.go", "types.go", golangTypes),
NewFile("guest.go", "guest.go", golangGuest),
NewFile("interfaces.go", "interfaces.go", golangInterfaces),
NewFile("go.mod", "go.mod", modfile),
}

rustTypes, err := rust.GenerateTypes(options.Extension, options.RustPackageName)
if err != nil {
return nil, err
}

rustGuest, err := rust.GenerateGuest(options.Extension, hashString, options.RustPackageName)
if err != nil {
return nil, err
}

cargofile, err := rust.GenerateCargofile(options.RustPackageName, options.RustPackageVersion)
if err != nil {
return nil, err
}

rustFiles := []File{
NewFile("types.rs", "types.rs", rustTypes),
NewFile("guest.rs", "guest.rs", rustGuest),
NewFile("Cargo.toml", "Cargo.toml", cargofile),
}

return &GuestLocalPackage{
GolangFiles: golangFiles,
RustFiles: rustFiles,
}, nil
}

func GenerateHostLocal(options *Options) (*HostLocalPackage, error) {
golangTypes, err := golang.GenerateTypes(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
}

golangHost, err := golang.GenerateHost(options.Extension, options.GolangPackageName, options.GolangPackageVersion)
if err != nil {
return nil, err
}

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName, options.GolangPackageVersion)
if err != nil {
return nil, err
}

modfile, err := golang.GenerateModfile(options.GolangPackageName)
if err != nil {
return nil, err
}

golangFiles := []File{
NewFile("types.go", "types.go", golangTypes),
NewFile("host.go", "host.go", golangHost),
NewFile("interfaces.go", "interfaces.go", golangInterfaces),
NewFile("go.mod", "go.mod", modfile),
}

return &HostLocalPackage{
GolangFiles: golangFiles,
}, nil
}
Loading
Loading