Skip to content

Commit

Permalink
Add task to generate wrapper designer class source
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Nov 1, 2021
1 parent 89f8ff8 commit 38f4e44
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,51 @@ Copyright (C) 2016 Xamarin. All rights reserved.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<UsingTask TaskName="Xamarin.Android.Tasks.GenerateResourceDesignerIntermediateClass" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateResourceDesignerAssembly" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />

<PropertyGroup>
<AndroidUseDesignerAssembly Condition=" '$(AndroidUseIntermediateDesignerFile)' == 'False' And '$(AndroidUseDesignerAssembly)' == '' ">True</AndroidUseDesignerAssembly>
<AndroidUseDesignerAssembly Condition=" '$(AndroidUseDesignerAssembly)' == '' ">False</AndroidUseDesignerAssembly>
<_GenerateResourceDesignerAssemblyOutput>$(IntermediateOutputPath)Xamarin.Android.Resource.Designer.dll</_GenerateResourceDesignerAssemblyOutput>
<_GenerateResourceDesignerClassFile>$(IntermediateOutputPath)_Xamarin.Android.Resource.Designer.cs</_GenerateResourceDesignerClassFile>
</PropertyGroup>

<Target Name="_GenerateResourceDesignerIntermediateClass"
Inputs="@(_AndroidMSBuildAllProjects)"
Outputs="$(_GenerateResourceDesignerClassFile)"
Condition=" '$(AndroidUseDesignerAssembly)' == 'True' "
>
<!-- Creates a Resource.designer.cs file in the Intermediate output path which derives from the
Designer Assembly, for backward compatability.
-->
<GenerateResourceDesignerIntermediateClass
Namespace="$(AndroidResgenNamespace)"
OutputFile="$(_GenerateResourceDesignerClassFile)"
>
</GenerateResourceDesignerIntermediateClass>
<ItemGroup>
<FileWrites Include="$(_GenerateResourceDesignerClassFile)" />
<Compile Include="$(_GenerateResourceDesignerClassFile)" />
</ItemGroup>
</Target>

<Target Name="_GenerateResourceDesignerAssembly"
Inputs="$(IntermediateOutputPath)R.txt"
Outputs="$(_GenerateResourceDesignerAssemblyOutput)"
Condition=" '$(AndroidUseDesignerAssembly)' == 'True' And Exists ('$(IntermediateOutputPath)R.txt') ">
<!-- <ItemGroup>
<_DesignerNamespaces Include="$(AndroidResgenNamespace)" />
</ItemGroup> -->
<GenerateResourceDesignerAssembly
ContinueOnError="$(DesignTimeBuild)"
RTxtFile="$(IntermediateOutputPath)R.txt"
IsApplication="$(AndroidApplication)"
DesignTimeBuild="$(DesignTimeBuild)"
OutputFile="$(_GenerateResourceDesignerAssemblyOutput)"
Namespaces="@(_DesignerNamespaces)"
TargetFrameworkIdentifier="$(TargetFrameworkIdentifier)"
TargetFrameworkVersion="$(TargetFrameworkVersion)"
UsingAndroidNETSdk="$(UsingAndroidNETSdk)"
ProjectDir="$(ProjectDir)"
Resources="@(AndroidResource);@(AndroidBoundLayout)"
ResourceDirectory="$(MonoAndroidResourcePrefix)"
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
Namespace="$(AndroidResgenNamespace)"
ContinueOnError="$(DesignTimeBuild)"
RTxtFile="$(IntermediateOutputPath)R.txt"
IsApplication="$(AndroidApplication)"
DesignTimeBuild="$(DesignTimeBuild)"
OutputFile="$(_GenerateResourceDesignerAssemblyOutput)"
TargetFrameworkIdentifier="$(TargetFrameworkIdentifier)"
TargetFrameworkVersion="$(TargetFrameworkVersion)"
ProjectDir="$(ProjectDir)"
Resources="@(AndroidResource);@(AndroidBoundLayout)"
ResourceDirectory="$(MonoAndroidResourcePrefix)"
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
>
</GenerateResourceDesignerAssembly>
<ItemGroup>
Expand All @@ -59,7 +74,8 @@ Copyright (C) 2016 Xamarin. All rights reserved.

<PropertyGroup>
<BuildResourceDesignerDependsOn>
$(_GenerateResourceDesignerAssembly)
$(_GenerateResourceDesignerIntermediateClass);
$(_GenerateResourceDesignerAssembly);
</BuildResourceDesignerDependsOn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_CheckForDeletedResourceFile;
_ComputeAndroidResourcePaths;
_UpdateAndroidResgen;
_GenerateResourceDesignerIntermediateClass;
_GenerateResourceDesignerAssembly;
_CreateAar;
</_UpdateAndroidResourcesDependsOn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class GenerateResourceDesignerAssembly : AndroidTask
const string DesignerAssemblyName = "Xamarin.Android.Resource.Designer";
public override string TaskPrefix => "GRDA";

public string Namespace { get; set; }

[Required]
public ITaskItem RTxtFile { get; set; }

Expand Down Expand Up @@ -51,18 +49,13 @@ public class GenerateResourceDesignerAssembly : AndroidTask

public ITaskItem[] AdditionalResourceDirectories { get; set; }

public bool UsingAndroidNETSdk { get; set; } = false;

public string[] Namespaces { get; set; }

TypeReference intArray;
TypeReference intRef;
TypeReference objectRef;
Dictionary<string, string> resource_fixup = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase);

public override bool RunTask ()
{
Namespace = Namespace ?? "Application";
// ResourceDirectory may be a relative path, and
// we need to compare it to absolute paths
ResourceDirectory = Path.GetFullPath (ResourceDirectory);
Expand Down Expand Up @@ -112,10 +105,6 @@ public override bool RunTask ()

ProcessRtxtFile (RTxtFile.ItemSpec, resourceDesigner, module);

foreach (var ns in (Namespaces ?? Array.Empty<string> ())) {
module.Types.Add (new TypeDefinition (ns, "Resource", att, resourceDesigner));
}

assembly.Write (OutputFile.ItemSpec);
return !Log.HasLoggedErrors;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks
{
public class GenerateResourceDesignerIntermediateClass : AndroidTask
{
public override string TaskPrefix => "GRDIC";

public string Namespace { get; set; }
public ITaskItem OutputFile { get; set; }
public override bool RunTask ()
{
File.WriteAllText (OutputFile.ItemSpec, $@"// This is an Auto Generated file DO NOT EDIT
using System;
namespace {Namespace} {{
public class Resource : Xamarin.Android.Resource.Designer.Resource {{
}}
}}
", System.Text.Encoding.UTF8);
return !Log.HasLoggedErrors;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ projects. .NET 5 projects will not import this file.
_CheckForDeletedResourceFile;
_ComputeAndroidResourcePaths;
_UpdateAndroidResgen;
_GenerateResourceDesignerIntermediateClass;
_GenerateResourceDesignerAssembly;
_CreateManagedLibraryResourceArchive;
</_UpdateAndroidResourcesDependsOn>
Expand Down

0 comments on commit 38f4e44

Please sign in to comment.