mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-15 23:25:49 +01:00
* Add strong event typings * Make `EmitEvent` take one data argument only * Add event registration logic * Report event cancellation to the emitter * Prevent registration of system events * Add support for typed event data initialisation * Binding generation for events * Tests for event bindings * Add vite plugin for typed events * Fix dev command execution order Co-authored-by: Fabio Massaioli <fabio.massaioli@gmail.com> * Propagate module path to templates * Update templates Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com> * Go mod tidy for examples * Switch to tsconfig.json for jetbrains IDE support * Replace jsconfig in example * Convert vite plugin to typescript * Downgrade vite for now The templates all use 5.x * Remove root plugins dir from npm files It's now '/dist/plugins' * Include types for Create But keep out of the docs * Assign a type for cancelAll results * Restore variadic argument in EmitEvent methods * Support registered events with void data * Test cases for void alias support * Support strict mode * Support custom event hooks * Update docs * Update changelog * Testdata for typed events * Test data for void alias support * fix webview_window emit event * Update changelog.mdx * Update events * Fix generator test path normalization for cross-platform compatibility The generator tests were failing on CI because they compared absolute file paths in warning messages. These paths differ between development machines and CI environments. Changes: - Normalize file paths in warnings to be relative to testcases/ directory - Handle both Unix and Windows path separators - Use Unix line endings consistently in test output - Update all test expectation files to use normalized paths This ensures tests pass consistently across different environments including Windows, macOS, Linux, and CI systems. * Remove stale comment * Handle errors returned from validation * Restore variadic argument to Emit (fix bad rebase) * Event emitters return a boolean * Don't use `EmitEvent` in docs Supposedly it's for internal use, according to comment * Fix event docs (from rebase) * Ensure all templates specify @wailsio/runtime: "latest" * Fix Windows test failure due to CRLF line endings The test was failing on Windows because: 1. Hardcoded "\n" was being used instead of render.Newline when writing warning logs, causing CRLF vs LF mismatch 2. The render package import was missing 3. .got.log files weren't being skipped when building expected file list Changes: - Add render package import - Use render.Newline instead of hardcoded "\n" for cross-platform compatibility - Skip .got.log files in test file walker 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix template tests by using local runtime package The template tests were failing because they were installing @wailsio/runtime@latest from npm, which doesn't have the new vite plugin yet. This change packs the local runtime and uses it in template tests instead. Changes: - Pack the runtime to a tarball in test_js job - Upload the runtime package as an artifact - Download and install the local runtime in template tests before building - Update cleanup job to delete the runtime package artifact * Apply suggestion from @leaanthony * Fix: Install local runtime in frontend directory with correct path The previous fix wasn't working because: 1. npm install was run in the project root, not in frontend/ 2. wails3 build runs npm install again, which would reinstall from npm Fixed by: - Using npm pkg set to modify package.json to use file:// protocol - This ensures subsequent npm install calls use the local tarball * Fix Vue template syntax conflicts with Go template delimiters The Vue templates were converted to .tmpl files to support dynamic module paths, but Vue's template syntax {{ }} conflicts with Go's template syntax. Fixed by escaping Vue template braces: - {{ becomes {{"{{"}} - }} becomes {{"}}"}} This allows the Go template engine to output the literal {{ }} for Vue to process. * Fix Vue template escaping and Windows shell compatibility Two issues fixed: 1. Vue template escaping: Changed from {{"{{"}} to {{ "{{" }} - The previous syntax caused "missing value for command" error - Correct Go template syntax uses spaces between delimiters and strings 2. Windows PowerShell compatibility: Added 'shell: bash' to template generation step - The bash syntax (ls, head, $()) doesn't work in PowerShell - Git Bash is available on all GitHub runners including Windows * Fix: test_templates depends on test_js for runtime package artifact The runtime-package artifact is created in test_js job, not test_go. Added test_js to the needs array so the artifact is available for download. * Fix Windows path compatibility for runtime package artifact Changed from absolute Unix path '/tmp/wails-runtime' to relative path 'wails-runtime-temp' which works cross-platform. Using realpath to convert to absolute path for file:// URL in npm pkg set command. * Fix realpath issue on Windows for runtime package realpath on Windows Git Bash was producing malformed paths with duplicate drive letters (D:\d\a\...). Replaced with portable solution using pwd that works correctly across all platforms. * Use pwd -W on Windows to get native Windows paths Git Bash's pwd returns Unix-style paths (/d/a/wails/wails) which npm then incorrectly resolves as D:/d/a/wails/wails. Using pwd -W returns native Windows paths (D:\a\wails\wails) that npm can handle correctly. This is the root cause of all the Windows path issues. * Improve typechecking for Events.Emit() * [docs] Clarify where `Events` is imported from in each example * Add docs for runtime Events.Emit() * Revert to v2-style Events.Emit (name, data) * Update changelog --------- Co-authored-by: Fabio Massaioli <fabio.massaioli@gmail.com> Co-authored-by: Atterpac <Capretta.Michael@gmail.com> Co-authored-by: Lea Anthony <lea.anthony@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
280 lines
7.9 KiB
Go
280 lines
7.9 KiB
Go
package generator
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v3/internal/flags"
|
|
"github.com/wailsapp/wails/v3/internal/generator/collect"
|
|
"github.com/wailsapp/wails/v3/internal/generator/config"
|
|
"github.com/wailsapp/wails/v3/internal/generator/render"
|
|
)
|
|
|
|
// Generator wraps all bookkeeping data structures that are needed
|
|
// to generate bindings for a set of packages.
|
|
type Generator struct {
|
|
options *flags.GenerateBindingsOptions
|
|
creator config.FileCreator
|
|
|
|
// serviceFiles maps service file paths to their type object.
|
|
// It is used for lower/upper-case collision detection.
|
|
// Keys are strings, values are *types.TypeName.
|
|
serviceFiles sync.Map
|
|
|
|
collector *collect.Collector
|
|
renderer *render.Renderer
|
|
|
|
logger *ErrorReport
|
|
scheduler scheduler
|
|
}
|
|
|
|
// NewGenerator configures a new generator instance.
|
|
// The options argument must not be nil.
|
|
// If creator is nil, no output file will be created.
|
|
// If logger is not nil, it is used to report messages interactively.
|
|
func NewGenerator(options *flags.GenerateBindingsOptions, creator config.FileCreator, logger config.Logger) *Generator {
|
|
if creator == nil {
|
|
creator = config.NullCreator
|
|
}
|
|
|
|
report := NewErrorReport(logger)
|
|
|
|
return &Generator{
|
|
options: options,
|
|
creator: config.FileCreatorFunc(func(path string) (io.WriteCloser, error) {
|
|
report.Debugf("writing output file %s", path)
|
|
return creator.Create(path)
|
|
}),
|
|
|
|
logger: report,
|
|
}
|
|
}
|
|
|
|
// Generate runs the binding generation process
|
|
// for the packages specified by the given patterns.
|
|
//
|
|
// Concurrent or repeated calls to Generate with the same receiver
|
|
// are not allowed.
|
|
//
|
|
// The stats result field is never nil.
|
|
//
|
|
// The error result field is nil in case of complete success (no warning).
|
|
// Otherwise, it may either report errors that occured while loading
|
|
// the initial set of packages, or errors returned by the static analyser,
|
|
// or be an [ErrorReport] instance.
|
|
//
|
|
// If error is an ErrorReport, it may have accumulated no errors, just warnings.
|
|
// When this is the case, all bindings have been generated successfully.
|
|
//
|
|
// Parsing/type-checking errors or errors encountered while writing
|
|
// individual files will be printed directly to the [config.Logger] instance
|
|
// provided during initialisation.
|
|
func (generator *Generator) Generate(patterns ...string) (stats *collect.Stats, err error) {
|
|
stats = &collect.Stats{}
|
|
stats.Start()
|
|
defer stats.Stop()
|
|
|
|
// Validate file names.
|
|
err = generator.validateFileNames()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Parse build flags.
|
|
buildFlags, err := generator.options.BuildFlags()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Start package loading feedback.
|
|
var lpkgMutex sync.Mutex
|
|
generator.logger.Statusf("Loading packages...")
|
|
go func() {
|
|
time.Sleep(5 * time.Second)
|
|
if lpkgMutex.TryLock() {
|
|
generator.logger.Statusf("Loading packages... (this may take a long time)")
|
|
lpkgMutex.Unlock()
|
|
}
|
|
}()
|
|
|
|
systemPaths, err := ResolveSystemPaths(buildFlags)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Load initial packages.
|
|
pkgs, err := LoadPackages(buildFlags, patterns...)
|
|
|
|
// Suppress package loading feedback.
|
|
lpkgMutex.Lock()
|
|
|
|
// Check for loading errors.
|
|
if err != nil {
|
|
return
|
|
}
|
|
if len(patterns) > 0 && len(pkgs) == 0 {
|
|
err = ErrNoPackages
|
|
return
|
|
}
|
|
|
|
// Report parsing/type-checking errors.
|
|
for _, pkg := range pkgs {
|
|
for _, err := range pkg.Errors {
|
|
generator.logger.Warningf("%v", err)
|
|
}
|
|
}
|
|
|
|
// Panic on repeated calls.
|
|
if generator.collector != nil {
|
|
panic("Generate() must not be called more than once on the same receiver")
|
|
}
|
|
|
|
// Update status.
|
|
if generator.options.NoEvents {
|
|
generator.logger.Statusf("Looking for services...")
|
|
} else {
|
|
generator.logger.Statusf("Looking for services and events...")
|
|
}
|
|
serviceOrEventFound := sync.OnceFunc(func() { generator.logger.Statusf("Generating bindings...") })
|
|
|
|
// Run static analysis.
|
|
services, registerEvent, err := FindServices(pkgs, systemPaths, generator.logger)
|
|
|
|
// Initialise subcomponents.
|
|
generator.collector = collect.NewCollector(pkgs, registerEvent, systemPaths, generator.options, &generator.scheduler, generator.logger)
|
|
generator.renderer = render.NewRenderer(generator.collector, generator.options)
|
|
|
|
// Check for analyser errors.
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Discard unneeded data.
|
|
pkgs = nil
|
|
|
|
// Schedule collection and code generation for event data types.
|
|
if !generator.options.NoEvents {
|
|
generator.scheduler.Schedule(func() {
|
|
events := generator.collector.EventMap().Collect()
|
|
if len(events.Defs) > 0 {
|
|
serviceOrEventFound()
|
|
// Not a data race because we wait for this scheduled task
|
|
// to complete before accessing stats again.
|
|
stats.Add(events.Stats())
|
|
}
|
|
generator.generateEvents(events)
|
|
})
|
|
}
|
|
|
|
// Schedule code generation for each found service.
|
|
for obj := range services {
|
|
serviceOrEventFound()
|
|
generator.scheduler.Schedule(func() {
|
|
generator.generateService(obj)
|
|
})
|
|
}
|
|
|
|
// Wait until all services have been generated and all models collected.
|
|
generator.scheduler.Wait()
|
|
|
|
// Invariants:
|
|
// - Service files have been generated for all discovered services;
|
|
// - ModelInfo.Collect has been called on all discovered models, and therefore
|
|
// - all required models have been discovered.
|
|
|
|
// Update status.
|
|
if generator.options.NoIndex {
|
|
generator.logger.Statusf("Generating models...")
|
|
} else {
|
|
generator.logger.Statusf("Generating models and index files...")
|
|
}
|
|
|
|
// Schedule models, index and included files generation for each package.
|
|
for pkg := range generator.collector.Iterate {
|
|
generator.scheduler.Schedule(func() {
|
|
generator.generateModelsIndexIncludes(pkg)
|
|
})
|
|
}
|
|
|
|
// Wait until all models and indices have been generated.
|
|
generator.scheduler.Wait()
|
|
|
|
// Populate stats.
|
|
generator.logger.Statusf("Collecting stats...")
|
|
for info := range generator.collector.Iterate {
|
|
stats.Add(info.Stats())
|
|
}
|
|
|
|
// Return non-empty error report.
|
|
if generator.logger.HasErrors() || generator.logger.HasWarnings() {
|
|
err = generator.logger
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// generateModelsIndexIncludes schedules generation of public/private model files,
|
|
// included files and, if allowed by the options,
|
|
// of an index file for the given package.
|
|
func (generator *Generator) generateModelsIndexIncludes(pkg *collect.PackageInfo) {
|
|
index := pkg.Index(generator.options.TS)
|
|
|
|
// info.Index implies info.Collect: goroutines spawned below
|
|
// can access package information freely.
|
|
|
|
if len(index.Models) > 0 {
|
|
generator.scheduler.Schedule(func() {
|
|
generator.generateModels(pkg, index.Models)
|
|
})
|
|
}
|
|
|
|
if len(index.Package.Includes) > 0 {
|
|
generator.scheduler.Schedule(func() {
|
|
generator.generateIncludes(index)
|
|
})
|
|
}
|
|
|
|
if !generator.options.NoIndex && !index.IsEmpty() {
|
|
generator.generateIndex(index)
|
|
}
|
|
}
|
|
|
|
// validateFileNames validates user-provided filenames.
|
|
func (generator *Generator) validateFileNames() error {
|
|
switch {
|
|
case generator.options.ModelsFilename == "":
|
|
return fmt.Errorf("models filename must not be empty")
|
|
|
|
case !generator.options.NoIndex && generator.options.IndexFilename == "":
|
|
return fmt.Errorf("package index filename must not be empty")
|
|
|
|
case generator.options.ModelsFilename != strings.ToLower(generator.options.ModelsFilename):
|
|
return fmt.Errorf("models filename must not contain uppercase characters")
|
|
|
|
case generator.options.IndexFilename != strings.ToLower(generator.options.IndexFilename):
|
|
return fmt.Errorf("package index filename must not contain uppercase characters")
|
|
|
|
case !generator.options.NoIndex && generator.options.ModelsFilename == generator.options.IndexFilename:
|
|
return fmt.Errorf("models and package indexes cannot share the same filename")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// scheduler provides an implementation of the [collect.Scheduler] interface.
|
|
type scheduler struct {
|
|
sync.WaitGroup
|
|
}
|
|
|
|
// Schedule runs the given function concurrently,
|
|
// registering it on the scheduler's wait group.
|
|
func (sched *scheduler) Schedule(task func()) {
|
|
sched.Add(1)
|
|
go func() {
|
|
defer sched.Done()
|
|
task()
|
|
}()
|
|
}
|