-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version with guest/host generated in cli working
Signed-off-by: Jimmy Moore <[email protected]>
- Loading branch information
Showing
31 changed files
with
793 additions
and
1,657 deletions.
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,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 | ||
} |
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,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 | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.