Skip to content

Commit ba722ca

Browse files
committed
Nuget cache clearing improvements
1 parent 1c29042 commit ba722ca

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

Directory.Build.targets

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
<Project>
2-
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
3-
<Exec Condition="'$(IsWindows)'=='true' And $(PackageId) != ''" Command="RD /S /Q &quot;%25USERPROFILE%25\.nuget\packages\$(PackageId)&quot;" />
4-
<Exec Condition="'$(IsLinux)'=='true' And $(PackageId) != ''" Command="rm -rf &quot;%25USERPROFILE%25\.nuget\packages\$(PackageId)&quot;" />
5-
</Target>
2+
<!-- Clear NuGet packages after successful build for projects that generate packages -->
3+
<Target Name="ClearLocalNuGetPackages" AfterTargets="Build" Condition="'$(GeneratePackageOnBuild)' == 'true'">
4+
<PropertyGroup>
5+
<NuGetPackagesPath>$(USERPROFILE)\.nuget\packages</NuGetPackagesPath>
6+
<!-- Use PackageId if specified, otherwise use AssemblyName, otherwise use MSBuildProjectName -->
7+
<CurrentPackageId Condition="'$(PackageId)' != ''">$(PackageId)</CurrentPackageId>
8+
<CurrentPackageId Condition="'$(CurrentPackageId)' == '' AND '$(AssemblyName)' != ''">$(AssemblyName)</CurrentPackageId>
9+
<CurrentPackageId Condition="'$(CurrentPackageId)' == ''">$(MSBuildProjectName)</CurrentPackageId>
10+
<!-- Convert to lowercase for NuGet folder name -->
11+
<CurrentPackageIdLower>$(CurrentPackageId.ToLowerInvariant())</CurrentPackageIdLower>
12+
</PropertyGroup>
13+
14+
<Message Text="Clearing local NuGet package cache for $(CurrentPackageId)..." Importance="high" />
15+
16+
<!-- Windows -->
17+
<Exec Condition="'$(OS)' == 'Windows_NT'"
18+
Command="if exist &quot;$(NuGetPackagesPath)\$(CurrentPackageIdLower)&quot; ( echo Deleting $(NuGetPackagesPath)\$(CurrentPackageIdLower) &amp;&amp; rd /s /q &quot;$(NuGetPackagesPath)\$(CurrentPackageIdLower)&quot; )"
19+
ContinueOnError="true" />
20+
21+
<!-- Linux/Mac -->
22+
<Exec Condition="'$(OS)' != 'Windows_NT'"
23+
Command="if [ -d &quot;$(NuGetPackagesPath)/$(CurrentPackageIdLower)&quot; ]; then echo &quot;Deleting $(NuGetPackagesPath)/$(CurrentPackageIdLower)&quot;; rm -rf &quot;$(NuGetPackagesPath)/$(CurrentPackageIdLower)&quot;; fi"
24+
ContinueOnError="true" />
25+
26+
<Message Text="Finished clearing local NuGet package cache for $(CurrentPackageId)." Importance="high" />
27+
</Target>
28+
629
</Project>

0 commit comments

Comments
 (0)