5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-08 01:11:06 +08:00
wails/v3/internal/generator/collect/imports.go
Fabio Massaioli 90b7ea944d
[v3] New binding generator (#3468)
* Support variadic arguments and slice, pointer types

* Fix computation of type namespaces

* Improve comments and general formatting

* Set default values correctly for composite types

* Add templates for bindings

Additionally:
* fixes generation of tuple return type
* improves imports and namespacing in JS mode
* general cleanup of generated code

* Simplify import list construction

* Refactor type generation code

Improves support for unknown types (encoded as any) and maps (using
Typescript index signatures)

* Support slices with pointer elements

* Match encoding/json behaviour in struct parser

* Update tests and example

* Add tests for complex method signatures and json tag parsing

* Add test `function_multiple_files`

* Attempt looking up idents with missing denotation

* Update test data

* fix quoted bool field

* Test quoted booleans

* Delete old parser code

* Remove old test data

* Update bindgen flags

* Makes call by ID the default

* Add package loading code

* Add static analyser

* Temporarily ignore binding generation code

* Add complex slice expressions test

* Fix variable reference analysis

* Unwrap casts to interface types

* Complete code comments

* Refactor static analyser

* Restrict options struct usage

* Update tests

* Fix method selector sink and source processing

* Improve Set API

* Add package info collector

* Rename analyser package to analyse

* Improve template functions

* Add index file templates

* Add glue code for binding generation

* Refactor collection and rendering code

* Implement binding generator

* Implement global index generation

* Improve marshaler and alias handling

* Use package path in binding calls by name

* Implement model collection and rendering

* Fix wrong exit condition in analyser

* Fix enum rendering

* Generate shortcuts for all packages.

* Implement generator tests

* Ignore non-pointer bound types

* Treat main package specially

* Compute stats

* Plug new API into generate command

* Support all named types

* Update JS runtime

* Report dual role types

* Remove go1.22 syntax

* Fix type assertion in TS bindings

* encoding/json compliance for arrays and slices

* Ignore got files in testdata

* Cleanup type rendering mechanism

* Update JS runtime

* Implement generic models

* Add missing field in renderer initialisation

* Improve generic creation code

* Add generic model test

* Add error reporting infrastructure

* Support configurable file names

* Detect file naming collisions

* Print final error report

* New shortcut file structure + collision detection

* Update test layout and data

* Autoconfiguration for analyser tests

* Live progress reporting

* Update code comments

* Fix model doc rendering

* Simplify name resolution

* Add test for out of tree types

* Fix generic creation code

* Fix potential collisions between methods and models

* Fix generic class alias rendering

* Report model discovery in debug mode

* Add interface mode for JS

* Collect interface method comments

* Add interface methods test

* Unwrap generic instantiations in method receivers

* Fix rendering of nullable types in interface mode

* Fix rendering of class aliases

* Expose promise cancel method to typescript

* Update test data

* Update binding example

* Fix rendering of aliased quoted type params

* Move to strongly typed bindings

* Implement lightweight analyser

* Update test cases

* Update binding example

* Add complex instantiation test

* Load full dependency tree

* Rewrite collector

* Update renderer to match new collector

* Update generator to match new collector

* Update test data

* Update binding example

* Configure includes and injections by language

* Improve system path resolution

* Support rich conditions in inject/include directives

* Fix error handling in Generator.Generate

* Retrieve compiled go file paths from fileset

* Do not rely on struct info in struct flattening algorithm

* Fix doc comment for findDeclaraion

* Fix bugs in embedded field handling

* Fix bugs and comments in package collection

* Remove useless fields from ServiceInfo

* Fix empty line at the beginning of TS indexes

* Remove global index and shortcuts

* Remove generation tests for individual packages

* Enforce lower-case file names

* Update test data

* Improve error reporting

* Update binding example

* Reintroduce go1.22 syntax

* Improve relative import path computation

* Improve alias support

* Add alias test

* Update test data

* Remove no services error

* Rename global analyser test

* Add workaround and test for bug in typeutil.Map

* Update test data

* Do not split fully qualified names

* Update typeutil package and remove workaround

* Unify alias/named type handling

* Fix rendering of generic named class aliases

* Fix rendering of array types

* Minor tweaks and cleanups

* Rmove namespaced export construct

* Update test data

* Update binding example

* Break type cycles

* Fix typo in comment

* Fix creation code for cyclic types

* Fix type of variadic params in interface mode

* Update test data

* Fix bad whitespace

* Refactor type assertions inside bound methods

* Update test data

* Rename field application.Options.Bind to Services

* Rename parser package to generator

* Update binding example

* Update test data

* Update generator readme

* Add typescript test harness

* Move test output to new subfolder

* Fix code generation bugs

* Use .js extensions in TS mode imports

* Update test data

* Revert default generator output dir to frontend/bindings

* Bump runtime package version

* Update templates

* Update changelog

* Improve newline handling

---------

Co-authored-by: Andreas Bichinger <andreas.bichinger@gmail.com>
2024-05-19 20:40:44 +10:00

280 lines
7.0 KiB
Go

package collect
import (
"go/types"
"path/filepath"
)
type (
// ImportMap records deduplicated imports by a binding or models module.
// It computes relative import paths and assigns import names,
// taking care to avoid collisions.
ImportMap struct {
// Self records the path of the importing package.
Self string
// ImportModels records whether models from the current package may be needed.
ImportModels bool
// ImportInternal records whether internal models from the current package may be needed.
ImportInternal bool
// External records information about each imported package,
// keyed by package path.
External map[string]ImportInfo
// counters holds the occurence count for each package name in External.
counters map[string]int
collector *Collector
}
// ImportInfo records information about a single import.
ImportInfo struct {
Name string
Index int // Progressive number for identically named imports, starting from 0 for each distinct name.
RelPath string
}
)
// NewImportMap initialises an import map for the given importer package.
// The argument may be nil, in which case import paths will be relative
// to the root output directory.
func NewImportMap(importer *PackageInfo) *ImportMap {
var (
self string
collector *Collector
)
if importer != nil {
self = importer.Path
collector = importer.collector
}
return &ImportMap{
Self: self,
External: make(map[string]ImportInfo),
counters: make(map[string]int),
collector: collector,
}
}
// Merge merges the given import map into the receiver.
// The importing package must be the same.
func (imports *ImportMap) Merge(other *ImportMap) {
if other.Self != imports.Self {
panic("cannot merge import maps with different importing package")
}
if other.ImportModels {
imports.ImportModels = true
}
if other.ImportInternal {
imports.ImportInternal = true
}
for path, info := range other.External {
if _, ok := imports.External[path]; ok {
continue
}
counter := imports.counters[info.Name]
imports.counters[info.Name] = counter + 1
imports.External[path] = ImportInfo{
Name: info.Name,
Index: counter,
RelPath: info.RelPath,
}
}
}
// Add adds the given package to the import map if not already present,
// choosing import names so as to avoid collisions.
//
// Add does not support unsynchronised concurrent calls
// on the same receiver.
func (imports *ImportMap) Add(pkg *PackageInfo) {
if pkg.Path == imports.Self {
// Do not import self.
return
}
if imports.External[pkg.Path].Name != "" {
// Package already imported.
return
}
// Fetch and update counter for name.
counter := imports.counters[pkg.Name]
imports.counters[pkg.Name] = counter + 1
// Always add counters to
imports.External[pkg.Path] = ImportInfo{
Name: pkg.Name,
Index: counter,
RelPath: computeImportPath(imports.Self, pkg.Path),
}
}
// AddType adds all dependencies of the given type to the import map
// and marks all referenced named types as models.
//
// It is a runtime error to call AddType on an ImportMap
// created with nil importing package.
//
// AddType does not support unsynchronised concurrent calls
// on the same receiver.
func (imports *ImportMap) AddType(typ types.Type) {
imports.addTypeImpl(typ, make(map[*types.TypeName]bool))
}
// addTypeImpl provides the actual implementation of AddType.
// The visited parameter is used to break cycles.
func (imports *ImportMap) addTypeImpl(typ types.Type, visited map[*types.TypeName]bool) {
collector := imports.collector
if collector == nil {
panic("AddType called on ImportMap with nil importing package")
}
for { // Avoid recursion where possible.
switch t := typ.(type) {
case *types.Alias, *types.Named:
obj := typ.(interface{ Obj() *types.TypeName }).Obj()
if visited[obj] {
return
}
visited[obj] = true
if obj.Pkg() == nil {
// Ignore universe type.
return
}
if obj.Pkg().Path() == imports.Self {
// Record self import.
if obj.Exported() {
imports.ImportModels = true
} else {
imports.ImportInternal = true
}
}
// Record model.
imports.collector.Model(obj)
// Import parent package.
imports.Add(collector.Package(obj.Pkg()))
instance, _ := t.(interface{ TypeArgs() *types.TypeList })
if instance != nil {
// Record type argument dependencies.
if targs := instance.TypeArgs(); targs != nil {
for i := range targs.Len() {
imports.addTypeImpl(targs.At(i), visited)
}
}
}
if collector.options.UseInterfaces {
// No creation/initialisation code required.
return
}
if _, isAlias := t.(*types.Alias); isAlias {
// Aliased type might be needed during
// JS value creation and initialisation.
typ = types.Unalias(typ)
break
}
if IsClass(typ) || IsString(typ) || IsAny(typ) {
return
}
// If named type does not map to a class, string or unknown type,
// its underlying type may be needed during JS value creation.
typ = typ.Underlying()
case *types.Basic:
if t.Info()&types.IsComplex != 0 {
// Complex types are not supported by encoding/json
collector.logger.Warningf("complex types are not supported by encoding/json")
}
return
case *types.Array, *types.Pointer, *types.Slice:
typ = typ.(interface{ Elem() types.Type }).Elem()
case *types.Chan:
collector.logger.Warningf("channel types are not supported by encoding/json")
return
case *types.Map:
if IsMapKey(t.Key()) {
if IsString(t.Key()) {
// This model type is always rendered as a string alias,
// hence we can generate it and use it as a type for JS object keys.
imports.addTypeImpl(t.Key(), visited)
}
} else {
collector.logger.Warningf(
"%s is used as a map key, but does not implement encoding.TextMarshaler: this will likely result in runtime errors",
types.TypeString(t.Key(), nil),
)
}
typ = t.Elem()
case *types.Signature:
collector.logger.Warningf("function types are not supported by encoding/json")
return
case *types.Struct:
if t.NumFields() == 0 {
// Empty struct.
return
}
// Retrieve struct info and ensure it is complete.
info := collector.Struct(t).Collect()
if len(info.Fields) == 0 {
// No visible fields.
return
}
// Add field dependencies.
for i := range len(info.Fields) - 1 {
imports.addTypeImpl(info.Fields[i].Type, visited)
}
// Process last field without recursion.
typ = info.Fields[len(info.Fields)-1].Type
case *types.Interface, *types.TypeParam:
// No dependencies.
return
default:
collector.logger.Warningf("unknown type %s: please report this to Wails maintainers", typ)
return
}
}
}
// computeImportPath returns the shortest relative import path
// through which the importer package can reference the imported one.
func computeImportPath(importer string, imported string) string {
rel, err := filepath.Rel(importer, imported)
if err != nil {
panic(err)
}
rel = filepath.ToSlash(rel)
if rel[0] == '.' {
return rel
} else {
return "./" + rel
}
}