buildengine

package
v0.361.5 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: Apache-2.0 Imports: 47 Imported by: 0

Documentation

Overview

Package buildengine provides a framework for building FTL modules.

Index

Constants

View Source
const BuildLockTimeout = time.Minute

Variables

View Source
var ErrSkip = errors.New("skip directory")

ErrSkip can be returned by the WalkDir callback to skip a file or directory.

Functions

func Build

func Build(ctx context.Context, projectRootDir string, sch *schema.Schema, module Module, filesTransaction ModifyFilesTransaction, buildEnv []string) error

Build a module in the given directory given the schema and module config.

A lock file is used to ensure that only one build is running at a time.

func CleanStubs

func CleanStubs(ctx context.Context, projectRoot string) error

CleanStubs removes all generated stubs.

func ComputeFileHash

func ComputeFileHash(baseDir, srcPath string, watch []string) (hash []byte, matched bool, err error)

func Deploy

func Deploy(ctx context.Context, module Module, replicas int32, waitForDeployOnline bool, client DeployClient) error

Deploy a module to the FTL controller with the given number of replicas. Optionally wait for the deployment to become ready.

func FindFilesToDeploy

func FindFilesToDeploy(moduleConfig moduleconfig.AbsModuleConfig) ([]string, error)

FindFilesToDeploy returns a list of files to deploy for the given module.

func GenerateStubs

func GenerateStubs(ctx context.Context, projectRoot string, modules []*schema.Module, moduleConfigs []moduleconfig.ModuleConfig) error

GenerateStubs generates stubs for the given modules.

Currently, only Go stubs are supported. Kotlin and other language stubs can be added in the future.

func SetPOMProperties

func SetPOMProperties(ctx context.Context, baseDir string) error

SetPOMProperties updates the ftl.version properties in the pom.xml file in the given base directory.

func SyncStubReferences

func SyncStubReferences(ctx context.Context, projectRoot string, moduleNames []string, moduleConfigs []moduleconfig.ModuleConfig) error

SyncStubReferences syncs the references in the generated stubs.

For Go, this means updating all the go.work files to include all known modules in the shared stubbed modules directory.

func TopologicalSort

func TopologicalSort(graph map[string][]string) (groups [][]string, cycleError error)

TopologicalSort attempts to order the modules supplied in the graph based on their topologically sorted order. A cycle in the module dependency graph will cause this sort to be incomplete. The sorted modules are returned as a sequence of `groups` of modules that may be built in parallel. The `unsorted` modules impacted by a dependency cycle get reported as an error.

func WalkDir

func WalkDir(dir string, fn func(path string, d fs.DirEntry) error) error

WalkDir performs a depth-first walk of the file tree rooted at dir, calling fn for each file or directory in the tree, including dir.

It will adhere to .gitignore files. The callback "fn" can return ErrSkip to skip recursion.

Types

type CompilerBuildError

type CompilerBuildError struct {
	// contains filtered or unexported fields
}

func (CompilerBuildError) Error

func (e CompilerBuildError) Error() string

func (CompilerBuildError) Unwrap

func (e CompilerBuildError) Unwrap() error

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine for building a set of modules.

func New

func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, projectRoot string, moduleDirs []string, options ...Option) (*Engine, error)

New constructs a new Engine.

Completely offline builds are possible if the full dependency graph is locally available. If the FTL controller is available, it will be used to pull in missing schemas.

"dirs" are directories to scan for local modules.

func (*Engine) Build

func (e *Engine) Build(ctx context.Context) error

Build attempts to build all local modules.

func (*Engine) BuildAndDeploy

func (e *Engine) BuildAndDeploy(ctx context.Context, replicas int32, waitForDeployOnline bool, moduleNames ...string) error

BuildAndDeploy attempts to build and deploy all local modules.

func (*Engine) Close

func (e *Engine) Close() error

Close stops the Engine's schema sync.

func (*Engine) Deploy

func (e *Engine) Deploy(ctx context.Context, replicas int32, waitForDeployOnline bool) error

Deploy attempts to deploy all (already compiled) local modules.

If waitForDeployOnline is true, this function will block until all deployments are online.

func (*Engine) Dev

func (e *Engine) Dev(ctx context.Context, period time.Duration) error

Dev builds and deploys all local modules and watches for changes, redeploying as necessary.

