Skip to content

Commit

Permalink
Improve targets/tasks
Browse files Browse the repository at this point in the history
- Consolidate targets into single target
- Add inputs file to invalidate builds when csproj metadata changes
  • Loading branch information
Redth committed Mar 4, 2020
1 parent f17b012 commit e9f07b9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 94 deletions.
15 changes: 9 additions & 6 deletions Resizetizer.NT/Resizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static string GetFileDestination(SharedImageInfo info, DpiPath dpi, strin
return destination;
}

public static string CopyFile(SharedImageInfo info, DpiPath dpi, string intermediateOutputPath, ILogger logger, bool isAndroid = false)
public static string CopyFile(SharedImageInfo info, DpiPath dpi, string intermediateOutputPath, string inputsFile, ILogger logger, bool isAndroid = false)
{
var destination = Resizer.GetFileDestination(info, dpi, intermediateOutputPath);
var androidVector = false;
Expand All @@ -54,7 +54,7 @@ public static string CopyFile(SharedImageInfo info, DpiPath dpi, string intermed
androidVector = true;
}

if (IsUpToDate(info.Filename, destination, logger))
if (IsUpToDate(info.Filename, destination, inputsFile, logger))
return destination;

if (androidVector)
Expand All @@ -71,12 +71,15 @@ public static string CopyFile(SharedImageInfo info, DpiPath dpi, string intermed
return destination;
}

static bool IsUpToDate(string inputFile, string outputFile, ILogger logger)
static bool IsUpToDate(string inputFile, string outputFile, string inputsFile, ILogger logger)
{
var fileIn = new FileInfo(inputFile);
var fileOut = new FileInfo(outputFile);
var fileInputs = new FileInfo(inputsFile);

if (fileIn.Exists && fileOut.Exists && fileIn.LastWriteTimeUtc <= fileOut.LastWriteTimeUtc)
if (fileIn.Exists && fileOut.Exists && fileInputs.Exists
&& fileIn.LastWriteTimeUtc <= fileOut.LastWriteTimeUtc
&& fileInputs.LastWriteTimeUtc <= fileOut.LastWriteTimeUtc)
{
logger.Log($"Skipping '{inputFile}' as output '{outputFile}' is already up to date.");
return true;
Expand All @@ -85,7 +88,7 @@ static bool IsUpToDate(string inputFile, string outputFile, ILogger logger)
return false;
}

public string Resize(DpiPath dpi)
public string Resize(DpiPath dpi, string inputsFile)
{
Logger?.Log($"Resizing: {Info.Filename}");

Expand All @@ -94,7 +97,7 @@ public string Resize(DpiPath dpi)
if (Info.IsVector)
destination = Path.ChangeExtension(destination, ".png");

if (IsUpToDate(Info.Filename, destination, Logger))
if (IsUpToDate(Info.Filename, destination, inputsFile, Logger))
return destination;

if (Info.IsVector)
Expand Down
6 changes: 4 additions & 2 deletions Resizetizer.NT/ResizetizeSharedImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ResizetizeSharedImages : Task, ILogger
[Required]
public string IntermediateOutputPath { get; set; }

public string InputsFile { get; set; }

public ITaskItem[] SharedImages { get; set; }

[Output]
Expand Down Expand Up @@ -55,15 +57,15 @@ public override bool Execute()

foreach (var dpi in dpis)
{
var r = resizer.Resize(dpi);
var r = resizer.Resize(dpi, InputsFile);
resizedImages.Add(r);
}
}
else
{
op = "Copy";
// Otherwise just copy the thing over to the 1.0 scale
var dest = Resizer.CopyFile(img, originalScaleDpi, IntermediateOutputPath, this, PlatformType.ToLower().Equals("android"));
var dest = Resizer.CopyFile(img, originalScaleDpi, IntermediateOutputPath, InputsFile, this, PlatformType.ToLower().Equals("android"));
resizedImages.Add(dest);
}

Expand Down
159 changes: 73 additions & 86 deletions Resizetizer.NT/Resizetizer.NT.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</ItemGroup>

<UsingTask
AssemblyFile="Resizetizer.NT.dll"
TaskName="Resizetizer.ResizetizeSharedImages" />
AssemblyFile="Resizetizer.NT.dll"
TaskName="Resizetizer.ResizetizeSharedImages" />

<PropertyGroup>
<CleanDependsOn>
Expand All @@ -19,20 +19,52 @@
<_ResizetizerIsiOS Condition="'$(TargetFrameworkIdentifier)'=='Xamarin.iOS' And ('$(OutputType)' != 'Library' OR '$(IsAppExtension)'=='True')">True</_ResizetizerIsiOS>
<_ResizetizerIsUWP Condition="'$(TargetPlatformIdentifier)'=='UAP' And '$(OutputType)' == 'AppContainerExe'">True</_ResizetizerIsUWP>

<ResizetizeCollectImagesBeforeTargets Condition="'$(_ResizetizerIsiOS)' == 'True'">
<_ResizetizerInputsFile>$(IntermediateOutputPath)resizetizer.inputs</_ResizetizerInputsFile>
</PropertyGroup>

<!-- iOS -->
<PropertyGroup Condition="'$(_ResizetizerIsiOS)' == 'True'">
<ResizetizerPlatformType>ios</ResizetizerPlatformType>

<ResizetizeCollectImagesBeforeTargets>
$(ResizetizeCollectImagesBeforeTargets);
_CollectBundleResources;
</ResizetizeCollectImagesBeforeTargets>

<ResizetizeCollectImagesAfterTargets Condition="'$(_ResizetizerIsAndroid)' == 'True'">
<ResizetizeAfterTargets>
$(ResizetizeAfterTargets);
ResizetizeCollectImages;
</ResizetizeAfterTargets>
</PropertyGroup>

<!-- Android -->
<PropertyGroup Condition="'$(_ResizetizerIsAndroid)' == 'True'">
<ResizetizerPlatformType>android</ResizetizerPlatformType>

<ResizetizeCollectImagesAfterTargets>
$(ResizetizeCollectImagesBeforeTargets);
ResolveProjectReferences;
</ResizetizeCollectImagesAfterTargets>

<!--<_CollectBundleResourcesDependsOn Condition="'$(_ResizetizerIsiOS)' == 'True'">
<ResizetizeAfterTargets>
$(ResizetizeAfterTargets);
ResizetizeCollectImages;
</ResizetizeAfterTargets>
</PropertyGroup>

<!-- UWP -->
<PropertyGroup Condition="'$(_ResizetizerIsUWP)' == 'True'">
<ResizetizerPlatformType>uwp</ResizetizerPlatformType>

<ResizetizeDependsOnTargets>
$(ResizetizeDependsOnTargets);
ResizetizeCollectImages;
$(_CollectBundleResourcesDependsOn);
</_CollectBundleResourcesDependsOn>-->
</ResizetizeDependsOnTargets>

<ResizetizeBeforeTargets>
$(ResizetizeBeforeTargets);
AssignTargetPaths;
</ResizetizeBeforeTargets>
</PropertyGroup>

<!-- Finds absolute paths to any SharedImage in this project -->
Expand Down Expand Up @@ -61,23 +93,35 @@
TaskParameter="TargetOutputs"
ItemName="SharedImage" />
</MSBuild>

<!-- Write out item spec and metadata to a file we can use as an inputs for the resize target -->
<!-- This allows us to invalidate the build based on not just input image files changing but project item metadata as well -->
<WriteLinesToFile
File="$(_ResizetizerInputsFile)"
Lines="@(SharedImage->'File=%(Identity);BaseSize=%(BaseSize)')"
Overwrite="true"
WriteOnlyWhenDifferent="true" />

</Target>


<Target Name="ResizetizeiOS"
Condition="'$(_ResizetizerIsiOS)' == 'True'"
Inputs="@(SharedImage)"
Outputs="$(IntermediateOutputPath)resizetizer.stamp"
AfterTargets="ResizetizeCollectImages">
<Target Name="ResizetizeImages"
Inputs="@(SharedImage);$(_ResizetizerInputsFile)"
Outputs="$(IntermediateOutputPath)resizetizer.stamp"
AfterTargets="$(ResizetizeAfterTargets)"
BeforeTargets="$(ResizetizeBeforeTargets)"
DependsOnTargets="$(ResizetizeDependsOnTargets)">

<!-- Where in obj/ to store these -->
<PropertyGroup>
<ResizetizerIntermediateOutputPath>$(IntermediateOutputPath)resizetizer\</ResizetizerIntermediateOutputPath>
</PropertyGroup>

<!-- Resize the images -->
<ResizetizeSharedImages
PlatformType="ios"
PlatformType="$(ResizetizerPlatformType)"
IntermediateOutputPath="$(ResizetizerIntermediateOutputPath)"
InputsFile="$(_ResizetizerInputsFile)"
SharedImages="@(SharedImage)">
</ResizetizeSharedImages>

Expand All @@ -86,7 +130,10 @@
<!-- Either from the task, or if the task was skipped (up to date), use the wildcard lookup -->
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' != ''" Include="@(CopiedResources)" />
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' == ''" Include="$(ResizetizerIntermediateOutputPath)**\*" />
</ItemGroup>

<!-- iOS -->
<ItemGroup Condition="'$(_ResizetizerIsiOS)' == 'True'">
<!-- Batch the collectd items into BundleResource which iOS expects -->
<_ResizetizerCollectedBundleResourceImages Include="@(_ResizetizerCollectedImages->'%(FullPath)')">
<LogicalName>%(_ResizetizerCollectedImages.Filename)%(_ResizetizerCollectedImages.Extension)</LogicalName>
Expand All @@ -95,104 +142,44 @@

<!-- iOS Expects images in this group -->
<BundleResource Include="@(_ResizetizerCollectedBundleResourceImages)" />

<FileWrites Include="@(_ResizetizerCollectedBundleResourceImages)" />
</ItemGroup>

<!-- iOS Only -->
<!-- If on Windows, using build host, copy the files over to build server host too -->
<CopyFilesToBuildServer
Condition="'$(IsMacEnabled)'=='true'"
Condition="'$(_ResizetizerIsiOS)' == 'True' And '$(IsMacEnabled)'=='true'"
SessionId="$(BuildSessionId)"
Files="@(_ResizetizerCollectedBundleResourceImages)" />

<!-- Touch/create our stamp file for outputs -->
<Touch Files="$(IntermediateOutputPath)resizetizer.stamp" AlwaysCreate="True" />

<!-- Include our images and stamp file as filewrites so they don't get rm'd -->
<ItemGroup>
<FileWrites Include="@(_ResizetizerCollectedBundleResourceImages)" />
<FileWrites Include="$(IntermediateOutputPath)resizetizer.stamp" />
</ItemGroup>

</Target>


<!-- This should copy images into the app's obj/ area and include them as android resources -->
<Target Name="ResizetizeAndroid"
Condition="'$(_ResizetizerIsAndroid)' == 'True'"
Inputs="@(SharedImage)"
Outputs="$(IntermediateOutputPath)resizetizer.stamp"
AfterTargets="ResizetizeCollectImages">

<!-- Where in obj/ to store these -->
<PropertyGroup>
<ResizetizerIntermediateOutputPath>$(IntermediateOutputPath)resizetizer\</ResizetizerIntermediateOutputPath>
</PropertyGroup>

<ResizetizeSharedImages
PlatformType="android"
IntermediateOutputPath="$(ResizetizerIntermediateOutputPath)"
SharedImages="@(SharedImage)">
<!-- <Output TaskParameter="CopiedResources" ItemName="FileWrites" /> -->
</ResizetizeSharedImages>

<ItemGroup>
<!-- Get Images that were generated -->
<!-- Either from the task, or if the task was skipped (up to date), use the wildcard lookup -->
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' != ''" Include="@(CopiedResources)" />
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' == ''" Include="$(ResizetizerIntermediateOutputPath)**\*" />

<!-- Android -->
<ItemGroup Condition="'$(_ResizetizerIsAndroid)' == 'True'">
<!-- If we had any images, add that obj/ folder as a resource directory -->
<LibraryResourceDirectories Condition="Exists ('$(ResizetizerIntermediateOutputPath)')" Include="$(ResizetizerIntermediateOutputPath)">
<StampFile>$(IntermediateOutputPath)resizetizer.stamp</StampFile>
</LibraryResourceDirectories>
</ItemGroup>

<Touch Files="$(IntermediateOutputPath)resizetizer.stamp" AlwaysCreate="True" />

<ItemGroup>
<FileWrites Include="@(_ResizetizerCollectedImages)" />
<FileWrites Include="$(IntermediateOutputPath)resizetizer.stamp" />
</ItemGroup>
</Target>

<Target Name="ResizetizeUWP"
Condition="'$(_ResizetizerIsUWP)' == 'True'"
Inputs="@(SharedImage)"
Outputs="$(IntermediateOutputPath)resizetizer.stamp"
DependsOnTargets="ResizetizeCollectImages"
BeforeTargets="AssignTargetPaths">

<!-- Where in obj/ to store these -->
<PropertyGroup>
<ResizetizerIntermediateOutputPath>$(IntermediateOutputPath)resizetizer\</ResizetizerIntermediateOutputPath>
</PropertyGroup>

<ResizetizeSharedImages
PlatformType="uwp"
IntermediateOutputPath="$(ResizetizerIntermediateOutputPath)"
SharedImages="@(SharedImage)">
<!-- <Output TaskParameter="CopiedResources" ItemName="FileWrites" /> -->
</ResizetizeSharedImages>

<ItemGroup>
<!-- Get Images that were generated -->
<!-- Either from the task, or if the task was skipped (up to date), use the wildcard lookup -->
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' != ''" Include="@(CopiedResources)" />
<_ResizetizerCollectedImages Condition="'@(CopiedResources)' == ''" Include="$(ResizetizerIntermediateOutputPath)**\*" />

<!-- UWP -->
<ItemGroup Condition="'$(_ResizetizerIsUWP)' == 'True'">
<ContentWithTargetPath Include="@(_ResizetizerCollectedImages)">
<TargetPath>%(_ResizetizerCollectedImages.Filename)%(_ResizetizerCollectedImages.Extension)</TargetPath>
</ContentWithTargetPath>

<FileWrites Include="@(_ResizetizerCollectedImages)" />
</ItemGroup>

<!-- Touch/create our stamp file for outputs -->
<Touch Files="$(IntermediateOutputPath)resizetizer.stamp" AlwaysCreate="True" />

<!-- Include our images and stamp file as filewrites so they don't get rm'd -->
<ItemGroup>
<!-- If we had any images, add that obj/ folder as a resource directory -->
<FileWrites Include="@(_ResizetizerCollectedImages)" />

<FileWrites Include="$(IntermediateOutputPath)resizetizer.stamp" />
</ItemGroup>

</Target>

<Target Name="_CleanResizetizer">
Expand Down

0 comments on commit e9f07b9

Please sign in to comment.