|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Post-migration scan for legacy native widgets in a Mendix project. |
| 4 | +// |
| 5 | +// When a project is upgraded from Mendix 10.x to 11.x, Studio Pro does NOT |
| 6 | +// auto-rewrite native-stack widgets (e.g. Forms$DataGrid) to their pluggable |
| 7 | +// replacements. The author has to migrate them by hand. This scanner walks |
| 8 | +// every page and snippet, looks for legacy widget types listed in |
| 9 | +// executor.LegacyWidgets, and reports each occurrence with a hint to the |
| 10 | +// recommended pluggable equivalent. |
| 11 | + |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "fmt" |
| 16 | + "reflect" |
| 17 | + "sort" |
| 18 | + |
| 19 | + "github.com/mendixlabs/mxcli/mdl/executor" |
| 20 | + "github.com/mendixlabs/mxcli/mdl/linter" |
| 21 | + "github.com/mendixlabs/mxcli/sdk/mpr" |
| 22 | +) |
| 23 | + |
| 24 | +// legacyHit records one legacy widget occurrence with enough context to |
| 25 | +// produce a useful diagnostic. |
| 26 | +type legacyHit struct { |
| 27 | + Module string // qualified module name (may be empty if hierarchy resolution failed) |
| 28 | + Document string // page or snippet name |
| 29 | + DocKind string // "page" or "snippet" |
| 30 | + WidgetName string // widget instance name as authored in Studio Pro |
| 31 | + Entry *executor.LegacyWidget |
| 32 | +} |
| 33 | + |
| 34 | +// scanLegacyWidgets opens the given .mpr, walks all pages and snippets, and |
| 35 | +// returns a violation for every legacy native widget found that's deprecated |
| 36 | +// on the project's Mendix version. |
| 37 | +func scanLegacyWidgets(projectPath string) ([]linter.Violation, error) { |
| 38 | + reader, err := mpr.Open(projectPath) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("opening project: %w", err) |
| 41 | + } |
| 42 | + defer reader.Close() |
| 43 | + |
| 44 | + mendixVersion, _ := reader.GetMendixVersion() |
| 45 | + |
| 46 | + hierarchy, err := executor.NewContainerHierarchy(reader) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("building project hierarchy: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + var hits []legacyHit |
| 52 | + |
| 53 | + pgs, err := reader.ListPages() |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("listing pages: %w", err) |
| 56 | + } |
| 57 | + for _, pg := range pgs { |
| 58 | + module := hierarchy.GetModuleName(hierarchy.FindModuleID(pg.ContainerID)) |
| 59 | + walkForLegacyWidgets(reflect.ValueOf(pg), mendixVersion, func(entry *executor.LegacyWidget, name string) { |
| 60 | + hits = append(hits, legacyHit{ |
| 61 | + Module: module, Document: pg.Name, DocKind: "page", |
| 62 | + WidgetName: name, Entry: entry, |
| 63 | + }) |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + sns, err := reader.ListSnippets() |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("listing snippets: %w", err) |
| 70 | + } |
| 71 | + for _, sn := range sns { |
| 72 | + module := hierarchy.GetModuleName(hierarchy.FindModuleID(sn.ContainerID)) |
| 73 | + walkForLegacyWidgets(reflect.ValueOf(sn), mendixVersion, func(entry *executor.LegacyWidget, name string) { |
| 74 | + hits = append(hits, legacyHit{ |
| 75 | + Module: module, Document: sn.Name, DocKind: "snippet", |
| 76 | + WidgetName: name, Entry: entry, |
| 77 | + }) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + sortHits(hits) |
| 82 | + return hitsToViolations(hits, mendixVersion), nil |
| 83 | +} |
| 84 | + |
| 85 | +// walkForLegacyWidgets recursively walks the reflect.Value of a parsed page |
| 86 | +// or snippet. Any struct whose Go type name matches a known legacy widget |
| 87 | +// triggers the callback. |
| 88 | +// |
| 89 | +// We match by `reflect.Type.Name()` (e.g. "DataGrid") rather than by type |
| 90 | +// assertion against an interface, because the parsed page widgets in |
| 91 | +// sdk/pages don't all implement a common Widget interface uniformly. Type |
| 92 | +// names are stable enough — the catalog (executor.LegacyWidgets) is small |
| 93 | +// and hand-maintained. |
| 94 | +func walkForLegacyWidgets(v reflect.Value, version string, visit func(*executor.LegacyWidget, string)) { |
| 95 | + for v.Kind() == reflect.Pointer { |
| 96 | + if v.IsNil() { |
| 97 | + return |
| 98 | + } |
| 99 | + v = v.Elem() |
| 100 | + } |
| 101 | + |
| 102 | + switch v.Kind() { |
| 103 | + case reflect.Struct: |
| 104 | + if entry := executor.FindLegacyWidget(v.Type().Name()); entry != nil && entry.IsDeprecatedOnVersion(version) { |
| 105 | + visit(entry, widgetNameFrom(v)) |
| 106 | + } |
| 107 | + for i := 0; i < v.NumField(); i++ { |
| 108 | + f := v.Field(i) |
| 109 | + if !f.CanInterface() { |
| 110 | + continue |
| 111 | + } |
| 112 | + walkForLegacyWidgets(f, version, visit) |
| 113 | + } |
| 114 | + case reflect.Slice, reflect.Array: |
| 115 | + for i := 0; i < v.Len(); i++ { |
| 116 | + walkForLegacyWidgets(v.Index(i), version, visit) |
| 117 | + } |
| 118 | + case reflect.Interface: |
| 119 | + if !v.IsNil() { |
| 120 | + walkForLegacyWidgets(v.Elem(), version, visit) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// widgetNameFrom reads the Name field from an embedded BaseWidget if present. |
| 126 | +// Returns "" when no name is available. |
| 127 | +func widgetNameFrom(v reflect.Value) string { |
| 128 | + // BaseWidget.Name is reachable via field path on every widget struct. |
| 129 | + if f := v.FieldByName("BaseWidget"); f.IsValid() && f.Kind() == reflect.Struct { |
| 130 | + if n := f.FieldByName("Name"); n.IsValid() && n.Kind() == reflect.String { |
| 131 | + return n.String() |
| 132 | + } |
| 133 | + } |
| 134 | + // Fallback: direct Name field (some non-widget types). |
| 135 | + if n := v.FieldByName("Name"); n.IsValid() && n.Kind() == reflect.String { |
| 136 | + return n.String() |
| 137 | + } |
| 138 | + return "" |
| 139 | +} |
| 140 | + |
| 141 | +// sortHits orders hits by module, document, widget for stable output. |
| 142 | +func sortHits(hits []legacyHit) { |
| 143 | + sort.Slice(hits, func(i, j int) bool { |
| 144 | + if hits[i].Module != hits[j].Module { |
| 145 | + return hits[i].Module < hits[j].Module |
| 146 | + } |
| 147 | + if hits[i].Document != hits[j].Document { |
| 148 | + return hits[i].Document < hits[j].Document |
| 149 | + } |
| 150 | + return hits[i].WidgetName < hits[j].WidgetName |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +// hitsToViolations converts legacy hits into linter violations with the |
| 155 | +// MDL-WIDGET02 rule code. |
| 156 | +func hitsToViolations(hits []legacyHit, version string) []linter.Violation { |
| 157 | + if len(hits) == 0 { |
| 158 | + return nil |
| 159 | + } |
| 160 | + out := make([]linter.Violation, 0, len(hits)) |
| 161 | + for _, h := range hits { |
| 162 | + qualified := h.Document |
| 163 | + if h.Module != "" { |
| 164 | + qualified = h.Module + "." + h.Document |
| 165 | + } |
| 166 | + name := h.WidgetName |
| 167 | + if name == "" { |
| 168 | + name = "(unnamed)" |
| 169 | + } |
| 170 | + msg := fmt.Sprintf( |
| 171 | + "%s %s: widget `%s` uses deprecated native `%s` (deprecated from Mendix %s) — %s", |
| 172 | + h.DocKind, qualified, name, h.Entry.BSONType, h.Entry.DeprecatedFrom, h.Entry.Hint, |
| 173 | + ) |
| 174 | + if version != "" { |
| 175 | + msg += fmt.Sprintf(" (project is on %s)", version) |
| 176 | + } |
| 177 | + out = append(out, linter.Violation{ |
| 178 | + RuleID: "MDL-WIDGET02", |
| 179 | + Severity: linter.SeverityWarning, |
| 180 | + Message: msg, |
| 181 | + }) |
| 182 | + } |
| 183 | + return out |
| 184 | +} |
0 commit comments