|
5 | 5 | package executor |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "fmt" |
8 | 9 | "os" |
9 | 10 | "os/exec" |
10 | 11 | "path/filepath" |
11 | 12 | "sort" |
| 13 | + "strconv" |
12 | 14 | "strings" |
13 | 15 | "testing" |
14 | 16 |
|
15 | 17 | "github.com/mendixlabs/mxcli/mdl/ast" |
16 | 18 | "github.com/mendixlabs/mxcli/mdl/visitor" |
| 19 | + "github.com/mendixlabs/mxcli/sdk/mpr/version" |
17 | 20 | ) |
18 | 21 |
|
19 | 22 | // scriptModuleDeps maps script filenames to marketplace module MPKs they require. |
@@ -129,8 +132,15 @@ func TestMxCheck_DoctypeScripts(t *testing.T) { |
129 | 132 | } |
130 | 133 | } |
131 | 134 |
|
| 135 | + // Filter out version-gated sections that don't match this project's Mendix version |
| 136 | + pv := env.executor.reader.ProjectVersion() |
| 137 | + filtered, skippedLines := filterByVersion(string(content), pv) |
| 138 | + if skippedLines > 0 { |
| 139 | + t.Logf("Mendix %s: skipped %d version-gated lines", pv.ProductVersion, skippedLines) |
| 140 | + } |
| 141 | + |
132 | 142 | // Execute the script |
133 | | - prog, errs := visitor.Build(string(content)) |
| 143 | + prog, errs := visitor.Build(filtered) |
134 | 144 | if len(errs) > 0 { |
135 | 145 | t.Fatalf("Parse error: %v", errs[0]) |
136 | 146 | } |
@@ -192,3 +202,131 @@ func allErrorsKnown(output string, knownCodes []string) bool { |
192 | 202 | } |
193 | 203 | return true |
194 | 204 | } |
| 205 | + |
| 206 | +// versionConstraint represents a min/max Mendix version range for -- @version: directives. |
| 207 | +type versionConstraint struct { |
| 208 | + minMajor, minMinor int // -1 means no minimum |
| 209 | + maxMajor, maxMinor int // -1 means no maximum |
| 210 | +} |
| 211 | + |
| 212 | +// matches returns true if the project version satisfies this constraint. |
| 213 | +func (vc *versionConstraint) matches(pv *version.ProjectVersion) bool { |
| 214 | + if vc.minMajor >= 0 { |
| 215 | + if !pv.IsAtLeast(vc.minMajor, vc.minMinor) { |
| 216 | + return false |
| 217 | + } |
| 218 | + } |
| 219 | + if vc.maxMajor >= 0 { |
| 220 | + // Check that version is at most maxMajor.maxMinor |
| 221 | + if pv.MajorVersion > vc.maxMajor || (pv.MajorVersion == vc.maxMajor && pv.MinorVersion > vc.maxMinor) { |
| 222 | + return false |
| 223 | + } |
| 224 | + } |
| 225 | + return true |
| 226 | +} |
| 227 | + |
| 228 | +func (vc *versionConstraint) String() string { |
| 229 | + if vc.minMajor >= 0 && vc.maxMajor >= 0 { |
| 230 | + return fmt.Sprintf("%d.%d..%d.%d", vc.minMajor, vc.minMinor, vc.maxMajor, vc.maxMinor) |
| 231 | + } |
| 232 | + if vc.minMajor >= 0 { |
| 233 | + return fmt.Sprintf("%d.%d+", vc.minMajor, vc.minMinor) |
| 234 | + } |
| 235 | + if vc.maxMajor >= 0 { |
| 236 | + return fmt.Sprintf("..%d.%d", vc.maxMajor, vc.maxMinor) |
| 237 | + } |
| 238 | + return "any" |
| 239 | +} |
| 240 | + |
| 241 | +// parseVersionDirective parses a "-- @version: <constraint>" line. |
| 242 | +// Returns nil for "any" or unparseable directives. |
| 243 | +// Formats: "11.0+", "10.6..10.24", "..10.24", "any" |
| 244 | +func parseVersionDirective(line string) *versionConstraint { |
| 245 | + s := strings.TrimPrefix(line, "-- @version:") |
| 246 | + s = strings.TrimSpace(s) |
| 247 | + |
| 248 | + if s == "" || s == "any" { |
| 249 | + return nil |
| 250 | + } |
| 251 | + |
| 252 | + // Range: "10.6..10.24" |
| 253 | + if parts := strings.SplitN(s, "..", 2); len(parts) == 2 { |
| 254 | + vc := &versionConstraint{minMajor: -1, minMinor: -1, maxMajor: -1, maxMinor: -1} |
| 255 | + if parts[0] != "" { |
| 256 | + major, minor, ok := parseMajorMinor(parts[0]) |
| 257 | + if !ok { |
| 258 | + return nil |
| 259 | + } |
| 260 | + vc.minMajor, vc.minMinor = major, minor |
| 261 | + } |
| 262 | + if parts[1] != "" { |
| 263 | + major, minor, ok := parseMajorMinor(parts[1]) |
| 264 | + if !ok { |
| 265 | + return nil |
| 266 | + } |
| 267 | + vc.maxMajor, vc.maxMinor = major, minor |
| 268 | + } |
| 269 | + return vc |
| 270 | + } |
| 271 | + |
| 272 | + // Minimum: "11.0+" |
| 273 | + if strings.HasSuffix(s, "+") { |
| 274 | + s = strings.TrimSuffix(s, "+") |
| 275 | + major, minor, ok := parseMajorMinor(s) |
| 276 | + if !ok { |
| 277 | + return nil |
| 278 | + } |
| 279 | + return &versionConstraint{minMajor: major, minMinor: minor, maxMajor: -1, maxMinor: -1} |
| 280 | + } |
| 281 | + |
| 282 | + return nil |
| 283 | +} |
| 284 | + |
| 285 | +// parseMajorMinor parses "10.24" into (10, 24, true). |
| 286 | +func parseMajorMinor(s string) (int, int, bool) { |
| 287 | + parts := strings.SplitN(s, ".", 2) |
| 288 | + if len(parts) != 2 { |
| 289 | + return 0, 0, false |
| 290 | + } |
| 291 | + major, err := strconv.Atoi(parts[0]) |
| 292 | + if err != nil { |
| 293 | + return 0, 0, false |
| 294 | + } |
| 295 | + minor, err := strconv.Atoi(parts[1]) |
| 296 | + if err != nil { |
| 297 | + return 0, 0, false |
| 298 | + } |
| 299 | + return major, minor, true |
| 300 | +} |
| 301 | + |
| 302 | +// filterByVersion removes MDL content sections that don't match the project's Mendix version. |
| 303 | +// Sections are delimited by "-- @version: <constraint>" directives. |
| 304 | +// A directive applies to all following lines until the next directive or end of file. |
| 305 | +// "-- @version: any" resets to unconditional inclusion. |
| 306 | +func filterByVersion(content string, pv *version.ProjectVersion) (string, int) { |
| 307 | + var result strings.Builder |
| 308 | + var currentConstraint *versionConstraint // nil = no constraint (always include) |
| 309 | + skippedLines := 0 |
| 310 | + |
| 311 | + for _, line := range strings.Split(content, "\n") { |
| 312 | + trimmed := strings.TrimSpace(line) |
| 313 | + if strings.HasPrefix(trimmed, "-- @version:") { |
| 314 | + currentConstraint = parseVersionDirective(trimmed) |
| 315 | + // Keep the directive line as a comment (so line numbers stay close) |
| 316 | + result.WriteString(line) |
| 317 | + result.WriteString("\n") |
| 318 | + continue |
| 319 | + } |
| 320 | + if currentConstraint == nil || currentConstraint.matches(pv) { |
| 321 | + result.WriteString(line) |
| 322 | + result.WriteString("\n") |
| 323 | + } else { |
| 324 | + // Replace with empty line to preserve line numbering for error messages |
| 325 | + result.WriteString("\n") |
| 326 | + if strings.TrimSpace(line) != "" && !strings.HasPrefix(trimmed, "--") { |
| 327 | + skippedLines++ |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + return result.String(), skippedLines |
| 332 | +} |
0 commit comments