mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-02-06 15:56:51 +00:00
fix(tauri) environment variables usage (#688)
This commit is contained in:
parent
00f311a42c
commit
b04be6664e
7
.changes/no-server-fix.md
Normal file
7
.changes/no-server-fix.md
Normal file
@ -0,0 +1,7 @@
|
||||
---
|
||||
"tauri": minor
|
||||
"tauri-api": minor
|
||||
---
|
||||
|
||||
Fixes no-server mode not running on another machine due to fs::read_to_string usage instead of the include_str macro.
|
||||
Build no longer fails when compiling without environment variables, now the app will show an error.
|
||||
@ -30,7 +30,6 @@ tauri-dialog = { git = "https://github.com/tauri-apps/tauri-dialog-rs", version
|
||||
attohttpc = {version = "0.14.0", features=["json", "form" ]}
|
||||
http = "0.2"
|
||||
tauri-utils = {version = "0.5", path = "../tauri-utils"}
|
||||
envmnt = "0.8.2"
|
||||
clap = { git = "https://github.com/clap-rs/clap", rev = "1a276f8", version = "3.0.0-beta.1", optional = true }
|
||||
notify-rust = { version = "4.0.0", optional = true }
|
||||
once_cell = "1.4.0"
|
||||
|
||||
38
tauri-api/build.rs
Normal file
38
tauri-api/build.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use std::{
|
||||
env,
|
||||
error::Error,
|
||||
fs::{read_to_string, File},
|
||||
io::{BufWriter, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub fn main() -> Result<(), Box<dyn Error>> {
|
||||
let out_dir = env::var("OUT_DIR")?;
|
||||
|
||||
let dest_config_path = Path::new(&out_dir).join("tauri.conf.json");
|
||||
let mut config_file = BufWriter::new(File::create(&dest_config_path)?);
|
||||
|
||||
match env::var_os("TAURI_CONFIG") {
|
||||
Some(tauri_config) => {
|
||||
let tauri_config_string = tauri_config.into_string().unwrap();
|
||||
write!(config_file, "{}", tauri_config_string)?;
|
||||
}
|
||||
None => match env::var_os("TAURI_DIR") {
|
||||
Some(tauri_dir) => {
|
||||
let tauri_dir_string = tauri_dir.into_string().unwrap();
|
||||
|
||||
println!("cargo:rerun-if-changed={}", tauri_dir_string);
|
||||
|
||||
let original_config_path = Path::new(&tauri_dir_string).join("tauri.conf.json");
|
||||
let original_config = read_to_string(original_config_path)?;
|
||||
|
||||
write!(config_file, "{}", original_config)?;
|
||||
}
|
||||
None => {
|
||||
write!(config_file, "{{}}")?;
|
||||
println!("Build error: Couldn't find ENV: TAURI_CONFIG or TAURI_DIR");
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -6,7 +6,7 @@ macro_rules! bind_string_arg {
|
||||
clap_arg = clap_arg.$clap_field(value);
|
||||
}
|
||||
clap_arg
|
||||
}}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! bind_value_arg {
|
||||
@ -17,7 +17,7 @@ macro_rules! bind_value_arg {
|
||||
clap_arg = clap_arg.$field(value);
|
||||
}
|
||||
clap_arg
|
||||
}}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! bind_string_slice_arg {
|
||||
@ -29,7 +29,7 @@ macro_rules! bind_string_slice_arg {
|
||||
clap_arg = clap_arg.$field(&v);
|
||||
}
|
||||
clap_arg
|
||||
}}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! bind_if_arg {
|
||||
@ -41,5 +41,5 @@ macro_rules! bind_if_arg {
|
||||
clap_arg = clap_arg.$field(&v[0], &v[1]);
|
||||
}
|
||||
clap_arg
|
||||
}}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ use serde::Deserialize;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::collections::HashMap;
|
||||
use std::{fs, path};
|
||||
|
||||
static CONFIG: OnceCell<Config> = OnceCell::new();
|
||||
|
||||
@ -231,11 +230,8 @@ pub fn get() -> crate::Result<&'static Config> {
|
||||
let config: Config = match option_env!("TAURI_CONFIG") {
|
||||
Some(config) => serde_json::from_str(config).expect("failed to parse TAURI_CONFIG env"),
|
||||
None => {
|
||||
let env_var = envmnt::get_or("TAURI_DIR", "../dist");
|
||||
let path = path::Path::new(&env_var);
|
||||
let contents = fs::read_to_string(path.join("tauri.conf.json"))?;
|
||||
|
||||
serde_json::from_str(&contents).expect("failed to read tauri.conf.json")
|
||||
let config = include_str!(concat!(env!("OUT_DIR"), "/tauri.conf.json"));
|
||||
serde_json::from_str(&config).expect("failed to read tauri.conf.json")
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -6,18 +6,18 @@ use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Unable to determine target-architecture")]
|
||||
Architecture,
|
||||
#[error("Unable to determine target-os")]
|
||||
OS,
|
||||
#[error("Unable to determine target-environment")]
|
||||
Environment,
|
||||
#[error("Unknown target_os")]
|
||||
Unknown,
|
||||
#[error("Could not get parent process")]
|
||||
ParentProcess,
|
||||
#[error("Could not get parent PID")]
|
||||
ParentPID,
|
||||
#[error("Could not get child process")]
|
||||
ChildProcess,
|
||||
}
|
||||
#[error("Unable to determine target-architecture")]
|
||||
Architecture,
|
||||
#[error("Unable to determine target-os")]
|
||||
OS,
|
||||
#[error("Unable to determine target-environment")]
|
||||
Environment,
|
||||
#[error("Unknown target_os")]
|
||||
Unknown,
|
||||
#[error("Could not get parent process")]
|
||||
ParentProcess,
|
||||
#[error("Could not get parent PID")]
|
||||
ParentPID,
|
||||
#[error("Could not get child process")]
|
||||
ChildProcess,
|
||||
}
|
||||
|
||||
@ -7,13 +7,9 @@ pub use sysinfo::{Process, ProcessExt, Signal, System, SystemExt};
|
||||
pub fn get_parent_process(system: &mut sysinfo::System) -> Result<&Process, Error> {
|
||||
let pid = sysinfo::get_current_pid().unwrap();
|
||||
system.refresh_process(pid);
|
||||
let current_process = system
|
||||
.get_process(pid)
|
||||
.ok_or(Error::ParentProcess)?;
|
||||
let current_process = system.get_process(pid).ok_or(Error::ParentProcess)?;
|
||||
let parent_pid = current_process.parent().ok_or(Error::ParentPID)?;
|
||||
let parent_process = system
|
||||
.get_process(parent_pid)
|
||||
.ok_or(Error::ParentProcess)?;
|
||||
let parent_process = system.get_process(parent_pid).ok_or(Error::ParentProcess)?;
|
||||
|
||||
println!("{}", pid);
|
||||
Ok(parent_process)
|
||||
|
||||
@ -1,7 +1,22 @@
|
||||
#[cfg(any(feature = "embedded-server", feature = "no-server"))]
|
||||
pub fn main() {
|
||||
use std::{
|
||||
env,
|
||||
error::Error,
|
||||
fs::{read_to_string, File},
|
||||
io::{BufWriter, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
#[cfg(any(feature = "embedded-server", feature = "no-server"))]
|
||||
pub fn main() -> Result<(), Box<dyn Error>> {
|
||||
shared();
|
||||
match std::env::var_os("TAURI_DIST_DIR") {
|
||||
|
||||
let out_dir = env::var("OUT_DIR")?;
|
||||
|
||||
let dest_index_html_path = Path::new(&out_dir).join("index.tauri.html");
|
||||
let mut index_html_file = BufWriter::new(File::create(&dest_index_html_path)?);
|
||||
|
||||
match env::var_os("TAURI_DIST_DIR") {
|
||||
Some(dist_path) => {
|
||||
let dist_path_string = dist_path.into_string().unwrap();
|
||||
|
||||
@ -19,14 +34,31 @@ pub fn main() {
|
||||
// include assets
|
||||
tauri_includedir_codegen::start("ASSETS")
|
||||
.dir(
|
||||
dist_path_string,
|
||||
dist_path_string.clone(),
|
||||
tauri_includedir_codegen::Compression::None,
|
||||
)
|
||||
.build("data.rs", inlined_assets)
|
||||
.expect("failed to build data.rs")
|
||||
.expect("failed to build data.rs");
|
||||
|
||||
let original_index_html_path = Path::new(&dist_path_string).join("index.tauri.html");
|
||||
let original_index_html = read_to_string(original_index_html_path)?;
|
||||
|
||||
write!(index_html_file, "{}", original_index_html)?;
|
||||
}
|
||||
None => {
|
||||
// dummy assets
|
||||
tauri_includedir_codegen::start("ASSETS")
|
||||
.dir("".to_string(), tauri_includedir_codegen::Compression::None)
|
||||
.build("data.rs", vec![])
|
||||
.expect("failed to build data.rs");
|
||||
write!(
|
||||
index_html_file,
|
||||
"<html><body>Build error: Couldn't find ENV: TAURI_DIST_DIR</body></html>"
|
||||
)?;
|
||||
println!("Build error: Couldn't find ENV: TAURI_DIST_DIR");
|
||||
}
|
||||
None => println!("Build error: Couldn't find ENV: TAURI_DIST_DIR"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
|
||||
|
||||
@ -95,16 +95,8 @@ fn setup_content() -> crate::Result<Content<String>> {
|
||||
// setup content for no-server
|
||||
#[cfg(feature = "no-server")]
|
||||
fn setup_content() -> crate::Result<Content<String>> {
|
||||
let dist_dir = match option_env!("TAURI_DIST_DIR") {
|
||||
Some(d) => d.to_string(),
|
||||
None => env::current_dir()?
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect("Unable to convert to normal String"),
|
||||
};
|
||||
let index_path = Path::new(&dist_dir).join("index.tauri.html");
|
||||
|
||||
Ok(Content::Html(read_to_string(index_path)?))
|
||||
let html = include_str!(concat!(env!("OUT_DIR"), "/index.tauri.html"));
|
||||
Ok(Content::Html(html.to_string()))
|
||||
}
|
||||
|
||||
// get the port for the embedded server
|
||||
|
||||
@ -344,7 +344,8 @@ fn load_asset<T: 'static>(
|
||||
loop {
|
||||
read_asset = crate::assets::ASSETS.get(&format!(
|
||||
"{}/{}",
|
||||
env!("TAURI_DIST_DIR"),
|
||||
option_env!("TAURI_DIST_DIR")
|
||||
.expect("tauri apps should be built with the TAURI_DIST_DIR environment variable"),
|
||||
path.to_string_lossy()
|
||||
));
|
||||
if read_asset.is_err() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user