Skip to content

Commit

Permalink
Fixed the package icon task!
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Jan 26, 2024
1 parent bedebf5 commit ce6b798
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
26 changes: 3 additions & 23 deletions src/Build/PackageIcon.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,10 @@
-->

<Project>
<!-- <Import Project="PackageIcon.props" Condition="'$(PackageIcon_props)' == ''" /> -->
<UsingTask TaskName="Dgmjr.Sdk.Tasks.EnsurePackageIcon" AssemblyFile="$(DgmjrSdkDll)" />
<PropertyGroup>
<PackageIconFile Condition="'$(PackageIcon)' == '' And '$(PackageIconFile)' == '' And !Exists('$(PackageIcon)') And Exists('$(MSBuildProjectDirectory)/Icon.jpg')">$(MSBuildProjectDirectory)/Icon.jpg</PackageIconFile>
<PackageIconFile Condition="'$(PackageIcon)' == '' And '$(PackageIconFile)' == '' And !Exists('$(PackageIcon)') And Exists('$(MSBuildProjectDirectory)/Icon.jpeg')">$(MSBuildProjectDirectory)/Icon.jpeg</PackageIconFile>
<PackageIconFile Condition="'$(PackageIcon)' == '' And '$(PackageIconFile)' == '' And !Exists('$(PackageIcon)') And Exists('$(MSBuildProjectDirectory)/Icon.png')">$(MSBuildProjectDirectory)/Icon.png</PackageIconFile>
<PackageIconFile Condition="'$(PackageIcon)' == '' And '$(PackageIconFile)' == '' And !Exists('$(PackageIcon)') And Exists($(FallbackPackageIconFilePath))">$(FallbackPackageIconFilePath)</PackageIconFile>
</PropertyGroup>
<Target Name="EnsurePackageIcon" BeforeTargets="GetPackageMetadata" Condition="'$(IsOuterBuild)' != 'false'">
<EnsurePackageIcon MSBuildProjectFullPath="$(MSBuildProjectFullPath)" />
<!-- copy the icon file from wherever it is to ./Icon.(png/jpg/jpeg) -->
<!-- <Copy SourceFiles="$(PackageIconFile)" DestinationFiles="Icon.png" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.png')) And !Exists('$(MSBuildProjectDirectory)Icon.png')" />
<Copy SourceFiles="$(PackageIconFile)" DestinationFiles="Icon.jpg" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpg')) And !Exists('$(MSBuildProjectDirectory)Icon.jpg')" />
<Copy SourceFiles="$(PackageIconFile)" DestinationFiles="Icon.jpeg" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpeg')) And !Exists('$(MSBuildProjectDirectory)Icon.jpeg')" />
<ItemGroup>
<PackageFile Include="$(PackageIconFile)" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.png'))" PackagePath="Icon.png" Pack="true" IsPackageIcon="true" />
<PackageFile Include="$(PackageIconFile)" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpeg'))" PackagePath="Icon.jpeg" Pack="true" IsPackageIcon="true" />
<PackageFile Include="$(PackageIconFile)" Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpg'))" PackagePath="Icon.jpg" Pack="true" IsPackageIcon="true" />
</ItemGroup>
<PropertyGroup>
<PackageIcon Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.png'))">Icon.png</PackageIcon>
<PackageIcon Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpg'))">Icon.jpg</PackageIcon>
<PackageIcon Condition="Exists($(PackageIconFile)) And $(PackageIconFile.EndsWith('.jpeg'))">Icon.jpeg</PackageIcon>
</PropertyGroup>
<Message Text="PackageIcon: $(PackageIconFile)" /> -->
<EnsurePackageIcon MSBuildProjectFullPath="$(MSBuildProjectFullPath)">
<Output TaskParameter="PackageIcon" PropertyName="PackageIcon" />
</EnsurePackageIcon>
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/DgmjrSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<PackageReference Include="MSBuild.Usings" IncludeAssets="Build;BuildTransitive;BuildMultitargeting;Runtime;Compile" ExcludeAssets="ContentFiles;Native;Analyzers" PrivateAssets="None" />
</ItemGroup>
<ItemGroup>
<PackageFile Include="./bin/Local/$(TargetFramework)/*.dll" Pack="true" PackagePath="lib/$(TargetFramework)/%(Filename)%(Extension)" />
<PackageFile Include="./$(OutputPath)/**/*.dll" Pack="true" PackagePath="lib/%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<Using Include="System.Collections.Generic.IDictionary&lt;string,string&gt;" Alias="IStringDictionary" />
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/EnsureLicenseFileExists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EnsureLicenseFileExists : MSBTask

