Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove UnoImage from Content and AndroidAsset items #219

Merged
merged 10 commits into from
Mar 27, 2024
43 changes: 43 additions & 0 deletions src/.nuspec/Uno.Resizetizer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.WindowIconGeneratorTask_V0"/>

<UsingTask
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.CleanupAssetsTask_v0"/>

<UsingTask
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.RemoveSvgFromContentTask_v0"/>

<PropertyGroup>
<CleanDependsOn>
$(CleanDependsOn);
Expand Down Expand Up @@ -309,6 +317,41 @@
</Target>


<Target Name="CleanUpContent"
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
BeforeTargets="UnoAssetsGeneration;_UnoAddAssetsFilesToCopyLocal">

<CleanupAssetsTask_v0 UnoImagesCollection="@(UnoImage)"
ContentCollection="@(ContentFilesProjectOutputGroupOutput)"
AndroidAssetCollection="@(AndroidAsset)">
<Output TaskParameter="ContentCollection" ItemName="ContentResult"/>
<Output TaskParameter="AndroidAssetCollection" ItemName="AndroidAssetResult"/>
<Output TaskParameter="RemovedFiles" ItemName="ItemsToNotIncludeOnContentFilesProjectOutputGroup"/>
</CleanupAssetsTask_v0>

<ItemGroup>
<ContentFilesProjectOutputGroupOutput Remove="@(ContentFilesProjectOutputGroupOutput)" />
<ContentFilesProjectOutputGroupOutput Include="@(ContentResult)" />
</ItemGroup>

<ItemGroup Condition="$(_UnoResizetizerPlatformIsAndroid) == 'true'">
<AndroidAsset Remove="@(AndroidAsset)"/>
<AndroidAsset Include="@(AndroidAssetResult)"/>
</ItemGroup>
</Target>

<Target Name="CleanUpContentFilesProjectOutputGroupOutput"
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
BeforeTargets="_UnoAddAssetsFilesToCopyLocal"
AfterTargets="ContentFilesProjectOutputGroup">

<RemoveSvgFromContentTask_v0 CollectionToRemove="@(ContentFilesProjectOutputGroupOutput)">
<Output TaskParameter="RemovedItems" ItemName="FinalResult"/>
</RemoveSvgFromContentTask_v0>

<ItemGroup>
<ContentFilesProjectOutputGroupOutput Remove="@(FinalResult)"/>
</ItemGroup>
</Target>

<!-- Collect images from referenced projects -->
<Target Name="UnoResizetizeCollectItems"
Condition="'$(_UnoResizetizerIsCompatibleApp)' == 'True' And '$(DisableUnoResizetizer)' != 'true'"
Expand Down
69 changes: 69 additions & 0 deletions src/Resizetizer/src/CleanupAssetsTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Uno.Resizetizer;

public class CleanupAssetsTask_v0 : Task
{
[Required]
[Output]
public ITaskItem[] UnoImagesCollection { get; set; } = Array.Empty<ITaskItem>();

[Required]
[Output]
public ITaskItem[] ContentCollection { get; set; } = Array.Empty<ITaskItem>();

// This is just available on Android target, because of that isn't Required
[Output]
public ITaskItem[] AndroidAssetCollection { get; set; } = Array.Empty<ITaskItem>();

[Output]
public ITaskItem[] RemovedFiles { get; set; } = Array.Empty<ITaskItem>();

public override bool Execute()
{
#if DEBUG_RESIZETIZER
//System.Diagnostics.Debugger.Launch();
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
#endif
try
{
var removedItems = new List<ITaskItem>();
ContentCollection = RemoveUnoImageFrom(UnoImagesCollection, ContentCollection, removedItems);

var assetsCount = AndroidAssetCollection.Length;
AndroidAssetCollection = RemoveUnoImageFrom(UnoImagesCollection, AndroidAssetCollection, removedItems);

RemovedFiles = removedItems.ToArray();

return true;
}
catch (Exception ex)
{
Log.LogError($"error: {ex} message: {ex.Message}");
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

static ITaskItem[] RemoveUnoImageFrom(ITaskItem[] unoImages, ITaskItem[] assets, List<ITaskItem> removedItems)
{
var count = assets.Length;

for (var i = 0; i < count; i++)
{
foreach (var unoImage in unoImages)
{
if (assets[i].ItemSpec == unoImage.ItemSpec)
{
removedItems.Add(assets[i]);
assets[i] = null;
break;
}
}
}

return assets.Where(x => x is not null).ToArray();
}
}
68 changes: 68 additions & 0 deletions src/Resizetizer/src/RemoveSvgFromContentTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Uno.Resizetizer;

public class RemoveSvgFromContentTask_v0 : Task
{
[Required]
public ITaskItem[] CollectionToRemove { get; set; } = Array.Empty<ITaskItem>();

[Output]
public ITaskItem[] RemovedItems { get; set; } = Array.Empty<ITaskItem>();

public override bool Execute()
{
try
{
#if DEBUG_RESIZETIZER
System.Diagnostics.Debugger.Launch();
#endif
RemovedItems = RemoveDuplicateSvgAndPngFiles(CollectionToRemove);
return true;
}
catch (Exception ex)
{
Log.LogError($"error: {ex} message: {ex.Message}");
return false;
}
}

static ITaskItem[] RemoveDuplicateSvgAndPngFiles(ITaskItem[] assets)
{
var list = new List<ITaskItem>();
var count = assets.Length;
for (var i = 0; i < count; i++)
{
var item = assets[i].ItemSpec;
var extension = Path.GetExtension(item) ?? string.Empty;
if (!extension.Equals(".svg", StringComparison.CurrentCultureIgnoreCase))
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
{
continue;
}
var svgFileName = Path.GetFileNameWithoutExtension(item);

foreach (var item2 in assets)
{
var extension2 = Path.GetExtension(item2.ItemSpec) ?? string.Empty;
if (extension2.Equals(".svg", StringComparison.CurrentCultureIgnoreCase)
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
|| !extension2.Equals(".png", StringComparison.CurrentCultureIgnoreCase))
{
continue;
}

if (svgFileName == Path.GetFileNameWithoutExtension(item2.ItemSpec))
{
list.Add(assets[i]);
assets[i] = null;
pedrojesus-work marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return list.ToArray();
}
}
Loading