NETCmd.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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" || command_ == "genresources")
  58. {
  59. for (unsigned i = startIndex + 3; i < arguments.Size(); i++)
  60. {
  61. if (arguments[i].Length() > 1 && arguments[i][0] == '-')
  62. {
  63. String argument = arguments[i].Substring(1).ToLower();
  64. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  65. if (argument == "platform" && !value.Empty())
  66. {
  67. platforms_.Push(value);
  68. i++;
  69. }
  70. else if (argument == "config" && !value.Empty())
  71. {
  72. configurations_.Push(value);
  73. i++;
  74. }
  75. }
  76. }
  77. }
  78. if (command_ == "compile")
  79. {
  80. // solution
  81. solutionPath_ = startIndex + 2 < arguments.Size() ? arguments[startIndex + 2] : String::EMPTY;
  82. bool exists = false;
  83. if (solutionPath_.Length())
  84. {
  85. FileSystem* fs = GetSubsystem<FileSystem>();
  86. exists = fs->FileExists(solutionPath_);
  87. }
  88. if (!exists)
  89. {
  90. errorMsg = ToString("Solution file not found: %s", solutionPath_.CString());
  91. return false;
  92. }
  93. if (!platforms_.Size())
  94. {
  95. errorMsg = "Platform not specified";
  96. return false;
  97. }
  98. if (!configurations_.Size())
  99. {
  100. errorMsg = "configuration not specified";
  101. return false;
  102. }
  103. return true;
  104. }
  105. else if (command_ == "genresources")
  106. {
  107. projectPath_ = startIndex + 2 < arguments.Size() ? arguments[startIndex + 2] : String::EMPTY;
  108. requiresProjectLoad_ = true;
  109. if (!platforms_.Size())
  110. {
  111. errorMsg = "Platform not specified";
  112. return false;
  113. }
  114. }
  115. else
  116. {
  117. errorMsg = "Unknown net command";
  118. return false;
  119. }
  120. return true;
  121. }
  122. void NETCmd::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  123. {
  124. using namespace NETBuildResult;
  125. if (eventData[P_SUCCESS].GetBool())
  126. {
  127. ATOMIC_LOGINFOF("NETBuild Success for solution: %s", solutionPath_.CString());
  128. Finished();
  129. }
  130. else
  131. {
  132. const String& errorText = eventData[P_ERRORTEXT].GetString();
  133. ATOMIC_LOGERRORF("\n%s\n", errorText.CString());
  134. Error(ToString("NETBuild Error for solution: %s", solutionPath_.CString()));
  135. Finished();
  136. }
  137. }
  138. void NETCmd::Run()
  139. {
  140. if (command_ == "compile")
  141. {
  142. NETBuildSystem* buildSystem = new NETBuildSystem(context_);
  143. context_->RegisterSubsystem(buildSystem);
  144. NETBuild* build = 0;
  145. String solutionPath;
  146. String fileName;
  147. String ext;
  148. // detect project
  149. SplitPath(solutionPath_, solutionPath, fileName, ext);
  150. if (ext == ".atomic")
  151. {
  152. SharedPtr<NETProjectGen> gen(new NETProjectGen(context_));
  153. if (!gen->LoadAtomicProject(solutionPath_))
  154. {
  155. Error(ToString("NETProjectGen: Error loading project (%s)", solutionPath.CString()));
  156. Finished();
  157. return;
  158. }
  159. if (!gen->Generate())
  160. {
  161. Error(ToString("NETProjectGen: Error generating project (%s)", solutionPath.CString()));
  162. Finished();
  163. return;
  164. }
  165. solutionPath_ = solutionPath + "/AtomicNET/Solution/" + gen->GetProjectSettings()->GetName() + ".sln";
  166. }
  167. // json project file
  168. build = buildSystem->Build(solutionPath_, platforms_, configurations_);
  169. if (!build)
  170. {
  171. Error("Unable to start build");
  172. Finished();
  173. return;
  174. }
  175. build->SubscribeToEvent(E_NETBUILDRESULT, ATOMIC_HANDLER(NETCmd, HandleNETBuildResult));
  176. }
  177. else if (command_ == "genresources")
  178. {
  179. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  180. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  181. Project* project = toolSystem->GetProject();
  182. if (!project)
  183. {
  184. Error("Unable to get project");
  185. Finished();
  186. return;
  187. }
  188. buildSystem->SetBuildPath(project->GetProjectPath() + "AtomicNET/Resources/");
  189. Platform* platform = toolSystem->GetPlatformByName(platforms_[0]);
  190. if (!platform)
  191. {
  192. Error(ToString("Unknown platform %s", platforms_[0].CString()));
  193. Finished();
  194. return;
  195. }
  196. BuildBase* buildBase = platform->NewBuild(project);
  197. buildBase->SetResourcesOnly(true);
  198. buildBase->SetVerbose(true);
  199. buildSystem->QueueBuild(buildBase);
  200. buildSystem->StartNextBuild();
  201. Finished();
  202. return;
  203. }
  204. }
  205. }