Skip to content

Commit

Permalink
feat(cli): make tauri/cli fully support projects with non-standard st…
Browse files Browse the repository at this point in the history
…ructure (#11258)

* feat(cli): add support for providing custom app/src paths to tauri's CLI via optional env vars

* fix tests

* rename env vars (app vs src is confusing)

* add change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
  • Loading branch information
regexident and lucasfernog authored Oct 17, 2024
1 parent 2e88633 commit e4c9268
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .changes/support-custom-repo-structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@tauri-apps/cli": patch:enhance
"tauri-cli": patch:enhance
---

Support custom project directory structure where the Tauri app folder is not a subfolder of the frontend project.
The frontend and Tauri app project paths can be set with the `TAURI_FRONTEND_PATH` and the `TAURI_APP_PATH` environment variables respectively.
62 changes: 37 additions & 25 deletions crates/tauri-cli/src/helpers/app_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use tauri_utils::{
};

const TAURI_GITIGNORE: &[u8] = include_bytes!("../../tauri.gitignore");
// path to the Tauri app (Rust crate) directory, usually <cwd>/src-tauri
const ENV_TAURI_APP_PATH: &str = "TAURI_APP_PATH";
// path to the frontend app directory
const ENV_TAURI_FRONTEND_PATH: &str = "TAURI_FRONTEND_PATH";

static APP_DIR: OnceLock<PathBuf> = OnceLock::new();
static TAURI_DIR: OnceLock<PathBuf> = OnceLock::new();

Expand Down Expand Up @@ -68,29 +73,33 @@ fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
None
}

pub fn resolve_tauri_dir() -> Option<PathBuf> {
let Ok(cwd) = current_dir() else {
return None;
};
fn env_tauri_app_path() -> Option<PathBuf> {
std::env::var(ENV_TAURI_APP_PATH)
.map(PathBuf::from)
.ok()?
.canonicalize()
.ok()
}

if cwd.join(ConfigFormat::Json.into_file_name()).exists()
|| cwd.join(ConfigFormat::Json5.into_file_name()).exists()
|| cwd.join(ConfigFormat::Toml.into_file_name()).exists()
{
return Some(cwd);
}
fn env_tauri_frontend_path() -> Option<PathBuf> {
std::env::var(ENV_TAURI_FRONTEND_PATH)
.map(PathBuf::from)
.ok()?
.canonicalize()
.ok()
}

pub fn resolve_tauri_dir() -> Option<PathBuf> {
let src_dir = env_tauri_frontend_path().or_else(|| current_dir().ok())?;

let src_tauri = cwd.join("src-tauri");
if src_tauri.join(ConfigFormat::Json.into_file_name()).exists()
|| src_tauri
.join(ConfigFormat::Json5.into_file_name())
.exists()
|| src_tauri.join(ConfigFormat::Toml.into_file_name()).exists()
if src_dir.join(ConfigFormat::Json.into_file_name()).exists()
|| src_dir.join(ConfigFormat::Json5.into_file_name()).exists()
|| src_dir.join(ConfigFormat::Toml.into_file_name()).exists()
{
return Some(src_tauri);
return Some(src_dir);
}

lookup(&cwd, |path| {
lookup(&src_dir, |path| {
folder_has_configuration_file(Target::Linux, path) || is_configuration_file(Target::Linux, path)
})
.map(|p| {
Expand All @@ -103,13 +112,15 @@ pub fn resolve_tauri_dir() -> Option<PathBuf> {
}

pub fn resolve() {
TAURI_DIR.set(resolve_tauri_dir().unwrap_or_else(||
panic!("Couldn't recognize the current folder as a Tauri project. It must contain a `{}`, `{}` or `{}` file in any subfolder.",
TAURI_DIR.set(resolve_tauri_dir().unwrap_or_else(|| {
let env_var_name = env_tauri_frontend_path().is_some().then(|| format!("`{ENV_TAURI_FRONTEND_PATH}`"));
panic!("Couldn't recognize the {} folder as a Tauri project. It must contain a `{}`, `{}` or `{}` file in any subfolder.",
env_var_name.as_deref().unwrap_or("current"),
ConfigFormat::Json.into_file_name(),
ConfigFormat::Json5.into_file_name(),
ConfigFormat::Toml.into_file_name()
)
)).expect("tauri dir already resolved");
})).expect("tauri dir already resolved");
APP_DIR
.set(resolve_app_dir().unwrap_or_else(|| tauri_dir().parent().unwrap().to_path_buf()))
.expect("app dir already resolved");
Expand All @@ -122,12 +133,13 @@ pub fn tauri_dir() -> &'static PathBuf {
}

pub fn resolve_app_dir() -> Option<PathBuf> {
let cwd = current_dir().expect("failed to read cwd");
if cwd.join("package.json").exists() {
return Some(cwd);
let app_dir = env_tauri_app_path().unwrap_or_else(|| current_dir().expect("failed to read cwd"));

if app_dir.join("package.json").exists() {
return Some(app_dir);
}

lookup(&cwd, |path| {
lookup(&app_dir, |path| {
if let Some(file_name) = path.file_name() {
file_name == OsStr::new("package.json")
} else {
Expand Down

0 comments on commit e4c9268

Please sign in to comment.