|
| 1 | +using Microsoft.Build.Framework; |
| 2 | +using Microsoft.Build.Utilities; |
| 3 | + |
| 4 | +namespace JD.Efcpt.Build.Tasks; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// MSBuild task that detects whether the current project is a SQL database project. |
| 8 | +/// Uses the SqlProjectDetector to check for SDK-based projects first, then falls back to property-based detection. |
| 9 | +/// </summary> |
| 10 | +// Note: Fully qualifying Task to avoid ambiguity with System.Threading.Tasks.Task |
| 11 | +public sealed class DetectSqlProject : Microsoft.Build.Utilities.Task |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Gets or sets the full path to the project file. |
| 15 | + /// </summary> |
| 16 | + [Required] |
| 17 | + public string? ProjectPath { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets or sets the SqlServerVersion property (for legacy SSDT detection). |
| 21 | + /// </summary> |
| 22 | + public string? SqlServerVersion { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the DSP property (for legacy SSDT detection). |
| 26 | + /// </summary> |
| 27 | + public string? DSP { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets a value indicating whether the project is a SQL project. |
| 31 | + /// </summary> |
| 32 | + [Output] |
| 33 | + public bool IsSqlProject { get; private set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Executes the task to detect if the project is a SQL database project. |
| 37 | + /// </summary> |
| 38 | + /// <returns>True if the task executes successfully; otherwise, false.</returns> |
| 39 | + public override bool Execute() |
| 40 | + { |
| 41 | + if (string.IsNullOrWhiteSpace(ProjectPath)) |
| 42 | + { |
| 43 | + Log.LogError("ProjectPath is required."); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // First, check if project uses a modern SQL SDK via SDK attribute |
| 48 | + var usesModernSdk = SqlProjectDetector.IsSqlProjectReference(ProjectPath); |
| 49 | + |
| 50 | + if (usesModernSdk) |
| 51 | + { |
| 52 | + IsSqlProject = true; |
| 53 | + Log.LogMessage(MessageImportance.Low, |
| 54 | + "Detected SQL project via SDK attribute: {0}", ProjectPath); |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + // Fall back to property-based detection for legacy SSDT projects |
| 59 | + var hasLegacyProperties = !string.IsNullOrEmpty(SqlServerVersion) || !string.IsNullOrEmpty(DSP); |
| 60 | + |
| 61 | + if (hasLegacyProperties) |
| 62 | + { |
| 63 | + IsSqlProject = true; |
| 64 | + Log.LogMessage(MessageImportance.Low, |
| 65 | + "Detected SQL project via MSBuild properties (legacy SSDT): {0}", ProjectPath); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + IsSqlProject = false; |
| 70 | + Log.LogMessage(MessageImportance.Low, |
| 71 | + "Not a SQL project: {0}", ProjectPath); |
| 72 | + return true; |
| 73 | + } |
| 74 | +} |
0 commit comments