Skip to content

Commit

Permalink
Clean up applet initialization (#1036)
Browse files Browse the repository at this point in the history
* Clean up applet initialization

* Previously you would a `runtime.Applet{}`, optionally set some fields
  on it, and then call its `Load` function. That function would set some
  other fields, and it was all kind of a mess.

  Replace all this with a `runtime.NewApplet()` function that accepts
  various options.

* Get rid of the distinction between "filename" and "ID" for an applet.
  Each applet now only has an ID.

* Replace Starlark thread initializers with explicit options or other
  alternatives. For example, `Run()` now accepts a `context.Context`,
  and there is a `WithPrintFunc` option.

Minor:

* Upgrade to Go 1.22.2.

* Replace deprecated usages of `io/ioutil` and `pkg/errors`.

* Fix lint errors

* Add a function for running Starlark tests
  • Loading branch information
rohansingh authored Apr 17, 2024
1 parent c9bc089 commit 0491987
Show file tree
Hide file tree
Showing 58 changed files with 437 additions and 916 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22.2"

- name: Install buildifier
run: make install-buildifier
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22.2"

- name: Install Node
uses: actions/setup-node@v3
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22.2"

- name: Fetch Release Artifacts
uses: actions/download-artifact@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22.2"

- name: Install buildifier
run: make install-buildifier
Expand All @@ -34,7 +34,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.22.2"

- name: Install Node
uses: actions/setup-node@v3
Expand Down
16 changes: 3 additions & 13 deletions cmd/community/loadapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package community

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/spf13/cobra"
"go.starlark.net/starlark"
"tidbyt.dev/pixlet/runtime"
)

Expand All @@ -26,7 +25,7 @@ func LoadApp(cmd *cobra.Command, args []string) error {
return fmt.Errorf("script file must have suffix .star: %s", script)
}

