From c9594347e22556cb96a752b74c3dae69761bc0da Mon Sep 17 00:00:00 2001 From: Jimmy Moore Date: Thu, 14 Sep 2023 21:14:58 +0100 Subject: [PATCH] First ver compile golang guest Signed-off-by: Jimmy Moore --- build/golang.go | 5 ++++- compile/golang/generator.go | 8 +++++--- compile/golang/templates/mod.go.templ | 8 +++++++- extension/info.go | 7 +++++++ scalefile/scalefile.go | 25 ++++++++++++++++--------- 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 extension/info.go diff --git a/build/golang.go b/build/golang.go index 4b05d1b1..de7884a3 100644 --- a/build/golang.go +++ b/build/golang.go @@ -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" @@ -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) { @@ -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) } diff --git a/compile/golang/generator.go b/compile/golang/generator.go index a887bf07..bc9ce6b2 100644 --- a/compile/golang/generator.go +++ b/compile/golang/generator.go @@ -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) { @@ -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 } @@ -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, diff --git a/compile/golang/templates/mod.go.templ b/compile/golang/templates/mod.go.templ index e4c1d6a2..df708c6a 100644 --- a/compile/golang/templates/mod.go.templ +++ b/compile/golang/templates/mod.go.templ @@ -8,4 +8,10 @@ replace {{ .package_schema.Name }} v0.1.0 => {{ .function_import }} require ( signature v0.1.0 {{ .package_schema.Name }} v0.1.0 -) \ No newline at end of file +) + +{{ range $extension := .extensions -}} + replace {{ $extension.Name }} => {{ $extension.Path }} + + require {{ $extension.Name }} {{ $extension.Version }} +{{end -}} diff --git a/extension/info.go b/extension/info.go new file mode 100644 index 00000000..a91c4817 --- /dev/null +++ b/extension/info.go @@ -0,0 +1,7 @@ +package extension + +type ExtensionInfo struct { + Name string + Path string + Version string +} diff --git a/scalefile/scalefile.go b/scalefile/scalefile.go index 7bc1f664..65c0cfa6 100644 --- a/scalefile/scalefile.go +++ b/scalefile/scalefile.go @@ -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) {