cmWIXFilesSourceWriter.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmWIXFilesSourceWriter.h"
  4. #include "cmWIXAccessControlList.h"
  5. #include <cmInstalledFile.h>
  6. #include <cmSystemTools.h>
  7. #include <cmUuid.h>
  8. #include "cm_sys_stat.h"
  9. cmWIXFilesSourceWriter::cmWIXFilesSourceWriter(cmCPackLog* logger,
  10. std::string const& filename,
  11. GuidType componentGuidType)
  12. : cmWIXSourceWriter(logger, filename, componentGuidType)
  13. {
  14. }
  15. void cmWIXFilesSourceWriter::EmitShortcut(std::string const& id,
  16. cmWIXShortcut const& shortcut,
  17. std::string const& shortcutPrefix,
  18. size_t shortcutIndex)
  19. {
  20. std::ostringstream shortcutId;
  21. shortcutId << shortcutPrefix << id;
  22. if (shortcutIndex > 0) {
  23. shortcutId << "_" << shortcutIndex;
  24. }
  25. std::string fileId = std::string("CM_F") + id;
  26. BeginElement("Shortcut");
  27. AddAttribute("Id", shortcutId.str());
  28. AddAttribute("Name", shortcut.label);
  29. std::string target = "[#" + fileId + "]";
  30. AddAttribute("Target", target);
  31. AddAttribute("WorkingDirectory", shortcut.workingDirectoryId);
  32. EndElement("Shortcut");
  33. }
  34. void cmWIXFilesSourceWriter::EmitRemoveFolder(std::string const& id)
  35. {
  36. BeginElement("RemoveFolder");
  37. AddAttribute("Id", id);
  38. AddAttribute("On", "uninstall");
  39. EndElement("RemoveFolder");
  40. }
  41. void cmWIXFilesSourceWriter::EmitInstallRegistryValue(
  42. std::string const& registryKey, std::string const& cpackComponentName,
  43. std::string const& suffix)
  44. {
  45. std::string valueName;
  46. if (!cpackComponentName.empty()) {
  47. valueName = cpackComponentName + "_";
  48. }
  49. valueName += "installed";
  50. valueName += suffix;
  51. BeginElement("RegistryValue");
  52. AddAttribute("Root", "HKCU");
  53. AddAttribute("Key", registryKey);
  54. AddAttribute("Name", valueName);
  55. AddAttribute("Type", "integer");
  56. AddAttribute("Value", "1");
  57. AddAttribute("KeyPath", "yes");
  58. EndElement("RegistryValue");
  59. }
  60. void cmWIXFilesSourceWriter::EmitUninstallShortcut(
  61. std::string const& packageName)
  62. {
  63. BeginElement("Shortcut");
  64. AddAttribute("Id", "UNINSTALL");
  65. AddAttribute("Name", "Uninstall " + packageName);
  66. AddAttribute("Description", "Uninstalls " + packageName);
  67. AddAttribute("Target", "[SystemFolder]msiexec.exe");
  68. AddAttribute("Arguments", "/x [ProductCode]");
  69. EndElement("Shortcut");
  70. }
  71. std::string cmWIXFilesSourceWriter::EmitComponentCreateFolder(
  72. std::string const& directoryId, std::string const& guid,
  73. cmInstalledFile const* installedFile)
  74. {
  75. std::string componentId = std::string("CM_C_EMPTY_") + directoryId;
  76. BeginElement("DirectoryRef");
  77. AddAttribute("Id", directoryId);
  78. BeginElement("Component");
  79. AddAttribute("Id", componentId);
  80. AddAttribute("Guid", guid);
  81. BeginElement("CreateFolder");
  82. if (installedFile) {
  83. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  84. acl.Apply();
  85. }
  86. EndElement("CreateFolder");
  87. EndElement("Component");
  88. EndElement("DirectoryRef");
  89. return componentId;
  90. }
  91. std::string cmWIXFilesSourceWriter::EmitComponentFile(
  92. std::string const& directoryId, std::string const& id,
  93. std::string const& filePath, cmWIXPatch& patch,
  94. cmInstalledFile const* installedFile)
  95. {
  96. std::string componentId = std::string("CM_C") + id;
  97. std::string fileId = std::string("CM_F") + id;
  98. std::string guid = CreateGuidFromComponentId(componentId);
  99. BeginElement("DirectoryRef");
  100. AddAttribute("Id", directoryId);
  101. BeginElement("Component");
  102. AddAttribute("Id", componentId);
  103. AddAttribute("Guid", guid);
  104. if (installedFile) {
  105. if (installedFile->GetPropertyAsBool("CPACK_NEVER_OVERWRITE")) {
  106. AddAttribute("NeverOverwrite", "yes");
  107. }
  108. if (installedFile->GetPropertyAsBool("CPACK_PERMANENT")) {
  109. AddAttribute("Permanent", "yes");
  110. }
  111. }
  112. BeginElement("File");
  113. AddAttribute("Id", fileId);
  114. AddAttribute("Source", filePath);
  115. AddAttribute("KeyPath", "yes");
  116. mode_t fileMode = 0;
  117. cmSystemTools::GetPermissions(filePath.c_str(), fileMode);
  118. if (!(fileMode & S_IWRITE)) {
  119. AddAttribute("ReadOnly", "yes");
  120. }
  121. if (installedFile) {
  122. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  123. acl.Apply();
  124. }
  125. patch.ApplyFragment(fileId, *this);
  126. EndElement("File");
  127. patch.ApplyFragment(componentId, *this);
  128. EndElement("Component");
  129. EndElement("DirectoryRef");
  130. return componentId;
  131. }