2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-11 19:35:15 +03:00
|
|
|
use std::{
|
2022-02-15 17:40:21 +03:00
|
|
|
cmp::Ordering,
|
2022-02-11 19:35:15 +03:00
|
|
|
env::current_dir,
|
|
|
|
ffi::OsStr,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2021-01-30 18:15:47 +03:00
|
|
|
|
2022-02-12 01:41:50 +03:00
|
|
|
use ignore::WalkBuilder;
|
2021-01-30 18:15:47 +03:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2022-02-12 01:41:50 +03:00
|
|
|
const TAURI_GITIGNORE: &[u8] = include_bytes!("../../tauri.gitignore");
|
|
|
|
|
2022-02-11 19:35:15 +03:00
|
|
|
fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
|
2022-02-12 01:41:50 +03:00
|
|
|
let mut default_gitignore = std::env::temp_dir();
|
|
|
|
default_gitignore.push(".gitignore");
|
|
|
|
if !default_gitignore.exists() {
|
|
|
|
if let Ok(mut file) = std::fs::File::create(default_gitignore.clone()) {
|
|
|
|
use std::io::Write;
|
|
|
|
let _ = file.write_all(TAURI_GITIGNORE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut builder = WalkBuilder::new(dir);
|
|
|
|
let _ = builder.add_ignore(default_gitignore);
|
2022-02-15 17:40:21 +03:00
|
|
|
builder
|
|
|
|
.require_git(false)
|
|
|
|
.ignore(false)
|
|
|
|
.max_depth(Some(
|
|
|
|
std::env::var("TAURI_PATH_DEPTH")
|
|
|
|
.map(|d| {
|
|
|
|
d.parse()
|
|
|
|
.expect("`TAURI_PATH_DEPTH` environment variable must be a positive integer")
|
|
|
|
})
|
|
|
|
.unwrap_or(3),
|
|
|
|
))
|
|
|
|
.sort_by_file_path(|a, _| {
|
|
|
|
if a.extension().is_some() {
|
|
|
|
Ordering::Less
|
|
|
|
} else {
|
|
|
|
Ordering::Greater
|
|
|
|
}
|
|
|
|
});
|
2022-02-12 01:41:50 +03:00
|
|
|
|
|
|
|
for entry in builder.build().flatten() {
|
2022-02-11 19:35:15 +03:00
|
|
|
let path = dir.join(entry.path());
|
|
|
|
if checker(&path) {
|
|
|
|
return Some(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-09-27 20:54:41 +03:00
|
|
|
fn get_tauri_dir() -> PathBuf {
|
2022-02-11 19:35:15 +03:00
|
|
|
lookup(¤t_dir().expect("failed to read cwd"), |path| if let Some(file_name) = path.file_name() {
|
|
|
|
file_name == OsStr::new("tauri.conf.json") || file_name == OsStr::new("tauri.conf.json5")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
2021-09-27 20:54:41 +03:00
|
|
|
.map(|p| p.parent().unwrap().to_path_buf())
|
2022-02-11 19:35:15 +03:00
|
|
|
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` or `tauri.conf.json5` file in any subfolder.")
|
2021-09-27 20:54:41 +03:00
|
|
|
}
|
2021-01-30 18:15:47 +03:00
|
|
|
|
2021-09-27 20:54:41 +03:00
|
|
|
fn get_app_dir() -> Option<PathBuf> {
|
2022-02-11 19:35:15 +03:00
|
|
|
lookup(¤t_dir().expect("failed to read cwd"), |path| {
|
|
|
|
if let Some(file_name) = path.file_name() {
|
|
|
|
file_name == OsStr::new("package.json")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
2021-09-27 20:54:41 +03:00
|
|
|
.map(|p| p.parent().unwrap().to_path_buf())
|
2021-01-30 18:15:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn app_dir() -> &'static PathBuf {
|
2021-09-27 20:54:41 +03:00
|
|
|
static APP_DIR: Lazy<PathBuf> =
|
|
|
|
Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf()));
|
2021-01-30 18:15:47 +03:00
|
|
|
&APP_DIR
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tauri_dir() -> PathBuf {
|
2021-09-27 20:54:41 +03:00
|
|
|
get_tauri_dir()
|
2021-01-30 18:15:47 +03:00
|
|
|
}
|