diff --git a/msbuild/Xamarin.MacDev.Tasks/Tasks/FindAotCompilerTaskBase.cs b/msbuild/Xamarin.MacDev.Tasks/Tasks/FindAotCompilerTaskBase.cs index 917fdf0822b3..582add09e9b5 100644 --- a/msbuild/Xamarin.MacDev.Tasks/Tasks/FindAotCompilerTaskBase.cs +++ b/msbuild/Xamarin.MacDev.Tasks/Tasks/FindAotCompilerTaskBase.cs @@ -2,10 +2,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; - using Microsoft.Build.Framework; - using Xamarin.Localization.MSBuild; +using Threading = System.Threading.Tasks; namespace Xamarin.MacDev.Tasks { public abstract class FindAotCompilerTaskBase : XamarinTask { @@ -46,13 +45,10 @@ public override bool Execute () string ComputeAotCompilerPath () { var projectPath = Path.GetTempFileName (); - var outputFile = Path.GetTempFileName (); - var binlog = Path.GetTempFileName (); File.Delete (projectPath); projectPath += ".csproj"; - File.Delete (binlog); - binlog += ".binlog"; + var csproj = $@" @@ -68,17 +64,11 @@ string ComputeAotCompilerPath () "; File.WriteAllText (projectPath, csproj); - var executable = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH"); - if (string.IsNullOrEmpty (executable)) - executable = "dotnet"; + var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH"); - var arguments = new List (); - arguments.Add ("build"); - arguments.Add ("/p:OutputFilePath=" + outputFile); - arguments.Add ("/p:RuntimeIdentifier=" + RuntimeIdentifier); - arguments.Add ("/t:ComputeAotCompilerPath"); - arguments.Add ("/bl:" + binlog); - arguments.Add (projectPath); + if (string.IsNullOrEmpty (dotnetPath)) { + dotnetPath = "dotnet"; + } var environment = default (Dictionary); var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME"); @@ -88,21 +78,83 @@ string ComputeAotCompilerPath () } try { - ExecuteAsync (executable, arguments, environment: environment).Wait (); - return File.ReadAllText (outputFile).Trim (); + ExecuteRestoreAsync (dotnetPath, projectPath, environment).Wait (); + + return ExecuteBuildAsync (dotnetPath, projectPath, environment).Result; } finally { if (KeepTemporaryOutput) { - Log.LogMessage (MessageImportance.Normal, "Temporary files for the FindAotCompiler task:"); - Log.LogMessage (MessageImportance.Normal, $" Project file: {projectPath}"); - Log.LogMessage (MessageImportance.Normal, $" Output file: {outputFile}"); - Log.LogMessage (MessageImportance.Normal, $" Binary log: {binlog}"); + Log.LogMessage (MessageImportance.Normal, $"Temporary project for the FindAotCompiler task: {projectPath}"); } else { File.Delete (projectPath); + } + } + } + + async Threading.Task ExecuteRestoreAsync (string dotnetPath, string projectPath, Dictionary environment) + { + var binlog = GetTempBinLog (); + var arguments = new List (); + + arguments.Add ("restore"); + + var dotnetDir = Path.GetDirectoryName (dotnetPath); + var configFile = Path.Combine (dotnetDir, "NuGet.config"); + + if (File.Exists (configFile)) { + arguments.Add ("/p:RestoreConfigFile=" + configFile); + } + + arguments.Add ("/bl:" + binlog); + arguments.Add (projectPath); + + try { + await ExecuteAsync (dotnetPath, arguments, environment: environment); + } finally { + if (KeepTemporaryOutput) { + Log.LogMessage (MessageImportance.Normal, $"Temporary restore log for the FindAotCompiler task: {binlog}"); + } else { + File.Delete (binlog); + } + } + } + + async Threading.Task ExecuteBuildAsync (string dotnetPath, string projectPath, Dictionary environment) + { + var outputFile = Path.GetTempFileName (); + var binlog = GetTempBinLog (); + var arguments = new List (); + + arguments.Add ("build"); + arguments.Add ("/p:OutputFilePath=" + outputFile); + arguments.Add ("/p:RuntimeIdentifier=" + RuntimeIdentifier); + arguments.Add ("/t:ComputeAotCompilerPath"); + arguments.Add ("/bl:" + binlog); + arguments.Add (projectPath); + + try { + await ExecuteAsync (dotnetPath, arguments, environment: environment); + + return File.ReadAllText (outputFile).Trim (); + } finally { + if (KeepTemporaryOutput) { + Log.LogMessage (MessageImportance.Normal, $"Temporary output for the FindAotCompiler task: {outputFile}"); + Log.LogMessage (MessageImportance.Normal, $"Temporary build log for the FindAotCompiler task: {binlog}"); + } else { File.Delete (outputFile); File.Delete (binlog); } } } + + string GetTempBinLog () + { + var binlog = Path.GetTempFileName (); + + File.Delete (binlog); + binlog += ".binlog"; + + return binlog; + } } }