Skip to content

Commit

Permalink
First version with guest/host generated in cli working
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Moore <[email protected]>
  • Loading branch information
jimmyaxod committed Sep 19, 2023
1 parent b07b80e commit 845afa7
Show file tree
Hide file tree
Showing 31 changed files with 793 additions and 1,657 deletions.
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
}
135 changes: 135 additions & 0 deletions extension/generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
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"
)

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
}

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

modfile, err := golang.GenerateModfile(options.GolangPackageImportPath)
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),
}

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

modfile, err := golang.GenerateModfile(options.GolangPackageImportPath)
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
}
34 changes: 22 additions & 12 deletions extension/generator/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import (
"go/format"
"text/template"

polyglotVersion "github.com/loopholelabs/polyglot/version"
interfacesVersion "github.com/loopholelabs/scale-signature-interfaces/version"
"github.com/loopholelabs/scale/extension"

scaleVersion "github.com/loopholelabs/scale/version"

"github.com/loopholelabs/scale/extension/generator/templates"
"github.com/loopholelabs/scale/signature/generator/utils"
)
Expand All @@ -30,8 +34,8 @@ const (

var generator *Generator

func Generate(schema *extension.Schema, packageName string, version string) ([]byte, error) {
return generator.Generate(schema, packageName, version)
func GenerateTypes(schema *extension.Schema, packageName string) ([]byte, error) {
return generator.GenerateTypes(schema, packageName)
}

func GenerateInterfaces(schema *extension.Schema, packageName string, version string) ([]byte, error) {
Expand All @@ -42,8 +46,8 @@ func GenerateGuest(schema *extension.Schema, packageName string, version string)
return generator.GenerateGuest(schema, packageName, version)
}

func GenerateModfile(packageName string, polyglotVersion string) ([]byte, error) {
return generator.GenerateModfile(packageName, polyglotVersion)
func GenerateModfile(packageName string) ([]byte, error) {
return generator.GenerateModfile(packageName)
}

func GenerateHost(schema *extension.Schema, packageName string, version string) ([]byte, error) {
Expand Down Expand Up @@ -76,16 +80,21 @@ func New() (*Generator, error) {
}

// Generate generates the go code
func (g *Generator) Generate(schema *extension.Schema, packageName string, version string) ([]byte, error) {
func (g *Generator) GenerateTypes(schema *extension.Schema, packageName string) ([]byte, error) {
if packageName == "" {
packageName = defaultPackageName
}

ext, err := schema.CloneWithDisabledAccessorsValidatorsAndModifiers()
if err != nil {
return nil, err
}

buf := new(bytes.Buffer)
err := g.templ.ExecuteTemplate(buf, "types.go.templ", map[string]any{
"schema": schema,
"version": version,
"package": packageName,
err = g.templ.ExecuteTemplate(buf, "types.go.templ", map[string]any{
"signature_schema": ext,
"generator_version": scaleVersion.Version(),
"package_name": packageName,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,11 +141,12 @@ func (g *Generator) GenerateGuest(schema *extension.Schema, packageName string,
}

// GenerateModfile generates the modfile for the signature
func (g *Generator) GenerateModfile(packageName string, polyglotVersion string) ([]byte, error) {
func (g *Generator) GenerateModfile(packageImportPath string) ([]byte, error) {
buf := new(bytes.Buffer)
err := g.templ.ExecuteTemplate(buf, "mod.go.templ", map[string]any{
"polyglot_version": polyglotVersion,
"package": packageName,
"polyglot_version": polyglotVersion.Version(),
"scale_signature_interfaces_version": interfacesVersion.Version(),
"package_import_path": packageImportPath,
})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions extension/generator/golang/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGenerator(t *testing.T) {
require.NoError(t, err)
os.WriteFile("./interfaces.txt", f_interfaces, 0644)

formatted, err := Generate(s, "types", "v0.1.0")
formatted, err := GenerateTypes(s, "types")
require.NoError(t, err)

// Check things...
Expand All @@ -57,7 +57,7 @@ func TestGenerator(t *testing.T) {

os.WriteFile("./guest.txt", f_guest, 0644)

modf, err := GenerateModfile(packageName, "v0.1.0")
modf, err := GenerateModfile(packageName)
require.NoError(t, err)

os.WriteFile("./modfile.txt", modf, 0644)
Expand Down
37 changes: 0 additions & 37 deletions extension/generator/templates/arrays.rs.templ

This file was deleted.

Loading

0 comments on commit 845afa7

Please sign in to comment.