Skip to content

Commit aed0d8a

Browse files
committed
feat: port cli to go-cli-builder
also update to go 1.24.3 and point to the new wasm lib path
1 parent dbf5652 commit aed0d8a

File tree

17 files changed

+186
-107
lines changed

17 files changed

+186
-107
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func Build() error {
1414
if err != nil {
1515
return fmt.Errorf("failed to get GOROOT: %w", err)
1616
}
17-
wasmExec := filepath.Join(strings.TrimSpace(string(goroot)), "misc", "wasm", "wasm_exec.js")
17+
wasmExec := filepath.Join(strings.TrimSpace(string(goroot)), "lib", "wasm", "wasm_exec.js")
1818
if err := copyFile(wasmExec, "wasm_exec.js"); err != nil {
1919
return fmt.Errorf("failed to copy wasm_exec.js: %w", err)
2020
}

cmd/rfw/commands/build.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package commands
2+
3+
import (
4+
"github.com/mirkobrombin/go-cli-builder/v1/command"
5+
"github.com/rfwlab/rfw/cmd/rfw/build"
6+
)
7+
8+
// NewBuildCommand returns the build command.
9+
func NewBuildCommand() *command.Command {
10+
cmd := &command.Command{
11+
Name: "build",
12+
Usage: "build",
13+
Description: "Build the current project",
14+
Run: runBuild,
15+
}
16+
return cmd
17+
}
18+
19+
func runBuild(cmd *command.Command, _ *command.RootFlags, _ []string) error {
20+
if err := build.Build(); err != nil {
21+
return err
22+
}
23+
cmd.Logger.Success("Build completed")
24+
return nil
25+
}

cmd/rfw/commands/dev.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package commands
2+
3+
import (
4+
"github.com/mirkobrombin/go-cli-builder/v1/command"
5+
"github.com/rfwlab/rfw/cmd/rfw/server"
6+
)
7+
8+
// NewDevCommand returns the dev command.
9+
func NewDevCommand() *command.Command {
10+
cmd := &command.Command{
11+
Name: "dev",
12+
Usage: "dev [--port <port>] [--host]",
13+
Description: "Start the development server",
14+
Run: runDev,
15+
}
16+
cmd.AddFlag("port", "p", "Port to serve on", "8080", true)
17+
cmd.AddBoolFlag("host", "", "Expose the server to the network", false, false)
18+
return cmd
19+
}
20+
21+
func runDev(cmd *command.Command, _ *command.RootFlags, _ []string) error {
22+
port := cmd.GetFlagString("port")
23+
host := cmd.GetFlagBool("host")
24+
srv := server.NewServer(port, host)
25+
return srv.Start()
26+
}

cmd/rfw/commands/init.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mirkobrombin/go-cli-builder/v1/command"
7+
"github.com/rfwlab/rfw/cmd/rfw/initproj"
8+
)
9+
10+
// NewInitCommand returns the init command.
11+
func NewInitCommand() *command.Command {
12+
cmd := &command.Command{
13+
Name: "init",
14+
Usage: "init <project-name>",
15+
Description: "Initialize a new RFW project",
16+
Run: runInit,
17+
}
18+
return cmd
19+
}
20+
21+
func runInit(cmd *command.Command, _ *command.RootFlags, args []string) error {
22+
if len(args) < 1 {
23+
return fmt.Errorf("please specify a project name")
24+
}
25+
projectName := args[0]
26+
if err := initproj.InitProject(projectName); err != nil {
27+
return err
28+
}
29+
cmd.Logger.Success("Project initialized")
30+
return nil
31+
}
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ func InitProject(projectName string) error {
1717
projectName = strings.Split(projectName, "/")[len(strings.Split(projectName, "/"))-1]
1818

1919
projectPath := projectName
20-
if len(os.Args) > 3 {
21-
projectPath = os.Args[3]
22-
}
2320

2421
if _, err := os.Stat(projectPath); !os.IsNotExist(err) {
2522
return fmt.Errorf("project directory already exists")
@@ -40,6 +37,9 @@ func InitProject(projectName string) error {
4037

4138
relPath := strings.TrimPrefix(path, "./")
4239
targetPath := filepath.Join(projectPath, relPath)
40+
if strings.HasSuffix(targetPath, ".tmpl") {
41+
targetPath = strings.TrimSuffix(targetPath, ".tmpl")
42+
}
4343

4444
if d.IsDir() {
4545
return os.MkdirAll(targetPath, 0755)
@@ -58,10 +58,14 @@ func InitProject(projectName string) error {
5858
return fmt.Errorf("failed to copy template files: %w", err)
5959
}
6060

61-
if err := copyWasmExec(projectName); err != nil {
61+
if err := copyWasmExec(projectPath); err != nil {
6262
return fmt.Errorf("failed to copy wasm_exec.js: %w", err)
6363
}
6464

65+
if err := initGoModule(projectPath, projectName); err != nil {
66+
return fmt.Errorf("failed to initialize go module: %w", err)
67+
}
68+
6569
fmt.Printf("Project '%s' initialized successfully.\n", projectName)
6670
return nil
6771
}
@@ -88,3 +92,15 @@ func copyWasmExec(projectDir string) error {
8892

8993
return nil
9094
}
95+
96+
func initGoModule(projectPath, moduleName string) error {
97+
cmd := exec.Command("go", "mod", "init", moduleName)
98+
cmd.Dir = projectPath
99+
if err := cmd.Run(); err != nil {
100+
return err
101+
}
102+
103+
cmd = exec.Command("go", "mod", "tidy")
104+
cmd.Dir = projectPath
105+
return cmd.Run()
106+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build js && wasm
2+
3+
package components
4+
5+
import (
6+
_ "embed"
7+
8+
"github.com/rfwlab/rfw/v1/core"
9+
)
10+
11+
//go:embed templates/app_component.rtml
12+
var appComponentTpl []byte
13+
14+
type AppComponent struct {
15+
*core.HTMLComponent
16+
}
17+
18+
func NewAppComponent() *AppComponent {
19+
c := &AppComponent{
20+
HTMLComponent: core.NewHTMLComponent("AppComponent", appComponentTpl, nil),
21+
}
22+
c.SetComponent(c)
23+
c.Init(nil)
24+
return c
25+
}
26+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<root>
2+
<div class="p-4">
3+
<h1 class="text-2xl font-bold">Hello from {{packageName}}!</h1>
4+
<p>Edit the component to get started.</p>
5+
</div>
6+
</root>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>{{packageName}}</title>
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<script src="/wasm_exec.js"></script>
10+
<script>
11+
const go = new Go();
12+
WebAssembly.instantiateStreaming(fetch("/main.wasm"), go.importObject).then((result) => {
13+
go.run(result.instance);
14+
});
15+
</script>
16+
</body>
17+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build js && wasm
2+
3+
package main
4+
5+
import (
6+
"github.com/rfwlab/rfw/v1/core"
7+
"github.com/rfwlab/rfw/v1/router"
8+
"{{packageName}}/components"
9+
)
10+
11+
func main() {
12+
core.SetDevMode(true)
13+
router.RegisterRoute(router.Route{
14+
Path: "/",
15+
Component: func() core.Component { return components.NewAppComponent() },
16+
})
17+
router.InitRouter()
18+
select {}
19+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import (
44
"embed"
55
)
66

7-
//go:embed template/*
7+
//go:embed template/**
88
var TemplatesFS embed.FS

0 commit comments

Comments
 (0)