|
| 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 super::InvokeResponse; |
| 6 | +use crate::{api::path::BaseDirectory, Config, PackageInfo}; |
| 7 | +use serde::Deserialize; |
| 8 | +#[cfg(path_all)] |
| 9 | +use std::path::{Path, PathBuf}; |
| 10 | +use std::sync::Arc; |
| 11 | +/// The API descriptor. |
| 12 | +#[derive(Deserialize)] |
| 13 | +#[serde(tag = "cmd", rename_all = "camelCase")] |
| 14 | +pub enum Cmd { |
| 15 | + ResolvePath { |
| 16 | + path: String, |
| 17 | + directory: Option<BaseDirectory>, |
| 18 | + }, |
| 19 | + Resolve { |
| 20 | + paths: Vec<String>, |
| 21 | + }, |
| 22 | + Normalize { |
| 23 | + path: String, |
| 24 | + }, |
| 25 | + Join { |
| 26 | + paths: Vec<String>, |
| 27 | + }, |
| 28 | + Dirname { |
| 29 | + path: String, |
| 30 | + }, |
| 31 | + Extname { |
| 32 | + path: String, |
| 33 | + }, |
| 34 | + Basename { |
| 35 | + path: String, |
| 36 | + ext: Option<String>, |
| 37 | + }, |
| 38 | + IsAbsolute { |
| 39 | + path: String, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +impl Cmd { |
| 44 | + #[allow(unused_variables)] |
| 45 | + pub fn run( |
| 46 | + self, |
| 47 | + config: Arc<Config>, |
| 48 | + package_info: &PackageInfo, |
| 49 | + ) -> crate::Result<InvokeResponse> { |
| 50 | + #[cfg(path_all)] |
| 51 | + return match self { |
| 52 | + Cmd::ResolvePath { directory, path } => { |
| 53 | + resolve_path_handler(&config, package_info, path, directory).map(Into::into) |
| 54 | + } |
| 55 | + Cmd::Resolve { paths } => resolve(paths).map(Into::into), |
| 56 | + Cmd::Normalize { path } => normalize(path).map(Into::into), |
| 57 | + Cmd::Join { paths } => join(paths).map(Into::into), |
| 58 | + Cmd::Dirname { path } => dirname(path).map(Into::into), |
| 59 | + Cmd::Extname { path } => extname(path).map(Into::into), |
| 60 | + Cmd::Basename { path, ext } => basename(path, ext).map(Into::into), |
| 61 | + Cmd::IsAbsolute { path } => Ok(Path::new(&path).is_absolute()).map(Into::into), |
| 62 | + }; |
| 63 | + #[cfg(not(path_all))] |
| 64 | + Err(crate::Error::ApiNotAllowlisted("path".into())) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(path_all)] |
| 69 | +pub fn resolve_path_handler( |
| 70 | + config: &Config, |
| 71 | + package_info: &PackageInfo, |
| 72 | + path: String, |
| 73 | + directory: Option<BaseDirectory>, |
| 74 | +) -> crate::Result<PathBuf> { |
| 75 | + crate::api::path::resolve_path(config, package_info, path, directory).map_err(Into::into) |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(path_all)] |
| 79 | +fn resolve(paths: Vec<String>) -> crate::Result<String> { |
| 80 | + // start with the current directory |
| 81 | + let mut resolved_path = PathBuf::new().join("."); |
| 82 | + |
| 83 | + for path in paths { |
| 84 | + let path_buf = PathBuf::from(path); |
| 85 | + |
| 86 | + // if we encounter an absolute path, we use it as the starting path for next iteration |
| 87 | + if path_buf.is_absolute() { |
| 88 | + resolved_path = path_buf; |
| 89 | + } else { |
| 90 | + resolved_path = resolved_path.join(&path_buf); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + normalize(resolved_path.to_string_lossy().to_string()) |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(path_all)] |
| 98 | +fn normalize(path: String) -> crate::Result<String> { |
| 99 | + let path = std::fs::canonicalize(path)?; |
| 100 | + let path = path.to_string_lossy().to_string(); |
| 101 | + |
| 102 | + // remove `\\\\?\\` on windows, UNC path |
| 103 | + #[cfg(target_os = "windows")] |
| 104 | + let path = path.replace("\\\\?\\", ""); |
| 105 | + |
| 106 | + Ok(path) |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(path_all)] |
| 110 | +fn join(paths: Vec<String>) -> crate::Result<String> { |
| 111 | + let mut joined_path = PathBuf::new(); |
| 112 | + for path in paths { |
| 113 | + joined_path = joined_path.join(path); |
| 114 | + } |
| 115 | + normalize(joined_path.to_string_lossy().to_string()) |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(path_all)] |
| 119 | +fn dirname(path: String) -> crate::Result<String> { |
| 120 | + match Path::new(&path).parent() { |
| 121 | + Some(path) => Ok(path.to_string_lossy().to_string()), |
| 122 | + None => Err(crate::Error::FailedToExecuteApi(crate::api::Error::Path( |
| 123 | + "Couldn't get the parent directory".into(), |
| 124 | + ))), |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(path_all)] |
| 129 | +fn extname(path: String) -> crate::Result<String> { |
| 130 | + match Path::new(&path) |
| 131 | + .extension() |
| 132 | + .and_then(std::ffi::OsStr::to_str) |
| 133 | + { |
| 134 | + Some(path) => Ok(path.to_string()), |
| 135 | + None => Err(crate::Error::FailedToExecuteApi(crate::api::Error::Path( |
| 136 | + "Couldn't get the extension of the file".into(), |
| 137 | + ))), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#[cfg(path_all)] |
| 142 | +fn basename(path: String, ext: Option<String>) -> crate::Result<String> { |
| 143 | + match Path::new(&path) |
| 144 | + .file_name() |
| 145 | + .and_then(std::ffi::OsStr::to_str) |
| 146 | + { |
| 147 | + Some(path) => Ok(if let Some(ext) = ext { |
| 148 | + path.replace(ext.as_str(), "") |
| 149 | + } else { |
| 150 | + path.to_string() |
| 151 | + }), |
| 152 | + None => Err(crate::Error::FailedToExecuteApi(crate::api::Error::Path( |
| 153 | + "Couldn't get the basename".into(), |
| 154 | + ))), |
| 155 | + } |
| 156 | +} |
0 commit comments