gobuild

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 9 Imported by: 2

README

gobuild

Project Badges

Thread-safe Go/WASM build handler with sync/async compilation support.

Installation

go get github.com/cdvelop/gobuild

Quick Start

type Config struct {
    Command               string          // "go" or "tinygo"
    MainInputFileRelativePath  string          // relative Path to main.go
    OutName               string          // Output name (without extension)
    Extension             string          // ".exe", ".wasm", ""
    OutFolderRelativePath string          // relative Output directory
    Logger                io.Writer       // Output writer (optional)
    CompilingArguments    func() []string // Build arguments (optional)
    Callback              func(error)     // Async callback (optional)
    Timeout               time.Duration   // Default: 5s
    Env                   []string        // Environment variables (optional)
}
config := &Config{
    Command:                    "go",
    MainInputFileRelativePath:  "server/main.go",
    OutName:                    "app",
    Extension:                  ".exe",
    OutFolderRelativePath:      "dist",
    Logger:                     os.Stdout,
    Timeout:                    5 * time.Second,
    Env:                        []string{"GOOS=js", "GOARCH=wasm"}, // For WASM compilation
}

compiler := gobuild.New(config)
err := compiler.CompileProgram() // Synchronous

Async Compilation

config.Callback = func(err error) {
    if err != nil {
        log.Printf("Failed: %v", err)
    } else {
        log.Printf("Success!")
    }
}
err := compiler.CompileProgram() // Returns immediately

Thread-Safe Control

// Cancel ongoing compilation
compiler.Cancel()

// Check compilation status
if compiler.IsCompiling() {
    fmt.Println("Compilation in progress...")
}

Methods

  • CompileProgram() error - Compile (sync/async based on callback)
  • Cancel() error - Cancel current compilation
  • IsCompiling() bool - Check if compilation is active
  • MainOutputFileNameWithExtension() string - Get output filename with extension (e.g., "main.wasm")

Features

  • Thread-safe: Automatic cancellation of previous compilations
  • Unique temp files: Prevents conflicts during concurrent builds
  • Context-aware: Proper cancellation and timeout handling

Contributing

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"
	MainInputFileRelativePath 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'"}
	OutFolderRelativePath     string               // eg: web, web/public/wasm
	Logger                    func(message ...any) // 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
	Env                       []string             // environment variables, eg: []string{"GOOS=js", "GOARCH=wasm"}
}

Config holds the configuration for Go compilation

type GoBuild

type GoBuild struct {
	// 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) BuildArguments added in v0.0.3

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

BuildArguments returns the build arguments that would be used for compilation This is exposed for testing purposes

func (*GoBuild) Cancel added in v0.0.3

func (h *GoBuild) Cancel() error

Cancel cancels any active compilation

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 Thread-safe: cancels any previous compilation automatically

func (*GoBuild) IsCompiling added in v0.0.3

func (h *GoBuild) IsCompiling() bool

IsCompiling returns true if there's an active compilation

func (*GoBuild) MainInputFileRelativePath added in v0.0.11

func (h *GoBuild) MainInputFileRelativePath() string

MainInputFileRelativePath eg: cmd/main.go

func (*GoBuild) MainOutputFileNameWithExtension added in v0.0.3

func (h *GoBuild) MainOutputFileNameWithExtension() string

MainOutputFileNameWithExtension returns the output filename with extension (e.g., "main.wasm", "app.exe")

func (*GoBuild) RenameOutputFile added in v0.0.3

func (h *GoBuild) RenameOutputFile() error

RenameOutputFile renames the default temporary output file to the final output file This is exposed for testing purposes

func (*GoBuild) RenameOutputFileFrom added in v0.0.3

func (h *GoBuild) RenameOutputFileFrom(tempFileName string) error

RenameOutputFileFrom renames a specific temporary file to the final output file This is exposed for testing purposes

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