|
| 1 | +// Copyright 2019-2021 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use std::path::{Component, Path, PathBuf}; |
| 6 | + |
| 7 | +/// Given a path (absolute or relative) to a resource file, returns the |
| 8 | +/// relative path from the bundle resources directory where that resource |
| 9 | +/// should be stored. |
| 10 | +pub fn resource_relpath(path: &Path) -> PathBuf { |
| 11 | + let mut dest = PathBuf::new(); |
| 12 | + for component in path.components() { |
| 13 | + match component { |
| 14 | + Component::Prefix(_) => {} |
| 15 | + Component::RootDir => dest.push("_root_"), |
| 16 | + Component::CurDir => {} |
| 17 | + Component::ParentDir => dest.push("_up_"), |
| 18 | + Component::Normal(string) => dest.push(string), |
| 19 | + } |
| 20 | + } |
| 21 | + dest |
| 22 | +} |
| 23 | + |
| 24 | +/// Parses the external binaries to bundle, adding the target triple suffix to each of them. |
| 25 | +pub fn external_binaries(external_binaries: &[String], target_triple: &str) -> Vec<String> { |
| 26 | + let mut paths = Vec::new(); |
| 27 | + for curr_path in external_binaries { |
| 28 | + paths.push(format!( |
| 29 | + "{}-{}{}", |
| 30 | + curr_path, |
| 31 | + target_triple, |
| 32 | + if cfg!(windows) { ".exe" } else { "" } |
| 33 | + )); |
| 34 | + } |
| 35 | + paths |
| 36 | +} |
| 37 | + |
| 38 | +/// A helper to iterate through resources. |
| 39 | +pub struct ResourcePaths<'a> { |
| 40 | + /// the patterns to iterate. |
| 41 | + pattern_iter: std::slice::Iter<'a, String>, |
| 42 | + /// the glob iterator if the path from the current iteration is a glob pattern. |
| 43 | + glob_iter: Option<glob::Paths>, |
| 44 | + /// the walkdir iterator if the path from the current iteration is a directory. |
| 45 | + walk_iter: Option<walkdir::IntoIter>, |
| 46 | + /// whether the resource paths allows directories or not. |
| 47 | + allow_walk: bool, |
| 48 | + /// the pattern of the current iteration. |
| 49 | + current_pattern: Option<String>, |
| 50 | + /// whether the current pattern is valid or not. |
| 51 | + current_pattern_is_valid: bool, |
| 52 | +} |
| 53 | + |
| 54 | +impl<'a> ResourcePaths<'a> { |
| 55 | + /// Creates a new ResourcePaths from a slice of patterns to iterate |
| 56 | + pub fn new(patterns: &'a [String], allow_walk: bool) -> ResourcePaths<'a> { |
| 57 | + ResourcePaths { |
| 58 | + pattern_iter: patterns.iter(), |
| 59 | + glob_iter: None, |
| 60 | + walk_iter: None, |
| 61 | + allow_walk, |
| 62 | + current_pattern: None, |
| 63 | + current_pattern_is_valid: false, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<'a> Iterator for ResourcePaths<'a> { |
| 69 | + type Item = crate::Result<PathBuf>; |
| 70 | + |
| 71 | + fn next(&mut self) -> Option<crate::Result<PathBuf>> { |
| 72 | + loop { |
| 73 | + if let Some(ref mut walk_entries) = self.walk_iter { |
| 74 | + if let Some(entry) = walk_entries.next() { |
| 75 | + let entry = match entry { |
| 76 | + Ok(entry) => entry, |
| 77 | + Err(error) => return Some(Err(crate::Error::from(error))), |
| 78 | + }; |
| 79 | + let path = entry.path(); |
| 80 | + if path.is_dir() { |
| 81 | + continue; |
| 82 | + } |
| 83 | + self.current_pattern_is_valid = true; |
| 84 | + return Some(Ok(path.to_path_buf())); |
| 85 | + } |
| 86 | + } |
| 87 | + self.walk_iter = None; |
| 88 | + if let Some(ref mut glob_paths) = self.glob_iter { |
| 89 | + if let Some(glob_result) = glob_paths.next() { |
| 90 | + let path = match glob_result { |
| 91 | + Ok(path) => path, |
| 92 | + Err(error) => return Some(Err(error.into())), |
| 93 | + }; |
| 94 | + if path.is_dir() { |
| 95 | + if self.allow_walk { |
| 96 | + let walk = walkdir::WalkDir::new(path); |
| 97 | + self.walk_iter = Some(walk.into_iter()); |
| 98 | + continue; |
| 99 | + } else { |
| 100 | + return Some(Err(crate::Error::NotAllowedToWalkDir(path))); |
| 101 | + } |
| 102 | + } |
| 103 | + self.current_pattern_is_valid = true; |
| 104 | + return Some(Ok(path)); |
| 105 | + } else if let Some(current_path) = &self.current_pattern { |
| 106 | + if !self.current_pattern_is_valid { |
| 107 | + self.glob_iter = None; |
| 108 | + return Some(Err(crate::Error::GlobPathNotFound(current_path.clone()))); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + self.glob_iter = None; |
| 113 | + if let Some(pattern) = self.pattern_iter.next() { |
| 114 | + self.current_pattern = Some(pattern.to_string()); |
| 115 | + self.current_pattern_is_valid = false; |
| 116 | + let glob = match glob::glob(pattern) { |
| 117 | + Ok(glob) => glob, |
| 118 | + Err(error) => return Some(Err(error.into())), |
| 119 | + }; |
| 120 | + self.glob_iter = Some(glob); |
| 121 | + continue; |
| 122 | + } |
| 123 | + return None; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments