Skip to content

Commit

Permalink
Merge pull request #257 from unoplatform/pj/svg-clean-up-improvements
Browse files Browse the repository at this point in the history
Svg clean up improvements
  • Loading branch information
dansiegel authored May 6, 2024
2 parents 7e52601 + 826a90b commit bc176f8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,20 @@
</Content>
</ItemGroup>

<ItemGroup>
<UnoImage Remove="Assets\Svg\*.svg" />
<Content Update="Assets\Svg\*.svg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>


<Target Name="ValidateTransientLocalAssets" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<PropertyGroup>
<_ResizetizerAppIconImagesPath>$(_UnoIntermediateImages)Images\dotnet_bot.scale-200.png</_ResizetizerAppIconImagesPath>
<_SvgContent>$(OutputPath)Assets\Svg\back.svg</_SvgContent>
</PropertyGroup>
<Error Condition="!Exists('$(_ResizetizerAppIconImagesPath)')" Text="image file wasn't created." />
<Error Condition="!Exists('$(_SvgContent)')" Text="Svg file wasn't in place." />
</Target>
</Project>
3 changes: 2 additions & 1 deletion src/.nuspec/Uno.Resizetizer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@
BeforeTargets="_UnoAddAssetsFilesToCopyLocal"
AfterTargets="ContentFilesProjectOutputGroup">

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

Expand Down
35 changes: 15 additions & 20 deletions src/Resizetizer/src/RemoveSvgFromContentTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Uno.Resizetizer;

Expand All @@ -12,6 +11,8 @@ public class RemoveSvgFromContentTask_v0 : Task
[Required]
public ITaskItem[] CollectionToRemove { get; set; } = Array.Empty<ITaskItem>();

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

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

Expand All @@ -20,9 +21,9 @@ public override bool Execute()
try
{
#if DEBUG_RESIZETIZER
//System.Diagnostics.Debugger.Launch();
System.Diagnostics.Debugger.Launch();
#endif
RemovedItems = RemoveDuplicateSvgAndPngFiles(CollectionToRemove);
RemovedItems = RemoveUnoImagesSvgFromContent();
return true;
}
catch (Exception ex)
Expand All @@ -32,32 +33,26 @@ public override bool Execute()
}
}

static ITaskItem[] RemoveDuplicateSvgAndPngFiles(ITaskItem[] assets)
ITaskItem[] RemoveUnoImagesSvgFromContent()
{
var list = new List<ITaskItem>();
var count = assets.Length;
for (var i = 0; i < count; i++)
foreach (var asset in CollectionToRemove)
{
var item = assets[i].ItemSpec;
var extension = Path.GetExtension(item) ?? string.Empty;
if (!extension.Equals(".svg", StringComparison.OrdinalIgnoreCase))
var extension2 = Path.GetExtension(asset.ItemSpec) ?? string.Empty;
var isSvg = extension2.Equals(".svg", StringComparison.OrdinalIgnoreCase);

if (string.IsNullOrEmpty(extension2) || !isSvg)
{
continue;
}
var svgFileName = Path.GetFileNameWithoutExtension(item);

foreach (var item2 in assets)
var assetFullPath = asset.GetMetadata("fullpath");
foreach (var unoImage in UnoImages)
{
var extension2 = Path.GetExtension(item2.ItemSpec) ?? string.Empty;
if (extension2.Equals(".svg", StringComparison.OrdinalIgnoreCase)
|| !extension2.Equals(".png", StringComparison.CurrentCultureIgnoreCase))
{
continue;
}

if (svgFileName == Path.GetFileNameWithoutExtension(item2.ItemSpec))
var fullPath = unoImage.GetMetadata("fullpath");
if (fullPath == assetFullPath)
{
list.Add(assets[i]);
list.Add(asset);
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/Resizetizer/test/UnitTests/RemoveSvgFromContentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xunit;

namespace Uno.Resizetizer.Tests
{
public class RemoveSvgFromContentTests : MSBuildTaskTestFixture<RemoveSvgFromContentTask_v0>
{
RemoveSvgFromContentTask_v0 GetNewTask(ITaskItem[] collectionToRemove, ITaskItem[] unoImages)
{
return new()
{
CollectionToRemove = collectionToRemove,
UnoImages = unoImages
};
}


[Fact]
public void RemoveSvgContentIfItIsUnoImage()
{
var assetPath = "Assets/Icons/back.svg";
var unoImage = new TaskItem(assetPath);
var content = new TaskItem(assetPath);

var task = GetNewTask(new[] { content }, new[] { unoImage });

var success = task.Execute();


Assert.True(success);
Assert.True(task.RemovedItems.Length > 0);
}

[Fact]
public void DoNotRemoveSvgIfHasSameNameButInDifferentLocation()
{
var unoImage = new TaskItem("Assets/Icons/back.svg");
var content = new TaskItem("Assets/SVG/back.svg");

var task = GetNewTask(new[] { content }, new[] { unoImage });

var success = task.Execute();


Assert.True(success);
Assert.True(task.RemovedItems.Length is 0);
}
}
}

0 comments on commit bc176f8

Please sign in to comment.