Compare commits

..

1 Commits

Author SHA1 Message Date
Adam Tenderholt 536ae4ca68 Feature/v3 parser: expand TS model generation tests & some fixes (#2485)
* v3 parser: add tests for model generation

* v3 parser: use single quotes for got model.ts

* v3 parser: fixes for some failing tests

* v3 parser: misc simplification and cleanup

* v3 parser: fix model tests when no structs returned

* v3 parser: fix last failing test case

* Update contributors list

* v3 parser: update README

* Revert "Update contributors list"

This reverts commit f429d2ba890020d7ff0772e6207830508261b489.

* Changelog: add line about my contribution
2023-03-23 07:15:14 +11:00
354 changed files with 3846 additions and 6701 deletions
-1
View File
@@ -34,4 +34,3 @@ v2/cmd/wails/internal/commands/initialise/templates/testtemplates/
/v3/.task
/v3/examples/build/bin/testapp
/websitev3/site/
-1
View File
@@ -97,7 +97,6 @@ This project is supported by these kind people / companies:
<p align="center">
<img src="https://wails.io/img/sponsor/jetbrains-grayscale.webp" style="width: 100px"/>
</p>
## FAQ
- Is this an alternative to Electron?
+1 -1
View File
@@ -2,4 +2,4 @@
cd ../website
npx @crowdin/cli@latest pull -b master --export-only-approved
npx @crowdin/cli@latest pull -b master
+1 -1
View File
@@ -1 +1 @@
v2.4.1
v2.4.0
@@ -4,7 +4,6 @@
package edge
import (
"fmt"
"log"
"runtime"
"syscall"
@@ -250,7 +249,7 @@ func (e *ICoreWebView2Environment) CreateWebResourceResponse(content []byte, sta
return nil, err
}
var response *ICoreWebView2WebResourceResponse
hr, _, err := e.vtbl.CreateWebResourceResponse.Call(
_, _, err = e.vtbl.CreateWebResourceResponse.Call(
uintptr(unsafe.Pointer(e)),
stream,
uintptr(statusCode),
@@ -258,14 +257,7 @@ func (e *ICoreWebView2Environment) CreateWebResourceResponse(content []byte, sta
uintptr(unsafe.Pointer(_headers)),
uintptr(unsafe.Pointer(&response)),
)
if windows.Handle(hr) != windows.S_OK {
return nil, syscall.Errno(hr)
}
if response == nil {
if err == nil {
err = fmt.Errorf("unknown error")
}
if err != windows.ERROR_SUCCESS {
return nil, err
}
return response, nil
+4 -5
View File
@@ -62,13 +62,9 @@ func (d *DevWebServer) Run(ctx context.Context) error {
return err
}
var myLogger assetserver.Logger
if _logger := ctx.Value("logger"); _logger != nil {
myLogger = _logger.(*logger.Logger)
}
var assetHandler http.Handler
var wsHandler http.Handler
var myLogger *logger.Logger
_fronendDevServerURL, _ := ctx.Value("frontenddevserverurl").(string)
if _fronendDevServerURL == "" {
@@ -77,6 +73,9 @@ func (d *DevWebServer) Run(ctx context.Context) error {
return c.String(http.StatusOK, assetdir)
})
if _logger := ctx.Value("logger"); _logger != nil {
myLogger = _logger.(*logger.Logger)
}
var err error
assetHandler, err = assetserver.NewAssetHandler(assetServerConfig, myLogger)
if err != nil {
+5 -4
View File
@@ -66,7 +66,7 @@ window.wails = {
defaultCursor: null,
borderThickness: 6,
shouldDrag: false,
deferDragToMouseMove: true,
deferDragToMouseMove: false,
cssDragProperty: "--wails-draggable",
cssDragValue: "drag",
}
@@ -130,7 +130,7 @@ window.addEventListener('mousedown', (e) => {
}
}
if (window.wails.flags.deferDragToMouseMove) {
window.wails.flags.shouldDrag = true;
window.wails.flags.shouldDrag = true;
} else {
e.preventDefault()
window.WailsInvoke("drag");
@@ -152,9 +152,10 @@ function setResize(cursor) {
window.addEventListener('mousemove', function (e) {
if (window.wails.flags.shouldDrag) {
window.wails.flags.shouldDrag = false;
let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
if (mousePressed > 0) {
if(mousePressed <= 0) {
window.wails.flags.shouldDrag = false;
} else {
window.WailsInvoke("drag");
return;
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -5,6 +5,7 @@ go 1.20
require github.com/wailsapp/wails/v3 v3.0.0-alpha.0
require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leaanthony/slicer v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
@@ -13,9 +14,8 @@ require (
github.com/wailsapp/mimetype v1.4.1 // indirect
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
golang.org/x/exp v0.0.0-20220930202632-ec3f01382ef9 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
)
replace github.com/wailsapp/wails/v3 => ../..
replace github.com/wailsapp/wails/v2 => ../../../v2
replace github.com/wailsapp/wails/v2 => ../../../v2
+9 -2
View File
@@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY=
@@ -20,14 +22,19 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 h1:Wn+nhnS+VytzE0PegUzSh4T3hXJCtggKGD/4U5H9+wQ=
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6/go.mod h1:zlNLI0E2c2qA6miiuAHtp0Bac8FaGH0tlhA19OssR/8=
golang.org/x/exp v0.0.0-20220930202632-ec3f01382ef9 h1:RjggHMcaTVp0LOVZcW0bo8alwHrOaCrGUDgfWUHhnN4=
golang.org/x/exp v0.0.0-20220930202632-ec3f01382ef9/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+1 -1
View File
@@ -16,7 +16,7 @@
<key>CFBundleShortVersionString</key>
<string>{{.Info.ProductVersion}}</string>
<key>CFBundleIconFile</key>
<string>icons</string>
<string>iconfile</string>
<key>LSMinimumSystemVersion</key>
<string>10.13.0</string>
<key>NSHighResolutionCapable</key>
+2 -2
View File
@@ -4,7 +4,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleName</key>
<string>My Productname</string>
<string>{{.Info.ProductName}}</string>
<key>CFBundleExecutable</key>
<string>{{.Name}}</string>
<key>CFBundleIdentifier</key>
@@ -16,7 +16,7 @@
<key>CFBundleShortVersionString</key>
<string>{{.Info.ProductVersion}}</string>
<key>CFBundleIconFile</key>
<string>icons</string>
<string>iconfile</string>
<key>LSMinimumSystemVersion</key>
<string>10.13.0</string>
<key>NSHighResolutionCapable</key>
+1
View File
@@ -2,6 +2,7 @@ package commands
import (
"fmt"
"github.com/wailsapp/wails/v3/internal/flags"
"github.com/wailsapp/wails/v3/internal/templates"
-56
View File
@@ -1,56 +0,0 @@
package debug
import (
"github.com/samber/lo"
"path/filepath"
"runtime"
)
import "runtime/debug"
// Why go doesn't provide this as a map already is beyond me.
var buildSettings = map[string]string{}
var LocalModulePath = ""
func init() {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return
}
buildSettings = lo.Associate(buildInfo.Settings, func(setting debug.BuildSetting) (string, string) {
return setting.Key, setting.Value
})
if isLocalBuild() {
modulePath := RelativePath("..", "..", "..")
LocalModulePath, _ = filepath.Abs(modulePath)
}
}
func isLocalBuild() bool {
return buildSettings["vcs.modified"] == "true"
}
// RelativePath returns a qualified path created by joining the
// directory of the calling file and the given relative path.
//
// Example: RelativePath("..") in *this* file would give you '/path/to/wails2/v2/internal`
func RelativePath(relativepath string, optionalpaths ...string) string {
_, thisFile, _, _ := runtime.Caller(1)
localDir := filepath.Dir(thisFile)
// If we have optional paths, join them to the relativepath
if len(optionalpaths) > 0 {
paths := []string{relativepath}
paths = append(paths, optionalpaths...)
relativepath = filepath.Join(paths...)
}
result, err := filepath.Abs(filepath.Join(localDir, relativepath))
if err != nil {
// I'm allowing this for 1 reason only: It's fatal if the path
// supplied is wrong as it's only used internally in Wails. If we get
// that path wrong, we should know about it immediately. The other reason is
// that it cuts down a ton of unnecassary error handling.
panic(err)
}
return result
}
+7 -4
View File
@@ -4,8 +4,11 @@ type Init struct {
Common
TemplateName string `name:"t" description:"Name of built-in template to use, path to template or template url" default:"vanilla"`
ProjectName string `name:"n" description:"Name of project" default:""`
ProjectDir string `name:"d" description:"Project directory" default:"."`
Quiet bool `name:"q" description:"Suppress output to console"`
List bool `name:"l" description:"List templates"`
ProjectName string `name:"n" description:"Name of project" default:"."`
//CIMode bool `name:"ci" description:"CI Mode"`
ProjectDir string `name:"d" description:"Project directory" default:"."`
Quiet bool `name:"q" description:"Suppress output to console"`
//InitGit bool `name:"g" description:"Initialise git repository"`
//IDE string `name:"ide" description:"Generate IDE project files"`
List bool `name:"l" description:"List templates"`
}
+2 -2
View File
@@ -53,10 +53,10 @@ This package contains the static analyser used for parsing Wails projects so tha
- [x] Recursive
- [x] Anonymous
- [ ] Generation of models
- [ ] Scalars
- [x] Scalars
- [ ] Arrays
- [ ] Maps
- [ ] Structs
- [x] Structs
- [ ] Generation of bindings
## Limitations
+26 -3
View File
@@ -4,6 +4,8 @@ import (
"bytes"
"embed"
"io"
"sort"
"strings"
"text/template"
)
@@ -35,13 +37,34 @@ const modelsHeader = `// @ts-check
// This file is automatically generated. DO NOT EDIT
`
func pkgAlias(fullPkg string) string {
pkgParts := strings.Split(fullPkg, "/")
return pkgParts[len(pkgParts)-1]
}
func GenerateModels(models map[packagePath]map[structName]*StructDef) (string, error) {
if models == nil {
return "", nil
}
var buffer bytes.Buffer
buffer.WriteString(modelsHeader)
for pkg, pkgModels := range models {
// sort pkgs by alias (e.g. services) instead of full pkg name (e.g. github.com/wailsapp/wails/somedir/services)
// and then sort resulting list by the alias
var keys []string
for pkg, _ := range models {
keys = append(keys, pkg)
}
sort.Slice(keys, func(i, j int) bool {
return pkgAlias(keys[i]) < pkgAlias(keys[j])
})
for _, pkg := range keys {
err := GenerateModel(&buffer, &ModelDefinitions{
Package: pkg,
Models: pkgModels,
Package: pkgAlias(pkg),
Models: models[pkg],
})
if err != nil {
return "", err
+62 -118
View File
@@ -2,132 +2,76 @@ package parser
import (
"github.com/google/go-cmp/cmp"
"strings"
"os"
"path/filepath"
"testing"
)
const expected = `
export namespace main {
export class Person {
name: string;
parent: Person;
details: anon1;
address: package.Address;
static createFrom(source: any = {}) {
return new Person(source);
}
func TestGenerateModels(t *testing.T) {
constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}
this.name = source["name"]
this.parent = source["parent"]
this.details = source["details"]
this.address = source["address"]
}
}
export class anon1 {
age: int;
address: string;
static createFrom(source: any = {}) {
return new anon1(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) {
source = JSON.parse(source);
}
this.age = source["age"]
this.address = source["address"]
}
}
}
`
func TestGenerateClass(t *testing.T) {
person := StructDef{
Name: "Person",
Fields: []*Field{
{
Name: "Name",
Type: &ParameterType{
Name: "string",
},
},
{
Name: "Parent",
Type: &ParameterType{
Name: "Person",
IsStruct: true,
IsPointer: true,
Package: "main",
},
},
{
Name: "Details",
Type: &ParameterType{
Name: "anon1",
IsStruct: true,
Package: "main",
},
},
{
Name: "Address",
Type: &ParameterType{
Name: "Address",
IsStruct: true,
IsPointer: true,
Package: "github.com/some/other/package",
},
},
tests := []struct {
dir string
want string
}{
{
"testdata/function_single",
"",
},
}
anon1 := StructDef{
Name: "anon1",
Fields: []*Field{
{
Name: "Age",
Type: &ParameterType{
Name: "int",
},
},
{
Name: "Address",
Type: &ParameterType{
Name: "string",
},
},
{
"testdata/function_from_imported_package",
getFile("testdata/function_from_imported_package/models.ts"),
},
{
"testdata/variable_single",
"",
},
{
"testdata/variable_single_from_function",
"",
},
{
"testdata/variable_single_from_other_function",
getFile("testdata/variable_single_from_other_function/models.ts"),
},
{
"testdata/struct_literal_single",
getFile("testdata/struct_literal_single/models.ts"),
},
{
"testdata/struct_literal_multiple",
"",
},
{
"testdata/struct_literal_multiple_other",
getFile("testdata/struct_literal_multiple_other/models.ts"),
},
{
"testdata/struct_literal_multiple_files",
"",
},
}
for _, tt := range tests {
t.Run(tt.dir, func(t *testing.T) {
// Run parser on directory
project, err := ParseProject(tt.dir)
if err != nil {
t.Fatalf("ParseProject() error = %v", err)
}
var builder strings.Builder
models := make(map[string]*StructDef)
models["Person"] = &person
models["anon1"] = &anon1
def := ModelDefinitions{
Package: "main",
Models: models,
}
// Generate Models
got, err := GenerateModels(project.Models)
if err != nil {
t.Fatalf("GenerateModels() error = %v", err)
}
err := GenerateModel(&builder, &def)
if err != nil {
t.Fatal(err)
}
text := builder.String()
println("Built string")
println(text)
if diff := cmp.Diff(expected, text); diff != "" {
t.Errorf("GenerateClass() failed:\n" + diff)
if diff := cmp.Diff(tt.want, got); diff != "" {
err = os.WriteFile(filepath.Join(tt.dir, "models.got.ts"), []byte(got), 0644)
if err != nil {
t.Errorf("os.WriteFile() error = %v", err)
return
}
t.Fatalf("GenerateModels() mismatch (-want +got):\n%s", diff)
}
})
}
}
+29 -6
View File
@@ -90,16 +90,39 @@ func (f *Field) JSName() string {
return strings.ToLower(f.Name[0:1]) + f.Name[1:]
}
func (f *Field) JSDef(pkg string) string {
name := f.JSName()
var result string
// TSBuild contains the typescript to build a field for a JS object
// via assignment for simple types or constructors for structs
func (f *Field) TSBuild(pkg string) string {
if !f.Type.IsStruct {
return fmt.Sprintf("source['%s']", f.JSName())
}
if f.Type.Package == "" || f.Type.Package == pkg {
result += fmt.Sprintf("%s: %s;", name, f.Type.Name)
return fmt.Sprintf("%s.createFrom(source['%s'])", f.Type.Name, f.JSName())
}
return fmt.Sprintf("%s.%s.createFrom(source['%s'])", pkgAlias(f.Type.Package), f.Type.Name, f.JSName())
}
func (f *Field) JSDef(pkg string) string {
var jsType string
switch f.Type.Name {
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "float32", "float64":
jsType = "number"
case "string":
jsType = "string"
case "bool":
jsType = "boolean"
default:
jsType = f.Type.Name
}
var result string
if f.Type.Package == "" || f.Type.Package == pkg {
result += fmt.Sprintf("%s: %s;", f.JSName(), jsType)
} else {
parts := strings.Split(f.Type.Package, "/")
result += fmt.Sprintf("%s: %s.%s;", name, parts[len(parts)-1], f.Type.Name)
result += fmt.Sprintf("%s: %s.%s;", f.JSName(), parts[len(parts)-1], jsType)
}
if !ast.IsExported(f.Name) {

Some files were not shown because too many files have changed in this diff Show More