uSync files default to Content CopyIfNewer. Change to "only for publish".
Is your feature request related to a problem? Please describe.
When there is a large amount of uSync files, like thousands of content items, the build time of projects increase by an order of magnitude. It is totally unnecessary to copy the uSync files to the output folders of regular builds. Not to mention the effect being transitive for dependent projects like integration tests.
I just shaved 90 seconds off a solution build (Only 30 sec now) by only allowing copying the uSync files on publish.
Describe the solution you'd like
Prevent uSync from copying files unless the web project is being published, while allowing consumers to override the behavior.
Describe alternatives you've considered
Add a .targets file in the uSync package that prevents copying on regular builds.
For instance:
uSync.targets:
<Project>
<PropertyGroup>
<!-- Allow users to disable the default uSync publish behavior -->
<EnableUSyncPublishDefaults Condition="'$(EnableUSyncPublishDefaults)' == ''">true</EnableUSyncPublishDefaults>
</PropertyGroup>
<ItemGroup Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Content Remove="uSync/**"/>
</ItemGroup>
<Target Name="CollectUSyncFilesForPublish" BeforeTargets="AddUSyncFilesToPublishedItems" Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Message Text="Collecting the initial files" Importance="High"/>
<ItemGroup>
<USyncFilesToCopy Include="uSync/**" />
</ItemGroup>
</Target>
<Target Name="AddUSyncFilesToPublishedItems" BeforeTargets="GetCopyToPublishDirectoryItems" Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Message Text="Adding the files we've collected to the ContentWithTargetPath group" Importance="High"/>
<ItemGroup>
<ContentWithTargetPath Include="@(USyncFilesToCopy)" TargetPath="%(Identity)" CopyToOutputDirectory="Never" CopyToPublishDirectory="Always" />
</ItemGroup>
</Target>
</Project>
Consumer .csproj:
<Target Name="RemoveUSyncFilesWeDontWant"
AfterTargets="CollectUSyncFilesForPublish"
BeforeTargets="AddUSyncFilesToPublishedItems"
Condition="'$(EnableUSyncPublishDefaults)' == 'true'"
>
<Message Text="Removing the files we don't want" Importance="High"/>
<ItemGroup>
<USyncFilesToCopy Remove="uSync/v*/Content*/**" />
<USyncFilesToCopy Remove="uSync/v*/Media*/**" />
</ItemGroup>
</Target>
The above targets have been tested in a single .csproj, but should work fine if put in a buildTransient/uSync.targets file in the package.
Hi,
Yeah - i think we can achive this with a simple target file.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Content Update="uSync\**" >
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
Then the uSync folder never makes it to the output folders, but always makes it to publish, I think that would mean as expected behavior will continue for everyone (e.g the usync folder in the site is used during development).
still just checking this, (because .target files are the WORST!). but i think that does it.
also , adding this directly to your .csproj now does the same thing.
Might be way simpler than I'm thinking, but I love to be able to modify "forced" targets and behavior from packages. I tried the following in the main web .csproj, and it seems to behave correctly if I toggle the "enable" property, and my "exclude content and media after all" target executes just in time to remove those two:
<PropertyGroup>
<!-- Allow users to disable the default uSync publish behavior -->
<EnableUSyncPublishDefaults Condition="'$(EnableUSyncPublishDefaults)' == ''">true</EnableUSyncPublishDefaults>
</PropertyGroup>
<ItemGroup Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Content Remove="uSync/**"/>
</ItemGroup>
<Target Name="CollectUSyncFilesForPublish" BeforeTargets="AddUSyncFilesToPublishedItems" Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Message Text="Collecting the initial files" Importance="High"/>
<ItemGroup>
<USyncFilesToCopy Include="uSync/**" />
</ItemGroup>
</Target>
<Target Name="AddUSyncFilesToPublishedItems" BeforeTargets="GetCopyToPublishDirectoryItems" Condition="'$(EnableUSyncPublishDefaults)' == 'true'">
<Message Text="Adding the files we've collected to the ContentWithTargetPath group" Importance="High"/>
<ItemGroup>
<ContentWithTargetPath Include="@(USyncFilesToCopy)" TargetPath="%(Identity)" CopyToOutputDirectory="Never" CopyToPublishDirectory="Always" />
</ItemGroup>
</Target>
<!-- Consumer overrides default behavior -->
<Target Name="RemoveUSyncFilesWeDontWant"
AfterTargets="CollectUSyncFilesForPublish"
BeforeTargets="AddUSyncFilesToPublishedItems"
Condition="'$(EnableUSyncPublishDefaults)' == 'true'"
>
<Message Text="Removing the files we don't want" Importance="High"/>
<ItemGroup>
<USyncFilesToCopy Remove="uSync/v*/Content*/**" />
<USyncFilesToCopy Remove="uSync/v*/Media*/**" />
</ItemGroup>
</Target>
Though it might be possible to just add <Content Remove="uSync/v*/Content/**" CopyToPublishDirectory="Never"/> if you remove the files from content to output by default.
I think, you could add those two as item groups updates wthout the need for the <target> elements in the files.
with the PR we are now not including "uSync" files in build but we do include the uSync folder in publish
then In your .csproj you can add additional remove rules if you don't want certain folders. e.g.
<ItemGroup>
<Content Remove="uSync\**\Content\**" />
<Content Remove="uSync\**\Media\**" />
</ItemGroup>
works (just tried it, and this means published folder doesn't get the content or media folders.
Nightly build :
https://dev.azure.com/jumoo/Public/_artifacts/feed/nightly/NuGet/uSync/overview/16.1.0-build.20251021.21
*(yes .21 because thats how much of a pain .target files are 😄 )