From a677c1794db64d5559f53a960927447bac3063a2 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 5 Sep 2018 10:15:39 -0500 Subject: [PATCH] [Xamarin.Android.Build.Tasks] precompile SetVsMonoAndroidRegistryKey (#2130) In `Xamarin.Android.Sdk.props` we have some inline C# code for a MSBuild task named `SetVsMonoAndroidRegistryKey`. Which is understandable, since the code needed is so simple. But thinking about it, MSBuild has to *compile* this code to run it. Which seems inherently slower than if we compiled the code during our build. Currently its entire parent target, `RedirectMonoAndroidSdkPaths`, takes: 157 ms RedirectMonoAndroidSdkPaths 1 calls I moved this task inside `Xamarin.Android.Build.Tasks.dll`, and we appear to get some improvements "for free": 76 ms RedirectMonoAndroidSdkPaths 1 calls I looked at some past logs, and there are some times where the `RedirectMonoAndroidSdkPaths` target was taking over 200ms. I suspect MSBuild might also have some caching functionality for inlined C# code. There might be a cached assembly somewhere? Not sure. What makes this a better fix is that this target runs for all builds--even builds with no changes. General changes: - The `` doesn't need a condition, since it will be lazily evaluated anyways. - Used string interpolation where it looked nicer. - Used `LogDebugMessage()` and improved the log message. --- .../MSBuild/Xamarin/Xamarin.Android.Sdk.props | 13 +--------- .../Tasks/SetVsMonoAndroidRegistryKey.cs | 25 +++++++++++++++++++ .../Xamarin.Android.Build.Tasks.csproj | 1 + 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/SetVsMonoAndroidRegistryKey.cs diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Xamarin.Android.Sdk.props b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Xamarin.Android.Sdk.props index b915d4871e8..bce872f190e 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Xamarin.Android.Sdk.props +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Xamarin.Android.Sdk.props @@ -43,18 +43,7 @@ Copyright (C) 2011-2016 Xamarin. All rights reserved. - - - - - - - - Environment.SetEnvironmentVariable("XAMARIN_ANDROID_REGKEY", string.Format(@"SOFTWARE\Xamarin\VisualStudio\{0}_{1}\Android", VisualStudioVersion, InstallationID), EnvironmentVariableTarget.Process); - Log.LogMessage(Environment.GetEnvironmentVariable("XAMARIN_ANDROID_REGKEY")); - - - + true diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/SetVsMonoAndroidRegistryKey.cs b/src/Xamarin.Android.Build.Tasks/Tasks/SetVsMonoAndroidRegistryKey.cs new file mode 100644 index 00000000000..62bcb42e7ee --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/SetVsMonoAndroidRegistryKey.cs @@ -0,0 +1,25 @@ +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System; + +namespace Xamarin.Android.Tasks +{ + public class SetVsMonoAndroidRegistryKey : Task + { + [Required] + public string InstallationID { get; set; } + + [Required] + public string VisualStudioVersion { get; set; } + + const string EnvironmentVariable = "XAMARIN_ANDROID_REGKEY"; + + public override bool Execute () + { + string value = $@"SOFTWARE\Xamarin\VisualStudio\{VisualStudioVersion}_{InstallationID}\Android"; + Log.LogDebugMessage ($"Setting %{EnvironmentVariable}%=\"{value}\""); + Environment.SetEnvironmentVariable (EnvironmentVariable, value, EnvironmentVariableTarget.Process); + return !Log.HasLoggedErrors; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index 0a43a76f68a..8492a495dd3 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -151,6 +151,7 @@ +