Skip to content

Commit

Permalink
First ver compile golang guest
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 7ad49d2 commit c959434
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
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 extension/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package extension

type ExtensionInfo struct {
Name string
Path string
Version string
}
25 changes: 16 additions & 9 deletions scalefile/scalefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,23 @@ type SignatureSchema struct {
Tag string `hcl:"tag,attr"`
}

type ExtensionSchema struct {
Organization string `hcl:"organization,optional"`
Name string `hcl:"name,attr"`
Tag string `hcl:"tag,attr"`
}

type Schema struct {
Version string `hcl:"version,attr"`
Name string `hcl:"name,attr"`
Tag string `hcl:"tag,attr"`
Language string `hcl:"language,attr"`
Signature SignatureSchema `hcl:"signature,block"`
Stateless bool `hcl:"stateless,optional"`
Function string `hcl:"function,attr"`
Initialize string `hcl:"initialize,attr"`
Description string `hcl:"description,optional"`
Version string `hcl:"version,attr"`
Name string `hcl:"name,attr"`
Tag string `hcl:"tag,attr"`
Language string `hcl:"language,attr"`
Signature SignatureSchema `hcl:"signature,block"`
Stateless bool `hcl:"stateless,optional"`
Function string `hcl:"function,attr"`
Initialize string `hcl:"initialize,attr"`
Description string `hcl:"description,optional"`
Extensions []ExtensionSchema `hcl:"extension,block"`
}

func ReadSchema(path string) (*Schema, error) {
Expand Down

0 comments on commit c959434

Please sign in to comment.