gobuild

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2025 License: MIT Imports: 8 Imported by: 2

README

gobuild

Minimal Go/WASM build handler with sync/async compilation support.

Installation

go get github.com/cdvelop/gobuild

Usage

package main

import (
    "os"
    "github.com/cdvelop/gobuild"
)

func main() {
    config := &gobuild.Config{
        Command:      "go",           // or "tinygo"
        MainFilePath: "main.go",
        OutName:      "app",
        Extension:    ".exe",         // ".wasm" for WASM, "" for Unix
        OutFolder:    "dist",
        Log:          os.Stdout,
        // Optional: Timeout, Callback for async, CompilingArguments
    }

    compiler := gobuild.New(config)
    
    if err := compiler.CompileProgram(); err != nil {
        panic(err)
    }
}
Async compilation
config.Callback = func(err error) {
    if err != nil {
        log.Printf("Failed: %v", err)
    }
}
Custom build args
config.CompilingArguments = func() []string {
    return []string{"-race", "-ldflags", "-s -w"}
}

API Reference

Config
type Config struct {
    Command             string          // "go" or "tinygo"
    MainFilePath        string          // Path to main.go
    OutName             string          // Output name (without extension)
    Extension           string          // ".exe", ".wasm", ""
    OutFolder           string          // Output directory
    Log                 io.Writer       // Output writer
    CompilingArguments  func() []string // Build arguments
    Callback            func(error)     // Async callback (optional)
    Timeout             time.Duration   // Default: 5s
}
Methods
  • gobuild.New(config *Config) *GoBuild - Create compiler
  • compiler.CompileProgram() error - Compile (sync if no callback, async if callback set)
  • compiler.UnobservedFiles() []string - Get temp files list

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompileCallback

type CompileCallback func(error)

CompileCallback is called when compilation completes (success or failure)

type Config

type Config struct {
	Command            string          // eg: "go", "tinygo"
	MainFilePath       string          // eg: web/main.server.go, web/main.wasm.go
	OutName            string          // eg: app, user, main.server
	Extension          string          // eg: .exe, .wasm
	CompilingArguments func() []string // eg: []string{"-X 'main.version=v1.0.0'"}
	OutFolder          string          // eg: web, web/public/wasm
	Log                io.Writer       // output for log messages to integrate with other tools (e.g., TUI)
	Callback           CompileCallback // optional callback for async compilation
	Timeout            time.Duration   // max compilation time, defaults to 5 seconds if not set
}

Config holds the configuration for Go compilation

type GoBuild

type GoBuild struct {
	*Config
	Cmd *exec.Cmd
	// contains filtered or unexported fields
}

GoBuild represents a Go compiler instance

func New

func New(c *Config) *GoBuild

New creates a new GoBuild instance with the given configuration

func (*GoBuild) CompileProgram

func (h *GoBuild) CompileProgram() error

CompileProgram compiles the Go program If a callback is configured, it runs asynchronously and returns immediately Otherwise, it runs synchronously and returns the compilation result

func (*GoBuild) UnobservedFiles

func (h *GoBuild) UnobservedFiles() []string

UnobservedFiles returns the list of files that should not be tracked by file watchers eg: main.exe, main_temp.exe

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL