File size: 54,424 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 | // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{
collections::HashMap,
ffi::OsStr,
fs::FileType,
io::{BufRead, Write},
path::{Path, PathBuf},
process::Command,
str::FromStr,
sync::{mpsc::sync_channel, Arc, Mutex},
time::Duration,
};
use dunce::canonicalize;
use glob::glob;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use notify::RecursiveMode;
use notify_debouncer_full::new_debouncer;
use serde::{Deserialize, Deserializer};
use tauri_bundler::{
AppCategory, AppImageSettings, BundleBinary, BundleSettings, DebianSettings, DmgSettings,
IosSettings, MacOsSettings, PackageSettings, Position, RpmSettings, Size, UpdaterSettings,
WindowsSettings,
};
use tauri_utils::config::{parse::is_configuration_file, DeepLinkProtocol, RunnerConfig, Updater};
use super::{AppSettings, DevProcess, ExitReason, Interface};
use crate::{
error::{Context, Error, ErrorExt},
helpers::{
app_paths::{frontend_dir, tauri_dir},
config::{nsis_settings, reload as reload_config, wix_settings, BundleResources, Config},
},
ConfigValue,
};
use tauri_utils::{display_path, platform::Target as TargetPlatform};
mod cargo_config;
mod desktop;
pub mod installation;
pub mod manifest;
use crate::helpers::config::custom_sign_settings;
use cargo_config::Config as CargoConfig;
use manifest::{rewrite_manifest, Manifest};
#[derive(Debug, Default, Clone)]
pub struct Options {
pub runner: Option<RunnerConfig>,
pub debug: bool,
pub target: Option<String>,
pub features: Vec<String>,
pub args: Vec<String>,
pub config: Vec<ConfigValue>,
pub no_watch: bool,
pub skip_stapling: bool,
pub additional_watch_folders: Vec<PathBuf>,
}
impl From<crate::build::Options> for Options {
fn from(options: crate::build::Options) -> Self {
Self {
runner: options.runner,
debug: options.debug,
target: options.target,
features: options.features,
args: options.args,
config: options.config,
no_watch: true,
skip_stapling: options.skip_stapling,
additional_watch_folders: Vec::new(),
}
}
}
impl From<crate::bundle::Options> for Options {
fn from(options: crate::bundle::Options) -> Self {
Self {
debug: options.debug,
config: options.config,
target: options.target,
features: options.features,
no_watch: true,
skip_stapling: options.skip_stapling,
..Default::default()
}
}
}
impl From<crate::dev::Options> for Options {
fn from(options: crate::dev::Options) -> Self {
Self {
runner: options.runner,
debug: !options.release_mode,
target: options.target,
features: options.features,
args: options.args,
config: options.config,
no_watch: options.no_watch,
skip_stapling: false,
additional_watch_folders: options.additional_watch_folders,
}
}
}
#[derive(Debug, Clone)]
pub struct MobileOptions {
pub debug: bool,
pub features: Vec<String>,
pub args: Vec<String>,
pub config: Vec<ConfigValue>,
pub no_watch: bool,
pub additional_watch_folders: Vec<PathBuf>,
}
#[derive(Debug, Clone)]
pub struct WatcherOptions {
pub config: Vec<ConfigValue>,
pub additional_watch_folders: Vec<PathBuf>,
}
#[derive(Debug)]
pub struct RustupTarget {
name: String,
installed: bool,
}
pub struct Rust {
app_settings: Arc<RustAppSettings>,
config_features: Vec<String>,
available_targets: Option<Vec<RustupTarget>>,
main_binary_name: Option<String>,
}
impl Interface for Rust {
type AppSettings = RustAppSettings;
fn new(config: &Config, target: Option<String>) -> crate::Result<Self> {
let manifest = {
let (tx, rx) = sync_channel(1);
let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
if let Ok(_events) = r {
let _ = tx.send(());
}
})
.unwrap();
watcher
.watch(tauri_dir().join("Cargo.toml"), RecursiveMode::NonRecursive)
.with_context(|| {
format!(
"failed to watch {}",
tauri_dir().join("Cargo.toml").display()
)
})?;
let (manifest, modified) = rewrite_manifest(config)?;
if modified {
// Wait for the modified event so we don't trigger a re-build later on
let _ = rx.recv_timeout(Duration::from_secs(2));
}
manifest
};
let target_ios = target
.as_ref()
.is_some_and(|target| target.ends_with("ios") || target.ends_with("ios-sim"));
if target_ios {
std::env::set_var(
"IPHONEOS_DEPLOYMENT_TARGET",
&config.bundle.ios.minimum_system_version,
);
}
let app_settings = RustAppSettings::new(config, manifest, target)?;
Ok(Self {
app_settings: Arc::new(app_settings),
config_features: config.build.features.clone().unwrap_or_default(),
main_binary_name: config.main_binary_name.clone(),
available_targets: None,
})
}
fn app_settings(&self) -> Arc<Self::AppSettings> {
self.app_settings.clone()
}
fn build(&mut self, options: Options) -> crate::Result<PathBuf> {
desktop::build(
options,
&self.app_settings,
&mut self.available_targets,
self.config_features.clone(),
self.main_binary_name.as_deref(),
)
}
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
mut options: Options,
on_exit: F,
) -> crate::Result<()> {
let on_exit = Arc::new(on_exit);
let mut run_args = Vec::new();
dev_options(
false,
&mut options.args,
&mut run_args,
&mut options.features,
&self.app_settings,
);
if options.no_watch {
let (tx, rx) = sync_channel(1);
self.run_dev(options, run_args, move |status, reason| {
on_exit(status, reason);
tx.send(()).unwrap();
})?;
rx.recv().unwrap();
Ok(())
} else {
let merge_configs = options.config.iter().map(|c| &c.0).collect::<Vec<_>>();
let run = Arc::new(|rust: &mut Rust| {
let on_exit = on_exit.clone();
rust.run_dev(options.clone(), run_args.clone(), move |status, reason| {
on_exit(status, reason)
})
});
self.run_dev_watcher(&options.additional_watch_folders, &merge_configs, run)
}
}
fn mobile_dev<R: Fn(MobileOptions) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
mut options: MobileOptions,
runner: R,
) -> crate::Result<()> {
let mut run_args = Vec::new();
dev_options(
true,
&mut options.args,
&mut run_args,
&mut options.features,
&self.app_settings,
);
if options.no_watch {
runner(options)?;
Ok(())
} else {
self.watch(
WatcherOptions {
config: options.config.clone(),
additional_watch_folders: options.additional_watch_folders.clone(),
},
move || runner(options.clone()),
)
}
}
fn watch<R: Fn() -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
options: WatcherOptions,
runner: R,
) -> crate::Result<()> {
let merge_configs = options.config.iter().map(|c| &c.0).collect::<Vec<_>>();
let run = Arc::new(|_rust: &mut Rust| runner());
self.run_dev_watcher(&options.additional_watch_folders, &merge_configs, run)
}
fn env(&self) -> HashMap<&str, String> {
let mut env = HashMap::new();
env.insert(
"TAURI_ENV_TARGET_TRIPLE",
self.app_settings.target_triple.clone(),
);
let target_triple = &self.app_settings.target_triple;
let target_components: Vec<&str> = target_triple.split('-').collect();
let (arch, host, _host_env) = match target_components.as_slice() {
// 3 components like aarch64-apple-darwin
[arch, _, host] => (*arch, *host, None),
// 4 components like x86_64-pc-windows-msvc and aarch64-apple-ios-sim
[arch, _, host, host_env] => (*arch, *host, Some(*host_env)),
_ => {
log::warn!("Invalid target triple: {}", target_triple);
return env;
}
};
env.insert("TAURI_ENV_ARCH", arch.into());
env.insert("TAURI_ENV_PLATFORM", host.into());
env.insert(
"TAURI_ENV_FAMILY",
match host {
"windows" => "windows".into(),
_ => "unix".into(),
},
);
env
}
}
struct IgnoreMatcher(Vec<Gitignore>);
impl IgnoreMatcher {
fn is_ignore(&self, path: &Path, is_dir: bool) -> bool {
for gitignore in &self.0 {
if path.starts_with(gitignore.path())
&& gitignore
.matched_path_or_any_parents(path, is_dir)
.is_ignore()
{
return true;
}
}
false
}
}
fn build_ignore_matcher(dir: &Path) -> IgnoreMatcher {
let mut matchers = Vec::new();
// ignore crate doesn't expose an API to build `ignore::gitignore::GitIgnore`
// with custom ignore file names so we have to walk the directory and collect
// our custom ignore files and add it using `ignore::gitignore::GitIgnoreBuilder::add`
for entry in ignore::WalkBuilder::new(dir)
.require_git(false)
.ignore(false)
.overrides(
ignore::overrides::OverrideBuilder::new(dir)
.add(".taurignore")
.unwrap()
.build()
.unwrap(),
)
.build()
.flatten()
{
let path = entry.path();
if path.file_name() == Some(OsStr::new(".taurignore")) {
let mut ignore_builder = GitignoreBuilder::new(path.parent().unwrap());
ignore_builder.add(path);
if let Ok(ignore_file) = std::env::var("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
ignore_builder.add(dir.join(ignore_file));
}
for line in crate::dev::TAURI_CLI_BUILTIN_WATCHER_IGNORE_FILE
.lines()
.map_while(Result::ok)
{
let _ = ignore_builder.add_line(None, &line);
}
matchers.push(ignore_builder.build().unwrap());
}
}
IgnoreMatcher(matchers)
}
fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
let mut default_gitignore = std::env::temp_dir();
default_gitignore.push(".tauri");
let _ = std::fs::create_dir_all(&default_gitignore);
default_gitignore.push(".gitignore");
if !default_gitignore.exists() {
if let Ok(mut file) = std::fs::File::create(default_gitignore.clone()) {
let _ = file.write_all(crate::dev::TAURI_CLI_BUILTIN_WATCHER_IGNORE_FILE);
}
}
let mut builder = ignore::WalkBuilder::new(dir);
builder.add_custom_ignore_filename(".taurignore");
let _ = builder.add_ignore(default_gitignore);
if let Ok(ignore_file) = std::env::var("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
builder.add_ignore(ignore_file);
}
builder.require_git(false).ignore(false).max_depth(Some(1));
for entry in builder.build().flatten() {
f(entry.file_type().unwrap(), dir.join(entry.path()));
}
}
fn dev_options(
mobile: bool,
args: &mut Vec<String>,
run_args: &mut Vec<String>,
features: &mut Vec<String>,
app_settings: &RustAppSettings,
) {
let mut dev_args = Vec::new();
let mut reached_run_args = false;
for arg in args.clone() {
if reached_run_args {
run_args.push(arg);
} else if arg == "--" {
reached_run_args = true;
} else {
dev_args.push(arg);
}
}
*args = dev_args;
if mobile {
args.push("--lib".into());
}
if !args.contains(&"--no-default-features".into()) {
let manifest_features = app_settings.manifest.lock().unwrap().features();
let enable_features: Vec<String> = manifest_features
.get("default")
.cloned()
.unwrap_or_default()
.into_iter()
.filter(|feature| {
if let Some(manifest_feature) = manifest_features.get(feature) {
!manifest_feature.contains(&"tauri/custom-protocol".into())
} else {
feature != "tauri/custom-protocol"
}
})
.collect();
args.push("--no-default-features".into());
features.extend(enable_features);
}
}
// Copied from https://github.com/rust-lang/cargo/blob/69255bb10de7f74511b5cef900a9d102247b6029/src/cargo/core/workspace.rs#L665
fn expand_member_path(path: &Path) -> crate::Result<Vec<PathBuf>> {
let path = path.to_str().context("path is not UTF-8 compatible")?;
let res = glob(path).with_context(|| format!("failed to expand glob pattern for {path}"))?;
let res = res
.map(|p| p.with_context(|| format!("failed to expand glob pattern for {path}")))
.collect::<Result<Vec<_>, _>>()?;
Ok(res)
}
fn get_watch_folders(additional_watch_folders: &[PathBuf]) -> crate::Result<Vec<PathBuf>> {
let tauri_path = tauri_dir();
let workspace_path = get_workspace_dir()?;
// We always want to watch the main tauri folder.
let mut watch_folders = vec![tauri_path.to_path_buf()];
// Add the additional watch folders, resolving the path from the tauri path if it is relative
watch_folders.extend(additional_watch_folders.iter().filter_map(|dir| {
let path = if dir.is_absolute() {
dir.to_owned()
} else {
tauri_path.join(dir)
};
let canonicalized = canonicalize(&path).ok();
if canonicalized.is_none() {
log::warn!(
"Additional watch folder '{}' not found, ignoring",
path.display()
);
}
canonicalized
}));
// We also try to watch workspace members, no matter if the tauri cargo project is the workspace root or a workspace member
let cargo_settings = CargoSettings::load(&workspace_path)?;
if let Some(members) = cargo_settings.workspace.and_then(|w| w.members) {
for p in members {
let p = workspace_path.join(p);
match expand_member_path(&p) {
// Sometimes expand_member_path returns an empty vec, for example if the path contains `[]` as in `C:/[abc]/project/`.
// Cargo won't complain unless theres a workspace.members config with glob patterns so we should support it too.
Ok(expanded_paths) => {
if expanded_paths.is_empty() {
watch_folders.push(p);
} else {
watch_folders.extend(expanded_paths);
}
}
Err(err) => {
// If this fails cargo itself should fail too. But we still try to keep going with the unexpanded path.
log::error!("Error watching {}: {}", p.display(), err);
watch_folders.push(p);
}
};
}
}
Ok(watch_folders)
}
impl Rust {
pub fn build_options(&self, args: &mut Vec<String>, features: &mut Vec<String>, mobile: bool) {
features.push("tauri/custom-protocol".into());
if mobile {
args.push("--lib".into());
} else {
args.push("--bins".into());
}
}
fn run_dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
options: Options,
run_args: Vec<String>,
on_exit: F,
) -> crate::Result<Box<dyn DevProcess + Send>> {
desktop::run_dev(
options,
run_args,
&mut self.available_targets,
self.config_features.clone(),
on_exit,
)
.map(|c| Box::new(c) as Box<dyn DevProcess + Send>)
}
fn run_dev_watcher<F: Fn(&mut Rust) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
additional_watch_folders: &[PathBuf],
merge_configs: &[&serde_json::Value],
run: Arc<F>,
) -> crate::Result<()> {
let child = run(self)?;
let process = Arc::new(Mutex::new(child));
let (tx, rx) = sync_channel(1);
let frontend_path = frontend_dir();
let watch_folders = get_watch_folders(additional_watch_folders)?;
let common_ancestor = common_path::common_path_all(watch_folders.iter().map(Path::new))
.expect("watch_folders should not be empty");
let ignore_matcher = build_ignore_matcher(&common_ancestor);
let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
if let Ok(events) = r {
tx.send(events).unwrap()
}
})
.unwrap();
for path in watch_folders {
if !ignore_matcher.is_ignore(&path, true) {
log::info!("Watching {} for changes...", display_path(&path));
lookup(&path, |file_type, p| {
if p != path {
log::debug!("Watching {} for changes...", display_path(&p));
let _ = watcher.watch(
&p,
if file_type.is_dir() {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
},
);
}
});
}
}
loop {
if let Ok(events) = rx.recv() {
for event in events {
if event.kind.is_access() {
continue;
}
if let Some(event_path) = event.paths.first() {
if !ignore_matcher.is_ignore(event_path, event_path.is_dir()) {
if is_configuration_file(self.app_settings.target_platform, event_path) {
if let Ok(config) = reload_config(merge_configs) {
let (manifest, modified) =
rewrite_manifest(config.lock().unwrap().as_ref().unwrap())?;
if modified {
*self.app_settings.manifest.lock().unwrap() = manifest;
// no need to run the watcher logic, the manifest was modified
// and it will trigger the watcher again
continue;
}
}
}
log::info!(
"File {} changed. Rebuilding application...",
display_path(event_path.strip_prefix(frontend_path).unwrap_or(event_path))
);
let mut p = process.lock().unwrap();
p.kill().context("failed to kill app process")?;
// wait for the process to exit
// note that on mobile, kill() already waits for the process to exit (duct implementation)
loop {
if !matches!(p.try_wait(), Ok(None)) {
break;
}
}
*p = run(self)?;
}
}
}
}
}
}
}
// Taken from https://github.com/rust-lang/cargo/blob/70898e522116f6c23971e2a554b2dc85fd4c84cd/src/cargo/util/toml/mod.rs#L1008-L1065
/// Enum that allows for the parsing of `field.workspace = true` in a Cargo.toml
///
/// It allows for things to be inherited from a workspace or defined as needed
#[derive(Clone, Debug)]
pub enum MaybeWorkspace<T> {
Workspace(TomlWorkspaceField),
Defined(T),
}
impl<'de, T: Deserialize<'de>> serde::de::Deserialize<'de> for MaybeWorkspace<T> {
fn deserialize<D>(deserializer: D) -> Result<MaybeWorkspace<T>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let value = serde_value::Value::deserialize(deserializer)?;
if let Ok(workspace) = TomlWorkspaceField::deserialize(
serde_value::ValueDeserializer::<D::Error>::new(value.clone()),
) {
return Ok(MaybeWorkspace::Workspace(workspace));
}
T::deserialize(serde_value::ValueDeserializer::<D::Error>::new(value))
.map(MaybeWorkspace::Defined)
}
}
impl<T> MaybeWorkspace<T> {
fn resolve(
self,
label: &str,
get_ws_field: impl FnOnce() -> crate::Result<T>,
) -> crate::Result<T> {
match self {
MaybeWorkspace::Defined(value) => Ok(value),
MaybeWorkspace::Workspace(TomlWorkspaceField { workspace: true }) => get_ws_field()
.with_context(|| {
format!(
"error inheriting `{label}` from workspace root manifest's `workspace.package.{label}`"
)
}),
MaybeWorkspace::Workspace(TomlWorkspaceField { workspace: false }) => Err(
crate::Error::GenericError("`workspace=false` is unsupported for `package.{label}`".into()),
),
}
}
fn _as_defined(&self) -> Option<&T> {
match self {
MaybeWorkspace::Workspace(_) => None,
MaybeWorkspace::Defined(defined) => Some(defined),
}
}
}
#[derive(Deserialize, Clone, Debug)]
pub struct TomlWorkspaceField {
workspace: bool,
}
/// The `workspace` section of the app configuration (read from Cargo.toml).
#[derive(Clone, Debug, Deserialize)]
struct WorkspaceSettings {
/// the workspace members.
members: Option<Vec<String>>,
package: Option<WorkspacePackageSettings>,
}
#[derive(Clone, Debug, Deserialize)]
struct WorkspacePackageSettings {
authors: Option<Vec<String>>,
description: Option<String>,
homepage: Option<String>,
version: Option<String>,
license: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct BinarySettings {
name: String,
/// This is from nightly: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#different-binary-name
filename: Option<String>,
path: Option<String>,
required_features: Option<Vec<String>>,
}
impl BinarySettings {
/// The file name without the binary extension (e.g. `.exe`)
pub fn file_name(&self) -> &str {
self.filename.as_ref().unwrap_or(&self.name)
}
}
/// The package settings.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CargoPackageSettings {
/// the package's name.
pub name: String,
/// the package's version.
pub version: Option<MaybeWorkspace<String>>,
/// the package's description.
pub description: Option<MaybeWorkspace<String>>,
/// the package's homepage.
pub homepage: Option<MaybeWorkspace<String>>,
/// the package's authors.
pub authors: Option<MaybeWorkspace<Vec<String>>>,
/// the package's license.
pub license: Option<MaybeWorkspace<String>>,
/// the default binary to run.
pub default_run: Option<String>,
}
/// The Cargo settings (Cargo.toml root descriptor).
#[derive(Clone, Debug, Deserialize)]
struct CargoSettings {
/// the package settings.
///
/// it's optional because ancestor workspace Cargo.toml files may not have package info.
package: Option<CargoPackageSettings>,
/// the workspace settings.
///
/// it's present if the read Cargo.toml belongs to a workspace root.
workspace: Option<WorkspaceSettings>,
/// the binary targets configuration.
bin: Option<Vec<BinarySettings>>,
}
impl CargoSettings {
/// Try to load a set of CargoSettings from a "Cargo.toml" file in the specified directory.
fn load(dir: &Path) -> crate::Result<Self> {
let toml_path = dir.join("Cargo.toml");
let toml_str = std::fs::read_to_string(&toml_path)
.fs_context("Failed to read Cargo manifest", toml_path.clone())?;
toml::from_str(&toml_str).context(format!(
"failed to parse Cargo manifest at {}",
toml_path.display()
))
}
}
pub struct RustAppSettings {
manifest: Mutex<Manifest>,
cargo_settings: CargoSettings,
cargo_package_settings: CargoPackageSettings,
cargo_ws_package_settings: Option<WorkspacePackageSettings>,
package_settings: PackageSettings,
cargo_config: CargoConfig,
target_triple: String,
target_platform: TargetPlatform,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum DesktopDeepLinks {
One(DeepLinkProtocol),
List(Vec<DeepLinkProtocol>),
}
#[derive(Deserialize)]
pub struct UpdaterConfig {
/// Signature public key.
pub pubkey: String,
/// The Windows configuration for the updater.
#[serde(default)]
pub windows: UpdaterWindowsConfig,
}
/// Install modes for the Windows update.
#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub enum WindowsUpdateInstallMode {
/// Specifies there's a basic UI during the installation process, including a final dialog box at the end.
BasicUi,
/// The quiet mode means there's no user interaction required.
/// Requires admin privileges if the installer does.
Quiet,
/// Specifies unattended mode, which means the installation only shows a progress bar.
#[default]
Passive,
// to add more modes, we need to check if the updater relaunch makes sense
// i.e. for a full UI mode, the user can also mark the installer to start the app
}
impl<'de> Deserialize<'de> for WindowsUpdateInstallMode {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.to_lowercase().as_str() {
"basicui" => Ok(Self::BasicUi),
"quiet" => Ok(Self::Quiet),
"passive" => Ok(Self::Passive),
_ => Err(serde::de::Error::custom(format!(
"unknown update install mode '{s}'"
))),
}
}
}
impl WindowsUpdateInstallMode {
/// Returns the associated `msiexec.exe` arguments.
pub fn msiexec_args(&self) -> &'static [&'static str] {
match self {
Self::BasicUi => &["/qb+"],
Self::Quiet => &["/quiet"],
Self::Passive => &["/passive"],
}
}
}
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdaterWindowsConfig {
#[serde(default, alias = "install-mode")]
pub install_mode: WindowsUpdateInstallMode,
}
impl AppSettings for RustAppSettings {
fn get_package_settings(&self) -> PackageSettings {
self.package_settings.clone()
}
fn get_bundle_settings(
&self,
options: &Options,
config: &Config,
features: &[String],
) -> crate::Result<BundleSettings> {
let arch64bits = self.target_triple.starts_with("x86_64")
|| self.target_triple.starts_with("aarch64")
|| self.target_triple.starts_with("riscv64");
let updater_enabled = config.bundle.create_updater_artifacts != Updater::Bool(false);
let v1_compatible = matches!(config.bundle.create_updater_artifacts, Updater::String(_));
let updater_settings = if updater_enabled {
let updater: UpdaterConfig = serde_json::from_value(
config
.plugins
.0
.get("updater")
.context("failed to get updater configuration: plugins > updater doesn't exist")?
.clone(),
)
.context("failed to parse updater plugin configuration")?;
Some(UpdaterSettings {
v1_compatible,
pubkey: updater.pubkey,
msiexec_args: updater.windows.install_mode.msiexec_args(),
})
} else {
None
};
let mut settings = tauri_config_to_bundle_settings(
self,
features,
config,
config.bundle.clone(),
updater_settings,
arch64bits,
)?;
settings.macos.skip_stapling = options.skip_stapling;
if let Some(plugin_config) = config
.plugins
.0
.get("deep-link")
.and_then(|c| c.get("desktop").cloned())
{
let protocols: DesktopDeepLinks =
serde_json::from_value(plugin_config).context("failed to parse desktop deep links from Tauri configuration > plugins > deep-link > desktop")?;
settings.deep_link_protocols = Some(match protocols {
DesktopDeepLinks::One(p) => vec![p],
DesktopDeepLinks::List(p) => p,
});
}
if let Some(open) = config.plugins.0.get("shell").and_then(|v| v.get("open")) {
if open.as_bool().is_some_and(|x| x) || open.is_string() {
settings.appimage.bundle_xdg_open = true;
}
}
if let Some(deps) = self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("dependencies")
.and_then(|f| f.as_table())
{
if deps.contains_key("tauri-plugin-opener") {
settings.appimage.bundle_xdg_open = true;
};
}
Ok(settings)
}
fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf> {
let binaries = self.get_binaries(options)?;
let bin_name = binaries
.iter()
.find(|x| x.main())
.context("failed to find main binary, make sure you have a `package > default-run` in the Cargo.toml file")?
.name();
let out_dir = self
.out_dir(options)
.context("failed to get project out directory")?;
let mut path = out_dir.join(bin_name);
if matches!(self.target_platform, TargetPlatform::Windows) {
// Append the `.exe` extension without overriding the existing extensions
let extension = if let Some(extension) = path.extension() {
let mut extension = extension.to_os_string();
extension.push(".exe");
extension
} else {
"exe".into()
};
path.set_extension(extension);
};
Ok(path)
}
fn get_binaries(&self, options: &Options) -> crate::Result<Vec<BundleBinary>> {
let mut binaries = Vec::new();
if let Some(bins) = &self.cargo_settings.bin {
let default_run = self
.package_settings
.default_run
.clone()
.unwrap_or_default();
for bin in bins {
if let Some(req_features) = &bin.required_features {
// Check if all required features are enabled.
if !req_features
.iter()
.all(|feat| options.features.contains(feat))
{
continue;
}
}
let file_name = bin.file_name();
let is_main = file_name == self.cargo_package_settings.name || file_name == default_run;
binaries.push(BundleBinary::with_path(
file_name.to_owned(),
is_main,
bin.path.clone(),
))
}
}
let tauri_dir = tauri_dir();
let mut binaries_paths = std::fs::read_dir(tauri_dir.join("src/bin"))
.map(|dir| {
dir
.into_iter()
.flatten()
.map(|entry| {
(
entry
.path()
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.into_owned(),
entry.path(),
)
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
if !binaries_paths
.iter()
.any(|(_name, path)| path == Path::new("src/main.rs"))
&& tauri_dir.join("src/main.rs").exists()
{
binaries_paths.push((
self.cargo_package_settings.name.clone(),
tauri_dir.join("src/main.rs"),
));
}
for (name, path) in binaries_paths {
// see https://github.com/tauri-apps/tauri/pull/10977#discussion_r1759742414
let bin_exists = binaries
.iter()
.any(|bin| bin.name() == name || path.ends_with(bin.src_path().unwrap_or(&"".to_string())));
if !bin_exists {
binaries.push(BundleBinary::new(name, false))
}
}
if let Some(default_run) = self.package_settings.default_run.as_ref() {
if let Some(binary) = binaries.iter_mut().find(|bin| bin.name() == default_run) {
binary.set_main(true);
} else {
binaries.push(BundleBinary::new(default_run.clone(), true));
}
}
match binaries.len() {
0 => binaries.push(BundleBinary::new(
self.cargo_package_settings.name.clone(),
true,
)),
1 => binaries.get_mut(0).unwrap().set_main(true),
_ => {}
}
Ok(binaries)
}
fn app_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("package")?
.as_table()?
.get("name")?
.as_str()
.map(|n| n.to_string())
}
fn lib_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("lib")?
.as_table()?
.get("name")?
.as_str()
.map(|n| n.to_string())
}
}
impl RustAppSettings {
pub fn new(config: &Config, manifest: Manifest, target: Option<String>) -> crate::Result<Self> {
let tauri_dir = tauri_dir();
let cargo_settings = CargoSettings::load(tauri_dir).context("failed to load Cargo settings")?;
let cargo_package_settings = match &cargo_settings.package {
Some(package_info) => package_info.clone(),
None => {
return Err(crate::Error::GenericError(
"No package info in the config file".to_owned(),
))
}
};
let ws_package_settings = CargoSettings::load(&get_workspace_dir()?)
.context("failed to load Cargo settings from workspace root")?
.workspace
.and_then(|v| v.package);
let version = config.version.clone().unwrap_or_else(|| {
cargo_package_settings
.version
.clone()
.expect("Cargo manifest must have the `package.version` field")
.resolve("version", || {
ws_package_settings
.as_ref()
.and_then(|p| p.version.clone())
.context("Couldn't inherit value for `version` from workspace")
})
.expect("Cargo project does not have a version")
});
let package_settings = PackageSettings {
product_name: config
.product_name
.clone()
.unwrap_or_else(|| cargo_package_settings.name.clone()),
version,
description: cargo_package_settings
.description
.clone()
.map(|description| {
description
.resolve("description", || {
ws_package_settings
.as_ref()
.and_then(|v| v.description.clone())
.context("Couldn't inherit value for `description` from workspace")
})
.unwrap()
})
.unwrap_or_default(),
homepage: cargo_package_settings.homepage.clone().map(|homepage| {
homepage
.resolve("homepage", || {
ws_package_settings
.as_ref()
.and_then(|v| v.homepage.clone())
.context("Couldn't inherit value for `homepage` from workspace")
})
.unwrap()
}),
authors: cargo_package_settings.authors.clone().map(|authors| {
authors
.resolve("authors", || {
ws_package_settings
.as_ref()
.and_then(|v| v.authors.clone())
.context("Couldn't inherit value for `authors` from workspace")
})
.unwrap()
}),
default_run: cargo_package_settings.default_run.clone(),
};
let cargo_config = CargoConfig::load(tauri_dir)?;
let target_triple = target.unwrap_or_else(|| {
cargo_config
.build()
.target()
.map(|t| t.to_string())
.unwrap_or_else(|| {
let output = Command::new("rustc")
.args(["-vV"])
.output()
.expect("\"rustc\" could not be found, did you install Rust?");
let stdout = String::from_utf8_lossy(&output.stdout);
stdout
.split('\n')
.find(|l| l.starts_with("host:"))
.unwrap()
.replace("host:", "")
.trim()
.to_string()
})
});
let target_platform = TargetPlatform::from_triple(&target_triple);
Ok(Self {
manifest: Mutex::new(manifest),
cargo_settings,
cargo_package_settings,
cargo_ws_package_settings: ws_package_settings,
package_settings,
cargo_config,
target_triple,
target_platform,
})
}
fn target<'a>(&'a self, options: &'a Options) -> Option<&'a str> {
options
.target
.as_deref()
.or_else(|| self.cargo_config.build().target())
}
pub fn out_dir(&self, options: &Options) -> crate::Result<PathBuf> {
get_target_dir(self.target(options), options)
}
}
#[derive(Deserialize)]
pub(crate) struct CargoMetadata {
pub(crate) target_directory: PathBuf,
pub(crate) workspace_root: PathBuf,
}
pub(crate) fn get_cargo_metadata() -> crate::Result<CargoMetadata> {
let output = Command::new("cargo")
.args(["metadata", "--no-deps", "--format-version", "1"])
.current_dir(tauri_dir())
.output()
.map_err(|error| Error::CommandFailed {
command: "cargo metadata --no-deps --format-version 1".to_string(),
error,
})?;
if !output.status.success() {
return Err(Error::CommandFailed {
command: "cargo metadata".to_string(),
error: std::io::Error::other(String::from_utf8_lossy(&output.stderr)),
});
}
serde_json::from_slice(&output.stdout).context("failed to parse cargo metadata")
}
/// Get the cargo target directory based on the provided arguments.
/// If "--target-dir" is specified in args, use it as the target directory (relative to current directory).
/// Otherwise, use the target directory from cargo metadata.
pub(crate) fn get_cargo_target_dir(args: &[String]) -> crate::Result<PathBuf> {
let path = if let Some(target) = get_cargo_option(args, "--target-dir") {
std::env::current_dir()
.context("failed to get current directory")?
.join(target)
} else {
get_cargo_metadata()
.context("failed to run 'cargo metadata' command to get target directory")?
.target_directory
};
Ok(path)
}
/// This function determines the 'target' directory and suffixes it with the profile
/// to determine where the compiled binary will be located.
fn get_target_dir(triple: Option<&str>, options: &Options) -> crate::Result<PathBuf> {
let mut path = get_cargo_target_dir(&options.args)?;
if let Some(triple) = triple {
path.push(triple);
}
path.push(get_profile_dir(options));
Ok(path)
}
#[inline]
fn get_cargo_option<'a>(args: &'a [String], option: &'a str) -> Option<&'a str> {
args
.iter()
.position(|a| a.starts_with(option))
.and_then(|i| {
args[i]
.split_once('=')
.map(|(_, p)| Some(p))
.unwrap_or_else(|| args.get(i + 1).map(|s| s.as_str()))
})
}
/// Executes `cargo metadata` to get the workspace directory.
pub fn get_workspace_dir() -> crate::Result<PathBuf> {
Ok(
get_cargo_metadata()
.context("failed to run 'cargo metadata' command to get workspace directory")?
.workspace_root,
)
}
pub fn get_profile(options: &Options) -> &str {
get_cargo_option(&options.args, "--profile").unwrap_or(if options.debug {
"dev"
} else {
"release"
})
}
pub fn get_profile_dir(options: &Options) -> &str {
match get_profile(options) {
"dev" => "debug",
profile => profile,
}
}
#[allow(unused_variables, deprecated)]
fn tauri_config_to_bundle_settings(
settings: &RustAppSettings,
features: &[String],
tauri_config: &Config,
config: crate::helpers::config::BundleConfig,
updater_config: Option<UpdaterSettings>,
arch64bits: bool,
) -> crate::Result<BundleSettings> {
let enabled_features = settings
.manifest
.lock()
.unwrap()
.all_enabled_features(features);
#[allow(unused_mut)]
let mut resources = config
.resources
.unwrap_or(BundleResources::List(Vec::new()));
#[allow(unused_mut)]
let mut depends_deb = config.linux.deb.depends.unwrap_or_default();
#[allow(unused_mut)]
let mut depends_rpm = config.linux.rpm.depends.unwrap_or_default();
#[allow(unused_mut)]
let mut appimage_files = config.linux.appimage.files;
// set env vars used by the bundler and inject dependencies
#[cfg(target_os = "linux")]
{
let mut libs: Vec<String> = Vec::new();
if enabled_features.contains(&"tray-icon".into())
|| enabled_features.contains(&"tauri/tray-icon".into())
{
let (tray_kind, path) = std::env::var("TAURI_LINUX_AYATANA_APPINDICATOR")
.map(|ayatana| {
if ayatana == "true" || ayatana == "1" {
(
pkgconfig_utils::TrayKind::Ayatana,
format!(
"{}/libayatana-appindicator3.so.1",
pkgconfig_utils::get_library_path("ayatana-appindicator3-0.1")
.expect("failed to get ayatana-appindicator library path using pkg-config.")
),
)
} else {
(
pkgconfig_utils::TrayKind::Libappindicator,
format!(
"{}/libappindicator3.so.1",
pkgconfig_utils::get_library_path("appindicator3-0.1")
.expect("failed to get libappindicator-gtk library path using pkg-config.")
),
)
}
})
.unwrap_or_else(|_| pkgconfig_utils::get_appindicator_library_path());
match tray_kind {
pkgconfig_utils::TrayKind::Ayatana => {
depends_deb.push("libayatana-appindicator3-1".into());
libs.push("libayatana-appindicator3.so.1".into());
}
pkgconfig_utils::TrayKind::Libappindicator => {
depends_deb.push("libappindicator3-1".into());
libs.push("libappindicator3.so.1".into());
}
}
// conditionally setting it in case the user provided its own version for some reason
let path = PathBuf::from(path);
if !appimage_files.contains_key(&path) {
// manually construct target path, just in case the source path is something unexpected
appimage_files.insert(Path::new("/usr/lib/").join(path.file_name().unwrap()), path);
}
}
depends_deb.push("libwebkit2gtk-4.1-0".to_string());
depends_deb.push("libgtk-3-0".to_string());
libs.push("libwebkit2gtk-4.1.so.0".into());
libs.push("libgtk-3.so.0".into());
for lib in libs {
let mut requires = lib;
if arch64bits {
requires.push_str("()(64bit)");
}
depends_rpm.push(requires);
}
}
#[cfg(windows)]
{
if let crate::helpers::config::WebviewInstallMode::FixedRuntime { path } =
&config.windows.webview_install_mode
{
resources.push(path.display().to_string());
}
}
let signing_identity = match std::env::var_os("APPLE_SIGNING_IDENTITY") {
Some(signing_identity) => Some(
signing_identity
.to_str()
.expect("failed to convert APPLE_SIGNING_IDENTITY to string")
.to_string(),
),
None => config.macos.signing_identity,
};
let provider_short_name = match std::env::var_os("APPLE_PROVIDER_SHORT_NAME") {
Some(provider_short_name) => Some(
provider_short_name
.to_str()
.expect("failed to convert APPLE_PROVIDER_SHORT_NAME to string")
.to_string(),
),
None => config.macos.provider_short_name,
};
let (resources, resources_map) = match resources {
BundleResources::List(paths) => (Some(paths), None),
BundleResources::Map(map) => (None, Some(map)),
};
#[cfg(target_os = "macos")]
let entitlements = if let Some(plugin_config) = tauri_config
.plugins
.0
.get("deep-link")
.and_then(|c| c.get("desktop").cloned())
{
let protocols: DesktopDeepLinks =
serde_json::from_value(plugin_config).context("failed to parse deep link plugin config")?;
let domains = match protocols {
DesktopDeepLinks::One(protocol) => protocol.domains,
DesktopDeepLinks::List(protocols) => protocols.into_iter().flat_map(|p| p.domains).collect(),
};
if domains.is_empty() {
config
.macos
.entitlements
.map(PathBuf::from)
.map(tauri_bundler::bundle::Entitlements::Path)
} else {
let mut app_links_entitlements = plist::Dictionary::new();
app_links_entitlements.insert(
"com.apple.developer.associated-domains".to_string(),
domains
.into_iter()
.map(|domain| format!("applinks:{domain}").into())
.collect::<Vec<_>>()
.into(),
);
let entitlements = if let Some(user_provided_entitlements) = config.macos.entitlements {
crate::helpers::plist::merge_plist(vec![
PathBuf::from(user_provided_entitlements).into(),
plist::Value::Dictionary(app_links_entitlements).into(),
])?
} else {
app_links_entitlements.into()
};
Some(tauri_bundler::bundle::Entitlements::Plist(entitlements))
}
} else {
config
.macos
.entitlements
.map(PathBuf::from)
.map(tauri_bundler::bundle::Entitlements::Path)
};
#[cfg(not(target_os = "macos"))]
let entitlements = None;
Ok(BundleSettings {
identifier: Some(tauri_config.identifier.clone()),
publisher: config.publisher,
homepage: config.homepage,
icon: Some(config.icon),
resources,
resources_map,
copyright: config.copyright,
category: match config.category {
Some(category) => Some(AppCategory::from_str(&category).map_err(|e| match e {
Some(e) => Error::GenericError(format!("invalid category, did you mean `{e}`?")),
None => Error::GenericError("invalid category".to_string()),
})?),
None => None,
},
file_associations: config.file_associations,
short_description: config.short_description,
long_description: config.long_description,
external_bin: config.external_bin,
deb: DebianSettings {
depends: if depends_deb.is_empty() {
None
} else {
Some(depends_deb)
},
recommends: config.linux.deb.recommends,
provides: config.linux.deb.provides,
conflicts: config.linux.deb.conflicts,
replaces: config.linux.deb.replaces,
files: config.linux.deb.files,
desktop_template: config.linux.deb.desktop_template,
section: config.linux.deb.section,
priority: config.linux.deb.priority,
changelog: config.linux.deb.changelog,
pre_install_script: config.linux.deb.pre_install_script,
post_install_script: config.linux.deb.post_install_script,
pre_remove_script: config.linux.deb.pre_remove_script,
post_remove_script: config.linux.deb.post_remove_script,
},
appimage: AppImageSettings {
files: appimage_files,
bundle_media_framework: config.linux.appimage.bundle_media_framework,
bundle_xdg_open: false,
},
rpm: RpmSettings {
depends: if depends_rpm.is_empty() {
None
} else {
Some(depends_rpm)
},
recommends: config.linux.rpm.recommends,
provides: config.linux.rpm.provides,
conflicts: config.linux.rpm.conflicts,
obsoletes: config.linux.rpm.obsoletes,
release: config.linux.rpm.release,
epoch: config.linux.rpm.epoch,
files: config.linux.rpm.files,
desktop_template: config.linux.rpm.desktop_template,
pre_install_script: config.linux.rpm.pre_install_script,
post_install_script: config.linux.rpm.post_install_script,
pre_remove_script: config.linux.rpm.pre_remove_script,
post_remove_script: config.linux.rpm.post_remove_script,
compression: config.linux.rpm.compression,
},
dmg: DmgSettings {
background: config.macos.dmg.background,
window_position: config
.macos
.dmg
.window_position
.map(|window_position| Position {
x: window_position.x,
y: window_position.y,
}),
window_size: Size {
width: config.macos.dmg.window_size.width,
height: config.macos.dmg.window_size.height,
},
app_position: Position {
x: config.macos.dmg.app_position.x,
y: config.macos.dmg.app_position.y,
},
application_folder_position: Position {
x: config.macos.dmg.application_folder_position.x,
y: config.macos.dmg.application_folder_position.y,
},
},
ios: IosSettings {
bundle_version: config.ios.bundle_version,
},
macos: MacOsSettings {
frameworks: config.macos.frameworks,
files: config.macos.files,
bundle_version: config.macos.bundle_version,
bundle_name: config.macos.bundle_name,
minimum_system_version: config.macos.minimum_system_version,
exception_domain: config.macos.exception_domain,
signing_identity,
skip_stapling: false,
hardened_runtime: config.macos.hardened_runtime,
provider_short_name,
entitlements,
#[cfg(not(target_os = "macos"))]
info_plist: None,
#[cfg(target_os = "macos")]
info_plist: {
let mut src_plists = vec![];
let path = tauri_dir().join("Info.plist");
if path.exists() {
src_plists.push(path.into());
}
if let Some(info_plist) = &config.macos.info_plist {
src_plists.push(info_plist.clone().into());
}
Some(tauri_bundler::bundle::PlistKind::Plist(
crate::helpers::plist::merge_plist(src_plists)?,
))
},
},
windows: WindowsSettings {
timestamp_url: config.windows.timestamp_url,
tsp: config.windows.tsp,
digest_algorithm: config.windows.digest_algorithm,
certificate_thumbprint: config.windows.certificate_thumbprint,
wix: config.windows.wix.map(wix_settings),
nsis: config.windows.nsis.map(nsis_settings),
icon_path: PathBuf::new(),
webview_install_mode: config.windows.webview_install_mode,
allow_downgrades: config.windows.allow_downgrades,
sign_command: config.windows.sign_command.map(custom_sign_settings),
},
license: config.license.or_else(|| {
settings
.cargo_package_settings
.license
.clone()
.map(|license| {
license
.resolve("license", || {
settings
.cargo_ws_package_settings
.as_ref()
.and_then(|v| v.license.clone())
.context("Couldn't inherit value for `license` from workspace")
})
.unwrap()
})
}),
license_file: config.license_file.map(|l| tauri_dir().join(l)),
updater: updater_config,
..Default::default()
})
}
#[cfg(target_os = "linux")]
mod pkgconfig_utils {
use std::process::Command;
pub enum TrayKind {
Ayatana,
Libappindicator,
}
pub fn get_appindicator_library_path() -> (TrayKind, String) {
match get_library_path("ayatana-appindicator3-0.1") {
Some(p) => (
TrayKind::Ayatana,
format!("{p}/libayatana-appindicator3.so.1"),
),
None => match get_library_path("appindicator3-0.1") {
Some(p) => (
TrayKind::Libappindicator,
format!("{p}/libappindicator3.so.1"),
),
None => panic!("Can't detect any appindicator library"),
},
}
}
/// Gets the folder in which a library is located using `pkg-config`.
pub fn get_library_path(name: &str) -> Option<String> {
let mut cmd = Command::new("pkg-config");
cmd.env("PKG_CONFIG_ALLOW_SYSTEM_LIBS", "1");
cmd.arg("--libs-only-L");
cmd.arg(name);
if let Ok(output) = cmd.output() {
if !output.stdout.is_empty() {
// output would be "-L/path/to/library\n"
let word = output.stdout[2..].to_vec();
Some(String::from_utf8_lossy(&word).trim().to_string())
} else {
None
}
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_cargo_option() {
let args = [
"build".into(),
"--".into(),
"--profile".into(),
"holla".into(),
"--features".into(),
"a".into(),
"b".into(),
"--target-dir".into(),
"path/to/dir".into(),
];
assert_eq!(get_cargo_option(&args, "--profile"), Some("holla"));
assert_eq!(get_cargo_option(&args, "--target-dir"), Some("path/to/dir"));
assert_eq!(get_cargo_option(&args, "--non-existent"), None);
}
#[test]
fn parse_profile_from_opts() {
let options = Options {
args: vec![
"build".into(),
"--".into(),
"--profile".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "testing");
let options = Options {
args: vec![
"build".into(),
"--".into(),
"--profile=customprofile".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "customprofile");
let options = Options {
debug: true,
args: vec![
"build".into(),
"--".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "dev");
let options = Options {
debug: false,
args: vec![
"build".into(),
"--".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "release");
let options = Options {
args: vec!["build".into(), "--".into(), "--profile".into()],
..Default::default()
};
assert_eq!(get_profile(&options), "release");
}
#[test]
fn parse_target_dir_from_opts() {
crate::helpers::app_paths::resolve();
let current_dir = std::env::current_dir().unwrap();
let options = Options {
args: vec![
"build".into(),
"--".into(),
"--target-dir".into(),
"path/to/some/dir".into(),
"--features".into(),
"feat1".into(),
],
debug: false,
..Default::default()
};
assert_eq!(
get_target_dir(None, &options).unwrap(),
current_dir.join("path/to/some/dir/release")
);
assert_eq!(
get_target_dir(Some("x86_64-pc-windows-msvc"), &options).unwrap(),
current_dir
.join("path/to/some/dir")
.join("x86_64-pc-windows-msvc")
.join("release")
);
let options = Options {
args: vec![
"build".into(),
"--".into(),
"--features".into(),
"feat1".into(),
],
debug: false,
..Default::default()
};
#[cfg(windows)]
assert!(get_target_dir(Some("x86_64-pc-windows-msvc"), &options)
.unwrap()
.ends_with("x86_64-pc-windows-msvc\\release"));
#[cfg(not(windows))]
assert!(get_target_dir(Some("x86_64-pc-windows-msvc"), &options)
.unwrap()
.ends_with("x86_64-pc-windows-msvc/release"));
#[cfg(windows)]
{
std::env::set_var("CARGO_TARGET_DIR", "D:\\path\\to\\env\\dir");
assert_eq!(
get_target_dir(None, &options).unwrap(),
PathBuf::from("D:\\path\\to\\env\\dir\\release")
);
assert_eq!(
get_target_dir(Some("x86_64-pc-windows-msvc"), &options).unwrap(),
PathBuf::from("D:\\path\\to\\env\\dir\\x86_64-pc-windows-msvc\\release")
);
}
#[cfg(not(windows))]
{
std::env::set_var("CARGO_TARGET_DIR", "/path/to/env/dir");
assert_eq!(
get_target_dir(None, &options).unwrap(),
PathBuf::from("/path/to/env/dir/release")
);
assert_eq!(
get_target_dir(Some("x86_64-pc-windows-msvc"), &options).unwrap(),
PathBuf::from("/path/to/env/dir/x86_64-pc-windows-msvc/release")
);
}
}
}
|