Skip to content

Commit 266156a

Browse files
authored
feat(core): add BaseDirectory::Temp and $TEMP variable (#3763)
1 parent 6054e98 commit 266156a

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
lines changed

.changes/temp-base-directory.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"api": patch
3+
"tauri": patch
4+
---
5+
6+
Added `Temp` to the `BaseDirectory` enum.

.changes/temp-scope-variable.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Added `$TEMP` to the allowed variables to the filesystem and asset protocol scopes.

core/tauri-utils/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ macro_rules! check_feature {
780780
/// Each pattern can start with a variable that resolves to a system base directory.
781781
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
782782
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
783-
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.
783+
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
784784
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
785785
#[cfg_attr(feature = "schema", derive(JsonSchema))]
786786
#[serde(untagged)]
@@ -1071,7 +1071,7 @@ pub struct ShellAllowedCommand {
10711071
/// It can start with a variable that resolves to a system base directory.
10721072
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
10731073
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
1074-
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.
1074+
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
10751075
#[serde(rename = "cmd")]
10761076
pub command: PathBuf,
10771077

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/path.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
//! Types and functions related to file system path operations.
66
7-
use std::path::{Component, Path, PathBuf};
7+
use std::{
8+
env::temp_dir,
9+
path::{Component, Path, PathBuf},
10+
};
811

912
use crate::{Config, Env, PackageInfo};
1013

@@ -61,6 +64,9 @@ pub enum BaseDirectory {
6164
/// Resolves to `BaseDirectory::Home/Library/Logs/{bundle_identifier}` on macOS
6265
/// and `BaseDirectory::Config/{bundle_identifier}/logs` on linux and windows.
6366
Log,
67+
/// A temporary directory.
68+
/// Resolves to [`temp_dir`].
69+
Temp,
6470
}
6571

6672
impl BaseDirectory {
@@ -86,6 +92,7 @@ impl BaseDirectory {
8692
Self::Resource => "$RESOURCE",
8793
Self::App => "$APP",
8894
Self::Log => "$LOG",
95+
Self::Temp => "$TEMP",
8996
}
9097
}
9198

@@ -111,6 +118,7 @@ impl BaseDirectory {
111118
"$RESOURCE" => Self::Resource,
112119
"$APP" => Self::App,
113120
"$LOG" => Self::Log,
121+
"$TEMP" => Self::Temp,
114122
_ => return None,
115123
};
116124
Some(res)
@@ -162,6 +170,7 @@ pub fn parse<P: AsRef<Path>>(
162170
}
163171
p.push(component);
164172
}
173+
println!("res {:?}", p);
165174

166175
Ok(p)
167176
}
@@ -236,6 +245,7 @@ pub fn resolve_path<P: AsRef<Path>>(
236245
BaseDirectory::Resource => resource_dir(package_info, env),
237246
BaseDirectory::App => app_dir(config),
238247
BaseDirectory::Log => log_dir(config),
248+
BaseDirectory::Temp => Some(temp_dir()),
239249
};
240250
if let Some(mut base_dir_path_value) = base_dir_path {
241251
// use the same path resolution mechanism as the bundler's resource injection algorithm

tooling/api/src/fs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
* [[path.localDataDir | `$LOCALDATA`]], [[path.desktopDir | `$DESKTOP`]], [[path.documentDir | `$DOCUMENT`]],
6060
* [[path.downloadDir | `$DOWNLOAD`]], [[path.executableDir | `$EXE`]], [[path.fontDir | `$FONT`]], [[path.homeDir | `$HOME`]],
6161
* [[path.pictureDir | `$PICTURE`]], [[path.publicDir | `$PUBLIC`]], [[path.runtimeDir | `$RUNTIME`]],
62-
* [[path.templateDir | `$TEMPLATE`]], [[path.videoDir | `$VIDEO`]], [[path.resourceDir | `$RESOURCE`]], [[path.appDir | `$APP`]].
62+
* [[path.templateDir | `$TEMPLATE`]], [[path.videoDir | `$VIDEO`]], [[path.resourceDir | `$RESOURCE`]], [[path.appDir | `$APP`]],
63+
* [[path.logDir | `$LOG`]], [[os.tempdir | `$TEMP`]].
6364
*
6465
* Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
6566
*
@@ -89,7 +90,8 @@ export enum BaseDirectory {
8990
Video,
9091
Resource,
9192
App,
92-
Log
93+
Log,
94+
Temp
9395
}
9496

9597
interface FsOptions {

tooling/cli/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@
10301030
"additionalProperties": false
10311031
},
10321032
"FsAllowlistScope": {
1033-
"description": "Filesystem scope definition. It is a list of glob patterns that restrict the API access from the webview.\n\nEach pattern can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.",
1033+
"description": "Filesystem scope definition. It is a list of glob patterns that restrict the API access from the webview.\n\nEach pattern can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.",
10341034
"anyOf": [
10351035
{
10361036
"description": "A list of paths that are allowed by this scope.",
@@ -1429,7 +1429,7 @@
14291429
]
14301430
},
14311431
"cmd": {
1432-
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.",
1432+
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.",
14331433
"type": "string"
14341434
},
14351435
"name": {

0 commit comments

Comments
 (0)