NETCompile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Poco/Exception.h>
  23. #include <Poco/Environment.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Subprocess/SubprocessSystem.h"
  29. #include "NETCompile.h"
  30. namespace ToolCore
  31. {
  32. NETCompile::NETCompile(Context* context) :
  33. Object(context)
  34. {
  35. }
  36. NETCompile::~NETCompile()
  37. {
  38. }
  39. void NETCompile::HandleSubprocessOutput(StringHash eventType, VariantMap& eventData)
  40. {
  41. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  42. compileText_ += text;
  43. }
  44. void NETCompile::HandleCompileProcessComplete(StringHash eventType, VariantMap& eventData)
  45. {
  46. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  47. VariantMap compileData;
  48. using namespace NETCompilerResult;
  49. if (!code)
  50. {
  51. compileData[P_SUCCESS] = true;
  52. }
  53. else
  54. {
  55. compileData[P_SUCCESS] = false;
  56. compileText_ += ToString("\nCompilation Command: %s", allArgs_.CString());
  57. compileData[P_ERRORTEXT] = compileText_;
  58. }
  59. SendEvent(E_NETCOMPILERESULT, compileData);
  60. }
  61. bool NETCompile::Compile(const String& solutionPath, const String& configuration)
  62. {
  63. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  64. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  65. ToolPrefs* prefs = tenv->GetToolPrefs();
  66. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  67. solutionPath_ = solutionPath;
  68. compileText_.Clear();
  69. String cmdToolsPath = Poco::Environment::get("VS140COMNTOOLS").c_str();
  70. VariantMap compileData;
  71. if (!cmdToolsPath.Length())
  72. {
  73. compileData[NETCompilerResult::P_SUCCESS] = false;
  74. compileData[NETCompilerResult::P_ERRORTEXT] = "NETCompile::Compile - VS140COMNTOOLS environment variable not found, cannot proceed";
  75. SendEvent(E_NETCOMPILERESULT, compileData);
  76. return false;
  77. }
  78. // This will need to be fixed for deployed build
  79. String nugetBin = ToString("%s/Build/Managed/nuget/nuget", tenv->GetRootSourceDir().CString());
  80. String vcvars64 = ToString("%s..\\..\\VC\\bin\\amd64\\vcvars64.bat", cmdToolsPath.CString());
  81. String cmd = "cmd";
  82. Vector<String> args;
  83. args.Push("/A");
  84. args.Push("/C");
  85. args.Push(ToString("\"\"%s\" && \"%s\" restore \"%s\" && msbuild \"%s\" /p:Configuration=%s /p:Platform=\"Any CPU\"\"",
  86. vcvars64.CString(), nugetBin.CString(), solutionPath.CString(), solutionPath.CString(), configuration.CString()));
  87. allArgs_.Join(args, " ");
  88. LOGINFOF("Compiling NET Solution: %s", solutionPath.CString());
  89. Subprocess* subprocess = nullptr;
  90. try
  91. {
  92. subprocess = subs->Launch(cmd, args, "C:\\");
  93. }
  94. catch (Poco::SystemException)
  95. {
  96. subprocess = nullptr;
  97. }
  98. if (!subprocess)
  99. {
  100. String allArgs;
  101. compileData[NETCompilerResult::P_SUCCESS] = false;
  102. compileData[NETCompilerResult::P_ERRORTEXT] = ToString("NETCompile::Compile - Unable to launch MSBuild subprocess\n%s", allArgs_.CString());
  103. SendEvent(E_NETCOMPILERESULT, compileData);
  104. return false;
  105. }
  106. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(NETCompile, HandleCompileProcessComplete));
  107. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(NETCompile, HandleSubprocessOutput));
  108. return true;
  109. }
  110. }