Skip to content

Commit

Permalink
[Resizetizer] Do not update images if they have not been updated. (#1…
Browse files Browse the repository at this point in the history
…3224)

* [Resizetizer] Do not update images if they have not been updated.

Rather than generating the images on every build we should
check to see if the source has changed before calling the
expensive generation processes. This seems to take about 150-200ms
off an incremental build when only one image file was changed.

* Added Unit Tests. Added support for iOS and Windows

* Updated based on feedback

* Rework

* Rework

* Thank you for your pull request. We are auto-formating your source code to follow our code guidelines.

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
  • Loading branch information
dellis1972 and GitHub Actions Autoformatter authored Feb 14, 2023
1 parent 857950e commit 8e32da6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
35 changes: 29 additions & 6 deletions src/SingleProject/Resizetizer/src/AndroidAdaptiveIconGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

Expand Down Expand Up @@ -52,7 +53,7 @@ public IEnumerable<ResizedImageInfo> Generate()
void ProcessBackground(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath)
{
var backgroundFile = Info.Filename;
var backgroundExists = File.Exists(backgroundFile);
var (backgroundExists, backgroundModified) = Utils.FileExists(backgroundFile);
var backgroundDestFilename = AppIconName + "_background.png";

if (backgroundExists)
Expand All @@ -64,8 +65,16 @@ void ProcessBackground(List<ResizedImageInfo> results, DirectoryInfo fullInterme
{
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path);
var destination = Path.Combine(dir, backgroundDestFilename);
var (destinationExists, destinationModified) = Utils.FileExists(destination);
Directory.CreateDirectory(dir);

if (destinationModified > backgroundModified)
{
Logger.Log($"Skipping `{backgroundFile}` => `{destination}` file is up to date.");
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination });
continue;
}

Logger.Log($"App Icon Background Part: " + destination);

