Skip to content

Commit

Permalink
feat(cli.rs): env vars for beforeDev/beforeBuild commands, closes #2610
Browse files Browse the repository at this point in the history
… (#2655)
  • Loading branch information
lucasfernog authored Sep 26, 2021
1 parent 0fe680d commit 8599313
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/before-script-envs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Define `PLATFORM`, `ARCH`, `FAMILY` and `PLATFORM_TYPE` environment variables for the `beforeDevCommand` and `beforeBuildCommand` scripts.
4 changes: 2 additions & 2 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ In addition to the JSON defined on the `tauri.conf.json` file, Tauri reads a pla
The target directory <em>must</em> contain an index.html file.
</div>`},
{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).`},
{property: "beforeDevCommand", optional: true, type: "string", description: `A command to run before starting Tauri in dev mode.`},
{property: "beforeBuildCommand", optional: true, type: "string", description: `A command to run before starting Tauri in build mode.`},
{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.`},
{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.`},
{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."}
]}/>

Expand Down
12 changes: 8 additions & 4 deletions tooling/cli.rs/config_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,14 +815,18 @@ pub struct BuildConfig {
/// The path or URL to use on development.
#[serde(default = "default_dev_path")]
pub dev_path: AppUrl,
/// the path to the app's dist dir. This path must contain your index.html file.
/// The path to the app's dist dir. This path must contain your index.html file.
#[serde(default = "default_dist_dir")]
pub dist_dir: AppUrl,
/// a shell command to run before `tauri dev` kicks in
/// A shell command to run before `tauri dev` kicks in.
///
/// The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.
pub before_dev_command: Option<String>,
/// a shell command to run before `tauri build` kicks in
/// A shell command to run before `tauri build` kicks in.
///
/// The PLATFORM, ARCH, FAMILY and PLATFORM_TYPE environment variables are set if you perform conditional compilation.
pub before_build_command: Option<String>,
/// features passed to `cargo` commands
/// Features passed to `cargo` commands.
pub features: Option<Vec<String>>,
/// Whether we should inject the Tauri API on `window.__TAURI__` or not.
#[serde(default)]
Expand Down
8 changes: 4 additions & 4 deletions tooling/cli.rs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@
"type": "object",
"properties": {
"beforeBuildCommand": {
"description": "a shell command to run before `tauri build` kicks in",
"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.",
"type": [
"string",
"null"
]
},
"beforeDevCommand": {
"description": "a shell command to run before `tauri dev` kicks in",
"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.",
"type": [
"string",
"null"
Expand All @@ -284,7 +284,7 @@
]
},
"distDir": {
"description": "the path to the app's dist dir. This path must contain your index.html file.",
"description": "The path to the app's dist dir. This path must contain your index.html file.",
"default": "../dist",
"allOf": [
{
Expand All @@ -293,7 +293,7 @@
]
},
"features": {
"description": "features passed to `cargo` commands",
"description": "Features passed to `cargo` commands.",
"type": [
"array",
"null"
Expand Down
7 changes: 5 additions & 2 deletions tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tauri_bundler::bundle::{bundle_project, PackageType};

use crate::helpers::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, AppUrl},
execute_with_output,
manifest::rewrite_manifest,
Expand Down Expand Up @@ -89,15 +90,17 @@ impl Build {
&mut Command::new("cmd")
.arg("/C")
.arg(before_build)
.current_dir(app_dir()),
.current_dir(app_dir())
.envs(command_env()),
)
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?;
#[cfg(not(target_os = "windows"))]
execute_with_output(
&mut Command::new("sh")
.arg("-c")
.arg(before_build)
.current_dir(app_dir()),
.current_dir(app_dir())
.envs(command_env()),
)
.with_context(|| format!("failed to run `{}` with `sh -c`", before_build))?;
}
Expand Down
3 changes: 3 additions & 0 deletions tooling/cli.rs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::helpers::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, reload as reload_config},
manifest::{get_workspace_members, rewrite_manifest},
Logger,
Expand Down Expand Up @@ -141,13 +142,15 @@ impl Dev {
.arg("/C")
.arg(before_dev)
.current_dir(app_dir())
.envs(command_env())
.spawn()
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_dev))?;
#[cfg(not(target_os = "windows"))]
let child = Command::new("sh")
.arg("-c")
.arg(before_dev)
.current_dir(app_dir())
.envs(command_env())
.spawn()
.with_context(|| format!("failed to run `{}` with `sh -c`", before_dev))?;
BEFORE_DEV.set(Mutex::new(child)).unwrap();
Expand Down
18 changes: 18 additions & 0 deletions tooling/cli.rs/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod updater_signature;
pub use logger::Logger;

use std::{
collections::HashMap,
io::{BufRead, BufReader},
process::{Command, Stdio},
};
Expand All @@ -37,3 +38,20 @@ pub fn execute_with_output(cmd: &mut Command) -> crate::Result<()> {
Err(anyhow::anyhow!("command failed"))
}
}

pub fn command_env() -> HashMap<String, String> {
let mut map = HashMap::new();
map.insert("PLATFORM".into(), std::env::consts::OS.into());
map.insert("ARCH".into(), std::env::consts::ARCH.into());
map.insert("FAMILY".into(), std::env::consts::FAMILY.into());
map.insert("VERSION".into(), os_info::get().version().to_string());

#[cfg(target_os = "linux")]
map.insert("PLATFORM_TYPE".into(), "Linux".into());
#[cfg(target_os = "windows")]
map.insert("PLATFORM_TYPE".into(), "Windows_NT".into());
#[cfg(target_os = "macos")]
map.insert("PLATFORM_TYPE".into(), "Darwing".into());

map
}

0 comments on commit 8599313

Please sign in to comment.