Skip to content

Commit

Permalink
cmd/mecha: add -type flag to mecha create module to support Rust and …
Browse files Browse the repository at this point in the history
…Zig module templates

Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Apr 1, 2024
1 parent 44bff01 commit 990fbb5
Show file tree
Hide file tree
Showing 5 changed files with 975 additions and 37 deletions.
16 changes: 15 additions & 1 deletion cmd/mecha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,27 @@ mecha new module mymodule

```

### New module based on template
### New TinyGo WASM module based on template

```bash
mecha new module -t=blink mymodule

```

### New Rust WASM module based on template

```bash
mecha new module -t=pingrs -type=rust pingrs

```

## New Zig WASM module based on template

```bash
mecha new module -t=pingzig -type=zig pingzig

```

### Build modules in current project

```bash
Expand Down
59 changes: 38 additions & 21 deletions cmd/mecha/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

getter "github.com/hashicorp/go-getter"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -61,6 +63,17 @@ func createModule(cCtx *cli.Context) error {
os.Chdir("modules")
defer os.Chdir("..")

tmpltype := cCtx.String("type")
switch {
case tmpltype == "rust":
return downloadFromTemplate(templateName, name)
case tmpltype == "zig":
return downloadFromTemplate(templateName, name)
case tmpltype == "tinygo":
default:
return fmt.Errorf("unknown template type %s", tmpltype)
}

if err := createFromTemplate(templateName, name); err != nil {
return err
}
Expand All @@ -85,27 +98,6 @@ func createFromTemplate(templ, proj string) error {
os.Exit(1)
}

// patch the go.mod file to use forked wazero
basename := filepath.Base(proj)
if err := os.Chdir(basename); err != nil {
fmt.Println(err)
os.Exit(1)
}
defer os.Chdir("..")

return nil // TODO: completely remove replaceWazeroWithFork()
}

func replaceWazeroWithFork() error {
var stdout, stderr bytes.Buffer
cmd := exec.Command("go", "mod", "edit", "-replace", "github.com/tetratelabs/wazero=github.com/hybridgroup/wazero@v0.0.0-20240328190114-79d4bea3ca005")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
fmt.Printf("%s: %v\n%s%s", cmd.String(), err, stderr.Bytes(), stdout.Bytes())
os.Exit(1)
}

return nil
}

Expand All @@ -122,3 +114,28 @@ func getModuleName() (string, error) {

return strings.TrimSuffix(stdout.String(), "\n"), nil
}

func downloadFromTemplate(templateName, name string) error {
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting pwd: %s", err)
return err
}

opts := []getter.ClientOption{}
client := &getter.Client{
Ctx: context.Background(),
Src: templateName,
Dst: filepath.Base(name),
Pwd: pwd,
Mode: getter.ClientModeDir,
Options: opts,
}

if err := client.Get(); err != nil {
fmt.Printf("Error downloading template: %s", err)
return err
}

return nil
}
33 changes: 32 additions & 1 deletion cmd/mecha/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,47 @@ go 1.22.0

require (
github.com/briandowns/spinner v1.23.0
github.com/hashicorp/go-getter v1.7.3
github.com/urfave/cli/v2 v2.27.1
)

require (
cloud.google.com/go v0.104.0 // indirect
cloud.google.com/go/compute v1.10.0 // indirect
cloud.google.com/go/iam v0.5.0 // indirect
cloud.google.com/go/storage v1.27.0 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.100.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading

0 comments on commit 990fbb5

Please sign in to comment.