|  | 
|  | 1 | +package _go | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"github.com/fsnotify/fsnotify" | 
|  | 6 | +	"log" | 
|  | 7 | +	"os" | 
|  | 8 | +	"os/exec" | 
|  | 9 | +	"path/filepath" | 
|  | 10 | +) | 
|  | 11 | + | 
|  | 12 | +// isGoImportsAvailable checks if the `goimports` command is available | 
|  | 13 | +func isGoImportsAvailable() bool { | 
|  | 14 | +	_, err := exec.LookPath("goimports") | 
|  | 15 | +	return err == nil | 
|  | 16 | +} | 
|  | 17 | + | 
|  | 18 | +// installGoImports installs the `goimports` tool using `go install` | 
|  | 19 | +func installGoImports() error { | 
|  | 20 | +	cmd := exec.Command("go", "install", "golang.org/x/tools/cmd/goimports@latest") | 
|  | 21 | +	cmd.Stdout = os.Stdout | 
|  | 22 | +	cmd.Stderr = os.Stderr | 
|  | 23 | +	return cmd.Run() | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +func buildProject(appPath, outPath string) error { | 
|  | 27 | +	absAppPath, err := filepath.Abs(appPath) | 
|  | 28 | +	if err != nil { | 
|  | 29 | +		fmt.Println("failed to resolve absolute app path:", err) | 
|  | 30 | +		return err | 
|  | 31 | +	} | 
|  | 32 | + | 
|  | 33 | +	// Step 1: go mod download | 
|  | 34 | +	cmdDownload := exec.Command("go", "mod", "download") | 
|  | 35 | +	cmdDownload.Dir = absAppPath | 
|  | 36 | +	cmdDownload.Stdout = os.Stdout | 
|  | 37 | +	cmdDownload.Stderr = os.Stderr | 
|  | 38 | +	if err := cmdDownload.Run(); err != nil { | 
|  | 39 | +		fmt.Println("failed to download modules:", err) | 
|  | 40 | +		return err | 
|  | 41 | +	} | 
|  | 42 | + | 
|  | 43 | +	// Step 2: GOOS=linux GOARCH=amd64 go build -o "$OUT_PATH" . | 
|  | 44 | +	cmdBuild := exec.Command("go", "build", "-o", outPath, ".") | 
|  | 45 | +	cmdBuild.Dir = absAppPath | 
|  | 46 | +	cmdBuild.Env = append(os.Environ(), | 
|  | 47 | +		"GOOS=linux", | 
|  | 48 | +		"GOARCH=amd64", | 
|  | 49 | +	) | 
|  | 50 | +	cmdBuild.Stdout = os.Stdout | 
|  | 51 | +	cmdBuild.Stderr = os.Stderr | 
|  | 52 | +	if err := cmdBuild.Run(); err != nil { | 
|  | 53 | +		fmt.Println("failed to build binary:", err) | 
|  | 54 | +		return err | 
|  | 55 | +	} | 
|  | 56 | + | 
|  | 57 | +	fmt.Println("build completed successfully:", outPath) | 
|  | 58 | +	return nil | 
|  | 59 | +} | 
|  | 60 | + | 
|  | 61 | +type Generator struct { | 
|  | 62 | +} | 
|  | 63 | + | 
|  | 64 | +func (g *Generator) Init() error { | 
|  | 65 | +	// Check if `goimports` is installed | 
|  | 66 | +	if !isGoImportsAvailable() { | 
|  | 67 | +		log.Println("goimports is not installed. Installing now...") | 
|  | 68 | + | 
|  | 69 | +		err := installGoImports() | 
|  | 70 | +		if err != nil { | 
|  | 71 | +			log.Printf("failed to install goimports: %s\nplease install it manually by running:\n\tgo install golang.org/x/tools/cmd/goimports@latest\n", err.Error()) | 
|  | 72 | +			return err | 
|  | 73 | +		} | 
|  | 74 | + | 
|  | 75 | +		log.Println("goimports successfully installed.") | 
|  | 76 | +	} | 
|  | 77 | + | 
|  | 78 | +	return nil | 
|  | 79 | +} | 
|  | 80 | + | 
|  | 81 | +func (g *Generator) OnChange(appPath string, outputPath string, event fsnotify.Event) error { | 
|  | 82 | +	if IsGoFile(event.Name) { | 
|  | 83 | +		if err := CheckFileCompilable(event.Name); err == nil { | 
|  | 84 | +			log.Printf("change detected in: %s, triggering generate\n", event.Name) | 
|  | 85 | +			return g.Generate(appPath, outputPath) | 
|  | 86 | +		} else { | 
|  | 87 | +			log.Printf("file not compilable: %s, error: %s\n", event.Name, err.Error()) | 
|  | 88 | +			return err | 
|  | 89 | +		} | 
|  | 90 | +	} | 
|  | 91 | + | 
|  | 92 | +	return nil | 
|  | 93 | +} | 
|  | 94 | + | 
|  | 95 | +func (g *Generator) Generate(appPath string, outputPath string) error { | 
|  | 96 | +	err := generateServices(appPath, true) | 
|  | 97 | +	if err != nil { | 
|  | 98 | +		fmt.Printf("error generating services, error: %s\n", err.Error()) | 
|  | 99 | +		return err | 
|  | 100 | +	} | 
|  | 101 | + | 
|  | 102 | +	err = generateRuntime(appPath) | 
|  | 103 | +	if err != nil { | 
|  | 104 | +		fmt.Printf("error generating runtime, error: %s\n", err.Error()) | 
|  | 105 | +		return err | 
|  | 106 | +	} | 
|  | 107 | + | 
|  | 108 | +	err = buildProject(appPath, outputPath) | 
|  | 109 | +	if err != nil { | 
|  | 110 | +		fmt.Printf("error building project, error: %s\n", err.Error()) | 
|  | 111 | +		return err | 
|  | 112 | +	} | 
|  | 113 | + | 
|  | 114 | +	return nil | 
|  | 115 | +} | 
0 commit comments