Skip to content

Commit 8599313

Browse files
authored
feat(cli.rs): env vars for beforeDev/beforeBuild commands, closes #2610 (#2655)
1 parent 0fe680d commit 8599313

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

.changes/before-script-envs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Define `PLATFORM`, `ARCH`, `FAMILY` and `PLATFORM_TYPE` environment variables for the `beforeDevCommand` and `beforeBuildCommand` scripts.

docs/api/config.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ In addition to the JSON defined on the `tauri.conf.json` file, Tauri reads a pla
2727
The target directory <em>must</em> contain an index.html file.
2828
</div>`},
2929
{property: "devPath", type: "string", description: `Can be a path to a folder (either absolute or relative to tauri.conf.json) or a URL (like a live reload server).`},
30-
{property: "beforeDevCommand", optional: true, type: "string", description: `A command to run before starting Tauri in dev mode.`},
31-
{property: "beforeBuildCommand", optional: true, type: "string", description: `A command to run before starting Tauri in build mode.`},
30+
{property: "beforeDevCommand", optional: true, type: "string", description: `A command to run before starting Tauri in dev mode. The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.`},
31+
{property: "beforeBuildCommand", optional: true, type: "string", description: `A command to run before starting Tauri's build pipeline. The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.`},
3232
{property: "withGlobalTauri", optional: true, type: "boolean", description: "Enables the API injection to the window.__TAURI__ object. Useful if you're using Vanilla JS instead of importing the API using Rollup or Webpack. Reduces the command security since any external code can access it, so be careful with XSS attacks."}
3333
]}/>
3434

tooling/cli.rs/config_definition.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,14 +815,18 @@ pub struct BuildConfig {
815815
/// The path or URL to use on development.
816816
#[serde(default = "default_dev_path")]
817817
pub dev_path: AppUrl,
818-
/// the path to the app's dist dir. This path must contain your index.html file.
818+
/// The path to the app's dist dir. This path must contain your index.html file.
819819
#[serde(default = "default_dist_dir")]
820820
pub dist_dir: AppUrl,
821-
/// a shell command to run before `tauri dev` kicks in
821+
/// A shell command to run before `tauri dev` kicks in.
822+
///
823+
/// The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.
822824
pub before_dev_command: Option<String>,
823-
/// a shell command to run before `tauri build` kicks in
825+
/// A shell command to run before `tauri build` kicks in.
826+
///
827+
/// The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.
824828
pub before_build_command: Option<String>,
825-
/// features passed to `cargo` commands
829+
/// Features passed to `cargo` commands.
826830
pub features: Option<Vec<String>>,
827831
/// Whether we should inject the Tauri API on `window.__TAURI__` or not.
828832
#[serde(default)]

tooling/cli.rs/schema.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@
261261
"type": "object",
262262
"properties": {
263263
"beforeBuildCommand": {
264-
"description": "a shell command to run before `tauri build` kicks in",
264+
"description": "A shell command to run before `tauri build` kicks in.\n\nThe PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.",
265265
"type": [
266266
"string",
267267
"null"
268268
]
269269
},
270270
"beforeDevCommand": {
271-
"description": "a shell command to run before `tauri dev` kicks in",
271+
"description": "A shell command to run before `tauri dev` kicks in.\n\nThe PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.",
272272
"type": [
273273
"string",
274274
"null"
@@ -284,7 +284,7 @@
284284
]
285285
},
286286
"distDir": {
287-
"description": "the path to the app's dist dir. This path must contain your index.html file.",
287+
"description": "The path to the app's dist dir. This path must contain your index.html file.",
288288
"default": "../dist",
289289
"allOf": [
290290
{
@@ -293,7 +293,7 @@
293293
]
294294
},
295295
"features": {
296-
"description": "features passed to `cargo` commands",
296+
"description": "Features passed to `cargo` commands.",
297297
"type": [
298298
"array",
299299
"null"

tooling/cli.rs/src/build.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use tauri_bundler::bundle::{bundle_project, PackageType};
99

1010
use crate::helpers::{
1111
app_paths::{app_dir, tauri_dir},
12+
command_env,
1213
config::{get as get_config, AppUrl},
1314
execute_with_output,
1415
manifest::rewrite_manifest,
@@ -89,15 +90,17 @@ impl Build {
8990
&mut Command::new("cmd")
9091
.arg("/C")
9192
.arg(before_build)
92-
.current_dir(app_dir()),
93+
.current_dir(app_dir())
94+
.envs(command_env()),
9395
)
9496
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?;
9597
#[cfg(not(target_os = "windows"))]
9698
execute_with_output(
9799
&mut Command::new("sh")
98100
.arg("-c")
99101
.arg(before_build)
100-
.current_dir(app_dir()),
102+
.current_dir(app_dir())
103+
.envs(command_env()),
101104
)
102105
.with_context(|| format!("failed to run `{}` with `sh -c`", before_build))?;
103106
}

tooling/cli.rs/src/dev.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::helpers::{
66
app_paths::{app_dir, tauri_dir},
7+
command_env,
78
config::{get as get_config, reload as reload_config},
89
manifest::{get_workspace_members, rewrite_manifest},
910
Logger,
@@ -141,13 +142,15 @@ impl Dev {
141142
.arg("/C")
142143
.arg(before_dev)
143144
.current_dir(app_dir())
145+
.envs(command_env())
144146
.spawn()
145147
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_dev))?;
146148
#[cfg(not(target_os = "windows"))]
147149
let child = Command::new("sh")
148150
.arg("-c")
149151
.arg(before_dev)
150152
.current_dir(app_dir())
153+
.envs(command_env())
151154
.spawn()
152155
.with_context(|| format!("failed to run `{}` with `sh -c`", before_dev))?;
153156
BEFORE_DEV.set(Mutex::new(child)).unwrap();

tooling/cli.rs/src/helpers/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod updater_signature;
1212
pub use logger::Logger;
1313

1414
use std::{
15+
collections::HashMap,
1516
io::{BufRead, BufReader},
1617
process::{Command, Stdio},
1718
};
@@ -37,3 +38,20 @@ pub fn execute_with_output(cmd: &mut Command) -> crate::Result<()> {
3738
Err(anyhow::anyhow!("command failed"))
3839
}
3940
}
41+
42+
pub fn command_env() -> HashMap<String, String> {
43+
let mut map = HashMap::new();
44+
map.insert("PLATFORM".into(), std::env::consts::OS.into());
45+
map.insert("ARCH".into(), std::env::consts::ARCH.into());
46+
map.insert("FAMILY".into(), std::env::consts::FAMILY.into());
47+
map.insert("VERSION".into(), os_info::get().version().to_string());
48+
49+
#[cfg(target_os = "linux")]
50+
map.insert("PLATFORM_TYPE".into(), "Linux".into());
51+
#[cfg(target_os = "windows")]
52+
map.insert("PLATFORM_TYPE".into(), "Windows_NT".into());
53+
#[cfg(target_os = "macos")]
54+
map.insert("PLATFORM_TYPE".into(), "Darwing".into());
55+
56+
map
57+
}

0 commit comments

Comments
 (0)