Skip to content

Commit

Permalink
Add default introduced, which will add introduced for types with no p…
Browse files Browse the repository at this point in the history
…latform tag for mac/ios
  • Loading branch information
chamons committed Feb 8, 2022
1 parent 5c84846 commit 650f584
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"args": [
"test",
"${workspaceFolder}/test/mellite.tests.csproj",
"--filter=StructStatic",
"--filter=DefaultPlatformAttributeFromNamespace",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
Expand Down
2 changes: 1 addition & 1 deletion scripts/convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ dotnet run --project $SCRIPT_DIR/../src/mellite.csproj -- --ignore=build $SDK_PA
echo "Completed - Please review and commit."
read _
echo "Running final conversion..."
dotnet run --project $SCRIPT_DIR/../src/mellite.csproj -- --ignore=build $SDK_PATH --harvest-assembly=/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll
dotnet run --project $SCRIPT_DIR/../src/mellite.csproj -- --ignore=build $SDK_PATH --harvest-assembly=/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll --add-default-introduced
echo "Completed - Please review and commit."
read _
30 changes: 29 additions & 1 deletion src/AssemblyHarvester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AssemblyHarvestInfo (Dictionary<string, List<HarvestedAvailabilityInfo>>
public class AssemblyHarvester {
Dictionary<string, List<HarvestedAvailabilityInfo>> Data = new Dictionary<string, List<HarvestedAvailabilityInfo>> ();

public AssemblyHarvestInfo Harvest (string path)
public AssemblyHarvestInfo Harvest (string path, bool addDefaultIntroduced = false)
{
Data = new Dictionary<string, List<HarvestedAvailabilityInfo>> ();

Expand All @@ -33,11 +33,39 @@ public AssemblyHarvestInfo Harvest (string path)
foreach (var module in assembly.Modules) {
foreach (var type in module.Types) {
Data [type.FullName] = GetAvailabilityAttributes (type.CustomAttributes).ToList ();
if (addDefaultIntroduced) {
ProcessDefaultIntroduced (type, Data);
}
}
}
return new AssemblyHarvestInfo (Data);
}

void ProcessDefaultIntroduced (TypeDefinition type, Dictionary<string, List<HarvestedAvailabilityInfo>> data)
{
string ns = type.FullName.Split ('.').First ();
List<string> defaultIntroducedPlatforms;
switch (ns) {
case "AppKit":
defaultIntroducedPlatforms = new List<string> { "PlatformName.MacOSX" };
break;
case "UIKit":
defaultIntroducedPlatforms = new List<string> { "PlatformName.iOS" };
break;
default:
defaultIntroducedPlatforms = new List<string> { "PlatformName.MacOSX", "PlatformName.iOS" };
break;
}

var introduced = Data [type.FullName].Where (d => d.Attribute.Name.ToString () == "Introduced").ToList ();

foreach (var platform in defaultIntroducedPlatforms) {
if (!introduced.Any (i => i.Attribute.ArgumentList!.Arguments [0].ToString () == platform)) {
Data [type.FullName].Add (new HarvestedAvailabilityInfo ("Introduced", platform));
}
}
}

IEnumerable<HarvestedAvailabilityInfo> GetAvailabilityAttributes (IEnumerable<CustomAttribute> attributes)
{
var availability = new List<HarvestedAvailabilityInfo> ();
Expand Down
1 change: 1 addition & 0 deletions src/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static void Main (string [] args)
{ "ignore-root", "Only process files in subdirectories of the target directory, do not process root level files", _ => locatorOptions.IgnoreRoot = true },
{ "harvest-assembly=", "Process assembly to provide additional context for partial only classes", a => assemblyPath = a },
{ "allow-errors", "Instead of crashing on first fatal error, just print and continue.", a => processOptions.AllowErrors = true },
{ "add-default-introduced", "When processing the harvested assembly treat types with no introduced attribute as ios/mac based upon namespace.", a => processOptions.AddDefaultIntroduced = true },
};

try {
Expand Down
3 changes: 2 additions & 1 deletion src/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ProcessOptions {
public string? AssemblyPath = null;
public string? VerboseConditional = null;
public bool AllowErrors = false;
public bool AddDefaultIntroduced = false;
}

public static class Processor {
Expand Down Expand Up @@ -55,7 +56,7 @@ public static void ProcessFile (string path, ProcessOptions options)
AssemblyHarvestInfo? assemblyInfo = null;
if (options.AssemblyPath != null) {
var harvester = new AssemblyHarvester ();
assemblyInfo = harvester.Harvest (options.AssemblyPath);
assemblyInfo = harvester.Harvest (options.AssemblyPath, options.AddDefaultIntroduced);
}
return Converter.Convert (text, options.Defines, options.VerboseConditional, assemblyInfo);
case ProcessSteps.StripExistingNET6Attributes:
Expand Down
81 changes: 77 additions & 4 deletions test/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ public struct AVCaptureViewControlsStyle {
}");
}

const string SystemXI = "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll";

// Convert a partial class's member with the parent info defined in another assembly (outside of our scope)
[Fact]
public void PartialInfoConversion ()
Expand All @@ -880,7 +882,7 @@ partial class NSDateComponentsFormatter {
[Export (""CustomStyle"")]
int CustomStyle { get; set; }
}
}", null, "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll");
}", null, SystemXI);
}

[Fact]
Expand All @@ -907,7 +909,7 @@ public partial class AVCaptureConnection {
public bool SupportsVideoMaxFrameDuration {
}
}
}", null, "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll");
}", null, SystemXI);
}