private string LicenseFilePath => Combine(ProjectDirectory, LICENSE_MD);

protected virtual Stream LicenseFileContent =>
protected virtual Stream OpenLicenseFileStream() =>
typeof(EnsureLicenseFileExists).Assembly.GetManifestResourceStream(LICENSE_MD);

[Required]
Expand All @@ -23,7 +23,7 @@ public override bool Execute()
$"License file '{LICENSE_MD}' not found in project directory: {ProjectDirectory}; adding it."
);
using var fs = File.Create(LicenseFilePath);
using var licenseFileStream = LicenseFileContent;
using var licenseFileStream = OpenLicenseFileStream();
licenseFileStream.CopyTo(fs);
}

Expand Down
35 changes: 24 additions & 11 deletions src/Tasks/EnsurePackageIcon.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Internal;

namespace Dgmjr.Sdk.Tasks;

public class EnsurePackageIcon : MSBTask
Expand All @@ -6,34 +8,45 @@ public class EnsurePackageIcon : MSBTask
private const string _png = ".png";
private const string _jpg = ".jpg";
private const string _svg = ".svg";
private static readonly string[] _extensions = { _png, _jpg, _svg };

const string DEFAULT_PACKAGE_ICON_PNG = "DEFAULT_PACKAGE_ICON.png";
private string ProjectDirectory => GetDirectoryName(MSBuildProjectFullPath);

private string PackageIconPath => Combine(ProjectDirectory, Icon);

protected virtual string? GetPackageIconPath() =>
_extensions
.Select(ext => Combine(ProjectDirectory, $"{Icon}{ext}"))
.FirstOrDefault(File.Exists);

[Output]
public string PackageIconFileName { get; }
public string? PackageIcon => GetFileName(GetPackageIconPath());

[Required]
public string MSBuildProjectFullPath { get; set; }

protected virtual string DefaultPackageIconExtension => _png;

protected Stream OpenCreateIconStream() =>
Create(Combine(ProjectDirectory, $"{Icon}{DefaultPackageIconExtension}"));

protected virtual Stream OpenDefaultIconStream() =>
typeof(EnsurePackageIcon).Assembly.GetManifestResourceStream(DEFAULT_PACKAGE_ICON_PNG);

public override bool Execute()
{
if (
!Exists(PackageIconPath + _png)
&& !Exists(PackageIconPath + _jpg)
&& !Exists(PackageIconPath + _svg)
)
if (PackageIcon is null)
{
Log.LogWarning(
$"Package icon '{Icon}{_png}/{Icon}{_jpg}' not found in project directory: {ProjectDirectory}; adding it as {Icon}{_png}."
$"Package icon '{Icon}({_png}/{_jpg}/{_svg}' not found in project directory: {ProjectDirectory}; adding it as {Icon}{DefaultPackageIconExtension}."
);
using var fs = Create(PackageIconPath + _png);
using var manifestStream = typeof(EnsurePackageIcon).Assembly.GetManifestResourceStream(
DEFAULT_PACKAGE_ICON_PNG
using var fs = OpenCreateIconStream();
using var defaultIconStream = OpenDefaultIconStream();
defaultIconStream.CopyTo(fs);
Log.LogMessage(
$"Added {Icon}{DefaultPackageIconExtension} to project directory: {ProjectDirectory}"
);
manifestStream.CopyTo(fs);
}

return true;
Expand Down

0 comments on commit ce6b798

Please sign in to comment.