mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-15 15:15:51 +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>
220 lines
6.5 KiB
Go
220 lines
6.5 KiB
Go
package generator
|
|
|
|
import (
|
|
"fmt"
|
|
"go/token"
|
|
"go/types"
|
|
"iter"
|
|
|
|
"github.com/wailsapp/wails/v3/internal/generator/config"
|
|
"golang.org/x/tools/go/packages"
|
|
)
|
|
|
|
// FindServices scans the given packages for invocations
|
|
// of the NewService function from the Wails application package.
|
|
//
|
|
// Whenever one is found and the type of its unique argument
|
|
// is a valid service type, the corresponding named type object
|
|
// is fed into the returned iterator.
|
|
//
|
|
// Results are deduplicated, i.e. the iterator yields any given object at most once.
|
|
func FindServices(pkgs []*packages.Package, systemPaths *config.SystemPaths, logger config.Logger) (iter.Seq[*types.TypeName], types.Object, error) {
|
|
type instanceInfo struct {
|
|
args *types.TypeList
|
|
pos token.Position
|
|
}
|
|
|
|
type target struct {
|
|
obj types.Object
|
|
param int
|
|
}
|
|
|
|
type targetInfo struct {
|
|
target
|
|
cause token.Position
|
|
}
|
|
|
|
// instances maps objects (TypeName or Func) to their instance list.
|
|
instances := make(map[types.Object][]instanceInfo)
|
|
|
|
// owner maps type parameter objects to their parent object (TypeName or Func)
|
|
owner := make(map[*types.TypeName]types.Object)
|
|
|
|
// scheduled holds the set of type parameters
|
|
// that have been already scheduled for analysis,
|
|
// for deduplication.
|
|
scheduled := make(map[target]bool)
|
|
|
|
// registerEvent holds the `application.RegisterEvent` function if found.
|
|
var registerEvent types.Object
|
|
|
|
// next lists type parameter objects that have yet to be analysed.
|
|
var next []targetInfo
|
|
|
|
// Initialise instance/owner maps and detect application.NewService.
|
|
for _, pkg := range pkgs {
|
|
for ident, instance := range pkg.TypesInfo.Instances {
|
|
obj := pkg.TypesInfo.Uses[ident]
|
|
|
|
// Add to instance map.
|
|
objInstances, seen := instances[obj]
|
|
instances[obj] = append(objInstances, instanceInfo{
|
|
instance.TypeArgs,
|
|
pkg.Fset.Position(ident.Pos()),
|
|
})
|
|
|
|
if seen {
|
|
continue
|
|
}
|
|
|
|
// Object seen for the first time:
|
|
// add type params to owner map.
|
|
var tp *types.TypeParamList
|
|
|
|
if t, ok := obj.Type().(interface{ TypeParams() *types.TypeParamList }); ok {
|
|
tp = t.TypeParams()
|
|
} else {
|
|
// Instantiated object has unexpected kind:
|
|
// the spec might have changed.
|
|
logger.Warningf(
|
|
"unexpected instantiation for %s: please report this to Wails maintainers",
|
|
types.ObjectString(obj, nil),
|
|
)
|
|
continue
|
|
}
|
|
|
|
// Add type params to owner map.
|
|
for i := range tp.Len() {
|
|
if param := tp.At(i).Obj(); param != nil {
|
|
owner[param] = obj
|
|
}
|
|
}
|
|
|
|
// If this is a named type, process methods.
|
|
if recv, ok := obj.Type().(*types.Named); ok && recv.NumMethods() > 0 {
|
|
// Register receiver type params.
|
|
for i := range recv.NumMethods() {
|
|
tp := recv.Method(i).Type().(*types.Signature).RecvTypeParams()
|
|
for j := range tp.Len() {
|
|
if param := tp.At(j).Obj(); param != nil {
|
|
owner[param] = obj
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Detect application.RegisterEvent
|
|
if registerEvent == nil && obj.Name() == "RegisterEvent" && obj.Pkg().Path() == systemPaths.ApplicationPackage {
|
|
fn, ok := obj.(*types.Func)
|
|
if !ok {
|
|
return nil, nil, ErrBadApplicationPackage
|
|
}
|
|
|
|
signature := fn.Type().(*types.Signature)
|
|
if signature.Params().Len() != 1 || signature.Results().Len() != 0 || signature.TypeParams().Len() != 1 {
|
|
logger.Warningf("application.RegisterService params: %d, results: %d, typeparams: %d", signature.Params().Len(), signature.Results().Len(), signature.TypeParams().Len())
|
|
return nil, nil, ErrBadApplicationPackage
|
|
}
|
|
|
|
if !types.Identical(signature.Params().At(0).Type(), types.Universe.Lookup("string").Type()) {
|
|
logger.Warningf("application.RegisterService parameter type: %v", signature.Params().At(0).Type())
|
|
return nil, nil, ErrBadApplicationPackage
|
|
}
|
|
|
|
registerEvent = obj
|
|
continue
|
|
}
|
|
|
|
// Detect application.NewService
|
|
if len(next) == 0 && obj.Name() == "NewService" && obj.Pkg().Path() == systemPaths.ApplicationPackage {
|
|
fn, ok := obj.(*types.Func)
|
|
if !ok {
|
|
return nil, nil, ErrBadApplicationPackage
|
|
}
|
|
|
|
signature := fn.Type().(*types.Signature)
|
|
if signature.Params().Len() != 1 || signature.Results().Len() != 1 || tp.Len() != 1 {
|
|
logger.Warningf("application.NewService params: %d, results: %d, typeparams: %d", signature.Params().Len(), signature.Results().Len(), tp.Len())
|
|
return nil, nil, ErrBadApplicationPackage
|
|
}
|
|
|
|
// Schedule unique type param for analysis.
|
|
tgt := target{obj, 0}
|
|
scheduled[tgt] = true
|
|
next = append(next, targetInfo{target: tgt})
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// found tracks service types that have been found so far, for deduplication.
|
|
found := make(map[*types.TypeName]bool)
|
|
|
|
return func(yield func(*types.TypeName) bool) {
|
|
// Process targets.
|
|
for len(next) > 0 {
|
|
// Pop one target off the next list.
|
|
tgt := next[len(next)-1]
|
|
next = next[:len(next)-1]
|
|
|
|
// Prepare indirect binding message.
|
|
indirectMsg := ""
|
|
if tgt.cause.IsValid() {
|
|
indirectMsg = fmt.Sprintf(" (indirectly bound at %s)", tgt.cause)
|
|
}
|
|
|
|
for _, instance := range instances[tgt.obj] {
|
|
// Retrieve type argument.
|
|
serviceType := types.Unalias(instance.args.At(tgt.param))
|
|
|
|
var named *types.Named
|
|
|
|
switch t := serviceType.(type) {
|
|
case *types.Named:
|
|
// Process named type.
|
|
named = t.Origin()
|
|
|
|
case *types.TypeParam:
|
|
// Schedule type parameter for analysis.
|
|
newtgt := target{owner[t.Obj()], t.Index()}
|
|
if !scheduled[newtgt] {
|
|
scheduled[newtgt] = true
|
|
|
|
// Retrieve position of call to application.NewService
|
|
// that caused this target to be scheduled.
|
|
cause := tgt.cause
|
|
if !tgt.cause.IsValid() {
|
|
// This _is_ a call to application.NewService.
|
|
cause = instance.pos
|
|
}
|
|
|
|
// Push on next list.
|
|
next = append(next, targetInfo{newtgt, cause})
|
|
}
|
|
continue
|
|
|
|
default:
|
|
logger.Warningf("%s: ignoring anonymous service type %s%s", instance.pos, serviceType, indirectMsg)
|
|
continue
|
|
}
|
|
|
|
// Reject interfaces and generic types.
|
|
if types.IsInterface(named.Underlying()) {
|
|
logger.Warningf("%s: ignoring interface service type %s%s", instance.pos, named, indirectMsg)
|
|
continue
|
|
} else if named.TypeParams() != nil {
|
|
logger.Warningf("%s: ignoring generic service type %s%s", instance.pos, named, indirectMsg)
|
|
continue
|
|
}
|
|
|
|
// Record and yield type object.
|
|
if !found[named.Obj()] {
|
|
found[named.Obj()] = true
|
|
if !yield(named.Obj()) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, registerEvent, nil
|
|
}
|