[Fact]
Expand All @@ -926,7 +928,7 @@ public void ConvertWithSomeAttributesDefinedOff ()
public enum AVCaptureDeviceTransportControlsPlaybackMode : long {
NotPlaying, Playing
}
}", ProcessSteps.ConvertXamarinAttributes, @"", null, "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll"));
}", ProcessSteps.ConvertXamarinAttributes, @"", null, SystemXI));
}

[Fact]
Expand Down Expand Up @@ -1015,7 +1017,7 @@ public class ABPerson {
#endif
public static ABPersonCompositeNameFormat GetCompositeNameFormat (ABRecord? record) {}
}
}", null, "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll");
}", null, SystemXI);
}

[Fact]
Expand Down Expand Up @@ -1208,5 +1210,76 @@ public bool Apply (NWTxtRecordApplyDelegate handler)
}
");
}

[Fact]
public void DefaultPlatformAttributeFromNamespace ()
{
TestUtilities.TestProcess (@"namespace Network {
public class NWTxtRecord : NativeObject {
[Mac (10, 10)]
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", @"namespace Network {
public class NWTxtRecord : NativeObject {
#if NET
[SupportedOSPlatform (""macos10.10"")]
[SupportedOSPlatform (""ios"")]
#else
[Mac (10, 10)]
#endif
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", new ProcessOptions () { AddDefaultIntroduced = true, AssemblyPath = SystemXI });

TestUtilities.TestProcess (@"namespace AppKit {
public class NWTxtRecord : NativeObject {
[Mac (10, 10)]
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", @"namespace AppKit {
public class NWTxtRecord : NativeObject {
#if NET
[SupportedOSPlatform (""macos10.10"")]
#else
[Mac (10, 10)]
#endif
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", new ProcessOptions () { AddDefaultIntroduced = true, AssemblyPath = SystemXI });

TestUtilities.TestProcess (@"namespace UIKit {
public class NWTxtRecord : NativeObject {
[iOS (10, 0)]
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", @"namespace UIKit {
public class NWTxtRecord : NativeObject {
#if NET
[SupportedOSPlatform (""ios10.0"")]
#else
[iOS (10, 0)]
#endif
public bool Apply (NWTxtRecordApplyDelegate handler)
{
}
}
}
", new ProcessOptions () { AddDefaultIntroduced = true, AssemblyPath = SystemXI });
}
}
}

0 comments on commit 650f584

Please sign in to comment.