cmWIXFilesSourceWriter.cxx 4.5 KB

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