Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 16 additions & 6 deletions cmd/llpyg/llpyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,22 @@ func main() {
}

// init work dir
initWorkDir(&args, cfg)
if err := initWorkDir(&args, cfg); err != nil {
log.Fatal(err)
}

// LLGo Bindings generation
generateFromConfig(cfg, args.OutputDir)

// tidy go module
goModTidy(args.OutputDir)
if err := goModTidy(args.OutputDir); err != nil {
log.Fatal(err)
}

// format go code
if err := codeFormat(args.OutputDir); err != nil {
log.Fatal(err)
}

fmt.Printf("LLGo bindings generated successfully in %s\n", args.OutputDir)
}
Expand Down Expand Up @@ -141,23 +150,24 @@ func readConfig(cfgPath string) (cfg Config) {
}

// init work dir, include go module, llpyg.cfg
func initWorkDir(args *Args, cfg Config) {
func initWorkDir(args *Args, cfg Config) error {
args.OutputDir = filepath.Join(args.OutputDir, cfg.Name)
// remove origin output dir
if err := os.RemoveAll(args.OutputDir); err != nil {
log.Fatalf("error: failed to remove output directory %s: %v\n", args.OutputDir, err)
return fmt.Errorf("failed to remove output directory %s: %v", args.OutputDir, err)
}
// write config file
if err := writeConfig(cfg, args.OutputDir); err != nil {
log.Fatalf("error: failed to write config file %s: %v\n", args.OutputDir, err)
return fmt.Errorf("failed to write config file %s: %v", args.OutputDir, err)
}
// init go module
if args.ModName == "" {
args.ModName = cfg.Name
}
if err := initGoModule(args.ModName, args.OutputDir); err != nil {
log.Fatal(err)
return err
}
return nil
Comment thread
luoliwoshang marked this conversation as resolved.
Outdated
}

func generateFromConfig(cfg Config, outDir string) {
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.Fatal(err)
}
Comment thread
luoliwoshang marked this conversation as resolved.
err = codeFormat(tempDir)
if err != nil {
t.Fatal(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