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

Package HasValidTarget #7

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Community.PowerToys.Run.Plugin.Lint/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.Versioning;
using System.Text.RegularExpressions;

namespace Community.PowerToys.Run.Plugin.Lint;
Expand Down Expand Up @@ -37,6 +38,22 @@ public static bool IsZip(this Asset asset)
(asset?.name != null && asset.name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
}

public static bool HasValidTargetFramework(this Package package)
{
return package.AssemblyAttributeValue(typeof(TargetFrameworkAttribute)) == ".NETCoreApp,Version=v8.0";
}

public static bool HasValidTargetPlatform(this Package package)
{
return package.AssemblyAttributeValue(typeof(TargetPlatformAttribute))?.StartsWith("Windows", StringComparison.Ordinal) == true;
}

private static string? AssemblyAttributeValue(this Package package, Type type)
{
var attribute = package?.AssemblyDefinition?.CustomAttributes.SingleOrDefault(x => x.AttributeType.FullName == type.FullName);
return attribute?.ConstructorArguments[0].Value as string;
}

[GeneratedRegex(@"https:\/\/github.com\/([^\/]+)\/([^\/]+)$")]
private static partial Regex GitHubRegex();
}
4 changes: 3 additions & 1 deletion src/Community.PowerToys.Run.Plugin.Lint/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Package(Asset asset, string path) : IDisposable
public Metadata? Metadata { get; private set; }
public AssemblyDefinition? AssemblyDefinition { get; private set; }

public void Load()
public Package Load()
{
FileStream = FileInfo.OpenRead();
ZipArchive = new ZipArchive(FileStream, ZipArchiveMode.Read);
Expand All @@ -56,6 +56,8 @@ public void Load()
memoryStream.Position = 0; // rewind
return AssemblyDefinition.ReadAssembly(memoryStream);
}

return this;
}

public void Dispose()
Expand Down
11 changes: 2 additions & 9 deletions src/Community.PowerToys.Run.Plugin.Lint/Rules.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -323,18 +322,12 @@ public IEnumerable<string> Validate()
yield break;
}

if (AttributeValue(typeof(TargetFrameworkAttribute)) != ".NETCoreApp,Version=v8.0") yield return $"Target framework should be {"net8.0".ToQuote()}";
if (AttributeValue(typeof(TargetPlatformAttribute)) != "Windows7.0") yield return $"Target platform should be {"windows".ToQuote()}";
if (!package.HasValidTargetFramework()) yield return $"Target framework should be {"net8.0".ToQuote()}";
if (!package.HasValidTargetPlatform()) yield return $"Target platform should be {"windows".ToQuote()}";

var pluginId = PluginID(MainTypeDefinition());
if (pluginId != package.Metadata?.ID) yield return $"Main.PluginID does not match metadata {"plugin.json".ToFilename()} ID";

string? AttributeValue(Type type)
{
var attribute = package.AssemblyDefinition.CustomAttributes.SingleOrDefault(x => x.AttributeType.FullName == type.FullName);
return attribute?.ConstructorArguments[0].Value as string;
}

TypeDefinition? MainTypeDefinition()
{
foreach (var module in package.AssemblyDefinition.Modules)
Expand Down
26 changes: 26 additions & 0 deletions tests/Community.PowerToys.Run.Plugin.Lint.Tests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,31 @@ public void IsZip_should_determine_if_Asset_is_zip_file()
new Asset().IsZip().Should().BeFalse();
((Asset)null!).IsZip().Should().BeFalse();
}

[Test]
public void HasValidTargetFramework_should_validate_Assembly()
{
new Package(new(), @"..\..\..\Packages\Valid-0.82.1-x64.zip").Load()
.HasValidTargetFramework().Should().BeTrue();
new Package(new(), @"..\..\..\Packages\InvalidTarget-0.82.1-x64.zip").Load()
.HasValidTargetFramework().Should().BeFalse();
new Package(new(), "Community.PowerToys.Run.Plugin.Lint.Tests.dll")
.HasValidTargetFramework().Should().BeFalse();
((Package)null!)
.HasValidTargetFramework().Should().BeFalse();
}

[Test]
public void HasValidTargetPlatform_should_validate_Assembly()
{
new Package(new(), @"..\..\..\Packages\Valid-0.82.1-x64.zip").Load()
.HasValidTargetPlatform().Should().BeTrue();
new Package(new(), @"..\..\..\Packages\InvalidTarget-0.82.1-x64.zip").Load()
.HasValidTargetPlatform().Should().BeFalse();
new Package(new(), "Community.PowerToys.Run.Plugin.Lint.Tests.dll")
.HasValidTargetPlatform().Should().BeFalse();
((Package)null!)
.HasValidTargetPlatform().Should().BeFalse();
}
}
}