ProjectCmd.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/StringUtils.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include "../ToolSystem.h"
  27. #include "../Project/ProjectFile.h"
  28. #include "../Project/ProjectEvents.h"
  29. #include "ProjectCmd.h"
  30. #include <Poco/File.h>
  31. #define PROJECTCMD_CACHE_CLEAN 0x1 // Cache command first cleans current cache
  32. namespace ToolCore
  33. {
  34. ProjectCmd::ProjectCmd(Context* context) :
  35. Command(context),
  36. requiresProjectLoad_(false),
  37. options_(0)
  38. {
  39. }
  40. ProjectCmd::~ProjectCmd()
  41. {
  42. }
  43. // usage: project <projectPath> [cache (-clean)]
  44. bool ProjectCmd::Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  45. {
  46. String argument = arguments[startIndex].ToLower();
  47. if (argument != "project")
  48. {
  49. errorMsg = "Unable to parse project command";
  50. return false;
  51. }
  52. command_ = startIndex + 2 < arguments.Size() ? arguments[startIndex + 2] : String::EMPTY;
  53. if (command_ == "cache")
  54. {
  55. requiresProjectLoad_ = true;
  56. SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(ProjectCmd, HandleProjectLoaded));
  57. for (unsigned i = startIndex + 3; i < arguments.Size(); i++)
  58. {
  59. if (arguments[i].Length() > 1 && arguments[i][0] == '-')
  60. {
  61. String argument = arguments[i].Substring(1).ToLower();
  62. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  63. if (argument == "clean")
  64. {
  65. options_ = options_ | PROJECTCMD_CACHE_CLEAN;
  66. SubscribeToEvent(E_PROJECTBEGINLOAD, ATOMIC_HANDLER(ProjectCmd, HandleProjectBeginLoad));
  67. i++;
  68. }
  69. }
  70. }
  71. }
  72. else
  73. {
  74. errorMsg = "Unknown project command";
  75. return false;
  76. }
  77. projectPath_ = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1] : String::EMPTY;
  78. return true;
  79. }
  80. void ProjectCmd::HandleProjectBeginLoad(StringHash eventType, VariantMap& eventData)
  81. {
  82. if (options_ & PROJECTCMD_CACHE_CLEAN)
  83. {
  84. String cachePath = GetPath(eventData[ProjectBeginLoad::P_PROJECTPATH].GetString());
  85. cachePath = AddTrailingSlash(cachePath) + "Cache";
  86. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  87. if (!fileSystem->RemoveDir(cachePath, true))
  88. Error("Cache clean failed");
  89. }
  90. }
  91. void ProjectCmd::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  92. {
  93. bool result = eventData[ProjectLoaded::P_RESULT].GetBool();
  94. if (!eventData[ProjectLoaded::P_RESULT].GetBool())
  95. Error("Project load failed");
  96. else
  97. Finished();
  98. }
  99. void ProjectCmd::Run()
  100. {
  101. if (command_ == "cache")
  102. {
  103. // Nothing to do here
  104. }
  105. else
  106. {
  107. Finished();
  108. }
  109. }
  110. }