NETCmd.cpp 7.2 KB

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