dotnet_solution.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*************************************************************************/
  2. /* dotnet_solution.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "dotnet_solution.h"
  31. #include "core/os/dir_access.h"
  32. #include "core/os/file_access.h"
  33. #include "../utils/path_utils.h"
  34. #include "../utils/string_utils.h"
  35. #include "csharp_project.h"
  36. #define SOLUTION_TEMPLATE \
  37. "Microsoft Visual Studio Solution File, Format Version 12.00\n" \
  38. "# Visual Studio 2012\n" \
  39. "%0\n" \
  40. "Global\n" \
  41. "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n" \
  42. "%1\n" \
  43. "\tEndGlobalSection\n" \
  44. "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n" \
  45. "%2\n" \
  46. "\tEndGlobalSection\n" \
  47. "EndGlobal\n"
  48. #define PROJECT_DECLARATION "Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"%0\", \"%1\", \"{%2}\"\nEndProject"
  49. #define SOLUTION_PLATFORMS_CONFIG "\t%0|Any CPU = %0|Any CPU"
  50. #define PROJECT_PLATFORMS_CONFIG \
  51. "\t\t{%0}.%1|Any CPU.ActiveCfg = %1|Any CPU\n" \
  52. "\t\t{%0}.%1|Any CPU.Build.0 = %1|Any CPU"
  53. void DotNetSolution::add_new_project(const String &p_name, const ProjectInfo &p_project_info) {
  54. projects[p_name] = p_project_info;
  55. }
  56. bool DotNetSolution::has_project(const String &p_name) const {
  57. return projects.find(p_name) != NULL;
  58. }
  59. const DotNetSolution::ProjectInfo &DotNetSolution::get_project_info(const String &p_name) const {
  60. return projects[p_name];
  61. }
  62. bool DotNetSolution::remove_project(const String &p_name) {
  63. return projects.erase(p_name);
  64. }
  65. Error DotNetSolution::save() {
  66. bool dir_exists = DirAccess::exists(path);
  67. ERR_EXPLAIN("The directory does not exist.");
  68. ERR_FAIL_COND_V(!dir_exists, ERR_FILE_NOT_FOUND);
  69. String projs_decl;
  70. String sln_platform_cfg;
  71. String proj_platform_cfg;
  72. for (Map<String, ProjectInfo>::Element *E = projects.front(); E; E = E->next()) {
  73. const String &name = E->key();
  74. const ProjectInfo &proj_info = E->value();
  75. bool is_front = E == projects.front();
  76. if (!is_front)
  77. projs_decl += "\n";
  78. projs_decl += sformat(PROJECT_DECLARATION, name, proj_info.relpath.replace("/", "\\"), proj_info.guid);
  79. for (int i = 0; i < proj_info.configs.size(); i++) {
  80. const String &config = proj_info.configs[i];
  81. if (i != 0 || !is_front) {
  82. sln_platform_cfg += "\n";
  83. proj_platform_cfg += "\n";
  84. }
  85. sln_platform_cfg += sformat(SOLUTION_PLATFORMS_CONFIG, config);
  86. proj_platform_cfg += sformat(PROJECT_PLATFORMS_CONFIG, proj_info.guid, config);
  87. }
  88. }
  89. String content = sformat(SOLUTION_TEMPLATE, projs_decl, sln_platform_cfg, proj_platform_cfg);
  90. FileAccess *file = FileAccess::open(path_join(path, name + ".sln"), FileAccess::WRITE);
  91. ERR_FAIL_NULL_V(file, ERR_FILE_CANT_WRITE);
  92. file->store_string(content);
  93. file->close();
  94. memdelete(file);
  95. return OK;
  96. }
  97. bool DotNetSolution::set_path(const String &p_existing_path) {
  98. if (p_existing_path.is_abs_path()) {
  99. path = p_existing_path;
  100. } else {
  101. String abspath;
  102. if (!rel_path_to_abs(p_existing_path, abspath))
  103. return false;
  104. path = abspath;
  105. }
  106. return true;
  107. }
  108. String DotNetSolution::get_path() {
  109. return path;
  110. }
  111. DotNetSolution::DotNetSolution(const String &p_name) {
  112. name = p_name;
  113. }