src, err := ioutil.ReadFile(script)
src, err := os.ReadFile(script)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", script, err)
}
Expand All @@ -35,16 +34,7 @@ func LoadApp(cmd *cobra.Command, args []string) error {
runtime.InitHTTP(cache)
runtime.InitCache(cache)

// Remove the print function from the starlark thread.
initializers := []runtime.ThreadInitializer{}
initializers = append(initializers, func(thread *starlark.Thread) *starlark.Thread {
thread.Print = func(thread *starlark.Thread, msg string) {}
return thread
})

applet := runtime.Applet{}
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
if err != nil {
if _, err := runtime.NewApplet(script, src, runtime.WithPrintDisabled()); err != nil {
return fmt.Errorf("failed to load applet: %w", err)
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/community/validateicons.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func ValidateIcons(cmd *cobra.Command, args []string) error {
return thread
})

applet := runtime.Applet{}
err = applet.LoadWithInitializers("", args[0], src, nil, initializers...)
applet, err := runtime.NewApplet(args[0], src, runtime.WithPrintDisabled())
if err != nil {
return fmt.Errorf("failed to load applet: %w", err)
}
Expand All @@ -69,7 +68,7 @@ func ValidateIcons(cmd *cobra.Command, args []string) error {
}

if _, ok := icons.IconsMap[field.Icon]; !ok {
return fmt.Errorf("app '%s' contains unknown icon: '%s'", applet.Filename, field.Icon)
return fmt.Errorf("app '%s' contains unknown icon: '%s'", applet.ID, field.Icon)
}
}

Expand Down
16 changes: 4 additions & 12 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -97,7 +97,7 @@ func profile(cmd *cobra.Command, args []string) error {
}

func ProfileApp(script string, config map[string]string) (*pprof_profile.Profile, error) {
src, err := ioutil.ReadFile(script)
src, err := os.ReadFile(script)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", script, err)
}
Expand All @@ -106,15 +106,7 @@ func ProfileApp(script string, config map[string]string) (*pprof_profile.Profile
runtime.InitHTTP(cache)
runtime.InitCache(cache)

// Remove the print function from the starlark thread.
initializers := []runtime.ThreadInitializer{}
initializers = append(initializers, func(thread *starlark.Thread) *starlark.Thread {
thread.Print = func(thread *starlark.Thread, msg string) {}
return thread
})

applet := runtime.Applet{}
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
applet, err := runtime.NewApplet(script, src, runtime.WithPrintDisabled())
if err != nil {
return nil, fmt.Errorf("failed to load applet: %w", err)
}
Expand All @@ -124,7 +116,7 @@ func ProfileApp(script string, config map[string]string) (*pprof_profile.Profile
return nil, fmt.Errorf("error starting profiler: %w", err)
}

_, err = applet.Run(config, initializers...)
_, err = applet.RunWithConfig(context.Background(), config)
if err != nil {
_ = starlark.StopProfile()
return nil, fmt.Errorf("error running script: %w", err)
Expand Down
35 changes: 12 additions & 23 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import (
"context"
"fmt"
"image"
"io/ioutil"
"os"
"strings"
"time"

"github.com/spf13/cobra"
"go.starlark.net/starlark"

"tidbyt.dev/pixlet/encode"
"tidbyt.dev/pixlet/globals"
"tidbyt.dev/pixlet/runtime"
"tidbyt.dev/pixlet/starlarkutil"
)

var (
Expand Down Expand Up @@ -106,45 +103,37 @@ func render(cmd *cobra.Command, args []string) error {
config[split[0]] = strings.Join(split[1:], "=")
}

src, err := ioutil.ReadFile(script)
src, err := os.ReadFile(script)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", script, err)
}

// Remove the print function from the starlark thread if the silent flag is
// passed.
initializers := []runtime.ThreadInitializer{}
var opts []runtime.AppletOption
if silenceOutput {
initializers = append(initializers, func(thread *starlark.Thread) *starlark.Thread {
thread.Print = func(thread *starlark.Thread, msg string) {}
return thread
})
opts = append(opts, runtime.WithPrintDisabled())
}

// Timeout?
ctx := context.Background()
if timeout > 0 {
initializers = append(initializers, func(thread *starlark.Thread) *starlark.Thread {
ctx, _ := context.WithTimeoutCause(
context.Background(),
time.Duration(timeout)*time.Millisecond,
fmt.Errorf("timeout after %dms", timeout),
)
starlarkutil.AttachThreadContext(ctx, thread)
return thread
})
ctx, _ = context.WithTimeoutCause(
ctx,
time.Duration(timeout)*time.Millisecond,
fmt.Errorf("timeout after %dms", timeout),
)
}

cache := runtime.NewInMemoryCache()
runtime.InitHTTP(cache)
runtime.InitCache(cache)

applet := runtime.Applet{}
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
applet, err := runtime.NewApplet(script, src, opts...)
if err != nil {
return fmt.Errorf("failed to load applet: %w", err)
}

roots, err := applet.Run(config, initializers...)
roots, err := applet.RunWithConfig(ctx, config)
if err != nil {
return fmt.Errorf("error running script: %w", err)
}
Expand Down Expand Up @@ -200,7 +189,7 @@ func render(cmd *cobra.Command, args []string) error {
if outPath == "-" {
_, err = os.Stdout.Write(buf)
} else {
err = ioutil.WriteFile(outPath, buf, 0644)
err = os.WriteFile(outPath, buf, 0644)
}

if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions docs/gen_widget_imgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package main
// documentation.

import (
"context"
"fmt"
"image"
"io/ioutil"
"os"
"strings"

"tidbyt.dev/pixlet/encode"
Expand Down Expand Up @@ -40,7 +41,7 @@ func Magnify(input image.Image) (image.Image, error) {
}

func main() {
files, err := ioutil.ReadDir(".")
files, err := os.ReadDir(".")
if err != nil {
panic(err)
}
Expand All @@ -51,7 +52,7 @@ func main() {
continue
}

content, err := ioutil.ReadFile(f.Name())
content, err := os.ReadFile(f.Name())
if err != nil {
panic(err)
}
Expand All @@ -67,13 +68,12 @@ def main():
return render.Root(child=w)
`, snippet)

app := runtime.Applet{}
err = app.Load(fmt.Sprintf("id-%s", name), name, []byte(src), nil)
app, err := runtime.NewApplet(name, []byte(src))
if err != nil {
panic(err)
}

roots, err := app.Run(nil)
roots, err := app.Run(context.Background())
if err != nil {
panic(err)
}
Expand All @@ -83,7 +83,7 @@ def main():
panic(err)
}

err = ioutil.WriteFile(fmt.Sprintf("img/widget_%s.gif", name), gif, 0644)
err = os.WriteFile(fmt.Sprintf("img/widget_%s.gif", name), gif, 0644)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/schema/location/example.star
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("encoding/json.star", "json")
load("render.star", "render")
load("schema.star", "schema")
load("encoding/json.star", "json")

DEFAULT_LOCATION = """
{
Expand Down
2 changes: 1 addition & 1 deletion docs/schema/locationbased/example.star
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("encoding/json.star", "json")
load("render.star", "render")
load("schema.star", "schema")
load("encoding/json.star", "json")

EXAMPLE_LOCATION = """
{
Expand Down
2 changes: 1 addition & 1 deletion docs/schema/oauth2/example.star
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("encoding/json.star", "json")
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")
load("secret.star", "secret")
load("encoding/json.star", "json")

OAUTH2_CLIENT_SECRET = secret.decrypt("your-client-secret")

Expand Down
2 changes: 1 addition & 1 deletion docs/schema/photoselect/example.star
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("encoding/base64.star", "base64")
load("render.star", "render")
load("schema.star", "schema")
load("encoding/base64.star", "base64")

def main(config):
encoded = config.get("photo", DEFAULT_PHOTO)
Expand Down
4 changes: 2 additions & 2 deletions encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package encode

import (
"crypto/sha256"
"fmt"
"image"

"github.com/pkg/errors"
"github.com/vmihailenco/msgpack/v5"

"tidbyt.dev/pixlet/render"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *Screens) Hash() ([]byte, error) {

j, err := msgpack.Marshal(hashable)
if err != nil {
return nil, errors.Wrap(err, "marshaling render tree to JSON")
return nil, fmt.Errorf("marshaling render tree to JSON: %w", err)
}

h := sha256.Sum256(j)
Expand Down
7 changes: 3 additions & 4 deletions encode/encode_bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package encode

import (
"context"
"testing"

"tidbyt.dev/pixlet/runtime"
Expand Down Expand Up @@ -71,15 +72,13 @@ def main(config):
`

func BenchmarkRunAndRender(b *testing.B) {
app := &runtime.Applet{}
err := app.Load("benchid", "benchmark.star", []byte(BenchmarkDotStar), nil)
app, err := runtime.NewApplet("benchmark.star", []byte(BenchmarkDotStar))
if err != nil {
b.Error(err)
}

config := map[string]string{}
for i := 0; i < b.N; i++ {
roots, err := app.Run(config)
roots, err := app.Run(context.Background())
if err != nil {
b.Error(err)
}
Expand Down
Loading

0 comments on commit 0491987

Please sign in to comment.