Skip to content

Commit

Permalink
Jm/ext - Go and Rust extension support. (#112)
Browse files Browse the repository at this point in the history
This is an initial implementation of scale extensions.
Implemented for go (host and guest), and rust (guest). 

Scale-63 Scale-68
---------

Signed-off-by: Jimmy Moore <[email protected]>
Signed-off-by: Shivansh Vij <[email protected]>
Co-authored-by: Shivansh Vij <[email protected]>
  • Loading branch information
jimmyaxod and ShivanshVij authored Nov 30, 2023
1 parent f317ae0 commit d726345
Show file tree
Hide file tree
Showing 66 changed files with 11,711 additions and 9 deletions.
21 changes: 20 additions & 1 deletion build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type LocalGolangOptions struct {

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

Extensions []extension.Info
}

func LocalGolang(options *LocalGolangOptions) (*scalefunc.V1BetaSchema, error) {
Expand Down Expand Up @@ -192,7 +194,24 @@ func LocalGolang(options *LocalGolangOptions) (*scalefunc.V1BetaSchema, error) {
_ = options.Storage.Delete(build)
}()

modfile, err := golang.GenerateGoModfile(signatureInfo, functionInfo)
// Copy over any replacements from the go.mod
replacements := make([]golang.GoModReplacement, 0)

r := manifest.GetReplacements()
for _, resp := range r {
// Check if the target is a local dir...
newPath := resp.New.Path

if !filepath.IsAbs(newPath) {
newPath = filepath.Join(options.SourceDirectory, newPath)
}
replacements = append(replacements, golang.GoModReplacement{
Name: fmt.Sprintf("%s %s", resp.Old.Path, resp.Old.Version),
Path: fmt.Sprintf("%s %s", newPath, resp.New.Version),
})
}

modfile, err := golang.GenerateGoModfile(signatureInfo, functionInfo, replacements)
if err != nil {
return nil, fmt.Errorf("unable to generate go.mod file: %w", err)
}
Expand Down
16 changes: 11 additions & 5 deletions compile/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import (

var generator *Generator

func GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo) ([]byte, error) {
return generator.GenerateGoModfile(signatureInfo, functionInfo)
type GoModReplacement struct {
Name string
Path string
}

func GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo, replacements []GoModReplacement) ([]byte, error) {
return generator.GenerateGoModfile(signatureInfo, functionInfo, replacements)
}

func GenerateGoMain(scalefileSchema *scalefile.Schema, signatureSchema *signature.Schema, functionInfo *FunctionInfo) ([]byte, error) {
Expand All @@ -50,14 +55,15 @@ func New() *Generator {
}
}

func (g *Generator) GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo) ([]byte, error) {
func (g *Generator) GenerateGoModfile(signatureInfo *SignatureInfo, functionInfo *FunctionInfo, replacements []GoModReplacement) ([]byte, error) {
signatureInfo.normalize()
functionInfo.normalize()

buf := new(bytes.Buffer)
err := g.template.ExecuteTemplate(buf, "mod.go.templ", map[string]interface{}{
"function": functionInfo,
"signature": signatureInfo,
"function": functionInfo,
"signature": signatureInfo,
"replacements": replacements,
})
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions compile/golang/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func ParseManifest(data []byte) (*Manifest, error) {
}, nil
}

func (m *Manifest) GetReplacements() []*modfile.Replace {
return m.modfile.Replace
}

func (m *Manifest) AddReplacement(oldDependency string, oldVersion string, newDependency string, newVersion string) error {
return m.modfile.AddReplace(oldDependency, oldVersion, newDependency, newVersion)
}
Expand Down
7 changes: 6 additions & 1 deletion compile/golang/templates/mod.go.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module compile

go 1.20

{{ if .signature.Local }}
replace signature v0.1.0 => {{ .signature.ImportPath }}
{{ else }}
Expand All @@ -9,7 +10,11 @@ replace signature v0.1.0 => {{ .signature.ImportPath }} {{ .signature.ImportVers

replace {{ .function.PackageName }} v0.1.0 => {{ .function.ImportPath }}

{{ range $replacements := .replacements }}
replace {{ $replacements.Name }} => {{ $replacements.Path }}
{{ end }}

require (
signature v0.1.0
{{ .function.PackageName }} v0.1.0
)
)
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
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
}
163 changes: 163 additions & 0 deletions extension/generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
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

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, hashString, options.GolangPackageName)
if err != nil {
return nil, err
}

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName)
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) {
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
}

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

golangInterfaces, err := golang.GenerateInterfaces(options.Extension, options.GolangPackageName)
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

0 comments on commit d726345

Please sign in to comment.