NETCmd.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <Atomic/Core/Context.h>
  23. #include <Atomic/Core/StringUtils.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/IPC/IPC.h>
  28. #include <Atomic/IPC/IPCEvents.h>
  29. #include <Atomic/IPC/IPCBroker.h>
  30. #include "../ToolSystem.h"
  31. #include "../ToolEnvironment.h"
  32. #include "../Project/Project.h"
  33. #include "../Project/ProjectSettings.h"
  34. #include "../Build/BuildSystem.h"
  35. #include "NETCmd.h"
  36. #include "../NETTools/AtomicNETService.h"
  37. #include "../NETTools/NETBuildSystem.h"
  38. #include "../NETTools/NETProjectGen.h"
  39. namespace ToolCore
  40. {
  41. NETCmd::NETCmd(Context* context) : Command(context),
  42. requiresProjectLoad_(false)
  43. {
  44. }
  45. NETCmd::~NETCmd()
  46. {
  47. }
  48. bool NETCmd::Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  49. {
  50. String argument = arguments[startIndex].ToLower();
  51. command_ = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1].ToLower() : String::EMPTY;
  52. if (argument != "net")
  53. {
  54. errorMsg = "Unable to parse net command";
  55. return false;
  56. }
  57. if (command_ == "compile")
  58. {
  59. solutionPath_ = startIndex + 2 < arguments.Size() ? arguments[startIndex + 2] : String::EMPTY;
  60. platform_ = startIndex + 3 < arguments.Size() ? arguments[startIndex + 3] : String::EMPTY;
  61. configuration_ = startIndex + 4 < arguments.Size() ? arguments[startIndex + 4] : "Release";
  62. bool exists = false;
  63. if (solutionPath_.Length())
  64. {
  65. FileSystem* fs = GetSubsystem<FileSystem>();
  66. exists = fs->FileExists(solutionPath_);
  67. }
  68. if (!exists)
  69. {
  70. errorMsg = ToString("Solution file not found: %s", solutionPath_.CString());
  71. return false;
  72. }
  73. if (!platform_.Length())
  74. {
  75. errorMsg = "Platform not specified";
  76. return false;
  77. }
  78. return true;
  79. }
  80. else if (command_ == "genresources")
  81. {
  82. projectPath_ = startIndex + 2 < arguments.Size() ? arguments[startIndex + 2] : String::EMPTY;
  83. platform_ = startIndex + 3 < arguments.Size() ? arguments[startIndex + 3] : String::EMPTY;
  84. requiresProjectLoad_ = true;
  85. }
  86. else
  87. {
  88. errorMsg = "Unknown net command";
  89. return false;
  90. }
  91. return true;
  92. }
  93. void NETCmd::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  94. {
  95. using namespace NETBuildResult;
  96. if (eventData[P_SUCCESS].GetBool())
  97. {
  98. ATOMIC_LOGINFOF("NETBuild Success for solution: %s", solutionPath_.CString());
  99. Finished();
  100. }
  101. else
  102. {
  103. const String& errorText = eventData[P_ERRORTEXT].GetString();
  104. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  105. Error(ToString("NETBuild Error for solution: %s", solutionPath_.CString()));
  106. Finished();
  107. }
  108. }
  109. void NETCmd::Run()
  110. {
  111. if (command_ == "compile")
  112. {
  113. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  114. NETBuildSystem* buildSystem = new NETBuildSystem(context_);
  115. context_->RegisterSubsystem(buildSystem);
  116. NETBuild* build = 0;
  117. String solutionPath;
  118. String fileName;
  119. String ext;
  120. // detect project
  121. SplitPath(solutionPath_, solutionPath, fileName, ext);
  122. if (ext == ".atomic")
  123. {
  124. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  125. if (!gen->LoadAtomicProject(solutionPath_))
  126. {
  127. Error(ToString("NETProjectGen: Error loading project (%s)", solutionPath.CString()));
  128. Finished();
  129. return;
  130. }
  131. if (!gen->Generate())
  132. {
  133. Error(ToString("NETProjectGen: Error generating project (%s)", solutionPath.CString()));
  134. Finished();
  135. return;
  136. }
  137. solutionPath_ = solutionPath + "/AtomicNET/Solution/" + gen->GetProjectSettings()->GetName() + ".sln";
  138. }
  139. // json project file
  140. build = buildSystem->Build(solutionPath_, platform_, configuration_);
  141. if (!build)
  142. {
  143. Error("Unable to start build");
  144. Finished();
  145. return;
  146. }
  147. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETCmd, HandleNETBuildResult));
  148. }
  149. else if (command_ == "genresources")
  150. {
  151. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  152. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  153. Project* project = toolSystem->GetProject();
  154. if (!project)
  155. {
  156. Error("Unable to get project");
  157. Finished();
  158. return;
  159. }
  160. buildSystem->SetBuildPath(project->GetProjectPath() + "AtomicNET/Resources/");
  161. Platform* platform = toolSystem->GetPlatformByName(platform_);
  162. if (!platform)
  163. {
  164. Error(ToString("Unknown platform %s", platform_.CString()));
  165. Finished();
  166. return;
  167. }
  168. BuildBase* buildBase = platform->NewBuild(project);
  169. buildBase->SetResourcesOnly(true);
  170. buildBase->SetVerbose(true);
  171. buildSystem->QueueBuild(buildBase);
  172. buildSystem->StartNextBuild();
  173. Finished();
  174. return;
  175. }
  176. }
  177. }