Skip to content

Commit 990fbb5

Browse files
committed
cmd/mecha: add -type flag to mecha create module to support Rust and Zig module templates
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 44bff01 commit 990fbb5

File tree

5 files changed

+975
-37
lines changed

5 files changed

+975
-37
lines changed

cmd/mecha/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,27 @@ mecha new module mymodule
6969

7070
```
7171

72-
### New module based on template
72+
### New TinyGo WASM module based on template
7373

7474
```bash
7575
mecha new module -t=blink mymodule
7676

7777
```
7878

79+
### New Rust WASM module based on template
80+
81+
```bash
82+
mecha new module -t=pingrs -type=rust pingrs
83+
84+
```
85+
86+
## New Zig WASM module based on template
87+
88+
```bash
89+
mecha new module -t=pingzig -type=zig pingzig
90+
91+
```
92+
7993
### Build modules in current project
8094

8195
```bash

cmd/mecha/create.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"os"
78
"os/exec"
89
"path/filepath"
910
"strings"
1011

12+
getter "github.com/hashicorp/go-getter"
1113
"github.com/urfave/cli/v2"
1214
)
1315

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

66+
tmpltype := cCtx.String("type")
67+
switch {
68+
case tmpltype == "rust":
69+
return downloadFromTemplate(templateName, name)
70+
case tmpltype == "zig":
71+
return downloadFromTemplate(templateName, name)
72+
case tmpltype == "tinygo":
73+
default:
74+
return fmt.Errorf("unknown template type %s", tmpltype)
75+
}
76+
6477
if err := createFromTemplate(templateName, name); err != nil {
6578
return err
6679
}
@@ -85,27 +98,6 @@ func createFromTemplate(templ, proj string) error {
8598
os.Exit(1)
8699
}
87100

88-
// patch the go.mod file to use forked wazero
89-
basename := filepath.Base(proj)
90-
if err := os.Chdir(basename); err != nil {
91-
fmt.Println(err)
92-
os.Exit(1)
93-
}
94-
defer os.Chdir("..")
95-
96-
return nil // TODO: completely remove replaceWazeroWithFork()
97-
}
98-
99-
func replaceWazeroWithFork() error {
100-
var stdout, stderr bytes.Buffer
101-
cmd := exec.Command("go", "mod", "edit", "-replace", "github.com/tetratelabs/wazero=github.com/hybridgroup/wazero@v0.0.0-20240328190114-79d4bea3ca005")
102-
cmd.Stdout = &stdout
103-
cmd.Stderr = &stderr
104-
if err := cmd.Run(); err != nil {
105-
fmt.Printf("%s: %v\n%s%s", cmd.String(), err, stderr.Bytes(), stdout.Bytes())
106-
os.Exit(1)
107-
}
108-
109101
return nil
110102
}
111103

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

123115
return strings.TrimSuffix(stdout.String(), "\n"), nil
124116
}
117+
118+
func downloadFromTemplate(templateName, name string) error {
119+
pwd, err := os.Getwd()
120+
if err != nil {
121+
fmt.Printf("Error getting pwd: %s", err)
122+
return err
123+
}
124+
125+
opts := []getter.ClientOption{}
126+
client := &getter.Client{
127+
Ctx: context.Background(),
128+
Src: templateName,
129+
Dst: filepath.Base(name),
130+
Pwd: pwd,
131+
Mode: getter.ClientModeDir,
132+
Options: opts,
133+
}
134+
135+
if err := client.Get(); err != nil {
136+
fmt.Printf("Error downloading template: %s", err)
137+
return err
138+
}
139+
140+
return nil
141+
}

cmd/mecha/go.mod

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,47 @@ go 1.22.0
44

55
require (
66
github.com/briandowns/spinner v1.23.0
7+
github.com/hashicorp/go-getter v1.7.3
78
github.com/urfave/cli/v2 v2.27.1
89
)
910

1011
require (
12+
cloud.google.com/go v0.104.0 // indirect
13+
cloud.google.com/go/compute v1.10.0 // indirect
14+
cloud.google.com/go/iam v0.5.0 // indirect
15+
cloud.google.com/go/storage v1.27.0 // indirect
16+
github.com/aws/aws-sdk-go v1.44.122 // indirect
17+
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
1118
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
1219
github.com/fatih/color v1.7.0 // indirect
20+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
21+
github.com/golang/protobuf v1.5.2 // indirect
22+
github.com/google/go-cmp v0.5.9 // indirect
23+
github.com/google/uuid v1.3.0 // indirect
24+
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
25+
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
26+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
27+
github.com/hashicorp/go-safetemp v1.0.0 // indirect
28+
github.com/hashicorp/go-version v1.6.0 // indirect
29+
github.com/jmespath/go-jmespath v0.4.0 // indirect
30+
github.com/klauspost/compress v1.15.11 // indirect
1331
github.com/mattn/go-colorable v0.1.2 // indirect
1432
github.com/mattn/go-isatty v0.0.8 // indirect
33+
github.com/mitchellh/go-homedir v1.1.0 // indirect
34+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
1535
github.com/russross/blackfriday/v2 v2.1.0 // indirect
36+
github.com/ulikunitz/xz v0.5.10 // indirect
1637
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
17-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
38+
go.opencensus.io v0.23.0 // indirect
39+
golang.org/x/net v0.1.0 // indirect
40+
golang.org/x/oauth2 v0.1.0 // indirect
41+
golang.org/x/sys v0.1.0 // indirect
1842
golang.org/x/term v0.1.0 // indirect
43+
golang.org/x/text v0.4.0 // indirect
44+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
45+
google.golang.org/api v0.100.0 // indirect
46+
google.golang.org/appengine v1.6.7 // indirect
47+
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
48+
google.golang.org/grpc v1.50.1 // indirect
49+
google.golang.org/protobuf v1.28.1 // indirect
1950
)

0 commit comments

Comments
 (0)