Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/llpyg/llpyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func main() {
// tidy go module
goModTidy(args.OutputDir)

// format go code
codeFormat(args.OutputDir)
Comment thread
luoliwoshang marked this conversation as resolved.
Outdated

fmt.Printf("LLGo bindings generated successfully in %s\n", args.OutputDir)
}

Expand Down
34 changes: 34 additions & 0 deletions cmd/llpyg/llpyg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,37 @@ func assertArgsEqual(t *testing.T, got, want Args) {
t.Errorf("unexpected Kwarg: got %q, want %q", got.Kwarg, want.Kwarg)
}
}


func TestGoModuleUtils(t *testing.T) {
tempDir := t.TempDir()
goFile := filepath.Join(tempDir, "test.go")
goContent := `package main

import (
"fmt"
"github.com/goplus/lib/py"
)

func main() {
a := py.Object{}
fmt.Printf("hello %v", a)
}
`
err := os.WriteFile(goFile, []byte(goContent), 0644)
if err != nil {
t.Fatalf("Failed to create test Go file: %v", err)
}
err = initGoModule("test", tempDir)
if err != nil {
t.Fatal(err)
}
err = goModTidy(tempDir)
if err != nil {
t.Log(err)
}
Comment thread
luoliwoshang marked this conversation as resolved.
err = codeFormat(tempDir)
if err != nil {
t.Log(err)
}
}
35 changes: 21 additions & 14 deletions cmd/llpyg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,43 @@ func writeConfig(cfg Config, outDir string) error {
}

func initGoModule(modName string, outDir string) error {
if err := os.Chdir(outDir); err != nil {
return fmt.Errorf("error: failed to change directory: %w", err)
}
// init go module
cmd := exec.Command("go", "mod", "init", modName)
cmd.Dir = outDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error: failed to initialize Go module: %w", err)
}

getCmd := exec.Command("go", "get", "github.com/goplus/lib/py")
getCmd.Stdout = os.Stdout
getCmd.Stderr = os.Stderr
if err := getCmd.Run(); err != nil {
cmd = exec.Command("go", "get", "github.com/goplus/lib/py")
cmd.Dir = outDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error: failed to get github.com/goplus/lib/py: %w", err)
}
return nil
}

func goModTidy(outDir string) error {
if err := os.Chdir(outDir); err != nil {
return fmt.Errorf("error: failed to change directory: %w", err)
}
tidyCmd := exec.Command("go", "mod", "tidy")
tidyCmd.Stdout = os.Stdout
tidyCmd.Stderr = os.Stderr
if err := tidyCmd.Run(); err != nil {
cmd := exec.Command("go", "mod", "tidy")
cmd.Dir = outDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error: failed to tidy Go module: %w", err)
}
return nil
}
Comment thread
toaction marked this conversation as resolved.

func codeFormat(outDir string) error {
Comment thread
toaction marked this conversation as resolved.
cmd := exec.Command("go", "fmt", "./...")
cmd.Dir = outDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("error: failed to format Go code: %w", err)
}
return nil
}
Comment thread
toaction marked this conversation as resolved.
Loading