Skip to content

Commit 11012a1

Browse files
fix(bundler): preserve mapped resource names in wix (#15681)
* fix(bundler): preserve mapped resource names in wix * Refactor the entire `generate_resource_data` * Add `ResourceFile::new` * Use back the hashmap but with entry * Remove `ResourceDirectory::name` and rely on `directories` * Fix tests * Fix git stage * Add change file * Make `target_name` optional * Cleanup --------- Co-authored-by: Tony <legendmastertony@gmail.com> Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com>
1 parent 5ab3175 commit 11012a1

2 files changed

Lines changed: 88 additions & 109 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-bundler": patch:bug
3+
"tauri-cli": patch:bug
4+
"@tauri-apps/cli": patch:bug
5+
---
6+
7+
8+
Fix WiX bundler doesn't respect the resource's target file name.

‎crates/tauri-bundler/src/bundle/windows/msi/mod.rs‎

Lines changed: 80 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ const UUID_NAMESPACE: [u8; 16] = [
9797
0xfd, 0x85, 0x95, 0xa8, 0x17, 0xa3, 0x47, 0x4e, 0xa6, 0x16, 0x76, 0x14, 0x8d, 0xfa, 0x0c, 0x7b,
9898
];
9999

100-
/// Mapper between a resource directory name and its ResourceDirectory descriptor.
101-
type ResourceMap = BTreeMap<String, ResourceDirectory>;
102-
103100
#[derive(Debug, Deserialize)]
104101
struct LanguageMetadata {
105102
#[serde(rename = "asciiCode")]
@@ -123,28 +120,36 @@ struct Binary {
123120

124121
/// A Resource file to bundle with WIX.
125122
/// This data structure is needed because WIX requires each path to have its own `id` and `guid`.
126-
#[derive(Serialize, Clone)]
127123
struct ResourceFile {
128124
/// the GUID to use on the WIX XML.
129125
guid: String,
130126
/// the id to use on the WIX XML.
131127
id: String,
132-
/// the file path.
133-
path: PathBuf,
128+
/// the source file path.
129+
source_path: PathBuf,
130+
/// file name override, defaulting to the file name of [`Self::source_path`].
131+
target_name_override: Option<String>,
132+
}
133+
134+
impl ResourceFile {
135+
fn new(source_path: PathBuf, target_name_override: Option<String>) -> Self {
136+
Self {
137+
id: format!("I{}", Uuid::new_v4().as_simple()),
138+
guid: Uuid::new_v4().to_string(),
139+
source_path,
140+
target_name_override,
141+
}
142+
}
134143
}
135144

136145
/// A resource directory to bundle with WIX.
137146
/// This data structure is needed because WIX requires each path to have its own `id` and `guid`.
138-
#[derive(Serialize)]
147+
#[derive(Default)]
139148
struct ResourceDirectory {
140-
/// the directory path.
141-
path: String,
142-
/// the directory name of the described resource.
143-
name: String,
144149
/// the files of the described resource directory.
145150
files: Vec<ResourceFile>,
146151
/// the directories that are children of the described resource directory.
147-
directories: Vec<ResourceDirectory>,
152+
directories: HashMap<String, ResourceDirectory>,
148153
}
149154

150155
impl ResourceDirectory {
@@ -154,38 +159,45 @@ impl ResourceDirectory {
154159
}
155160

156161
/// Generates the wix XML string to bundle this directory resources recursively
157-
fn get_wix_data(self) -> crate::Result<(String, Vec<String>)> {
162+
fn render_wix(self, directory_name: Option<String>) -> crate::Result<(String, Vec<String>)> {
158163
let mut files = String::from("");
159164
let mut file_ids = Vec::new();
160165
for file in self.files {
161-
file_ids.push(file.id.clone());
166+
let ResourceFile {
167+
id,
168+
guid,
169+
source_path,
170+
target_name_override,
171+
} = file;
172+
let name_attribute = target_name_override
173+
.map(|name| format!(r#"Name="{}" "#, html_escape(&name)))
174+
.unwrap_or_default();
175+
let source_path = html_escape(&source_path.to_string_lossy());
162176
files.push_str(
163-
format!(
164-
r#"<Component Id="{id}" Guid="{guid}" Win64="$(var.Win64)" KeyPath="yes"><File Id="PathFile_{id}" Source="{path}" /></Component>"#,
165-
id = file.id,
166-
guid = file.guid,
167-
path = html_escape(&file.path.display().to_string())
168-
).as_str()
177+
&format!(
178+
r#"<Component Id="{id}" Guid="{guid}" Win64="$(var.Win64)" KeyPath="yes"><File Id="PathFile_{id}" Source="{source_path}" {name_attribute}/></Component>"#,
179+
)
169180
);
181+
file_ids.push(id);
170182
}
171183
let mut directories = String::from("");
172-
for directory in self.directories {
173-
let (wix_string, ids) = directory.get_wix_data()?;
184+
for (directory_name, directory) in self.directories {
185+
let (wix_string, ids) = directory.render_wix(Some(directory_name))?;
174186
for id in ids {
175187
file_ids.push(id)
176188
}
177189
directories.push_str(wix_string.as_str());
178190
}
179-
let wix_string = if self.name.is_empty() {
180-
format!("{files}{directories}")
181-
} else {
191+
let wix_string = if let Some(directory_name) = directory_name {
182192
format!(
183193
r#"<Directory Id="I{id}" Name="{name}">{files}{directories}</Directory>"#,
184194
id = Uuid::new_v4().as_simple(),
185-
name = html_escape(&self.name),
195+
name = html_escape(&directory_name),
186196
files = files,
187197
directories = directories,
188198
)
199+
} else {
200+
format!("{files}{directories}")
189201
};
190202

191203
Ok((wix_string, file_ids))
@@ -633,15 +645,7 @@ pub fn build_wix_app_installer(
633645
data.insert("binaries", binaries_json);
634646

635647
let resources = generate_resource_data(settings)?;
636-
let mut resources_wix_string = String::from("");
637-
let mut files_ids = Vec::new();
638-
for (_, dir) in resources {
639-
let (wix_string, ids) = dir.get_wix_data()?;
640-
resources_wix_string.push_str(wix_string.as_str());
641-
for id in ids {
642-
files_ids.push(id);
643-
}
644-
}
648+
let (resources_wix_string, files_ids) = resources.render_wix(None)?;
645649

646650
data.insert("resources", to_json(resources_wix_string));
647651
data.insert("resource_file_ids", to_json(files_ids));
@@ -987,11 +991,11 @@ fn get_merge_modules(settings: &Settings) -> crate::Result<Vec<MergeModule>> {
987991
}
988992

989993
/// Generates the data required for the resource bundling on wix
990-
fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
991-
let mut resources = ResourceMap::new();
994+
fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceDirectory> {
992995
let cwd = std::env::current_dir()?;
993996

994-
let mut added_resources = Vec::new();
997+
let mut root_resource_directory = ResourceDirectory::default();
998+
let mut added_resources = HashSet::new();
995999

9961000
for resource in settings.resource_files().iter() {
9971001
let resource = resource?;
@@ -1004,75 +1008,44 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
10041008
if added_resources.contains(&resource_path) {
10051009
continue;
10061010
}
1007-
added_resources.push(resource_path.clone());
1011+
added_resources.insert(resource_path.clone());
10081012

10091013
if settings.windows().can_sign() && should_sign(&resource_path)? {
10101014
try_sign(&resource_path, settings)?;
10111015
}
10121016

1013-
let resource_entry = ResourceFile {
1014-
id: format!("I{}", Uuid::new_v4().as_simple()),
1015-
guid: Uuid::new_v4().to_string(),
1016-
path: resource_path.clone(),
1017-
};
1017+
let resource_entry = ResourceFile::new(
1018+
resource_path,
1019+
Some(
1020+
resource
1021+
.target()
1022+
.file_name()
1023+
.expect("failed to read resource file name")
1024+
.to_string_lossy()
1025+
.into_owned(),
1026+
),
1027+
);
10181028

1019-
// split the resource path directories
10201029
let target_path = resource.target();
10211030
let components_count = target_path.components().count();
10221031
let directories = target_path
10231032
.components()
10241033
.take(components_count - 1) // the last component is the file
10251034
.collect::<Vec<_>>();
10261035

1027-
// transform the directory structure to a chained vec structure
1028-
let first_directory = directories
1029-
.first()
1030-
.map(|d| d.as_os_str().to_string_lossy().into_owned())
1031-
.unwrap_or_else(String::new);
1032-
1033-
if !resources.contains_key(&first_directory) {
1034-
resources.insert(
1035-
first_directory.clone(),
1036-
ResourceDirectory {
1037-
path: first_directory.clone(),
1038-
name: first_directory.clone(),
1039-
directories: vec![],
1040-
files: vec![],
1041-
},
1042-
);
1043-
}
1036+
let mut directory_entry = &mut root_resource_directory;
10441037

1045-
let mut directory_entry = resources
1046-
.get_mut(&first_directory)
1047-
.expect("Unable to handle resources");
1048-
1049-
let mut path = String::new();
1050-
// the first component is already parsed on `first_directory` so we skip(1)
1051-
for directory in directories.into_iter().skip(1) {
1038+
for directory in directories {
10521039
let directory_name = directory
10531040
.as_os_str()
10541041
.to_os_string()
10551042
.into_string()
10561043
.expect("failed to read resource folder name");
1057-
path.push_str(directory_name.as_str());
1058-
path.push(std::path::MAIN_SEPARATOR);
10591044

1060-
let index = directory_entry
1045+
directory_entry = directory_entry
10611046
.directories
1062-
.iter()
1063-
.position(|f| f.path == path);
1064-
match index {
1065-
Some(i) => directory_entry = directory_entry.directories.get_mut(i).unwrap(),
1066-
None => {
1067-
directory_entry.directories.push(ResourceDirectory {
1068-
path: path.clone(),
1069-
name: directory_name,
1070-
directories: vec![],
1071-
files: vec![],
1072-
});
1073-
directory_entry = directory_entry.directories.iter_mut().last().unwrap();
1074-
}
1075-
}
1047+
.entry(directory_name)
1048+
.or_default();
10761049
}
10771050
directory_entry.add_file(resource_entry);
10781051
}
@@ -1085,12 +1058,8 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
10851058
if added_resources.contains(&resource_path.to_path_buf()) {
10861059
continue;
10871060
}
1088-
added_resources.push(resource_path.to_path_buf());
1089-
dlls.push(ResourceFile {
1090-
id: format!("I{}", Uuid::new_v4().as_simple()),
1091-
guid: Uuid::new_v4().to_string(),
1092-
path: resource_path.to_path_buf(),
1093-
});
1061+
added_resources.insert(resource_path.to_path_buf());
1062+
dlls.push(ResourceFile::new(resource_path.to_path_buf(), None));
10941063
}
10951064
}
10961065

@@ -1113,27 +1082,13 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
11131082
try_sign(resource_path, settings)?;
11141083
}
11151084

1116-
dlls.push(ResourceFile {
1117-
id: format!("I{}", Uuid::new_v4().as_simple()),
1118-
guid: Uuid::new_v4().to_string(),
1119-
path: resource_path.to_path_buf(),
1120-
});
1085+
dlls.push(ResourceFile::new(resource_path.to_path_buf(), None));
11211086
}
11221087
}
11231088

1124-
if !dlls.is_empty() {
1125-
resources
1126-
.entry("".to_string())
1127-
.and_modify(|r| r.files.append(&mut dlls))
1128-
.or_insert(ResourceDirectory {
1129-
path: "".to_string(),
1130-
name: "".to_string(),
1131-
directories: vec![],
1132-
files: dlls,
1133-
});
1134-
}
1089+
root_resource_directory.files.extend(dlls);
11351090

1136-
Ok(resources)
1091+
Ok(root_resource_directory)
11371092
}
11381093

11391094
#[cfg(test)]
@@ -1176,4 +1131,20 @@ mod tests {
11761131
assert_eq!(wix_identifier(""), "_");
11771132
assert_eq!(wix_identifier("app_1.2"), "app_1.2");
11781133
}
1134+
1135+
#[test]
1136+
fn includes_mapped_resource_file_name_in_wix_data() {
1137+
let resource = ResourceFile::new("MyFile".into(), Some("myFileRenamed".into()));
1138+
let resource_id = resource.id.clone();
1139+
let directory = ResourceDirectory {
1140+
files: vec![resource],
1141+
directories: HashMap::new(),
1142+
};
1143+
1144+
let (wix_data, file_ids) = directory.render_wix(None).unwrap();
1145+
1146+
assert_eq!(file_ids, vec![resource_id]);
1147+
assert!(wix_data.contains(r#"Name="myFileRenamed""#));
1148+
assert!(wix_data.contains(r#"Source="MyFile""#));
1149+
}
11791150
}

0 commit comments

Comments
 (0)