-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirectory.Build.targets
More file actions
353 lines (311 loc) · 17 KB
/
Copy pathDirectory.Build.targets
File metadata and controls
353 lines (311 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<Project>
<!-- Overrides to support multi-platform builds when using WPF -->
<Target Name="_CheckForInvalidConfigurationAndPlatform" />
<Target Name="_CheckForUnnecessaryWindowsDesktopSDK" />
<Target Name="_CheckForInvalidWindowsDesktopTargetingConfiguration" />
<Target Name="CleanProject"
BeforeTargets="Clean">
<ItemGroup>
<FoldersToClean Include="$(DistDir)" />
<FoldersToClean Include="$(FeedOutputPath)"
Condition="'$(UseLocalFeed)' != 'true'" />
<FoldersToClean Include="$(PackageOutputPath)"
Condition="'$(UseLocalFeed)' != 'true'" />
<FoldersToClean Include="$(MSBuildProjectDirectory)\bin" />
<FoldersToClean Include="$(MSBuildProjectDirectory)\logs" />
<FoldersToClean Include="$(MSBuildProjectDirectory)\obj" />
<FoldersToClean Include="$(SolutionDir)\docs\_build\_site" />
<FoldersToClean Include="$(SolutionDir)\docs\_build\_xmldoc" />
<FoldersToClean Include="$(MSBuildProjectDirectory)\html-coverage-report" />
<FilesToClean Include="$(MSBuildProjectDirectory)\coverage.cobertura.xml" />
<FilesToClean Include="$(MSBuildProjectDirectory)\coverage.json" />
</ItemGroup>
<RemoveDir Directories="@(FoldersToClean)" />
<Delete Files="@(FilesToClean)" />
</Target>
<Target Name="PreBuildTimestamp"
BeforeTargets="CoreCompile">
<PropertyGroup>
<BaseProjectOutputPath>$(MSBuildProjectDirectory)\$(IntermediateOutputPath)</BaseProjectOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectOutputFiles Include="$(BaseProjectOutputPath)$(MSBuildProjectName).exe;
$(BaseProjectOutputPath)$(MSBuildProjectName).dll" />
<ProjectOutputFiles Remove="@(_ProjectOutputFiles)"
Condition="!Exists('%(Identity)')" />
<ProjectUp2DateFile Include="$(BaseProjectOutputPath)$(MSBuildProjectName).csproj.Up2Date;
$(BaseProjectOutputPath)$(MSBuildProjectName).Version.cs.new" />
<ProjectUp2DateFile Remove="@(ProjectUp2DateFile)"
Condition="!Exists('%(Identity)')" />
</ItemGroup>
<PropertyGroup Condition="'$(IsBuildRunningInTest)' == 'false'">
<OutputTimeStampBeforeBuild>%(ProjectOutputFiles.ModifiedTime)</OutputTimeStampBeforeBuild>
<HasBuildRegenerated Condition="@(ProjectOutputFiles -> Count()) == 0">True</HasBuildRegenerated>
</PropertyGroup>
</Target>
<UsingTask
TaskName="CompareDates"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<FirstDate ParameterType="System.DateTime" Required="true" />
<SecondDate ParameterType="System.DateTime" Required="true" />
<Difference ParameterType="System.Double" Required="true" />
<Result ParameterType="System.Boolean" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
double ToSeconds(DateTime date) => TimeSpan.FromTicks(date.Ticks).TotalSeconds;
if (ToSeconds(FirstDate) <= ToSeconds(SecondDate) && Difference != 0.0)
Result = false;
else
Result = (ToSeconds(FirstDate) - ToSeconds(SecondDate)) > Difference;
]]>
</Code>
</Task>
</UsingTask>
<UsingTask
TaskName="ExtractPackages"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Packages ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<DestinationRoot ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.IO.Compression" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs"><![CDATA[
if (Packages == null || Packages.Length == 0)
{
Log.LogMessage("No packages to extract.");
return true;
}
Directory.CreateDirectory(DestinationRoot);
foreach (var pkg in Packages)
{
var path = pkg.ItemSpec;
if (!File.Exists(path))
{
Log.LogWarning($"Package not found: {path}");
continue;
}
var id = Regex.Replace(Path.GetFileNameWithoutExtension(path), @"\.[0-9].*$", "");
var dest = Path.Combine(DestinationRoot, id);
Directory.CreateDirectory(dest);
using var archive = ZipFile.OpenRead(path);
foreach (var entry in archive.Entries)
{
if (string.IsNullOrEmpty(entry.FullName)) continue;
var outPath = Path.Combine(dest, entry.FullName.Replace('/', Path.DirectorySeparatorChar));
var outDir = Path.GetDirectoryName(outPath);
if (!string.IsNullOrEmpty(outDir)) Directory.CreateDirectory(outDir);
if (entry.FullName.EndsWith("/")) continue; // directory
entry.ExtractToFile(outPath, overwrite: true);
}
Log.LogMessage(MessageImportance.High, $"Extracted {path} -> {dest}");
}
return true;
]]></Code>
</Task>
</UsingTask>
<Target Name="PostBuildTimestamp"
AfterTargets="CoreCompile">
<!-- Compare timestamps of the output files before and after build -->
<PropertyGroup>
<OutputTimeStampAfterBuild>%(ProjectOutputFiles.ModifiedTime)</OutputTimeStampAfterBuild>
</PropertyGroup>
<CompareDates FirstDate="$(OutputTimeStampAfterBuild)"
SecondDate="$(OutputTimeStampBeforeBuild)"
Difference="1.00"
Condition="'$(OutputTimeStampBeforeBuild)' != ''">
<Output TaskParameter="Result" PropertyName="HasTimestampUpdated" />
</CompareDates>
<PropertyGroup Condition="'$(HasTimestampUpdated)' == ''">
<HasTimestampUpdated>True</HasTimestampUpdated>
</PropertyGroup>
<PropertyGroup>
<OutputFilesModified>false</OutputFilesModified>
<OutputFilesModified Condition="'$(HasTimestampUpdated)' == 'True'">true</OutputFilesModified>
</PropertyGroup>
</Target>
<Target Name="SkipPostCompile"
BeforeTargets="CopyFilesToOutputDirectory"
AfterTargets="GenerateDependencyFile"
Condition="'$(OutputFilesModified)' == 'false'">
<ItemGroup>
<ProjectFileWrites Include="@(FileWrites)"
Condition="'%(Extension)' == '.exe' Or
'%(Extension)' == '.dll' Or
'%(Extension)' == '.pdb'" />
</ItemGroup>
<PropertyGroup Condition="@(ProjectFileWrites -> Count()) > 0">
<SkipCopyBuildProduct>true</SkipCopyBuildProduct>
</PropertyGroup>
</Target>
<Target Name="OverrideIncrementalClean"
BeforeTargets="IncrementalClean"
AfterTargets="_CleanGetCurrentAndPriorFileWrites"
Condition="'$(OutputFilesModified)' == 'false' And
'$(SkipCopyBuildProduct)' == 'true'">
<ItemGroup>
<ProjectFileWrites Include="$(OutputPath)$(MSBuildProjectName).exe;
$(OutputPath)$(MSBuildProjectName).dll;
$(OutputPath)$(MSBuildProjectName).pdb" />
<_CleanPriorFileWrites Remove="@(ProjectFileWrites -> '$(MSBuildProjectDirectory)\%(RelativeDir)%(Filename)%(Extension)')"
Condition="Exists('%(FullPath)')" />
</ItemGroup>
</Target>
<Target Name="EnsureReferencePath"
AfterTargets="AfterBuild"
Condition="'$(TargetRefPath)' != '' And !Exists('$(TargetRefPath)')">
<PropertyGroup>
<TargetRefExt>$([System.IO.Path]::GetExtension($(TargetRefPath)))</TargetRefExt>
<TargetRefDir>$([System.IO.Path]::GetDirectoryName($(TargetRefPath)))</TargetRefDir>
</PropertyGroup>
<Copy SourceFiles="$(MSBuildProjectDirectory)\$(IntermediateOutputPath)$(MSBuildProjectName)$(TargetRefExt)"
DestinationFolder="$(TargetRefDir)" />
</Target>
<!-- Override MTGOSDK package versions to use the local package feed -->
<Target Name="UseLocalFeedPackageVersions"
BeforeTargets="CollectPackageReferences"
Condition="'$(UseLocalFeed)' == 'true'">
<PropertyGroup>
<PrereleaseSpecifier>*-preview.*</PrereleaseSpecifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Update="MTGOSDK"
VersionOverride="$(PrereleaseSpecifier)" />
<PackageReference Update="MTGOSDK.MSBuild"
VersionOverride="$(PrereleaseSpecifier)" />
<PackageReference Update="MTGOSDK.Win32"
VersionOverride="$(PrereleaseSpecifier)" />
</ItemGroup>
</Target>
<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
<NuGetCommand>wine "$(NuGetExePath)"</NuGetCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<NuGetCommand>"$(NuGetExePath)"</NuGetCommand>
</PropertyGroup>
<Target Name="UpdateFeed"
AfterTargets="Pack"
Condition="'$(IsPackable)' == 'true' AND ('$(EnableWindowsTargeting)' == 'true' OR '$(OS)' == 'Windows_NT')">
<!-- Query all existing package versions in the feed -->
<PropertyGroup>
<FeedPackagePath>$(FeedOutputPath)\$(MSBuildProjectName.ToLower())</FeedPackagePath>
<CurrentPackageVersionPath>$(MSBuildProjectName.ToLower())\$(PackageVersion)</CurrentPackageVersionPath>
</PropertyGroup>
<ItemGroup>
<_SDKPackagePaths Include="$(FeedPackagePath)\**\*.nupkg" />
<_SDKPackagePaths>
<VersionPath>$([System.IO.Path]::GetRelativePath('$(FeedOutputPath)', [System.IO.Path]::GetDirectoryName('%(Identity)')))</VersionPath>
</_SDKPackagePaths>
<_SDKPackagePaths>
<Version>$([System.IO.Path]::GetFileName('%(VersionPath)'))</Version>
</_SDKPackagePaths>
</ItemGroup>
<!-- Clean the local feed of old package versions -->
<RemoveDir Directories="@(_SDKPackagePaths->'$(FeedOutputPath)\%(VersionPath)');
@(_SDKPackagePaths->'$(NuGetPackageRoot)\%(VersionPath)');
$(FeedOutputPath)\$(CurrentPackageVersionPath);
$(NuGetPackageRoot)\$(CurrentPackageVersionPath)" />
<!-- Add the built package to the local feed -->
<PropertyGroup>
<PackagePath>$(PackageOutputPath)\$(MSBuildProjectName).$(PackageVersion).nupkg</PackagePath>
</PropertyGroup>
<Exec Command="$(NuGetCommand) add "$(PackagePath)" -Source "$(FeedOutputPath)" -Verbosity quiet" Condition="'$(OS)' == 'Windows_NT' AND Exists('$(PackagePath)')" />
<Exec Command="$(NuGetCommand) add %24(winepath -w "$(PackagePath)") -Source %24(winepath -w "$(FeedOutputPath)") -Verbosity quiet" Condition="'$(OS)' != 'Windows_NT' AND Exists('$(PackagePath)')" />
</Target>
<!-- Custom target to build documentation using DocFX and DocProcessor -->
<Target Name="docfx" Condition="'$(MSBuildProjectName)' == 'MTGOSDK'">
<PropertyGroup>
<DocProcessorDir>$(MSBuildThisFileDirectory)tools\DocFX\</DocProcessorDir>
<DocFxDir>$(MSBuildThisFileDirectory)</DocFxDir>
</PropertyGroup>
<Message Text="=== DocFX Documentation Build Pipeline ===" Importance="High" />
<!-- 0. Ensure package contents are available for DocFX -->
<ItemGroup>
<DocfxPackages Include="$(DocFxDir)publish\**\*.nupkg" />
</ItemGroup>
<Message Text="Preparing packages for DocFX from $(DocFxDir)publish" Importance="High" />
<Error Condition="@(DocfxPackages->Count()) == 0"
Text="No packages found under $(DocFxDir)publish. Ensure package artifacts are downloaded (or built) before running the docfx target." />
<PropertyGroup>
<DocfxExtractRoot>$(DocFxDir)dist</DocfxExtractRoot>
</PropertyGroup>
<MakeDir Directories="$(DocfxExtractRoot)" />
<ExtractPackages Packages="@(DocfxPackages)"
DestinationRoot="$(DocfxExtractRoot)"
Condition="@(DocfxPackages->Count()) > 0" />
<!-- 1. Build DocProcessor Tool -->
<Message Text="Building DocProcessor tool..." Importance="High" />
<MSBuild Projects="$(DocProcessorDir)DocProcessor.csproj" Targets="Build" Properties="Configuration=Release" />
<!-- 2. Get Version Info from extracted nuspec (preferred) or fall back to 0.0.0 -->
<PropertyGroup>
<!-- Use the MTGOSDK package nuspec extracted to dist -->
<_DocfxPrimaryNuspec>$(DocfxExtractRoot)\MTGOSDK\MTGOSDK.nuspec</_DocfxPrimaryNuspec>
</PropertyGroup>
<Error Condition="!Exists('$(_DocfxPrimaryNuspec)')"
Text="DocFX build expected nuspec at $(_DocfxPrimaryNuspec) (ensure packages were extracted from publish/)." />
<XmlPeek Query="/*[local-name()='package']/*[local-name()='metadata']/*[local-name()='version']/text()"
XmlInputPath="$(_DocfxPrimaryNuspec)"
Condition="Exists('$(_DocfxPrimaryNuspec)')">
<Output TaskParameter="Result" PropertyName="_NuspecVersion" />
</XmlPeek>
<XmlPeek Query="/*[local-name()='package']/*[local-name()='metadata']/*[local-name()='repository']/@commit"
XmlInputPath="$(_DocfxPrimaryNuspec)"
Condition="Exists('$(_DocfxPrimaryNuspec)')">
<Output TaskParameter="Result" PropertyName="_NuspecCommit" />
</XmlPeek>
<PropertyGroup>
<!-- Split version into main and optional suffix from nuspec version (e.g., 1.2.3-preview.5) -->
<_VersionMain>$([System.Text.RegularExpressions.Regex]::Match('$(_NuspecVersion)', '^[^-]+').Value)</_VersionMain>
<_VersionSuffix>$([System.Text.RegularExpressions.Regex]::Replace('$(_NuspecVersion)', '^[^-]+-?', ''))</_VersionSuffix>
<_VersionMain Condition="'$(_VersionMain)' == ''">$(_NuspecVersion)</_VersionMain>
<_VersionMain Condition="'$(_VersionMain)' == ''">0.0.0</_VersionMain>
<_CommitHash Condition="'$(_NuspecCommit)' != ''">$(_NuspecCommit)</_CommitHash>
<_CommitHash Condition="'$(_CommitHash)' == ''"></_CommitHash>
</PropertyGroup>
<!-- Extract short hash and URL -->
<PropertyGroup>
<_ShortHash>$(_CommitHash)</_ShortHash>
<_ShortHash Condition="'$(_CommitHash)' != '' And $(_CommitHash.Length) >= 7">$(_CommitHash.Substring(0, 7))</_ShortHash>
<_CommitUrl Condition="'$(_ShortHash)' != ''">https://github.com/videre-project/MTGOSDK/commit/$(_ShortHash)</_CommitUrl>
</PropertyGroup>
<!-- Fallback to git hash if MinVer metadata is empty -->
<Exec Command="git rev-parse --short HEAD" ConsoleToMSBuild="true" Condition="'$(_ShortHash)' == ''">
<Output TaskParameter="ConsoleOutput" PropertyName="_ShortHash" />
</Exec>
<PropertyGroup>
<_CommitUrl Condition="'$(_ShortHash)' != ''">https://github.com/videre-project/MTGOSDK/commit/$(_ShortHash)</_CommitUrl>
</PropertyGroup>
<Message Text="Building docs for version: $(_VersionMain) $(_VersionSuffix) + $(_ShortHash)" Importance="High" />
<!-- 3. DocFX Metadata -->
<Message Text="[1/6] Generating metadata..." Importance="High" />
<Exec Command="dotnet docfx metadata $(DocFxDir)docfx.json" WorkingDirectory="$(DocFxDir)" />
<!-- 4. Reorder Members -->
<Message Text="[2/6] Reordering members by source order..." Importance="High" />
<Exec Command="dotnet run --project $(DocProcessorDir)DocProcessor.csproj -c Release --no-build -- reorder -SourceRoot "$(DocFxDir)MTGOSDK\src" -YamlDirectory "$(DocFxDir)docs\_build\_xmldoc\MTGOSDK"" />
<!-- 5. Remove Inherited Members -->
<Message Text="[3/6] Removing inherited members..." Importance="High" />
<Exec Command="dotnet run --project $(DocProcessorDir)DocProcessor.csproj -c Release --no-build -- remove-inherited -YamlDirectory "$(DocFxDir)docs\_build\_xmldoc\MTGOSDK"" />
<!-- 6. Reclassify Events -->
<Message Text="[4/6] Reclassifying event fields..." Importance="High" />
<Exec Command="dotnet run --project $(DocProcessorDir)DocProcessor.csproj -c Release --no-build -- reclassify-events -YamlDirectory "$(DocFxDir)docs\_build\_xmldoc\MTGOSDK"" />
<!-- 7. Organize TOC -->
<Message Text="[5/6] Organizing TOC..." Importance="High" />
<Exec Command="dotnet run --project $(DocProcessorDir)DocProcessor.csproj -c Release --no-build -- organize-toc -SourceRoot "$(DocFxDir)MTGOSDK\src" -TocPath "$(DocFxDir)docs\_build\_xmldoc\MTGOSDK\toc.yml"" />
<!-- 8. DocFX Build -->
<Message Text="[6/6] Building site..." Importance="High" />
<!-- Execute DocFX build via external PowerShell script -->
<Exec Command="powershell -ExecutionPolicy Bypass -NoProfile -File "$(DocFxDir)docs\_build\docfx\build.ps1" -DocFxDir "." -VersionMain "$(_VersionMain)" -VersionSuffix "$(_VersionSuffix)" -ShortHash "$(_ShortHash)" -CommitUrl "$(_CommitUrl)""
WorkingDirectory="$(DocFxDir)"
ConsoleToMSBuild="true" />
<Message Text="=== DocFX Build Complete ===" Importance="High" />
</Target>
</Project>