Skip to content

Commit

Permalink
refactor: change prefix of hook env vars to TAURI_ENV_, closes #8054 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Oct 19, 2023
1 parent 117a7dc commit ebcc21e
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 34 deletions.
10 changes: 10 additions & 0 deletions .changes/rename-hook-env-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@tauri-apps/cli": patch:breaking
"tauri-cli": patch:breaking
"tauri-build": patch:breaking
"tauri-utils": patch:breaking
"tauri-codegen": patch:breaking
"tauri-macros": patch:breaking
---

Renamed the beforeDevCommand, beforeBuildCommand and beforeBundleCommand hooks environment variables from `TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG` to `TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG` to differentiate the prefix with other CLI environment variables.
2 changes: 1 addition & 1 deletion core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {

let target_triple = std::env::var("TARGET").unwrap();

println!("cargo:rustc-env=TAURI_TARGET_TRIPLE={target_triple}");
println!("cargo:rustc-env=TAURI_ENV_TARGET_TRIPLE={target_triple}");

let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
// TODO: far from ideal, but there's no other way to get the target dir, see <https://github.com/rust-lang/cargo/issues/5457>
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
} = data;

let target = std::env::var("TARGET")
.or_else(|_| std::env::var("TAURI_TARGET_TRIPLE"))
.or_else(|_| std::env::var("TAURI_ENV_TARGET_TRIPLE"))
.as_deref()
.map(Target::from_triple)
.unwrap_or_else(|_| Target::current());
Expand Down
6 changes: 3 additions & 3 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@
]
},
"beforeDevCommand": {
"description": "A shell command to run before `tauri dev` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before `tauri dev` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/BeforeDevCommand"
Expand All @@ -2177,7 +2177,7 @@
]
},
"beforeBuildCommand": {
"description": "A shell command to run before `tauri build` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before `tauri build` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/HookCommand"
Expand All @@ -2188,7 +2188,7 @@
]
},
"beforeBundleCommand": {
"description": "A shell command to run before the bundling phase in `tauri build` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before the bundling phase in `tauri build` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/HookCommand"
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) struct ContextItems {
impl Parse for ContextItems {
fn parse(input: &ParseBuffer<'_>) -> syn::parse::Result<Self> {
let target = std::env::var("TARGET")
.or_else(|_| std::env::var("TAURI_TARGET_TRIPLE"))
.or_else(|_| std::env::var("TAURI_ENV_TARGET_TRIPLE"))
.as_deref()
.map(Target::from_triple)
.unwrap_or_else(|_| Target::current());
Expand Down
6 changes: 3 additions & 3 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,17 +1711,17 @@ pub struct BuildConfig {
pub dist_dir: AppUrl,
/// A shell command to run before `tauri dev` kicks in.
///
/// The TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.
/// The TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.
#[serde(alias = "before-dev-command")]
pub before_dev_command: Option<BeforeDevCommand>,
/// A shell command to run before `tauri build` kicks in.
///
/// The TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.
/// The TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.
#[serde(alias = "before-build-command")]
pub before_build_command: Option<HookCommand>,
/// A shell command to run before the bundling phase in `tauri build` kicks in.
///
/// The TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.
/// The TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.
#[serde(alias = "before-bundle-command")]
pub before_bundle_command: Option<HookCommand>,
/// Features passed to `cargo` commands.
Expand Down
4 changes: 2 additions & 2 deletions examples/api/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { internalIpV4 } from 'internal-ip'
// https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
const host =
process.env.TAURI_PLATFORM === 'android' ||
process.env.TAURI_PLATFORM === 'ios'
process.env.TAURI_ENV_PLATFORM === 'android' ||
process.env.TAURI_ENV_PLATFORM === 'ios'
? await internalIpV4()
: 'localhost'
return {
Expand Down
14 changes: 7 additions & 7 deletions tooling/cli/ENVIRONMENT_VARIABLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ These environment variables are inputs to the CLI which may have an equivalent C

These environment variables are set for each hook command (`beforeDevCommand`, `beforeBuildCommand`, ...etc) which could be useful to conditionally build your frontend or execute a specific action.

- `TAURI_ARCH` — Target arch, `x86_64`, `aarch64`...etc.
- `TAURI_PLATFORM` — Target platform, `windows`, `macos`, `linux`...etc.
- `TAURI_FAMILY` — Target platform family `unix` or `windows`.
- `TAURI_PLATFORM_TYPE` — Target platform type `Linux`, `Windows_NT` or `Darwin`
- `TAURI_PLATFORM_VERSION` — Build platform version
- `TAURI_DEBUG``true` for `dev` command, `false` for `build` command.
- `TAURI_TARGET_TRIPLE` — Target triple the CLI is building.
- `TAURI_ENV_ARCH` — Target arch, `x86_64`, `aarch64`...etc.
- `TAURI_ENV_PLATFORM` — Target platform, `windows`, `macos`, `linux`...etc.
- `TAURI_ENV_FAMILY` — Target platform family `unix` or `windows`.
- `TAURI_ENV_PLATFORM_TYPE` — Target platform type `Linux`, `Windows_NT` or `Darwin`
- `TAURI_ENV_PLATFORM_VERSION` — Build platform version
- `TAURI_ENV_DEBUG``true` for `dev` command, `false` for `build` command.
- `TAURI_ENV_TARGET_TRIPLE` — Target triple the CLI is building.
6 changes: 3 additions & 3 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@
]
},
"beforeDevCommand": {
"description": "A shell command to run before `tauri dev` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before `tauri dev` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/BeforeDevCommand"
Expand All @@ -2177,7 +2177,7 @@
]
},
"beforeBuildCommand": {
"description": "A shell command to run before `tauri build` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before `tauri build` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/HookCommand"
Expand All @@ -2188,7 +2188,7 @@
]
},
"beforeBundleCommand": {
"description": "A shell command to run before the bundling phase in `tauri build` kicks in.\n\nThe TAURI_PLATFORM, TAURI_ARCH, TAURI_FAMILY, TAURI_PLATFORM_VERSION, TAURI_PLATFORM_TYPE and TAURI_DEBUG environment variables are set if you perform conditional compilation.",
"description": "A shell command to run before the bundling phase in `tauri build` kicks in.\n\nThe TAURI_ENV_PLATFORM, TAURI_ENV_ARCH, TAURI_ENV_FAMILY, TAURI_ENV_PLATFORM_VERSION, TAURI_ENV_PLATFORM_TYPE and TAURI_ENV_DEBUG environment variables are set if you perform conditional compilation.",
"anyOf": [
{
"$ref": "#/definitions/HookCommand"
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub fn command_env(debug: bool) -> HashMap<&'static str, String> {
let mut map = HashMap::new();

map.insert(
"TAURI_PLATFORM_VERSION",
"TAURI_ENV_PLATFORM_VERSION",
os_info::get().version().to_string(),
);

if debug {
map.insert("TAURI_DEBUG", "true".into());
map.insert("TAURI_ENV_DEBUG", "true".into());
}

map
Expand Down
14 changes: 7 additions & 7 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,22 @@ impl Interface for Rust {
fn env(&self) -> HashMap<&str, String> {
let mut env = HashMap::new();
env.insert(
"TAURI_TARGET_TRIPLE",
"TAURI_ENV_TARGET_TRIPLE",
self.app_settings.target_triple.clone(),
);

let mut s = self.app_settings.target_triple.split('-');
let (arch, _, host) = (s.next().unwrap(), s.next().unwrap(), s.next().unwrap());
env.insert(
"TAURI_ARCH",
"TAURI_ENV_ARCH",
match arch {
// keeps compatibility with old `std::env::consts::ARCH` implementation
"i686" | "i586" => "x86".into(),
a => a.into(),
},
);
env.insert(
"TAURI_PLATFORM",
"TAURI_ENV_PLATFORM",
match host {
// keeps compatibility with old `std::env::consts::OS` implementation
"darwin" => "macos".into(),
Expand All @@ -250,17 +250,17 @@ impl Interface for Rust {
);

env.insert(
"TAURI_FAMILY",
"TAURI_ENV_FAMILY",
match host {
"windows" => "windows".into(),
_ => "unix".into(),
},
);

match host {
"linux" => env.insert("TAURI_PLATFORM_TYPE", "Linux".into()),
"windows" => env.insert("TAURI_PLATFORM_TYPE", "Windows_NT".into()),
"darwin" => env.insert("TAURI_PLATFORM_TYPE", "Darwin".into()),
"linux" => env.insert("TAURI_ENV_PLATFORM_TYPE", "Linux".into()),
"windows" => env.insert("TAURI_ENV_PLATFORM_TYPE", "Windows_NT".into()),
"darwin" => env.insert("TAURI_ENV_PLATFORM_TYPE", "Darwin".into()),
_ => None,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export default defineConfig({
port: 1420,
strictPort: true,
},
// to make use of `TAURI_DEBUG` and other env variables
// to make use of `TAURI_ENV_DEBUG` and other env variables
// https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
envPrefix: ["VITE_", "TAURI_"],
envPrefix: ["VITE_", "TAURI_ENV_"],
build: {
// Tauri supports es2021
target: ["es2021", "chrome100", "safari13"],
// don't minify for debug builds
minify: !process.env.TAURI_DEBUG ? "esbuild" : false,
minify: !process.env.TAURI_ENV_DEBUG ? "esbuild" : false,
// produce sourcemaps for debug builds
sourcemap: !!process.env.TAURI_DEBUG,
sourcemap: !!process.env.TAURI_ENV_DEBUG,
},
});

0 comments on commit ebcc21e

Please sign in to comment.