func (*Engine) Each

func (e *Engine) Each(fn func(Module) error) (err error)

Each iterates over all local modules.

func (*Engine) Graph

func (e *Engine) Graph(moduleNames ...string) (map[string][]string, error)

Graph returns the dependency graph for the given modules.

If no modules are provided, the entire graph is returned. An error is returned if any dependencies are missing.

func (*Engine) Import

func (e *Engine) Import(ctx context.Context, schema *schema.Module)

Import manually imports a schema for a module as if it were retrieved from the FTL controller.

func (*Engine) Modules

func (e *Engine) Modules() []string

Modules returns the names of all modules.

type FileChangeType

type FileChangeType rune
const (
	FileAdded   FileChangeType = '+'
	FileRemoved FileChangeType = '-'
	FileChanged FileChangeType = '*'
)

func CompareFileHashes

func CompareFileHashes(oldFiles, newFiles FileHashes) (FileChangeType, string, bool)

CompareFileHashes compares the hashes of the files in the oldFiles and newFiles maps.

Returns true if the hashes are equal, false otherwise.

If false, the returned string will be a file that caused the difference and the returned FileChangeType will be the type of change that occurred.

func (FileChangeType) GoString

func (f FileChangeType) GoString() string

func (FileChangeType) String

func (f FileChangeType) String() string

type FileHashes

type FileHashes map[string][]byte

func ComputeFileHashes

func ComputeFileHashes(module Module) (FileHashes, error)

ComputeFileHashes computes the SHA256 hash of all (non-git-ignored) files in the given directory.

type Listener

type Listener interface {
	// OnBuildStarted is called when a build is started for a project.
	OnBuildStarted(module Module)

	// OnBuildSuccess is called when all modules have been built successfully and deployed.
	OnBuildSuccess()

	// OnBuildFailed is called for any build failures.
	// OnBuildSuccess should not be called if this is called after a OnBuildStarted.
	OnBuildFailed(err error)
}

type ModifyFilesTransaction

type ModifyFilesTransaction interface {
	Begin() error
	ModifiedFiles(paths ...string) error
	End() error
}

ModifyFilesTransaction allows builds to modify files in a module without triggering a watch event. This helps us avoid infinite loops with builds changing files, and those changes triggering new builds.as a no-op

type Module

type Module struct {
	Config       moduleconfig.ModuleConfig
	Dependencies []string
}

Module represents an FTL module in the build engine

func DiscoverModules

func DiscoverModules(ctx context.Context, moduleDirs []string) ([]Module, error)

DiscoverModules recursively loads all modules under the given directories (or if none provided, the current working directory is used).

func LoadModule

func LoadModule(dir string) (Module, error)

LoadModule loads a module from the given directory.

func UpdateDependencies

func UpdateDependencies(ctx context.Context, module Module) (Module, error)

UpdateDependencies finds the dependencies for a module and returns a Module with those dependencies populated.

func (Module) CopyWithDependencies

func (m Module) CopyWithDependencies(dependencies []string) Module

type Option

type Option func(o *Engine)

func BuildEnv added in v0.361.0

func BuildEnv(env []string) Option

func Parallelism

func Parallelism(n int) Option

func WithListener

func WithListener(listener Listener) Option

WithListener sets the event listener for the Engine.

type WatchEvent

type WatchEvent interface {
	// contains filtered or unexported methods
}

A WatchEvent is an event that occurs when a module is added, removed, or changed.

type WatchEventModuleAdded

type WatchEventModuleAdded struct{ Module Module }

type WatchEventModuleChanged

type WatchEventModuleChanged struct {
	Module Module
	Change FileChangeType
	Path   string
	Time   time.Time
}

type WatchEventModuleRemoved

type WatchEventModuleRemoved struct{ Module Module }

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

func NewWatcher

func NewWatcher() *Watcher

func (*Watcher) GetTransaction

func (w *Watcher) GetTransaction(moduleDir string) ModifyFilesTransaction

func (*Watcher) Watch

func (w *Watcher) Watch(ctx context.Context, period time.Duration, moduleDirs []string) (*pubsub.Topic[WatchEvent], error)

Watch the given directories for new modules, deleted modules, and changes to existing modules, publishing a change event for each.

Jump to

Keyboard shortcuts

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