22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5-
65using Microsoft . Build . Framework ;
7-
86using Xamarin . Localization . MSBuild ;
7+ using Threading = System . Threading . Tasks ;
98
109namespace Xamarin . MacDev . Tasks {
1110 public abstract class FindAotCompilerTaskBase : XamarinTask {
@@ -46,13 +45,10 @@ public override bool Execute ()
4645 string ComputeAotCompilerPath ( )
4746 {
4847 var projectPath = Path . GetTempFileName ( ) ;
49- var outputFile = Path . GetTempFileName ( ) ;
50- var binlog = Path . GetTempFileName ( ) ;
5148
5249 File . Delete ( projectPath ) ;
5350 projectPath += ".csproj" ;
54- File . Delete ( binlog ) ;
55- binlog += ".binlog" ;
51+
5652 var csproj = $@ "<?xml version=""1.0"" encoding=""utf-8""?>
5753<Project Sdk=""Microsoft.NET.Sdk"">
5854 <PropertyGroup>
@@ -68,17 +64,11 @@ string ComputeAotCompilerPath ()
6864" ;
6965 File . WriteAllText ( projectPath , csproj ) ;
7066
71- var executable = Environment . GetEnvironmentVariable ( "DOTNET_HOST_PATH" ) ;
72- if ( string . IsNullOrEmpty ( executable ) )
73- executable = "dotnet" ;
67+ var dotnetPath = Environment . GetEnvironmentVariable ( "DOTNET_HOST_PATH" ) ;
7468
75- var arguments = new List < string > ( ) ;
76- arguments . Add ( "build" ) ;
77- arguments . Add ( "/p:OutputFilePath=" + outputFile ) ;
78- arguments . Add ( "/p:RuntimeIdentifier=" + RuntimeIdentifier ) ;
79- arguments . Add ( "/t:ComputeAotCompilerPath" ) ;
80- arguments . Add ( "/bl:" + binlog ) ;
81- arguments . Add ( projectPath ) ;
69+ if ( string . IsNullOrEmpty ( dotnetPath ) ) {
70+ dotnetPath = "dotnet" ;
71+ }
8272
8373 var environment = default ( Dictionary < string , string > ) ;
8474 var customHome = Environment . GetEnvironmentVariable ( "DOTNET_CUSTOM_HOME" ) ;
@@ -88,21 +78,83 @@ string ComputeAotCompilerPath ()
8878 }
8979
9080 try {
91- ExecuteAsync ( executable , arguments , environment : environment ) . Wait ( ) ;
92- return File . ReadAllText ( outputFile ) . Trim ( ) ;
81+ ExecuteRestoreAsync ( dotnetPath , projectPath , environment ) . Wait ( ) ;
82+
83+ return ExecuteBuildAsync ( dotnetPath , projectPath , environment ) . Result ;
9384 } finally {
9485 if ( KeepTemporaryOutput ) {
95- Log . LogMessage ( MessageImportance . Normal , "Temporary files for the FindAotCompiler task:" ) ;
96- Log . LogMessage ( MessageImportance . Normal , $ " Project file: { projectPath } ") ;
97- Log . LogMessage ( MessageImportance . Normal , $ " Output file: { outputFile } ") ;
98- Log . LogMessage ( MessageImportance . Normal , $ " Binary log: { binlog } ") ;
86+ Log . LogMessage ( MessageImportance . Normal , $ "Temporary project for the FindAotCompiler task: { projectPath } ") ;
9987 } else {
10088 File . Delete ( projectPath ) ;
89+ }
90+ }
91+ }
92+
93+ async Threading . Task ExecuteRestoreAsync ( string dotnetPath , string projectPath , Dictionary < string , string > environment )
94+ {
95+ var binlog = GetTempBinLog ( ) ;
96+ var arguments = new List < string > ( ) ;
97+
98+ arguments . Add ( "restore" ) ;
99+
100+ var dotnetDir = Path . GetDirectoryName ( dotnetPath ) ;
101+ var configFile = Path . Combine ( dotnetDir , "NuGet.config" ) ;
102+
103+ if ( File . Exists ( configFile ) ) {
104+ arguments . Add ( "/p:RestoreConfigFile=" + configFile ) ;
105+ }
106+
107+ arguments . Add ( "/bl:" + binlog ) ;
108+ arguments . Add ( projectPath ) ;
109+
110+ try {
111+ await ExecuteAsync ( dotnetPath , arguments , environment : environment ) ;
112+ } finally {
113+ if ( KeepTemporaryOutput ) {
114+ Log . LogMessage ( MessageImportance . Normal , $ "Temporary restore log for the FindAotCompiler task: { binlog } ") ;
115+ } else {
116+ File . Delete ( binlog ) ;
117+ }
118+ }
119+ }
120+
121+ async Threading . Task < string > ExecuteBuildAsync ( string dotnetPath , string projectPath , Dictionary < string , string > environment )
122+ {
123+ var outputFile = Path . GetTempFileName ( ) ;
124+ var binlog = GetTempBinLog ( ) ;
125+ var arguments = new List < string > ( ) ;
126+
127+ arguments . Add ( "build" ) ;
128+ arguments . Add ( "/p:OutputFilePath=" + outputFile ) ;
129+ arguments . Add ( "/p:RuntimeIdentifier=" + RuntimeIdentifier ) ;
130+ arguments . Add ( "/t:ComputeAotCompilerPath" ) ;
131+ arguments . Add ( "/bl:" + binlog ) ;
132+ arguments . Add ( projectPath ) ;
133+
134+ try {
135+ await ExecuteAsync ( dotnetPath , arguments , environment : environment ) ;
136+
137+ return File . ReadAllText ( outputFile ) . Trim ( ) ;
138+ } finally {
139+ if ( KeepTemporaryOutput ) {
140+ Log . LogMessage ( MessageImportance . Normal , $ "Temporary output for the FindAotCompiler task: { outputFile } ") ;
141+ Log . LogMessage ( MessageImportance . Normal , $ "Temporary build log for the FindAotCompiler task: { binlog } ") ;
142+ } else {
101143 File . Delete ( outputFile ) ;
102144 File . Delete ( binlog ) ;
103145 }
104146 }
105147 }
148+
149+ string GetTempBinLog ( )
150+ {
151+ var binlog = Path . GetTempFileName ( ) ;
152+
153+ File . Delete ( binlog ) ;
154+ binlog += ".binlog" ;
155+
156+ return binlog ;
157+ }
106158 }
107159}
108160
0 commit comments