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

[dotnet] Turn class-redirector into a Task #18244

Merged
merged 9 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 95 additions & 0 deletions tools/class-redirector/class-redirector/ClassRedirectorTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSBuild tasks should go in the msbuild/Xamarin.MacDev.Tasks/Tasks directory, together with all our other tasks.

using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

#nullable enable

namespace ClassRedirector
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved
{
public class ClassRedirectorTask : Microsoft.Build.Utilities.Task
{
public override bool Execute ()
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved
{
if (!Directory.Exists (InputDirectory)) {
Log.LogError ($"InputDirectory {InputDirectory} doesn't exist.");
return false;
}

if (!DirectoryIsWritable (InputDirectory)) {
Log.LogError ($"InputDirectory {InputDirectory} is not writable.");
return false;
}

if (!File.Exists (ClassMapPath)) {
Log.LogError ($"ClassMapPath file {ClassMapPath} does not exist.");
return false;
}

var dllsToProcess = CollectDlls (InputDirectory);
var xamarinDll = FindXamarinDll (dllsToProcess);

if (xamarinDll is null) {
Log.LogError ($"unable to find platform dll in {InputDirectory}");
return false;
}

var map = ReadRegistrarFile (ClassMapPath);

try {
Log.LogMessage ($"Redirecting class_handle usage from directory {InputDirectory} in the following dlls: {string.Join (",", dllsToProcess)}");
Log.LogMessage ($"Redirecting class_handle usage with the platform dll {xamarinDll}");
Log.LogMessage ($"Redirecting class_handle usage with the following {nameof (ClassMapPath)}: {ClassMapPath}");
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved
var rewriter = new Rewriter (map, xamarinDll, dllsToProcess);
rewriter.Process ();
} catch (Exception e) {
Log.LogErrorFromException (e);
}

return true;
}

[Required]
public string InputDirectory { get; set; } = "";
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved

[Required]
public string ClassMapPath { get; set; } = "";
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved

static bool DirectoryIsWritable (string path)
{
var info = new DirectoryInfo (path);
return !info.Attributes.HasFlag (FileAttributes.ReadOnly);
}

static string [] CollectDlls (string dir)
{
return Directory.GetFiles (dir, "*.dll"); // GetFiles returns full paths
}

static string [] xamarinDlls = new string [] {
"Microsoft.iOS.dll",
"Microsoft.macOS.dll",
"Microsoft.tvOS.dll",
};
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved

static bool IsXamarinDll (string p)
{
return xamarinDlls.FirstOrDefault (dll => p.EndsWith (dll, StringComparison.Ordinal)) is not null;
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved
}

static string? FindXamarinDll (string [] paths)
{
return paths.FirstOrDefault (IsXamarinDll);
}

static CSToObjCMap ReadRegistrarFile (string path)
{
var doc = XDocument.Load (path);
var map = CSToObjCMap.FromXDocument (doc);
if (map is null)
throw new Exception ($"Unable to read static registrar map file {path}");
return map;
}
}
}

106 changes: 0 additions & 106 deletions tools/class-redirector/class-redirector/Program.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>class_redirector</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Mono.Options" Version="6.12.0.148" />
rolfbjarne marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.5.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\common\CSToObjCMap.cs">
Expand Down