|
| 1 | +package migrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | +) |
| 9 | + |
| 10 | +// The v2 compatibility bridge lives in this repository as an internal package |
| 11 | +// so it is compiled, vetted and testable, but it is deliberately NOT part of |
| 12 | +// the public v3 API: `wails3 migrate` copies its source into the migrated |
| 13 | +// project instead. The user owns the copy and deletes functions (and finally |
| 14 | +// the whole package) as call sites are ported to the v3 API. |
| 15 | +// |
| 16 | +//go:embed v2compat/runtime/*.go |
| 17 | +var compatBridgeFS embed.FS |
| 18 | + |
| 19 | +// compatBridgeHeader is prepended to every generated bridge file. |
| 20 | +const compatBridgeHeader = `// Code generated by wails3 migrate. |
| 21 | +// |
| 22 | +// This package is a TEMPORARY bridge that implements the Wails v2 runtime API |
| 23 | +// on top of Wails v3. It is part of your project: as you port call sites to |
| 24 | +// the v3 API (github.com/wailsapp/wails/v3/pkg/application), delete the |
| 25 | +// functions they used, and delete the whole package when nothing imports it |
| 26 | +// any more. Each function documents its v3 replacement. |
| 27 | +
|
| 28 | +` |
| 29 | + |
| 30 | +// CompatRuntimeImport returns the project-local import path the bridge is |
| 31 | +// generated under. |
| 32 | +func (p *V2Project) CompatRuntimeImport() string { |
| 33 | + return p.ModulePath + "/v2compat/runtime" |
| 34 | +} |
| 35 | + |
| 36 | +// WriteCompatBridge copies the v2 compatibility bridge sources into the |
| 37 | +// output project under v2compat/runtime. |
| 38 | +func WriteCompatBridge(proj *V2Project, outDir string) error { |
| 39 | + targetDir := filepath.Join(outDir, "v2compat", "runtime") |
| 40 | + if err := os.MkdirAll(targetDir, 0o755); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + entries, err := fs.ReadDir(compatBridgeFS, "v2compat/runtime") |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + for _, entry := range entries { |
| 48 | + data, err := fs.ReadFile(compatBridgeFS, "v2compat/runtime/"+entry.Name()) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + out := append([]byte(compatBridgeHeader), data...) |
| 53 | + if err := os.WriteFile(filepath.Join(targetDir, entry.Name()), out, 0o644); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + } |
| 57 | + proj.Report.Note("A v2 runtime compatibility bridge was generated at `v2compat/runtime` in your project. It is yours: delete its functions as you port call sites to the v3 API, and remove the package when nothing imports it any more.") |
| 58 | + return nil |
| 59 | +} |
0 commit comments