2016-05-26 16:06:52 +03:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using NUnit.Framework ;
2019-02-04 09:59:29 +03:00
using Xamarin.Utils ;
2016-05-26 16:06:52 +03:00
2022-05-13 21:50:02 +03:00
#nullable disable // until we get around to fixing this file
2016-05-26 16:06:52 +03:00
namespace Xamarin.Tests
{
class XBuild
{
public static string ToolPath {
get
{
2018-11-20 20:14:16 +03:00
return Configuration . XIBuildPath ;
2016-05-26 16:06:52 +03:00
}
}
2021-10-29 18:20:44 +03:00
public static string BuildXM ( string project , string configuration = "Debug" , string platform = "x86" , string verbosity = null , TimeSpan ? timeout = null , string [ ] arguments = null , string targets = "Clean,Build" )
2016-05-26 16:06:52 +03:00
{
2020-09-30 20:09:55 +03:00
return Build ( project , ApplePlatform . MacOSX , configuration , platform , verbosity , timeout , arguments , targets ) ;
2019-02-04 09:59:29 +03:00
}
2020-08-04 09:08:44 +03:00
public static string BuildXI ( string project , string configuration = "Debug" , string platform = "iPhoneSimulator" , string verbosity = null , TimeSpan ? timeout = null , string [ ] arguments = null , string targets = "Clean,Build" )
2019-02-04 09:59:29 +03:00
{
2020-09-30 20:09:55 +03:00
return Build ( project , ApplePlatform . iOS , configuration , platform , verbosity , timeout , arguments , targets ) ;
2019-02-04 09:59:29 +03:00
}
2020-09-30 20:09:55 +03:00
static string Build ( string project , ApplePlatform applePlatform , string configuration = "Debug" , string platform = "iPhoneSimulator" , string verbosity = null , TimeSpan ? timeout = null , string [ ] arguments = null , string targets = "Clean,Build" )
2019-02-04 09:59:29 +03:00
{
2020-08-04 09:08:44 +03:00
return ExecutionHelper . Execute ( ToolPath ,
2019-02-04 09:59:29 +03:00
new string [ ] {
"--" ,
$"/p:Configuration={configuration}" ,
$"/p:Platform={platform}" ,
$"/verbosity:{(string.IsNullOrEmpty (verbosity) ? " normal " : verbosity)}" ,
2019-05-23 17:59:40 +03:00
"/r:True" , // restore nuget packages which are used in some test cases
2020-08-04 09:08:44 +03:00
$"/t:{targets}" , // clean and then build, in case we left something behind in a shared dir
2019-02-04 09:59:29 +03:00
project
} . Union ( arguments ? ? new string [ ] { } ) . ToArray ( ) ,
2020-09-30 20:09:55 +03:00
environmentVariables : Configuration . GetBuildEnvironment ( applePlatform ) ,
2019-02-04 09:59:29 +03:00
timeout : timeout ) ;
2016-05-26 16:06:52 +03:00
}
}
static class ExecutionHelper {
2019-10-14 17:18:46 +03:00
public static int Execute ( string fileName , IList < string > arguments )
{
return Execute ( fileName , arguments , null , null , null , null ) ;
}
public static int Execute ( string fileName , IList < string > arguments , TimeSpan ? timeout )
{
return Execute ( fileName , arguments , null , null , null , timeout ) ;
}
public static int Execute ( string fileName , IList < string > arguments , string working_directory = null , Action < string > stdout_callback = null , Action < string > stderr_callback = null , TimeSpan ? timeout = null )
2019-04-25 18:18:43 +03:00
{
2020-06-18 13:34:07 +03:00
return Execute ( fileName , arguments , timed_out : out var _ , workingDirectory : working_directory , stdout_callback : stdout_callback , stderr_callback : stderr_callback , timeout : timeout ) ;
2019-04-25 18:18:43 +03:00
}
2019-10-14 17:18:46 +03:00
public static int Execute ( string fileName , IList < string > arguments , out StringBuilder output )
{
return Execute ( fileName , arguments , out output , null , null ) ;
}
public static int Execute ( string fileName , IList < string > arguments , out StringBuilder output , string working_directory , TimeSpan ? timeout = null )
2019-04-25 18:18:43 +03:00
{
output = new StringBuilder ( ) ;
2020-06-18 13:34:07 +03:00
return Execute ( fileName , arguments , out var _ , workingDirectory : working_directory , stdout : output , stderr : output , timeout : timeout ) ;
2019-04-25 18:18:43 +03:00
}
2021-05-21 23:29:08 +03:00
public static int Execute ( string fileName , IList < string > arguments , out StringBuilder output , string working_directory , Dictionary < string , string > environment_variables , TimeSpan ? timeout = null )
{
output = new StringBuilder ( ) ;
return Execute ( fileName , arguments , out var _ , workingDirectory : working_directory , stdout : output , stderr : output , timeout : timeout , environment_variables : environment_variables ) ;
}
2020-06-18 13:34:07 +03:00
public static int Execute ( string fileName , IList < string > arguments , out bool timed_out , string workingDirectory = null , Dictionary < string , string > environment_variables = null , StringBuilder stdout = null , StringBuilder stderr = null , TimeSpan ? timeout = null )
2019-04-25 18:18:43 +03:00
{
2020-06-18 13:34:07 +03:00
var rv = Execution . RunWithStringBuildersAsync ( fileName , arguments , workingDirectory : workingDirectory , environment : environment_variables , standardOutput : stdout , standardError : stderr , timeout : timeout ) . Result ;
timed_out = rv . TimedOut ;
if ( rv . TimedOut )
2021-06-24 09:39:22 +03:00
Console . WriteLine ( $"Command '{fileName} {StringUtils.FormatArguments (arguments)}' didn't finish in {timeout.Value.TotalMilliseconds} ms, and was killed." , timeout . Value . TotalMinutes ) ;
2020-06-18 13:34:07 +03:00
return rv . ExitCode ;
2019-04-25 18:18:43 +03:00
}
2020-06-18 13:34:07 +03:00
public static int Execute ( string fileName , IList < string > arguments , out bool timed_out , string workingDirectory = null , Dictionary < string , string > environment_variables = null , Action < string > stdout_callback = null , Action < string > stderr_callback = null , TimeSpan ? timeout = null )
2019-04-25 18:18:43 +03:00
{
if ( stdout_callback = = null )
stdout_callback = Console . WriteLine ;
if ( stderr_callback = = null )
stderr_callback = Console . Error . WriteLine ;
2020-06-18 13:34:07 +03:00
var rv = Execution . RunWithCallbacksAsync ( fileName , arguments , workingDirectory : workingDirectory , environment : environment_variables , standardOutput : stdout_callback , standardError : stderr_callback , timeout : timeout ) . Result ;
timed_out = rv . TimedOut ;
if ( rv . TimedOut )
2021-06-24 09:39:22 +03:00
Console . WriteLine ( $"Command '{fileName} {StringUtils.FormatArguments (arguments)}' didn't finish in {timeout.Value.TotalMilliseconds} ms, and was killed." , timeout . Value . TotalMinutes ) ;
2020-06-18 13:34:07 +03:00
return rv . ExitCode ;
2016-05-26 16:06:52 +03:00
}
2019-10-14 17:18:46 +03:00
public static int Execute ( string fileName , IList < string > arguments , out string output , TimeSpan ? timeout = null )
2016-05-26 16:06:52 +03:00
{
var sb = new StringBuilder ( ) ;
2020-06-18 13:34:07 +03:00
var rv = Execute ( fileName , arguments , timed_out : out var _ , stdout : sb , stderr : sb , timeout : timeout ) ;
2016-05-26 16:06:52 +03:00
output = sb . ToString ( ) ;
return rv ;
}
2019-10-14 17:18:46 +03:00
public static int Execute ( string fileName , IList < string > arguments , Dictionary < string , string > environmentVariables , StringBuilder stdout , StringBuilder stderr , TimeSpan ? timeout = null , string workingDirectory = null )
2019-02-04 09:59:29 +03:00
{
2020-06-18 13:34:07 +03:00
return Execute ( fileName , arguments , timed_out : out var _ , workingDirectory : workingDirectory , environment_variables : environmentVariables , stdout : stdout , stderr : stderr , timeout : timeout ) ;
2016-05-26 16:06:52 +03:00
}
2019-10-14 17:18:46 +03:00
public static string Execute ( string fileName , IList < string > arguments , bool throwOnError = true , Dictionary < string , string > environmentVariables = null , bool hide_output = false , TimeSpan ? timeout = null )
2019-02-04 09:59:29 +03:00
{
2020-06-18 13:34:07 +03:00
var rv = Execution . RunAsync ( fileName , arguments , mergeOutput : true , environment : environmentVariables , timeout : timeout ) . Result ;
var output = rv . StandardOutput . ToString ( ) ;
var throw_exc = throwOnError & & rv . ExitCode ! = 0 ;
2019-02-14 18:33:46 +03:00
if ( ! hide_output | | throw_exc ) {
2020-06-18 13:34:07 +03:00
Console . WriteLine ( $"{fileName} {StringUtils.FormatArguments (arguments)} (exit code: {rv.ExitCode})" ) ;
2016-05-26 16:06:52 +03:00
Console . WriteLine ( output ) ;
2020-06-18 13:34:07 +03:00
Console . WriteLine ( "Exit code: {0}" , rv . ExitCode ) ;
2016-05-26 16:06:52 +03:00
}
2020-06-18 13:34:07 +03:00
if ( throwOnError & & rv . ExitCode ! = 0 )
2020-07-22 15:25:11 +03:00
throw new Exception ( $"Execution failed with exit code {rv.ExitCode}:\n{fileName} {StringUtils.FormatArguments (arguments)}\nReview standard output/standard error for more details." ) ;
2020-06-18 13:34:07 +03:00
return output ;
2016-05-26 16:06:52 +03:00
}
}
}