CacheCmd.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // Copyright (c) 2014-2017 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 "../Assets/AssetDatabase.h"
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Project/Project.h"
  29. #include "../Build/BuildEvents.h"
  30. #include "../Build/BuildSystem.h"
  31. #include "CacheCmd.h"
  32. namespace ToolCore
  33. {
  34. CacheCmd::CacheCmd(Context* context) : Command(context),
  35. cleanCache_(false),
  36. generateCache_(false)
  37. {
  38. }
  39. CacheCmd::~CacheCmd()
  40. {
  41. }
  42. bool CacheCmd::ParseInternal(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  43. {
  44. for (unsigned i = startIndex; i < arguments.Size(); i++)
  45. {
  46. if (arguments[i].Length() > 1)
  47. {
  48. String argument = arguments[i].ToLower();
  49. if (argument == "generate")
  50. {
  51. generateCache_ = true;
  52. continue;
  53. }
  54. // process any argument/value pairs
  55. if (arguments[i][0] != '-')
  56. continue;
  57. // eat additonal argument '-'
  58. while (argument.StartsWith("-"))
  59. {
  60. argument.Erase(0);
  61. }
  62. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  63. if (argument == "clean")
  64. cleanCache_ = true;
  65. }
  66. }
  67. return true;
  68. }
  69. bool CacheCmd::CreateCacheDirectory() const
  70. {
  71. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  72. if (fileSystem->DirExists(cachePath_))
  73. return true;
  74. fileSystem->CreateDir(cachePath_);
  75. if (!fileSystem->DirExists(cachePath_))
  76. {
  77. ATOMIC_LOGERRORF("Unable to create cache directory %s", cachePath_.CString());
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool CacheCmd::RemoveCacheDirectory() const
  83. {
  84. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  85. if (!fileSystem->DirExists(cachePath_))
  86. return true;
  87. fileSystem->RemoveDir(cachePath_, true);
  88. if (fileSystem->DirExists(cachePath_))
  89. {
  90. ATOMIC_LOGERRORF("Unable to remove cache directory %s", cachePath_.CString());
  91. return false;
  92. }
  93. return true;
  94. }
  95. bool CacheCmd::CleanCache() const
  96. {
  97. ATOMIC_LOGINFO("Cleaning Cache...");
  98. if (!RemoveCacheDirectory())
  99. return false;
  100. if (!CreateCacheDirectory())
  101. return false;
  102. return true;
  103. }
  104. bool CacheCmd::GenerateCache() const
  105. {
  106. ATOMIC_LOGINFO("Generating Cache... hold on");
  107. AssetDatabase* database = GetSubsystem<AssetDatabase>();
  108. database->ReimportAllAssets();
  109. ATOMIC_LOGINFO("Cache Generated");
  110. return true;
  111. }
  112. void CacheCmd::Run()
  113. {
  114. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  115. ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
  116. Project* project = tsystem->GetProject();
  117. cachePath_ = AddTrailingSlash(project->GetProjectPath()) + "Cache";
  118. if (cleanCache_)
  119. {
  120. if (!CleanCache())
  121. {
  122. Error("Unable to clean cache");
  123. return;
  124. }
  125. }
  126. if (generateCache_)
  127. {
  128. if (!GenerateCache())
  129. {
  130. Error("Unable to generate cache");
  131. return;
  132. }
  133. }
  134. Finished();
  135. }
  136. }