summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.UITest.TestCloud/TestCloudUtils.cs
blob: a554096f8e17e083cdc34b3a743f73324c0df886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Diagnostics;

namespace Xamarin.Forms.UITest.TestCloud
{
	internal static class TestCloudUtils
	{
		public static bool IsRunningOnMono()
		{
			return Type.GetType("Mono.Runtime") != null;
		}

		public static Tuple<DeviceSet.Platform, string> ParseArgs(string input)
		{
			string[] deviceArgs = input.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
			if (deviceArgs.Length != 2)
			{
				Console.WriteLine("!!! Invalid upload paramters, should be <Platform> <Category> : You entered : " + input);
				return null;
			}

			DeviceSet.Platform platform = deviceArgs[0] == "Android" ? DeviceSet.Platform.Android : DeviceSet.Platform.IOs;
			string category = deviceArgs[1];

			return Tuple.Create(platform, category);
		}

		public static int UploadApp(string command)
		{
			Tuple<string, string> execArgsPair = IsRunningOnMono() ? MonoExecArgs(command) : WindowsExecArgs(command);

			var process = new Process
			{
				StartInfo = new ProcessStartInfo
				{
					FileName = execArgsPair.Item1,
					Arguments = execArgsPair.Item2,
					UseShellExecute = false,
					RedirectStandardOutput = true,
					RedirectStandardError = true,
					CreateNoWindow = true
				}
			};

			try
			{
				process.Start();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				return 1;
			}

			while (!process.StandardOutput.EndOfStream)
			{
				string line = process.StandardOutput.ReadLine();
				Console.WriteLine(line);
			}
			while (!process.StandardError.EndOfStream)
			{
				string line = process.StandardError.ReadLine();
				Console.WriteLine(line);
			}
			process.WaitForExit();
			return process.ExitCode;
		}

		static Tuple<string, string> MonoExecArgs(string command)
		{
			return Tuple.Create("mono", command);
		}

		static Tuple<string, string> WindowsExecArgs(string command)
		{
			string[] commandArray = command.Split(' ');
			string executable = commandArray[0];
			string uploadCommand = string.Join(" ", commandArray, 1, commandArray.Length - 1);
			return Tuple.Create(executable, uploadCommand);
		}
	}
}