Skip to content

Instantly share code, notes, and snippets.

@bjorkstromm
Created August 8, 2019 20:14
Show Gist options
  • Save bjorkstromm/42dc82848f3a67a6f9f428968d6e3566 to your computer and use it in GitHub Desktop.
Save bjorkstromm/42dc82848f3a67a6f9f428968d6e3566 to your computer and use it in GitHub Desktop.
Xamarin Gazorator
#addin nuget:?package=Gazorator&version=0.3.0&loaddependencies=true
using System.Xml.Linq;
public class BindingProjectModel
{
public string Id { get; private set; } = Guid.NewGuid().ToString().ToUpperInvariant();
public string Name { get; set; } = string.Empty;
public string MavenGroupId { get; set; } = string.Empty;
public List<MavenArtifactModel> MavenArtifacts { get; set; } = new List<MavenArtifactModel>();
public string NuGetPackageId { get; set; } = string.Empty;
public string NuGetVersion { get; set; } = string.Empty;
public string AssemblyName { get; set; } = string.Empty;
public List<NuGetDependencyModel> NuGetDependencies { get; set; } = new List<NuGetDependencyModel>();
public List<string> ProjectReferences { get; set; } = new List<string>();
}
public class MavenArtifactModel
{
public string MavenGroupId { get; set; } = string.Empty;
public string MavenArtifactId { get; set; } = string.Empty;
public string MavenArtifactVersion { get; set; } = string.Empty;
public string MavenArtifactPackaging { get; set; } = string.Empty;
public string DownloadedArtifact { get; set; } = string.Empty;
public string ProguardFile { get; set; } = string.Empty;
}
public class NuGetDependencyModel
{
public bool IsProjectReference { get; set; }
public string NuGetPackageId { get; set; } = string.Empty;
public string NuGetVersion { get; set; } = string.Empty;
public MavenArtifactModel MavenArtifact { get; set; }
}
using (var writer = new StringWriter())
{
Gazorator.Gazorator.Default
.WithOutput(writer)
.WithModel(new BindingProjectModel())
.WithReferences(typeof(XDocument).Assembly)
.ProcessAsync("./Xamarin.cshtml").Wait();
System.Console.WriteLine(writer.ToString());
}
@inherits Gazorator.Scripting.RazorScriptHost<AndroidBinderator.BindingProjectModel>
@using System.Linq
@using System.Xml.Linq
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>MonoAndroid90</TargetFrameworks>
<IsBindingProject>true</IsBindingProject>
@if (!string.IsNullOrEmpty(Model.AssemblyName))
{
<AssemblyName>@(Model.AssemblyName)</AssemblyName>
}
else
{
<AssemblyName>@(Model.NuGetPackageId)</AssemblyName>
}
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<AndroidUseIntermediateDesignerFile>True</AndroidUseIntermediateDesignerFile>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
</PropertyGroup>
<PropertyGroup>
<PackageId>@(Model.NuGetPackageId)</PackageId>
<Title>Xamarin Android Support Library - @(Model.Name)</Title>
<Summary>Xamarin.Android bindings for Android Support Library - @(Model.Name)</Summary>
<Description>Xamarin.Android bindings for Android Support Library - @(Model.Name)</Description>
<PackageTags>Xamarin Android Xamarin.Android Support</PackageTags>
<Authors>Xamarin Inc.</Authors>
<Owners>Xamarin Inc.</Owners>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageProjectUrl>https://go.microsoft.com/fwlink/?linkid=865352</PackageProjectUrl>
<PackageLicenseUrl>https://go.microsoft.com/fwlink/?linkid=865381</PackageLicenseUrl>
<PackageIconUrl>https://raw.githubusercontent.com/xamarin/AndroidSupportComponents/master/icons/@(Model.Name)_128x128.png</PackageIconUrl>
<PackageVersion>@(Model.NuGetVersion)</PackageVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>
<PropertyGroup>
<AndroidClassParser>class-parse</AndroidClassParser>
<AndroidCodegenTarget>XAJavaInterop1</AndroidCodegenTarget>
</PropertyGroup>
<ItemGroup>
@foreach (var art in @Model.MavenArtifacts)
{
<TransformFile Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-paramnames.xml" Condition="Exists('..\..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-paramnames.xml')" />
}
</ItemGroup>
@*<ItemGroup>
@foreach (var art in @Model.MavenArtifacts) {
<JavaSourceJar Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-sources.jar" Condition="Exists('..\..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-sources.jar')" />
}
</ItemGroup>*@
<ItemGroup>
@foreach (var art in @Model.MavenArtifacts)
{
<_AndroidDocumentationPath Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-paramnames.txt" Condition="Exists('..\..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)-paramnames.txt')" />
}
</ItemGroup>
<ItemGroup>
<None Include="@(Model.NuGetPackageId).targets" Pack="True" PackagePath="build\$(TargetFramework)" />
</ItemGroup>
@if (@Model.MavenArtifacts.Count > 0)
{
<ItemGroup>
@foreach (var art in @Model.MavenArtifacts)
{
if (art.ProguardFile != null)
{
<None Include="..\..\@(art.ProguardFile)" Pack="True" PackagePath="proguard\" />
}
}
</ItemGroup>
}
<ItemGroup>
<Folder Include="Additions\" />
<Folder Include="Jars\" />
<Folder Include="Transforms\" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\source\AssemblyInfo.cs" />
<Compile Include="..\..\source\@(Model.MavenGroupId)\@(Model.Name)\additions\*.cs">
<Link>Additions/%(RecursiveDir)/%(Filename)%(Extension)</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<TransformFile Include="..\..\source\Metadata.common.xml">
<Link>Transforms/Metadata.common.xml</Link>
</TransformFile>
<TransformFile Include="..\..\source\@(Model.MavenGroupId)\@(Model.Name)\transforms\*.xml">
<Link>Transforms/%(RecursiveDir)/%(Filename)%(Extension)</Link>
</TransformFile>
</ItemGroup>
<ItemGroup>
@foreach (var art in @Model.MavenArtifacts)
{
if (art.MavenArtifactPackaging == "aar")
{
<None Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId).aar" Pack="True" PackagePath="aar\" />
}
}
</ItemGroup>
@if (@Model.MavenArtifacts.Count > 0)
{
<ItemGroup>
@foreach (var art in @Model.MavenArtifacts)
{
if (art.MavenArtifactPackaging == "aar")
{
<InputJar Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)\classes.jar" />
<!-- For those artifacts with lib/ folder -->
<InputJar Condition="Exists('..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)\libs\')"
Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId)\libs\*.jar" />
}
else
{
<EmbeddedJar Include="..\..\externals\@(art.MavenGroupId)\@(art.MavenArtifactId).jar" />
}
}
</ItemGroup>
}
<ItemGroup>
<!-- ProjectReference -->
@foreach (var dep in @Model.NuGetDependencies)
{
if (dep.IsProjectReference)
{
<ProjectReference Include="..\..\generated\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId)\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId).csproj" />
}
}
<!-- some additional fixes -->
@if (@Model.NuGetPackageId == "Xamarin.Android.Support.Transition")
{
<ProjectReference Include="..\com.android.support.support-fragment\com.android.support.support-fragment.csproj" />
}
</ItemGroup>
<ItemGroup>
<!-- PackageReference -->
@foreach (var dep in @Model.NuGetDependencies)
{
if (!dep.IsProjectReference)
{
<PackageReference Include="@(dep.NuGetPackageId)" Version="@(dep.NuGetVersion)" />
}
}
</ItemGroup>
@if (@Model.NuGetPackageId == "Xamarin.Android.Support.Annotations")
{
<ItemGroup>
<None Include="..\..\source\buildtasks\support-annotations\bin\$(Configuration)\Xamarin.Android.Support.BuildTasks.dll" Pack="True" PackagePath="build\$(TargetFramework)" />
</ItemGroup>
}
@if (@Model.NuGetPackageId == "Xamarin.Android.Support.Vector.Drawable")
{
<ItemGroup>
<None Include="..\..\source\buildtasks\support-vector-drawable\bin\$(Configuration)\Xamarin.Android.Support.Tasks.VectorDrawable.dll" Pack="True" PackagePath="build\$(TargetFramework)" />
</ItemGroup>
}
@if (@Model.NuGetPackageId == "Xamarin.Android.Support.v4")
{
<Target Name="AndroidSupportLibrariesTypeForwardersGenerationWithGenAPI">
<Message Text="AndroidSupportLibrariesTypeForwardersGenerationWithGenAPI"></Message>
<PropertyGroup>
<GenAPIPath>$(MSBuildProjectDirectory)/../../tools/microsoft.dotnet.buildtools.genapi.1.0.0-beta-00081/Microsoft.DotNet.BuildTools.GenAPI/tools/GenAPI.exe</GenAPIPath>
</PropertyGroup>
<ItemGroup>
@foreach (var dep in @Model.NuGetDependencies)
{
if (dep.IsProjectReference)
{
<GenAPIItemBase Include="../../generated/@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId)/">
<GenAPIAssembly>"$(IntermediateOutputPath)/@(dep.NuGetPackageId).dll"</GenAPIAssembly>
<GenAPICSCode>./Additions/@(dep.NuGetPackageId).cs</GenAPICSCode>
</GenAPIItemBase>
}
}
</ItemGroup>
<Message Text=" GenAPIItemBase = %(GenAPIItemBase.Identity) %0A %(GenAPIItemBase.GenAPICSCode) %0A %(GenAPIItemBase.GenAPIAssembly)"></Message>
<!--
// For some reason GenAPI.exe can't handle absolute paths on mac/unix properly, so always make them relative
// GenAPI.exe -libPath:$(MONOANDROID) -out:Some.generated.cs -w:TypeForwards ./relative/path/to/Assembly.dll
-->
<MakeDir Directories="./Additions" />
<Exec Command="mono $(GenAPIPath) -libPath:$(MONOANDROID) -out:%(GenAPIItemBase.GenAPICSCode) -w:TypeForwards %(GenAPIItemBase.Identity)/%(GenAPIItemBase.GenAPIAssembly) " />
</Target>
}
<ItemGroup>
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.4.0" PrivateAssets="All" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment