Skip to content

Commit 0bcf512

Browse files
committed
Add task to perform file inventory before and after each repo
build when a FileInventoryPath is passed to the build. This inventories the root/bin directory, the root/packages directory and the repo/src directory plus a couple of key files.
1 parent d774da5 commit 0bcf512

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

repos/dir.targets

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<UsingTask AssemblyFile="$(TasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.dll" TaskName="WriteBuildOutputProps" />
1212
<UsingTask AssemblyFile="$(TasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.dll" TaskName="WriteRestoreSourceProps" />
1313
<UsingTask AssemblyFile="$(TasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.dll" TaskName="WriteVersionsFile" />
14+
<UsingTask AssemblyFile="$(TasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.dll" TaskName="WriteFileInventory" />
1415

1516
<!--
1617
Central property to define that a repo doesn't implement any of the Repo API. If a repo adds an
@@ -50,6 +51,11 @@
5051
<RepoApiArgs>$(RepoApiArgs) /p:DotNetOutputBlobFeedDir=$(SourceBuiltBlobFeedDir)</RepoApiArgs>
5152
</PropertyGroup>
5253

54+
<PropertyGroup Condition="'$(FileInventoryPath)'!=''" >
55+
<FileInventoryBeforePath>$(FileInventoryPath)$(RepositoryName)/before/</FileInventoryBeforePath>
56+
<FileInventoryAfterPath>$(FileInventoryPath)$(RepositoryName)/after/</FileInventoryAfterPath>
57+
</PropertyGroup>
58+
5359
<Import Project="$(ProjectDirectory)dependencies.props"
5460
Condition="'$(DependencyVersionInputRepoApiImplemented)' != 'true' AND
5561
Exists('$(ProjectDirectory)dependencies.props')" />
@@ -76,6 +82,30 @@
7682
WorkingDirectory="$(ProjectDirectory)" />
7783
</Target>
7884

85+
<Target Name="InventoryFilesBeforeBuild"
86+
BeforeTargets="Build">
87+
88+
<WriteFileInventory SourcePath="$(BaseOutputPath)"
89+
OutputPath="$(FileInventoryBeforePath)binInventory.txt"
90+
Condition="'$(FileInventoryBeforePath)'!=''" />
91+
92+
<WriteFileInventory SourcePath="$(NUGET_PACKAGES)"
93+
OutputPath="$(FileInventoryBeforePath)packageInventory.txt"
94+
Condition="'$(FileInventoryBeforePath)'!=''" />
95+
96+
<WriteFileInventory SourcePath="$(ProjectDirectory)"
97+
OutputPath="$(FileInventoryBeforePath)repoSourceInventory.txt"
98+
Condition="'$(FileInventoryBeforePath)'!=''" />
99+
100+
<Copy SourceFiles="$(VersionFileLocation)"
101+
DestinationFolder="$(FileInventoryBeforePath)"
102+
Condition="Exists('$(VersionFileLocation)')" />
103+
104+
<Copy SourceFiles="$(PackageVersionPropsPath)"
105+
DestinationFolder="$(FileInventoryBeforePath)"
106+
Condition="Exists('$(PackageVersionPropsPath)')" />
107+
</Target>
108+
79109
<Target Name="UpdateNuGetConfig"
80110
BeforeTargets="Build"
81111
Condition="'$(NuGetConfigFile)' != ''">
@@ -173,6 +203,30 @@
173203
Condition="'@(_BuiltPackages)'!=''" />
174204
</Target>
175205

206+
<Target Name="InventoryFilesAfterBuild"
207+
AfterTargets="WriteVersions">
208+
209+
<WriteFileInventory SourcePath="$(BaseOutputPath)"
210+
OutputPath="$(FileInventoryAfterPath)binInventory.txt"
211+
Condition="'$(FileInventoryAfterPath)'!=''" />
212+
213+
<WriteFileInventory SourcePath="$(NUGET_PACKAGES)"
214+
OutputPath="$(FileInventoryAfterPath)packageInventory.txt"
215+
Condition="'$(FileInventoryAfterPath)'!=''" />
216+
217+
<WriteFileInventory SourcePath="$(ProjectDirectory)"
218+
OutputPath="$(FileInventoryAfterPath)repoSourceInventory.txt"
219+
Condition="'$(FileInventoryAfterPath)'!=''" />
220+
221+
<Copy SourceFiles="$(VersionFileLocation)"
222+
DestinationFolder="$(FileInventoryAfterPath)"
223+
Condition="Exists('$(VersionFileLocation)')" />
224+
225+
<Copy SourceFiles="$(PackageVersionPropsPath)"
226+
DestinationFolder="$(FileInventoryAfterPath)"
227+
Condition="Exists('$(PackageVersionPropsPath)')" />
228+
</Target>
229+
176230
<Target Name="Clean" Condition="'$(CleanCommand)' != ''" >
177231
<Exec Command="$(CleanCommand) $(RedirectRepoOutputToLog)" WorkingDirectory="$(ProjectDirectory)" EnvironmentVariables="@(EnvironmentVariables)" />
178232
</Target>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Text;
9+
using Microsoft.Build.Framework;
10+
using Microsoft.Build.Utilities;
11+
using NuGet.Packaging;
12+
using NuGet.Packaging.Core;
13+
14+
namespace Microsoft.DotNet.Build.Tasks
15+
{
16+
public class WriteFileInventory : Task
17+
{
18+
[Required]
19+
public string SourcePath { get; set; }
20+
21+
[Required]
22+
public string OutputPath { get; set; }
23+
24+
public override bool Execute()
25+
{
26+
List<string> files = new List<string>();
27+
foreach (string file in Directory.EnumerateFiles(SourcePath, "*", SearchOption.AllDirectories))
28+
{
29+
files.Add(string.Format("{0}, {1:u}", file, File.GetLastWriteTimeUtc(file)));
30+
}
31+
32+
Directory.CreateDirectory(Path.GetDirectoryName(OutputPath));
33+
34+
files.Sort();
35+
36+
using (Stream outStream = File.Open(OutputPath, FileMode.Create))
37+
{
38+
using (StreamWriter sw = new StreamWriter(outStream, new UTF8Encoding(false)))
39+
{
40+
foreach (string file in files)
41+
{
42+
sw.WriteLine(file);
43+
}
44+
}
45+
}
46+
47+
return true;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)