AppLovinCommandLine.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // AppLovinBuildPostProcessor.cs
  3. // AppLovin MAX Unity Plugin
  4. //
  5. // Created by Santosh Bagadi on 10/30/19.
  6. // Copyright © 2019 AppLovin. All rights reserved.
  7. //
  8. #if UNITY_IOS || UNITY_IPHONE
  9. using System.Diagnostics;
  10. using System.IO;
  11. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  12. {
  13. /// <summary>
  14. /// A helper class to run command line tools.
  15. ///
  16. /// TODO: Currently only supports shell (Linux). Add support for Windows machines.
  17. /// </summary>
  18. public static class AppLovinCommandLine
  19. {
  20. /// <summary>
  21. /// Result obtained by running a command line command.
  22. /// </summary>
  23. public class Result
  24. {
  25. /// <summary>
  26. /// Standard output stream from command line.
  27. /// </summary>
  28. public string StandardOutput;
  29. /// <summary>
  30. /// Standard error stream from command line.
  31. /// </summary>
  32. public string StandardError;
  33. /// <summary>
  34. /// Exit code returned from command line.
  35. /// </summary>
  36. public int ExitCode;
  37. /// <summary>
  38. /// The description of the result that can be used for error logging.
  39. /// </summary>
  40. public string Message;
  41. }
  42. /// <summary>
  43. /// Runs a command line tool using the provided <see cref="toolPath"/> and <see cref="arguments"/>.
  44. /// </summary>
  45. /// <param name="toolPath">The tool path to run</param>
  46. /// <param name="arguments">The arguments to be passed to the command line tool</param>
  47. /// <param name="workingDirectory">The directory from which to run this command.</param>
  48. /// <returns></returns>
  49. public static Result Run(string toolPath, string arguments, string workingDirectory)
  50. {
  51. var stdoutFileName = Path.GetTempFileName();
  52. var stderrFileName = Path.GetTempFileName();
  53. var process = new Process();
  54. process.StartInfo.UseShellExecute = true;
  55. process.StartInfo.CreateNoWindow = false;
  56. process.StartInfo.RedirectStandardInput = false;
  57. process.StartInfo.RedirectStandardOutput = false;
  58. process.StartInfo.RedirectStandardError = false;
  59. process.StartInfo.WorkingDirectory = workingDirectory;
  60. process.StartInfo.FileName = "bash";
  61. process.StartInfo.Arguments = string.Format("-l -c '\"{0}\" {1} 1> {2} 2> {3}'", toolPath, arguments, stdoutFileName, stderrFileName);
  62. process.Start();
  63. process.WaitForExit();
  64. var stdout = File.ReadAllText(stdoutFileName);
  65. var stderr = File.ReadAllText(stderrFileName);
  66. File.Delete(stdoutFileName);
  67. File.Delete(stderrFileName);
  68. var result = new Result();
  69. result.StandardOutput = stdout;
  70. result.StandardError = stderr;
  71. result.ExitCode = process.ExitCode;
  72. var messagePrefix = result.ExitCode == 0 ? "Command executed successfully" : "Failed to run command";
  73. result.Message = string.Format("{0}: '{1} {2}'\nstdout: {3}\nstderr: {4}\nExit code: {5}", messagePrefix, toolPath, arguments, stdout, stderr, process.ExitCode);
  74. return result;
  75. }
  76. }
  77. }
  78. #endif