if (backgroundExists)
Expand All @@ -88,7 +97,7 @@ void ProcessBackground(List<ResizedImageInfo> results, DirectoryInfo fullInterme
void ProcessForeground(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath)
{
var foregroundFile = Info.ForegroundFilename;
var foregroundExists = File.Exists(foregroundFile);
var (foregroundExists, foregroundModified) = Utils.FileExists(foregroundFile);
var foregroundDestFilename = AppIconName + "_foreground.png";

if (foregroundExists)
Expand All @@ -100,8 +109,16 @@ void ProcessForeground(List<ResizedImageInfo> results, DirectoryInfo fullInterme
{
var dir = Path.Combine(fullIntermediateOutputPath.FullName, dpi.Path);
var destination = Path.Combine(dir, foregroundDestFilename);
var (destinationExists, destinationModified) = Utils.FileExists(destination);
Directory.CreateDirectory(dir);

if (destinationModified > foregroundModified)
{
Logger.Log($"Skipping `{foregroundFile}` => `{destination}` file is up to date.");
results.Add(new ResizedImageInfo { Dpi = dpi, Filename = destination });
continue;
}

Logger.Log($"App Icon Foreground Part: " + destination);

if (foregroundExists)
Expand All @@ -123,14 +140,20 @@ void ProcessForeground(List<ResizedImageInfo> results, DirectoryInfo fullInterme

void ProcessAdaptiveIcon(List<ResizedImageInfo> results, DirectoryInfo fullIntermediateOutputPath)
{
var adaptiveIconXmlStr = AdaptiveIconDrawableXml
.Replace("{name}", AppIconName);

var dir = Path.Combine(fullIntermediateOutputPath.FullName, "mipmap-anydpi-v26");
var adaptiveIconDestination = Path.Combine(dir, AppIconName + ".xml");
var adaptiveIconRoundDestination = Path.Combine(dir, AppIconName + "_round.xml");
Directory.CreateDirectory(dir);

if (File.Exists(adaptiveIconDestination) && File.Exists(adaptiveIconRoundDestination)) {
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1), Filename = adaptiveIconDestination });
results.Add(new ResizedImageInfo { Dpi = new DpiPath("mipmap-anydpi-v26", 1, "_round"), Filename = adaptiveIconRoundDestination });
return;
}

var adaptiveIconXmlStr = AdaptiveIconDrawableXml
.Replace("{name}", AppIconName);

// Write out the adaptive icon xml drawables
File.WriteAllText(adaptiveIconDestination, adaptiveIconXmlStr);
File.WriteAllText(adaptiveIconRoundDestination, adaptiveIconXmlStr);
Expand Down
11 changes: 11 additions & 0 deletions src/SingleProject/Resizetizer/src/AppleIconAssetsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public IEnumerable<ResizedImageInfo> Generate()
var assetContentsFile = Path.Combine(outputAssetsDir, "Contents.json");
var appIconSetContentsFile = Path.Combine(outputAppIconSetDir, "Contents.json");

var (sourceExists, sourceModified) = Utils.FileExists(Info.Filename);
var (destinationExists, destinationModified) = Utils.FileExists(appIconSetContentsFile);

if (destinationModified > sourceModified)
{
Logger.Log($"Skipping `{Info.Filename}` => `{appIconSetContentsFile}` file is up to date.");
return new List<ResizedImageInfo> {
new ResizedImageInfo { Dpi = new DpiPath("", 1), Filename = appIconSetContentsFile }
};
}

var infoJsonProp = new JsonObject
{
["info"] = new JsonObject
Expand Down
8 changes: 8 additions & 0 deletions src/SingleProject/Resizetizer/src/ResizetizeImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,17 @@ void ProcessAppIcon(ResizeImageInfo img, ConcurrentBag<ResizedImageInfo> resized

var destination = Resizer.GetRasterFileDestination(img, dpi, IntermediateOutputPath)
.Replace("{name}", appIconName);
var (sourceExists, sourceModified) = Utils.FileExists(img.Filename);
var (destinationExists, destinationModified) = Utils.FileExists(destination);

LogDebugMessage($"App Icon Destination: " + destination);

if (destinationModified > sourceModified)
{
Logger.Log($"Skipping `{img.Filename}` => `{destination}` file is up to date.");
continue;
}

appTool.Resize(dpi, destination);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/SingleProject/Resizetizer/src/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Text.RegularExpressions;
using SkiaSharp;

Expand Down Expand Up @@ -49,5 +50,12 @@ public static bool IsValidResourceFilename(string filename)

return null;
}

public static (bool Exists, DateTime Modified) FileExists(string path)
{
var exists = File.Exists(path);
var modified = exists ? File.GetLastWriteTimeUtc(path) : System.DateTime.MinValue;
return (exists, modified);
}
}
}
12 changes: 11 additions & 1 deletion src/SingleProject/Resizetizer/src/WindowsIconGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using SkiaSharp;

namespace Microsoft.Maui.Resizetizer
Expand Down Expand Up @@ -27,11 +28,20 @@ public ResizedImageInfo Generate()
string destination = Path.Combine(destinationFolder, $"{fileName}.ico");
Directory.CreateDirectory(destinationFolder);

var (sourceExists, sourceModified) = Utils.FileExists(Info.Filename);
var (destinationExists, destinationModified) = Utils.FileExists(destination);

Logger.Log($"Generating ICO: {destination}");

var tools = new SkiaSharpAppIconTools(Info, Logger);
var dpi = new DpiPath(fileName, 1.0m, size: new SKSize(64, 64));

if (destinationModified > sourceModified)
{
Logger.Log($"Skipping `{Info.Filename}` => `{destination}` file is up to date.");
return new ResizedImageInfo { Dpi = dpi, Filename = destination };
}

MemoryStream memoryStream = new MemoryStream();
tools.Resize(dpi, destination, () => memoryStream);
memoryStream.Position = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,36 @@ public void ShouldResize(string filename, string baseSize, bool resize)
var size = ResizeImageInfo.Parse(item);
Assert.Equal(resize, size.Resize);
}

[Theory]
[InlineData("android")]
[InlineData("uwp")]
[InlineData("ios")]
public void GenerationSkippedOnIncrementalBuild(string platform)
{
var items = new[]
{
new TaskItem("images/dotnet_logo.svg", new Dictionary<string, string>
{
["IsAppIcon"] = bool.TrueString,
["ForegroundFile"] = $"images/dotnet_foreground.svg",
["Link"] = "appicon",
["BackgroundFile"] = $"images/dotnet_background.svg",
}),
};

var task = GetNewTask(platform, items);
var success = task.Execute();
Assert.True(success, LogErrorEvents.FirstOrDefault()?.Message);

LogErrorEvents.Clear();
LogMessageEvents.Clear();
task = GetNewTask(platform, items);
success = task.Execute();
Assert.True(success, LogErrorEvents.FirstOrDefault()?.Message);

Assert.True(LogMessageEvents.Any(x => x.Message.Contains("Skipping ", StringComparison.OrdinalIgnoreCase)), $"Image generation should have been skipped.");
}
}
}
}

0 comments on commit 8e32da6

Please sign in to comment.