Skip to content

Commit

Permalink
Bumping scale version, fixing analytics, adding very experimental typ…
Browse files Browse the repository at this point in the history
…escript support

Signed-off-by: Shivansh Vij <[email protected]>
  • Loading branch information
ShivanshVij committed Oct 5, 2023
1 parent 312fe98 commit 4506675
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 109 deletions.
37 changes: 23 additions & 14 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
/*
Copyright 2023 Loophole Labs
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
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
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.
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 analytics

import (
"github.com/loopholelabs/scale-cli/analytics/posthog"
)

var _ Handler = (*posthog.PostHog)(nil)

var (
handler Handler
)

func init() {
p := posthog.Init()
if p != nil {
handler = p
}
}

type Handler interface {
Event(name string, properties map[string]string)
AssociateUser(user string, organization string)
Expand All @@ -41,10 +54,6 @@ func AssociateUser(user string, organization string) {
}
}

func Init(h Handler) {
handler = h
}

func Cleanup() {
if handler != nil {
handler.Cleanup()
Expand Down
41 changes: 20 additions & 21 deletions analytics/posthog/posthog.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/*
Copyright 2023 Loophole Labs
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
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
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.
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 posthog

import (
"github.com/loopholelabs/scale-cli/analytics"
"github.com/loopholelabs/scale-cli/analytics/machine"
"github.com/posthog/posthog-go"
"time"
Expand All @@ -31,28 +30,28 @@ var (
APIHost = ""
)

func init() {
type PostHog struct {
client posthog.Client
identifier string
}

func Init() *PostHog {
if APIKey == "" || APIHost == "" || !machine.Available() {
return
return nil
}
client, _ := posthog.NewWithConfig(APIKey, posthog.Config{
Endpoint: APIHost,
BatchSize: 1,
Logger: new(noopLogger),
})
if client != nil {
analytics.Init(&PostHog{
return &PostHog{
client: client,
identifier: machine.ID(),
})
}
}
}

var _ analytics.Handler = (*PostHog)(nil)

type PostHog struct {
client posthog.Client
identifier string
return nil
}

func (p *PostHog) Event(name string, properties map[string]string) {
Expand Down
12 changes: 12 additions & 0 deletions cmd/function/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ func BuildCmd(hidden bool) command.SetupCommand[*config.Config] {
Args: cargoArgs,
}
scaleFunc, err = build.LocalRust(opts)
case scalefunc.TypeScript:
opts := &build.LocalTypescriptOptions{
Output: out,
Scalefile: sf,
SourceDirectory: sourceDir,
SignatureSchema: signatureSchema,
Storage: stb,
Release: release,
Target: build.WASITarget,
NPMBin: npmBin,
}
scaleFunc, err = build.LocalTypescript(opts)
default:
return fmt.Errorf("language %s is not supported for local builds", sf.Language)
}
Expand Down
117 changes: 67 additions & 50 deletions cmd/function/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
case scalefunc.Rust:
signatureVersion = ""
signaturePath = path.Join(signaturePath, "rust", "guest")
case scalefunc.TypeScript:
signatureVersion = ""
signaturePath = path.Join(signaturePath, "typescript", "guest")
default:
return fmt.Errorf("language %s is not supported", language)
}
Expand All @@ -138,6 +141,8 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
case scalefunc.Rust:
signatureVersion = "0.1.0"
signaturePath = ""
case scalefunc.TypeScript:
return fmt.Errorf("typescript functions are not currently supported via the registry")
default:
return fmt.Errorf("language %s is not supported", language)
}
Expand Down Expand Up @@ -173,17 +178,9 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}

err = modfileTempl.Execute(dependencyFile, map[string]interface{}{
"package": functionName,
"old_signature_dependency": "signature",
"old_signature_version": "",
"new_signature_dependency": signaturePath,
"new_signature_version": signatureVersion,
"dependencies": []template.Dependency{
{
Name: "signature",
Version: "v0.1.0",
},
},
"package_name": functionName,
"signature_path": signaturePath,
"signature_version": signatureVersion,
})
if err != nil {
_ = dependencyFile.Close()
Expand All @@ -206,8 +203,8 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}

err = funcTempl.Execute(funcFile, map[string]interface{}{
"package": functionName,
"context": signatureContext,
"package_name": functionName,
"context_name": signatureContext,
})
if err != nil {
_ = funcFile.Close()
Expand All @@ -234,13 +231,10 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}

err = cargofileTempl.Execute(dependencyFile, map[string]interface{}{
"package": functionName,
"version": "0.1.0",
"signature_dependency": "signature",
"signature_package": fmt.Sprintf("%s_%s_%s_guest", parsedSignature.Organization, parsedSignature.Name, parsedSignature.Tag),
"signature_version": signatureVersion,
"signature_path": signaturePath,
"registry": "scale",
"package_name": functionName,
"signature_package": fmt.Sprintf("%s_%s_%s_guest", parsedSignature.Organization, parsedSignature.Name, parsedSignature.Tag),
"signature_version": signatureVersion,
"signature_path": signaturePath,
})
if err != nil {
_ = dependencyFile.Close()
Expand All @@ -263,7 +257,7 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}

err = funcTempl.Execute(funcFile, map[string]interface{}{
"context": signatureContext,
"context_name": signatureContext,
})
if err != nil {
_ = funcFile.Close()
Expand All @@ -274,35 +268,58 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
if err != nil {
return fmt.Errorf("error closing lib.rs file: %w", err)
}
//case scalefunc.TypeScript:
// scaleFile.Language = scalefunc.TypeScript
// scaleFile.Dependencies = []scalefile.Dependency{
// {
// Name: "@loopholelabs/scale-signature-http",
// Version: "0.3.8",
// },
// {
// Name: "@loopholelabs/scale-signature",
// Version: "0.2.11",
// },
// }
//
// tmpl, err := textTemplate.New("dependencies").Parse(template.TypeScriptTemplate)
// if err != nil {
// return fmt.Errorf("error parsing dependency template: %w", err)
// }
//
// dependencyFile, err := os.Create(fmt.Sprintf("%s/package.json", directory))
// if err != nil {
// return fmt.Errorf("error creating dependencies file: %w", err)
// }
//
// err = tmpl.Execute(dependencyFile, scaleFile.Dependencies)
//
// if err != nil {
// _ = dependencyFile.Close()
// return fmt.Errorf("error writing dependencies file: %w", err)
// }
case scalefunc.TypeScript:
scaleFile.Language = string(scalefunc.TypeScript)
scaleFile.Function = "scale"
analytics.Event("new-function", map[string]string{"language": "typescript"})

packageTempl, err := textTemplate.New("dependencies").Parse(template.TypescriptPackageTemplate)
if err != nil {
return fmt.Errorf("error parsing package.json template: %w", err)
}

dependencyFile, err := os.Create(path.Join(sourceDir, "package.json"))
if err != nil {
return fmt.Errorf("error creating package.json file: %w", err)
}

err = packageTempl.Execute(dependencyFile, map[string]interface{}{
"package_name": functionName,
"signature_path": signaturePath,
"signature_version": signatureVersion,
})
if err != nil {
_ = dependencyFile.Close()
return fmt.Errorf("error writing package.json file: %w", err)
}

err = dependencyFile.Close()
if err != nil {
return fmt.Errorf("error closing package.json file: %w", err)
}

funcTempl, err := textTemplate.New("function").Parse(template.TypeScriptFunctionTemplate)
if err != nil {
return fmt.Errorf("error parsing function template: %w", err)
}

funcFile, err := os.Create(path.Join(sourceDir, "index.ts"))
if err != nil {
return fmt.Errorf("error creating index.ts file: %w", err)
}

err = funcTempl.Execute(funcFile, map[string]interface{}{
"context_name": signatureContext,
})
if err != nil {
_ = funcFile.Close()
return fmt.Errorf("error writing index.ts file: %w", err)
}

err = funcFile.Close()
if err != nil {
return fmt.Errorf("error closing index.ts file: %w", err)
}
default:
return fmt.Errorf("language %s is not supported", language)
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/signature/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/loopholelabs/scale-cli/utils"
"github.com/loopholelabs/scale/compile/golang"
"github.com/loopholelabs/scale/compile/rust"
"github.com/loopholelabs/scale/compile/typescript"
"github.com/loopholelabs/scale/scalefile"
"github.com/loopholelabs/scale/scalefunc"
"github.com/loopholelabs/scale/storage"
Expand Down Expand Up @@ -95,6 +96,8 @@ func UseCmd(hidden bool) command.SetupCommand[*config.Config] {
signaturePath = path.Join(signaturePath, "golang", "guest")
case scalefunc.Rust:
signaturePath = path.Join(signaturePath, "rust", "guest")
case scalefunc.TypeScript:
signaturePath = path.Join(signaturePath, "typescript", "guest")
default:
return fmt.Errorf("failed to use signature %s/%s:%s: unknown or unsupported language", parsed.Organization, parsed.Name, parsed.Tag)
}
Expand All @@ -114,6 +117,8 @@ func UseCmd(hidden bool) command.SetupCommand[*config.Config] {
signaturePath = res.GetPayload().GolangImportPathGuest
case scalefunc.Rust:
signaturePath = res.GetPayload().RustImportPathGuest
case scalefunc.TypeScript:
return fmt.Errorf("failed to use signature %s/%s:%s: typescript is not currently support via the registry", parsed.Organization, parsed.Name, parsed.Tag)
default:
return fmt.Errorf("failed to use signature %s/%s:%s: unknown or unsupported language", parsed.Organization, parsed.Name, parsed.Tag)
}
Expand Down Expand Up @@ -199,6 +204,35 @@ func UseCmd(hidden bool) command.SetupCommand[*config.Config] {
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}
case scalefunc.TypeScript:
packageFileData, err := os.ReadFile(path.Join(sourceDir, "package.json"))
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}

p, err := typescript.ParseManifest(packageFileData)
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}

err = p.RemoveDependency("signature")
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}

err = p.AddDependency("signature", signaturePath)
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}
packageFileData, err = p.Write()
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}

err = os.WriteFile(path.Join(sourceDir, "package.json"), packageFileData, 0644)
if err != nil {
return fmt.Errorf("failed to use signature %s/%s:%s: %w", parsed.Organization, parsed.Name, parsed.Tag, err)
}
default:
return fmt.Errorf("failed to use signature %s/%s:%s: unknown or unsupported language", parsed.Organization, parsed.Name, parsed.Tag)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/loopholelabs/auth v0.2.47
github.com/loopholelabs/cmdutils v0.1.4
github.com/loopholelabs/releaser v0.1.1
github.com/loopholelabs/scale v0.4.2
github.com/loopholelabs/scale v0.4.3
github.com/mattn/go-isatty v0.0.19
github.com/mitchellh/go-homedir v1.1.0
github.com/natefinch/lumberjack v2.0.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ github.com/loopholelabs/polyglot v1.1.3 h1:WUTcSZ2TQ1lv7CZ4I9nHFBUjf0hKJN+Yfz1rZ
github.com/loopholelabs/polyglot v1.1.3/go.mod h1:EA88BEkIluKHAWxhyOV88xXz68YkRdo9IzZ+1dj+7Ao=
github.com/loopholelabs/releaser v0.1.1 h1:o8Z4jIR4TcaSRiwFpAGIJSAkAUOxlXL1ML3EscGqxDU=
github.com/loopholelabs/releaser v0.1.1/go.mod h1:51hV+lDDYeJNzeVOZCQnGS5cJN8ODg/V5AxYtiL67UA=
github.com/loopholelabs/scale v0.4.2 h1:uIsUSeG0cPxTyt7GxJc+pY23QlBUAH5GsIobz6o/T5k=
github.com/loopholelabs/scale v0.4.2/go.mod h1:3dsQQCsIbJ5oOEpI/53jVBuDPYyi/Z85ZjroPDTKIzw=
github.com/loopholelabs/scale v0.4.3 h1:Z8Iggzt2UnIzs7nHvcCm4GlqziK+0EY/pkeKSOaxA50=
github.com/loopholelabs/scale v0.4.3/go.mod h1:3dsQQCsIbJ5oOEpI/53jVBuDPYyi/Z85ZjroPDTKIzw=
github.com/loopholelabs/scale-signature-interfaces v0.1.7 h1:aOJJZpCKn/Q5Q0Gj+/Q6c7/iABEbojjbCzIqw7Mxyi0=
github.com/loopholelabs/scale-signature-interfaces v0.1.7/go.mod h1:3XLMjJjBf5lYxMtNKk+2XAWye4UyrkvUBJ9L6x2QCAk=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
Loading

0 comments on commit 4506675

Please sign in to comment.