cargo-packager 0.11.8

Executable packager and bundler distributed as a CLI and library.
Documentation
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
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
// Copyright 2023-2023 CrabNebula Ltd.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! Configuration type and associated utilities.

use std::{
    collections::HashMap,
    ffi::OsString,
    fmt::{self, Display},
    fs,
    path::{Path, PathBuf},
};

use relative_path::PathExt;
use serde::{Deserialize, Serialize};

use crate::{util, Error};

mod builder;
mod category;

pub use builder::*;
pub use category::AppCategory;

pub use cargo_packager_utils::PackageFormat;

/// **macOS-only**. Corresponds to CFBundleTypeRole
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Default)]
pub enum BundleTypeRole {
    /// CFBundleTypeRole.Editor. Files can be read and edited.
    #[default]
    Editor,
    /// CFBundleTypeRole.Viewer. Files can be read.
    Viewer,
    /// CFBundleTypeRole.Shell
    Shell,
    /// CFBundleTypeRole.QLGenerator
    QLGenerator,
    /// CFBundleTypeRole.None
    None,
}

impl Display for BundleTypeRole {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Editor => write!(f, "Editor"),
            Self::Viewer => write!(f, "Viewer"),
            Self::Shell => write!(f, "Shell"),
            Self::QLGenerator => write!(f, "QLGenerator"),
            Self::None => write!(f, "None"),
        }
    }
}

/// A file association configuration.
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct FileAssociation {
    /// File extensions to associate with this app. e.g. 'png'
    pub extensions: Vec<String>,
    /// The mime-type e.g. 'image/png' or 'text/plain'. **Linux-only**.
    #[serde(alias = "mime-type", alias = "mime_type")]
    pub mime_type: Option<String>,
    /// The association description. **Windows-only**. It is displayed on the `Type` column on Windows Explorer.
    pub description: Option<String>,
    /// The name. Maps to `CFBundleTypeName` on macOS. Defaults to the first item in `ext`
    pub name: Option<String>,
    /// The app's role with respect to the type. Maps to `CFBundleTypeRole` on macOS.
    /// Defaults to [`BundleTypeRole::Editor`]
    #[serde(default)]
    pub role: BundleTypeRole,
}

impl FileAssociation {
    /// Creates a new [`FileAssociation`] using provided extensions.
    pub fn new<I, S>(extensions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            extensions: extensions.into_iter().map(Into::into).collect(),
            mime_type: None,
            description: None,
            name: None,
            role: BundleTypeRole::default(),
        }
    }

    /// Set the extenstions to associate with this app. e.g. 'png'.
    pub fn extensions<I, S>(mut self, extensions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.extensions = extensions.into_iter().map(Into::into).collect();
        self
    }

    /// Set the mime-type e.g. 'image/png' or 'text/plain'. **Linux-only**.
    pub fn mime_type<S: Into<String>>(mut self, mime_type: S) -> Self {
        self.mime_type.replace(mime_type.into());
        self
    }

    /// Se the association description. **Windows-only**. It is displayed on the `Type` column on Windows Explorer.
    pub fn description<S: Into<String>>(mut self, description: S) -> Self {
        self.description.replace(description.into());
        self
    }

    /// Set he name. Maps to `CFBundleTypeName` on macOS. Defaults to the first item in `ext`
    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
        self.name.replace(name.into());
        self
    }

    /// Set he app's role with respect to the type. Maps to `CFBundleTypeRole` on macOS.
    /// Defaults to [`BundleTypeRole::Editor`]
    pub fn role(mut self, role: BundleTypeRole) -> Self {
        self.role = role;
        self
    }
}

/// Deep link protocol
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct DeepLinkProtocol {
    /// URL schemes to associate with this app without `://`. For example `my-app`
    pub schemes: Vec<String>,
    /// The protocol name. **macOS-only** and maps to `CFBundleTypeName`. Defaults to `<bundle-id>.<schemes[0]>`
    pub name: Option<String>,
    /// The app's role for these schemes. **macOS-only** and maps to `CFBundleTypeRole`.
    #[serde(default)]
    pub role: BundleTypeRole,
}

impl DeepLinkProtocol {
    /// Creates a new [`DeepLinkProtocol``] using provided schemes.
    pub fn new<I, S>(schemes: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            schemes: schemes.into_iter().map(Into::into).collect(),
            name: None,
            role: BundleTypeRole::default(),
        }
    }

    /// Set the name. Maps to `CFBundleTypeName` on macOS. Defaults to the first item in `ext`
    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
        self.name.replace(name.into());
        self
    }

    /// Set he app's role with respect to the type. Maps to `CFBundleTypeRole` on macOS.
    /// Defaults to [`BundleTypeRole::Editor`]
    pub fn role(mut self, role: BundleTypeRole) -> Self {
        self.role = role;
        self
    }
}

/// The Linux Debian configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct DebianConfig {
    /// The list of Debian dependencies.
    pub depends: Option<Dependencies>,
    /// Path to a custom desktop file Handlebars template.
    ///
    /// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
    ///
    /// Default file contents:
    /// ```text
    /// [Desktop Entry]
    /// Categories={{categories}}
    /// {{#if comment}}
    /// Comment={{comment}}
    /// {{/if}}
    /// Exec={{exec}} {{exec_arg}}
    /// Icon={{icon}}
    /// Name={{name}}
    /// Terminal=false
    /// Type=Application
    /// {{#if mime_type}}
    /// MimeType={{mime_type}}
    /// {{/if}}
    /// ```
    ///
    /// The `{{exec_arg}}` will be set to:
    /// * "%F", if at least one [Config::file_associations] was specified but no deep link protocols were given.
    ///   * The "%F" arg means that your application can be invoked with multiple file paths.
    /// * "%U", if at least one [Config::deep_link_protocols] was specified.
    ///   * The "%U" arg means that your application can be invoked with multiple URLs.
    ///   * If both [Config::file_associations] and [Config::deep_link_protocols] were specified,
    ///     the "%U" arg will be used, causing the file paths to be passed to your app as `file://` URLs.
    /// * An empty string "" (nothing) if neither are given.
    ///   * This means that your application will never be invoked with any URLs or file paths.
    ///
    /// To specify a custom `exec_arg`, just use plaintext directly instead of `{{exec_arg}}`:
    /// ```text
    /// Exec={{exec}} %u
    /// ```
    ///
    /// See more here: <https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables>.
    #[serde(alias = "desktop-template", alias = "desktop_template")]
    pub desktop_template: Option<PathBuf>,
    /// Define the section in Debian Control file. See : <https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections>
    pub section: Option<String>,
    /// Change the priority of the Debian Package. By default, it is set to `optional`.
    /// Recognized Priorities as of now are :  `required`, `important`, `standard`, `optional`, `extra`
    pub priority: Option<String>,
    /// List of custom files to add to the deb package.
    /// Maps a dir/file to a dir/file inside the debian package.
    pub files: Option<HashMap<String, String>>,
    /// Name to use for the `Package` field in the Debian Control file.
    /// Defaults to [`Config::product_name`] converted to kebab-case.
    #[serde(alias = "package-name", alias = "package_name")]
    pub package_name: Option<String>,
}

impl DebianConfig {
    /// Creates a new [`DebianConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the list of Debian dependencies directly using an iterator of strings.
    pub fn depends<I, S>(mut self, depends: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.depends.replace(Dependencies::List(
            depends.into_iter().map(Into::into).collect(),
        ));
        self
    }

    /// Set the list of Debian dependencies indirectly via a path to a file,
    /// which must contain one dependency (a package name) per line.
    pub fn depends_path<P>(mut self, path: P) -> Self
    where
        P: Into<PathBuf>,
    {
        self.depends.replace(Dependencies::Path(path.into()));
        self
    }

    /// Set the path to a custom desktop file Handlebars template.
    ///
    /// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
    ///
    /// Default file contents:
    /// ```text
    /// [Desktop Entry]
    /// Categories={{categories}}
    /// {{#if comment}}
    /// Comment={{comment}}
    /// {{/if}}
    /// Exec={{exec}} {{exec_arg}}
    /// Icon={{icon}}
    /// Name={{name}}
    /// Terminal=false
    /// Type=Application
    /// {{#if mime_type}}
    /// MimeType={{mime_type}}
    /// {{/if}}
    /// ```
    pub fn desktop_template<P: Into<PathBuf>>(mut self, desktop_template: P) -> Self {
        self.desktop_template.replace(desktop_template.into());
        self
    }

    /// Define the section in Debian Control file. See : <https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections>
    pub fn section<S: Into<String>>(mut self, section: S) -> Self {
        self.section.replace(section.into());
        self
    }

    /// Change the priority of the Debian Package. By default, it is set to `optional`.
    /// Recognized Priorities as of now are :  `required`, `important`, `standard`, `optional`, `extra`
    pub fn priority<S: Into<String>>(mut self, priority: S) -> Self {
        self.priority.replace(priority.into());
        self
    }

    /// Set the list of custom files to add to the deb package.
    /// Maps a dir/file to a dir/file inside the debian package.
    pub fn files<I, S, T>(mut self, files: I) -> Self
    where
        I: IntoIterator<Item = (S, T)>,
        S: Into<String>,
        T: Into<String>,
    {
        self.files.replace(
            files
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }
}

/// A list of dependencies specified as either a list of Strings
/// or as a path to a file that lists the dependencies, one per line.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum Dependencies {
    /// The list of dependencies provided directly as a vector of Strings.
    List(Vec<String>),
    /// A path to the file containing the list of dependences, formatted as one per line:
    /// ```text
    /// libc6
    /// libxcursor1
    /// libdbus-1-3
    /// libasyncns0
    /// ...
    /// ```
    Path(PathBuf),
}
impl Dependencies {
    /// Returns the dependencies as a list of Strings.
    pub fn to_list(&self) -> crate::Result<Vec<String>> {
        match self {
            Self::List(v) => Ok(v.clone()),
            Self::Path(path) => {
                let trimmed_lines = fs::read_to_string(path)
                    .map_err(|e| Error::IoWithPath(path.clone(), e))?
                    .lines()
                    .filter_map(|line| {
                        let trimmed = line.trim();
                        if !trimmed.is_empty() {
                            Some(trimmed.to_owned())
                        } else {
                            None
                        }
                    })
                    .collect();
                Ok(trimmed_lines)
            }
        }
    }
}

/// The Linux AppImage configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct AppImageConfig {
    /// List of libs that exist in `/usr/lib*` to be include in the final AppImage.
    /// The libs will be searched for, using the command
    /// `find -L /usr/lib* -name <libname>`
    pub libs: Option<Vec<String>>,
    /// List of binary paths to include in the final AppImage.
    /// For example, if you want `xdg-open`, you'd specify `/usr/bin/xdg-open`
    pub bins: Option<Vec<String>>,
    /// List of custom files to add to the appimage package.
    /// Maps a dir/file to a dir/file inside the appimage package.
    pub files: Option<HashMap<String, String>>,
    /// A map of [`linuxdeploy`](https://github.com/linuxdeploy/linuxdeploy)
    /// plugin name and its URL to be downloaded and executed while packaing the appimage.
    /// For example, if you want to use the
    /// [`gtk`](https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh) plugin,
    /// you'd specify `gtk` as the key and its url as the value.
    #[serde(alias = "linuxdeploy-plugins", alias = "linuxdeploy_plugins")]
    pub linuxdeploy_plugins: Option<HashMap<String, String>>,
    /// List of globs of libraries to exclude from the final AppImage.
    /// For example, to exclude libnss3.so, you'd specify `libnss3*`
    #[serde(alias = "excluded-libraries", alias = "excluded_libraries")]
    pub excluded_libs: Option<Vec<String>>,
}

impl AppImageConfig {
    /// Creates a new [`DebianConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the list of libs that exist in `/usr/lib*` to be include in the final AppImage.
    /// The libs will be searched for using, the command
    /// `find -L /usr/lib* -name <libname>`
    pub fn libs<I, S>(mut self, libs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.libs
            .replace(libs.into_iter().map(Into::into).collect());
        self
    }

    /// Set the list of binary paths to include in the final AppImage.
    /// For example, if you want `xdg-open`, you'd specify `/usr/bin/xdg-open`
    pub fn bins<I, S>(mut self, bins: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.bins
            .replace(bins.into_iter().map(Into::into).collect());
        self
    }

    /// Set the list of custom files to add to the appimage package.
    /// Maps a dir/file to a dir/file inside the appimage package.
    pub fn files<I, S, T>(mut self, files: I) -> Self
    where
        I: IntoIterator<Item = (S, T)>,
        S: Into<String>,
        T: Into<String>,
    {
        self.files.replace(
            files
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }

    /// Set the map of [`linuxdeploy`](https://github.com/linuxdeploy/linuxdeploy)
    /// plugin name and its URL to be downloaded and executed while packaing the appimage.
    /// For example, if you want to use the
    /// [`gtk`](https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh) plugin,
    /// you'd specify `gtk` as the key and its url as the value.
    pub fn linuxdeploy_plugins<I, S, T>(mut self, linuxdeploy_plugins: I) -> Self
    where
        I: IntoIterator<Item = (S, T)>,
        S: Into<String>,
        T: Into<String>,
    {
        self.linuxdeploy_plugins.replace(
            linuxdeploy_plugins
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }
}

/// The Linux pacman configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct PacmanConfig {
    /// List of custom files to add to the pacman package.
    /// Maps a dir/file to a dir/file inside the pacman package.
    pub files: Option<HashMap<String, String>>,
    /// List of softwares that must be installed for the app to build and run.
    ///
    /// See : <https://wiki.archlinux.org/title/PKGBUILD#depends>
    pub depends: Option<Dependencies>,
    /// Additional packages that are provided by this app.
    ///
    /// See : <https://wiki.archlinux.org/title/PKGBUILD#provides>
    pub provides: Option<Vec<String>>,
    /// Packages that conflict or cause problems with the app.
    /// All these packages and packages providing this item will need to be removed
    ///
    /// See : <https://wiki.archlinux.org/title/PKGBUILD#conflicts>
    pub conflicts: Option<Vec<String>>,
    /// Only use if this app replaces some obsolete packages.
    /// For example, if you rename any package.
    ///
    /// See : <https://wiki.archlinux.org/title/PKGBUILD#replaces>
    pub replaces: Option<Vec<String>>,
    /// Source of the package to be stored at PKGBUILD.
    /// PKGBUILD is a bash script, so version can be referred as ${pkgver}
    pub source: Option<Vec<String>>,
}

impl PacmanConfig {
    /// Creates a new [`PacmanConfig`].
    pub fn new() -> Self {
        Self::default()
    }
    /// Set the list of custom files to add to the pacman package.
    /// Maps a dir/file to a dir/file inside the pacman package.
    pub fn files<I, S, T>(mut self, files: I) -> Self
    where
        I: IntoIterator<Item = (S, T)>,
        S: Into<String>,
        T: Into<String>,
    {
        self.files.replace(
            files
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }

    /// Set the list of pacman dependencies directly using an iterator of strings.
    pub fn depends<I, S>(mut self, depends: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.depends.replace(Dependencies::List(
            depends.into_iter().map(Into::into).collect(),
        ));
        self
    }

    /// Set the list of pacman dependencies indirectly via a path to a file,
    /// which must contain one dependency (a package name) per line.
    pub fn depends_path<P>(mut self, path: P) -> Self
    where
        P: Into<PathBuf>,
    {
        self.depends.replace(Dependencies::Path(path.into()));
        self
    }

    /// Set the list of additional packages that are provided by this app.
    pub fn provides<I, S>(mut self, provides: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.provides
            .replace(provides.into_iter().map(Into::into).collect());
        self
    }
    /// Set the list of packages that conflict with the app.
    pub fn conflicts<I, S>(mut self, conflicts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.conflicts
            .replace(conflicts.into_iter().map(Into::into).collect());
        self
    }
    /// Set the list of obsolete packages that are replaced by this package.
    pub fn replaces<I, S>(mut self, replaces: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.replaces
            .replace(replaces.into_iter().map(Into::into).collect());
        self
    }
    /// Set the list of sources where the package will be stored.
    pub fn source<I, S>(mut self, source: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.source
            .replace(source.into_iter().map(Into::into).collect());
        self
    }
}

/// Position coordinates struct.
#[derive(Default, Copy, Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Position {
    /// X coordinate.
    pub x: u32,
    /// Y coordinate.
    pub y: u32,
}

/// Size struct.
#[derive(Default, Copy, Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Size {
    /// Width.
    pub width: u32,
    /// Height.
    pub height: u32,
}

/// The Apple Disk Image (.dmg) configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct DmgConfig {
    /// Image to use as the background in dmg file. Accepted formats: `png`/`jpg`/`gif`.
    pub background: Option<PathBuf>,
    /// Position of volume window on screen.
    pub window_position: Option<Position>,
    /// Size of volume window.
    #[serde(alias = "window-size", alias = "window_size")]
    pub window_size: Option<Size>,
    /// Position of application file on window.
    #[serde(alias = "app-position", alias = "app_position")]
    pub app_position: Option<Position>,
    /// Position of application folder on window.
    #[serde(
        alias = "application-folder-position",
        alias = "application_folder_position"
    )]
    pub app_folder_position: Option<Position>,
}

impl DmgConfig {
    /// Creates a new [`DmgConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set an image to use as the background in dmg file. Accepted formats: `png`/`jpg`/`gif`.
    pub fn background<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.background.replace(path.into());
        self
    }

    /// Set the poosition of volume window on screen.
    pub fn window_position(mut self, position: Position) -> Self {
        self.window_position.replace(position);
        self
    }

    /// Set the size of volume window.
    pub fn window_size(mut self, size: Size) -> Self {
        self.window_size.replace(size);
        self
    }

    /// Set the poosition of app file on window.
    pub fn app_position(mut self, position: Position) -> Self {
        self.app_position.replace(position);
        self
    }

    /// Set the position of application folder on window.
    pub fn app_folder_position(mut self, position: Position) -> Self {
        self.app_folder_position.replace(position);
        self
    }
}

/// Notarization authentication credentials.
#[derive(Clone, Debug)]
pub enum MacOsNotarizationCredentials {
    /// Apple ID authentication.
    AppleId {
        /// Apple ID.
        apple_id: OsString,
        /// Password.
        password: OsString,
        /// Team ID.
        team_id: OsString,
    },
    /// App Store Connect API key.
    ApiKey {
        /// API key issuer.
        issuer: OsString,
        /// API key ID.
        key_id: OsString,
        /// Path to the API key file.
        key_path: PathBuf,
    },
    /// Keychain profile with a stored app-specific password for notarytool to use
    /// Passwords can be generated at https://account.apple.com when signed in with your developer account.
    /// The password must then be stored in your keychain for notarytool to access,
    /// using the following, with the appopriate Apple and Team IDs:
    /// `xcrun notarytool store-credentials --apple-id "name@example.com" --team-id "ABCD123456"`
    /// This will prompt for a keychain profile name, and the password itself.
    /// This setting can only be provided as an environment variable "APPLE_KEYCHAIN_PROFILE"
    KeychainProfile {
        /// The keychain profile name (as provided when the password was stored using notarytool)
        keychain_profile: OsString,
    },
}

/// The macOS configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct MacOsConfig {
    /// MacOS frameworks that need to be packaged with the app.
    ///
    /// Each string can either be the name of a framework (without the `.framework` extension, e.g. `"SDL2"`),
    /// in which case we will search for that framework in the standard install locations (`~/Library/Frameworks/`, `/Library/Frameworks/`, and `/Network/Library/Frameworks/`),
    /// or a path to a specific framework bundle (e.g. `./data/frameworks/SDL2.framework`).  Note that this setting just makes cargo-packager copy the specified frameworks into the OS X app bundle
    /// (under `Foobar.app/Contents/Frameworks/`); you are still responsible for:
    ///
    /// - arranging for the compiled binary to link against those frameworks (e.g. by emitting lines like `cargo:rustc-link-lib=framework=SDL2` from your `build.rs` script)
    ///
    /// - embedding the correct rpath in your binary (e.g. by running `install_name_tool -add_rpath "@executable_path/../Frameworks" path/to/binary` after compiling)
    pub frameworks: Option<Vec<String>>,
    /// A version string indicating the minimum MacOS version that the packaged app supports (e.g. `"10.11"`).
    /// If you are using this config field, you may also want have your `build.rs` script emit `cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11`.
    #[serde(alias = "minimum-system-version", alias = "minimum_system_version")]
    pub minimum_system_version: Option<String>,
    /// The exception domain to use on the macOS .app package.
    ///
    /// This allows communication to the outside world e.g. a web server you're shipping.
    #[serde(alias = "exception-domain", alias = "exception_domain")]
    pub exception_domain: Option<String>,
    /// Code signing identity.
    ///
    /// This is typically of the form: `"Developer ID Application: TEAM_NAME (TEAM_ID)"`.
    #[serde(alias = "signing-identity", alias = "signing_identity")]
    pub signing_identity: Option<String>,
    /// Codesign certificate (base64 encoded of the p12 file).
    ///
    /// Note: this field cannot be specified via a config file or Cargo package metadata.
    #[serde(skip)]
    pub signing_certificate: Option<OsString>,
    /// Password of the codesign certificate.
    ///
    /// Note: this field cannot be specified via a config file or Cargo package metadata.
    #[serde(skip)]
    pub signing_certificate_password: Option<OsString>,
    /// Notarization authentication credentials.
    ///
    /// Note: this field cannot be specified via a config file or Cargo package metadata.
    #[serde(skip)]
    pub notarization_credentials: Option<MacOsNotarizationCredentials>,
    /// Provider short name for notarization.
    #[serde(alias = "provider-short-name", alias = "provider_short_name")]
    pub provider_short_name: Option<String>,
    /// Path to the entitlements.plist file.
    pub entitlements: Option<String>,
    /// Path to the Info.plist file for the package.
    #[serde(alias = "info-plist-path", alias = "info_plist_path")]
    pub info_plist_path: Option<PathBuf>,
    /// Path to the embedded.provisionprofile file for the package.
    #[serde(
        alias = "embedded-provisionprofile-path",
        alias = "embedded_provisionprofile_path"
    )]
    pub embedded_provisionprofile_path: Option<PathBuf>,
    /// Apps that need to be packaged within the app.
    #[serde(alias = "embedded-apps", alias = "embedded_apps")]
    pub embedded_apps: Option<Vec<String>>,
    /// Whether this is a background application. If true, the app will not appear in the Dock.
    ///
    /// Sets the `LSUIElement` flag in the macOS plist file.
    #[serde(default, alias = "background_app", alias = "background-app")]
    pub background_app: bool,
}

impl MacOsConfig {
    /// Creates a new [`MacOsConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// MacOS frameworks that need to be packaged with the app.
    ///
    /// Each string can either be the name of a framework (without the `.framework` extension, e.g. `"SDL2"`),
    /// in which case we will search for that framework in the standard install locations (`~/Library/Frameworks/`, `/Library/Frameworks/`, and `/Network/Library/Frameworks/`),
    /// or a path to a specific framework bundle (e.g. `./data/frameworks/SDL2.framework`).  Note that this setting just makes cargo-packager copy the specified frameworks into the OS X app bundle
    /// (under `Foobar.app/Contents/Frameworks/`); you are still responsible for:
    ///
    /// - arranging for the compiled binary to link against those frameworks (e.g. by emitting lines like `cargo:rustc-link-lib=framework=SDL2` from your `build.rs` script)
    ///
    /// - embedding the correct rpath in your binary (e.g. by running `install_name_tool -add_rpath "@executable_path/../Frameworks" path/to/binary` after compiling)
    pub fn frameworks<I, S>(mut self, frameworks: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.frameworks
            .replace(frameworks.into_iter().map(Into::into).collect());
        self
    }

    /// A version string indicating the minimum MacOS version that the packaged app supports (e.g. `"10.11"`).
    /// If you are using this config field, you may also want have your `build.rs` script emit `cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11`.
    pub fn minimum_system_version<S: Into<String>>(mut self, minimum_system_version: S) -> Self {
        self.minimum_system_version
            .replace(minimum_system_version.into());
        self
    }

    /// The exception domain to use on the macOS .app package.
    ///
    /// This allows communication to the outside world e.g. a web server you're shipping.
    pub fn exception_domain<S: Into<String>>(mut self, exception_domain: S) -> Self {
        self.exception_domain.replace(exception_domain.into());
        self
    }

    /// Code signing identity.
    pub fn signing_identity<S: Into<String>>(mut self, signing_identity: S) -> Self {
        self.signing_identity.replace(signing_identity.into());
        self
    }

    /// Provider short name for notarization.
    pub fn provider_short_name<S: Into<String>>(mut self, provider_short_name: S) -> Self {
        self.provider_short_name.replace(provider_short_name.into());
        self
    }

    /// Path to the entitlements.plist file.
    pub fn entitlements<S: Into<String>>(mut self, entitlements: S) -> Self {
        self.entitlements.replace(entitlements.into());
        self
    }

    /// Path to the Info.plist file for the package.
    pub fn info_plist_path<S: Into<PathBuf>>(mut self, info_plist_path: S) -> Self {
        self.info_plist_path.replace(info_plist_path.into());
        self
    }

    /// Path to the embedded.provisionprofile file for the package.
    pub fn embedded_provisionprofile_path<S: Into<PathBuf>>(
        mut self,
        embedded_provisionprofile_path: S,
    ) -> Self {
        self.embedded_provisionprofile_path
            .replace(embedded_provisionprofile_path.into());
        self
    }

    /// Apps that need to be packaged within the app.
    pub fn embedded_apps<I, S>(mut self, embedded_apps: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.embedded_apps
            .replace(embedded_apps.into_iter().map(Into::into).collect());
        self
    }
}

/// Linux configuration
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct LinuxConfig {
    /// Flag to indicate if desktop entry should be generated.
    #[serde(
        default = "default_true",
        alias = "generate-desktop-entry",
        alias = "generate_desktop_entry"
    )]
    pub generate_desktop_entry: bool,
}

/// A wix language.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum WixLanguage {
    /// Built-in wix language identifier.
    Identifier(String),
    /// Custom wix language.
    Custom {
        /// Idenitifier of this language, for example `en-US`
        identifier: String,
        /// The path to a locale (`.wxl`) file. See <https://wixtoolset.org/documentation/manual/v3/howtos/ui_and_localization/build_a_localized_version.html>.
        path: Option<PathBuf>,
    },
}

impl Default for WixLanguage {
    fn default() -> Self {
        Self::Identifier("en-US".into())
    }
}

/// The wix format configuration
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct WixConfig {
    /// The app languages to build. See <https://docs.microsoft.com/en-us/windows/win32/msi/localizing-the-error-and-actiontext-tables>.
    pub languages: Option<Vec<WixLanguage>>,
    /// By default, the packager uses an internal template.
    /// This option allows you to define your own wix file.
    pub template: Option<PathBuf>,
    /// List of merge modules to include in your installer.
    /// For example, if you want to include [C++ Redis merge modules]
    ///
    /// [C++ Redis merge modules]: https://wixtoolset.org/docs/v3/howtos/redistributables_and_install_checks/install_vcredist/
    #[serde(alias = "merge-modules", alias = "merge_modules")]
    pub merge_modules: Option<Vec<PathBuf>>,
    /// A list of paths to .wxs files with WiX fragments to use.
    #[serde(alias = "fragment-paths", alias = "fragment_paths")]
    pub fragment_paths: Option<Vec<PathBuf>>,
    /// List of WiX fragments as strings. This is similar to `config.wix.fragments_paths` but
    /// is a string so you can define it inline in your config.
    ///
    /// ```text
    /// <?xml version="1.0" encoding="utf-8"?>
    /// <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    /// <Fragment>
    ///     <CustomAction Id="OpenNotepad" Directory="INSTALLDIR" Execute="immediate" ExeCommand="cmd.exe /c notepad.exe" Return="check" />
    ///     <InstallExecuteSequence>
    ///         <Custom Action="OpenNotepad" After="InstallInitialize" />
    ///     </InstallExecuteSequence>
    /// </Fragment>
    /// </Wix>
    /// ```
    pub fragments: Option<Vec<String>>,
    /// The ComponentGroup element ids you want to reference from the fragments.
    #[serde(alias = "component-group-refs", alias = "component_group_refs")]
    pub component_group_refs: Option<Vec<String>>,
    /// The Component element ids you want to reference from the fragments.
    #[serde(alias = "component-refs", alias = "component_refs")]
    pub component_refs: Option<Vec<String>>,
    /// The CustomAction element ids you want to reference from the fragments.
    #[serde(alias = "custom-action-refs", alias = "custom_action_refs")]
    pub custom_action_refs: Option<Vec<String>>,
    /// The FeatureGroup element ids you want to reference from the fragments.
    #[serde(alias = "feature-group-refs", alias = "feature_group_refs")]
    pub feature_group_refs: Option<Vec<String>>,
    /// The Feature element ids you want to reference from the fragments.
    #[serde(alias = "feature-refs", alias = "feature_refs")]
    pub feature_refs: Option<Vec<String>>,
    /// The Merge element ids you want to reference from the fragments.
    #[serde(alias = "merge-refs", alias = "merge_refs")]
    pub merge_refs: Option<Vec<String>>,
    /// Path to a bitmap file to use as the installation user interface banner.
    /// This bitmap will appear at the top of all but the first page of the installer.
    ///
    /// The required dimensions are 493px × 58px.
    #[serde(alias = "banner-path", alias = "banner_path")]
    pub banner_path: Option<PathBuf>,
    /// Path to a bitmap file to use on the installation user interface dialogs.
    /// It is used on the welcome and completion dialogs.
    /// The required dimensions are 493px × 312px.
    #[serde(alias = "dialog-image-path", alias = "dialog_image_path")]
    pub dialog_image_path: Option<PathBuf>,
    /// Enables FIPS compliant algorithms.
    #[serde(default, alias = "fips-compliant", alias = "fips_compliant")]
    pub fips_compliant: bool,
}

impl WixConfig {
    /// Creates a new [`WixConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the app languages to build. See <https://docs.microsoft.com/en-us/windows/win32/msi/localizing-the-error-and-actiontext-tables>.
    pub fn languages<I: IntoIterator<Item = WixLanguage>>(mut self, languages: I) -> Self {
        self.languages.replace(languages.into_iter().collect());
        self
    }

    /// By default, the packager uses an internal template.
    /// This option allows you to define your own wix file.
    pub fn template<P: Into<PathBuf>>(mut self, template: P) -> Self {
        self.template.replace(template.into());
        self
    }

    /// Set a list of merge modules to include in your installer.
    /// For example, if you want to include [C++ Redis merge modules]
    ///
    /// [C++ Redis merge modules]: https://wixtoolset.org/docs/v3/howtos/redistributables_and_install_checks/install_vcredist/
    pub fn merge_modules<I, P>(mut self, merge_modules: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        self.merge_modules
            .replace(merge_modules.into_iter().map(Into::into).collect());
        self
    }

    /// Set a list of paths to .wxs files with WiX fragments to use.
    pub fn fragment_paths<I, S>(mut self, fragment_paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<PathBuf>,
    {
        self.fragment_paths
            .replace(fragment_paths.into_iter().map(Into::into).collect());
        self
    }

    /// Set a list of WiX fragments as strings. This is similar to [`WixConfig::fragment_paths`] but
    /// is a string so you can define it inline in your config.
    ///
    /// ```text
    /// <?xml version="1.0" encoding="utf-8"?>
    /// <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    /// <Fragment>
    ///     <CustomAction Id="OpenNotepad" Directory="INSTALLDIR" Execute="immediate" ExeCommand="cmd.exe /c notepad.exe" Return="check" />
    ///     <InstallExecuteSequence>
    ///         <Custom Action="OpenNotepad" After="InstallInitialize" />
    ///     </InstallExecuteSequence>
    /// </Fragment>
    /// </Wix>
    /// ```
    pub fn fragments<I, S>(mut self, fragments: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.fragments
            .replace(fragments.into_iter().map(Into::into).collect());
        self
    }

    /// Set the ComponentGroup element ids you want to reference from the fragments.
    pub fn component_group_refs<I, S>(mut self, component_group_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.component_group_refs
            .replace(component_group_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set the Component element ids you want to reference from the fragments.
    pub fn component_refs<I, S>(mut self, component_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.component_refs
            .replace(component_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set the CustomAction element ids you want to reference from the fragments.
    pub fn custom_action_refs<I, S>(mut self, custom_action_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.custom_action_refs
            .replace(custom_action_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set he FeatureGroup element ids you want to reference from the fragments.
    pub fn feature_group_refs<I, S>(mut self, feature_group_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.feature_group_refs
            .replace(feature_group_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set the Feature element ids you want to reference from the fragments.
    pub fn feature_refs<I, S>(mut self, feature_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.feature_refs
            .replace(feature_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set he Merge element ids you want to reference from the fragments.
    pub fn merge_refs<I, S>(mut self, merge_refs: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.merge_refs
            .replace(merge_refs.into_iter().map(Into::into).collect());
        self
    }

    /// Set the path to a bitmap file to use as the installation user interface banner.
    /// This bitmap will appear at the top of all but the first page of the installer.
    ///
    /// The required dimensions are 493px × 58px.
    pub fn banner_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.banner_path.replace(path.into());
        self
    }

    /// Set the path to a bitmap file to use on the installation user interface dialogs.
    /// It is used on the welcome and completion dialogs.
    /// The required dimensions are 493px × 312px.
    pub fn dialog_image_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.dialog_image_path.replace(path.into());
        self
    }

    /// Set whether to enable or disable FIPS compliant algorithms.
    pub fn fips_compliant(mut self, fips_compliant: bool) -> Self {
        self.fips_compliant = fips_compliant;
        self
    }
}

/// Install Modes for the NSIS installer.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
#[derive(Default)]
pub enum NSISInstallerMode {
    /// Default mode for the installer.
    ///
    /// Install the app by default in a directory that doesn't require Administrator access.
    ///
    /// Installer metadata will be saved under the `HKCU` registry path.
    #[default]
    CurrentUser,
    /// Install the app by default in the `Program Files` folder directory requires Administrator
    /// access for the installation.
    ///
    /// Installer metadata will be saved under the `HKLM` registry path.
    PerMachine,
    /// Combines both modes and allows the user to choose at install time
    /// whether to install for the current user or per machine. Note that this mode
    /// will require Administrator access even if the user wants to install it for the current user only.
    ///
    /// Installer metadata will be saved under the `HKLM` or `HKCU` registry path based on the user's choice.
    Both,
}

/// Compression algorithms used in the NSIS installer.
///
/// See <https://nsis.sourceforge.io/Reference/SetCompressor>
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum NsisCompression {
    /// ZLIB uses the deflate algorithm, it is a quick and simple method. With the default compression level it uses about 300 KB of memory.
    Zlib,
    /// BZIP2 usually gives better compression ratios than ZLIB, but it is a bit slower and uses more memory. With the default compression level it uses about 4 MB of memory.
    Bzip2,
    /// LZMA (default) is a new compression method that gives very good compression ratios. The decompression speed is high (10-20 MB/s on a 2 GHz CPU), the compression speed is lower. The memory size that will be used for decompression is the dictionary size plus a few KBs, the default is 8 MB.
    Lzma,
    /// Disable compression.
    Off,
}

/// The NSIS format configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct NsisConfig {
    /// Set the compression algorithm used to compress files in the installer.
    ///
    /// See <https://nsis.sourceforge.io/Reference/SetCompressor>
    pub compression: Option<NsisCompression>,
    /// A custom `.nsi` template to use.
    ///
    /// See the default template here
    /// <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/package/nsis/installer.nsi>
    pub template: Option<PathBuf>,
    /// Logic of an NSIS section that will be ran before the install section.
    ///
    /// See the available libraries, dlls and global variables here
    /// <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/package/nsis/installer.nsi>
    ///
    /// ### Example
    /// ```toml
    /// [package.metadata.packager.nsis]
    /// preinstall-section = """
    ///     ; Setup custom messages
    ///     LangString webview2AbortError ${LANG_ENGLISH} "Failed to install WebView2! The app can't run without it. Try restarting the installer."
    ///     LangString webview2DownloadError ${LANG_ARABIC} "خطأ: فشل تنزيل WebView2 - $0"
    ///
    ///     Section PreInstall
    ///      ; <section logic here>
    ///     SectionEnd
    ///
    ///     Section AnotherPreInstall
    ///      ; <section logic here>
    ///     SectionEnd
    /// """
    /// ```
    #[serde(alias = "preinstall-section", alias = "preinstall_section")]
    pub preinstall_section: Option<String>,
    /// The path to a bitmap file to display on the header of installers pages.
    ///
    /// The recommended dimensions are 150px x 57px.
    #[serde(alias = "header-image", alias = "header_image")]
    pub header_image: Option<PathBuf>,
    /// The path to a bitmap file for the Welcome page and the Finish page.
    ///
    /// The recommended dimensions are 164px x 314px.
    #[serde(alias = "sidebar-image", alias = "sidebar_image")]
    pub sidebar_image: Option<PathBuf>,
    /// The path to an icon file used as the installer icon.
    #[serde(alias = "installer-icon", alias = "installer_icon")]
    pub installer_icon: Option<PathBuf>,
    /// Whether the installation will be for all users or just the current user.
    #[serde(default, alias = "installer-mode", alias = "installer_mode")]
    pub install_mode: NSISInstallerMode,
    /// A list of installer languages.
    /// By default the OS language is used. If the OS language is not in the list of languages, the first language will be used.
    /// To allow the user to select the language, set `display_language_selector` to `true`.
    ///
    /// See <https://github.com/kichik/nsis/tree/9465c08046f00ccb6eda985abbdbf52c275c6c4d/Contrib/Language%20files> for the complete list of languages.
    pub languages: Option<Vec<String>>,
    /// An key-value pair where the key is the language and the
    /// value is the path to a custom `.nsi` file that holds the translated text for cargo-packager's custom messages.
    ///
    /// See <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/nsis/languages/English.nsh> for an example `.nsi` file.
    ///
    /// **Note**: the key must be a valid NSIS language and it must be added to [`NsisConfig`]languages array,
    #[serde(alias = "custom-language-file", alias = "custom_language_file")]
    pub custom_language_files: Option<HashMap<String, PathBuf>>,
    /// Whether to display a language selector dialog before the installer and uninstaller windows are rendered or not.
    /// By default the OS language is selected, with a fallback to the first language in the `languages` array.
    #[serde(
        default,
        alias = "display-language-selector",
        alias = "display_language_selector"
    )]
    pub display_language_selector: bool,
    /// List of paths where your app stores data.
    /// This options tells the uninstaller to provide the user with an option
    /// (disabled by default) whether they want to rmeove your app data or keep it.
    ///
    /// The path should use a constant from <https://nsis.sourceforge.io/Docs/Chapter4.html#varconstant>
    /// in addition to `$IDENTIFIER`, `$PUBLISHER` and `$PRODUCTNAME`, for example, if you store your
    /// app data in `C:\\Users\\<user>\\AppData\\Local\\<your-company-name>\\<your-product-name>`
    /// you'd need to specify
    /// ```toml
    /// [package.metadata.packager.nsis]
    /// appdata-paths = ["$LOCALAPPDATA/$PUBLISHER/$PRODUCTNAME"]
    /// ```
    #[serde(default, alias = "appdata-paths", alias = "appdata_paths")]
    pub appdata_paths: Option<Vec<String>>,
}

impl NsisConfig {
    /// Creates a new [`NsisConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the compression algorithm used to compress files in the installer.
    ///
    /// See <https://nsis.sourceforge.io/Reference/SetCompressor>
    pub fn compression(mut self, compression: NsisCompression) -> Self {
        self.compression.replace(compression);
        self
    }

    /// Set a custom `.nsi` template to use.
    ///
    /// See the default template here
    /// <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/package/nsis/installer.nsi>
    pub fn template<P: Into<PathBuf>>(mut self, template: P) -> Self {
        self.template.replace(template.into());
        self
    }

    /// Set the logic of an NSIS section that will be ran before the install section.
    ///
    /// See the available libraries, dlls and global variables here
    /// <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/package/nsis/installer.nsi>
    ///
    /// ### Example
    /// ```toml
    /// [package.metadata.packager.nsis]
    /// preinstall-section = """
    ///     ; Setup custom messages
    ///     LangString webview2AbortError ${LANG_ENGLISH} "Failed to install WebView2! The app can't run without it. Try restarting the installer."
    ///     LangString webview2DownloadError ${LANG_ARABIC} "خطأ: فشل تنزيل WebView2 - $0"
    ///
    ///     Section PreInstall
    ///      ; <section logic here>
    ///     SectionEnd
    ///
    ///     Section AnotherPreInstall
    ///      ; <section logic here>
    ///     SectionEnd
    /// """
    /// ```
    pub fn preinstall_section<S: Into<String>>(mut self, preinstall_section: S) -> Self {
        self.preinstall_section.replace(preinstall_section.into());
        self
    }

    /// Set the path to a bitmap file to display on the header of installers pages.
    ///
    /// The recommended dimensions are 150px x 57px.
    pub fn header_image<P: Into<PathBuf>>(mut self, header_image: P) -> Self {
        self.header_image.replace(header_image.into());
        self
    }

    /// Set the path to a bitmap file for the Welcome page and the Finish page.
    ///
    /// The recommended dimensions are 164px x 314px.
    pub fn sidebar_image<P: Into<PathBuf>>(mut self, sidebar_image: P) -> Self {
        self.sidebar_image.replace(sidebar_image.into());
        self
    }

    /// Set the path to an icon file used as the installer icon.
    pub fn installer_icon<P: Into<PathBuf>>(mut self, installer_icon: P) -> Self {
        self.installer_icon.replace(installer_icon.into());
        self
    }

    /// Set whether the installation will be for all users or just the current user.
    pub fn install_mode(mut self, install_mode: NSISInstallerMode) -> Self {
        self.install_mode = install_mode;
        self
    }

    /// Set a list of installer languages.
    /// By default the OS language is used. If the OS language is not in the list of languages, the first language will be used.
    /// To allow the user to select the language, set `display_language_selector` to `true`.
    ///
    /// See <https://github.com/kichik/nsis/tree/9465c08046f00ccb6eda985abbdbf52c275c6c4d/Contrib/Language%20files> for the complete list of languages.
    pub fn languages<I, S>(mut self, languages: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.languages
            .replace(languages.into_iter().map(Into::into).collect());
        self
    }

    /// Set a map of key-value pair where the key is the language and the
    /// value is the path to a custom `.nsi` file that holds the translated text for cargo-packager's custom messages.
    ///
    /// See <https://github.com/crabnebula-dev/cargo-packager/blob/main/crates/packager/src/nsis/languages/English.nsh> for an example `.nsi` file.
    ///
    /// **Note**: the key must be a valid NSIS language and it must be added to [`NsisConfig`]languages array,
    pub fn custom_language_files<I, S, P>(mut self, custom_language_files: I) -> Self
    where
        I: IntoIterator<Item = (S, P)>,
        S: Into<String>,
        P: Into<PathBuf>,
    {
        self.custom_language_files.replace(
            custom_language_files
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }

    /// Set wether to display a language selector dialog before the installer and uninstaller windows are rendered or not.
    /// By default the OS language is selected, with a fallback to the first language in the `languages` array.
    pub fn display_language_selector(mut self, display: bool) -> Self {
        self.display_language_selector = display;
        self
    }

    /// Set a list of paths where your app stores data.
    /// This options tells the uninstaller to provide the user with an option
    /// (disabled by default) whether they want to rmeove your app data or keep it.
    ///
    /// The path should use a constant from <https://nsis.sourceforge.io/Docs/Chapter4.html#varconstant>
    /// in addition to `$IDENTIFIER`, `$PUBLISHER` and `$PRODUCTNAME`, for example, if you store your
    /// app data in `C:\\Users\\<user>\\AppData\\Local\\<your-company-name>\\<your-product-name>`
    /// you'd need to specify
    /// ```toml
    /// [package.metadata.packager.nsis]
    /// appdata-paths = ["$LOCALAPPDATA/$PUBLISHER/$PRODUCTNAME"]
    /// ```
    pub fn appdata_paths<I, S>(mut self, appdata_paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.appdata_paths
            .replace(appdata_paths.into_iter().map(Into::into).collect());
        self
    }
}

/// The Windows configuration.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct WindowsConfig {
    /// The file digest algorithm to use for creating file signatures. Required for code signing. SHA-256 is recommended.
    #[serde(alias = "digest-algorithm", alias = "digest_algorithm")]
    pub digest_algorithm: Option<String>,
    /// The SHA1 hash of the signing certificate.
    #[serde(alias = "certificate-thumbprint", alias = "certificate_thumbprint")]
    pub certificate_thumbprint: Option<String>,
    /// Whether to use Time-Stamp Protocol (TSP, a.k.a. RFC 3161) for the timestamp server. Your code signing provider may
    /// use a TSP timestamp server, like e.g. SSL.com does. If so, enable TSP by setting to true.
    #[serde(default)]
    pub tsp: bool,
    /// Server to use during timestamping.
    #[serde(alias = "timestamp-url", alias = "timestamp_url")]
    pub timestamp_url: Option<String>,
    /// Whether to validate a second app installation, blocking the user from installing an older version if set to `false`.
    ///
    /// For instance, if `1.2.1` is installed, the user won't be able to install app version `1.2.0` or `1.1.5`.
    ///
    /// The default value of this flag is `true`.
    #[serde(
        default = "default_true",
        alias = "allow-downgrades",
        alias = "allow_downgrades"
    )]
    pub allow_downgrades: bool,

    /// Specify a custom command to sign the binaries.
    /// This command needs to have a `%1` in it which is just a placeholder for the binary path,
    /// which we will detect and replace before calling the command.
    ///
    /// By Default we use `signtool.exe` which can be found only on Windows so
    /// if you are on another platform and want to cross-compile and sign you will
    /// need to use another tool like `osslsigncode`.
    #[serde(alias = "sign-command", alias = "sign_command")]
    pub sign_command: Option<String>,
}

impl Default for WindowsConfig {
    fn default() -> Self {
        Self {
            digest_algorithm: None,
            certificate_thumbprint: None,
            timestamp_url: None,
            tsp: false,
            allow_downgrades: true,
            sign_command: None,
        }
    }
}

impl WindowsConfig {
    /// Creates a new [`WindowsConfig`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the file digest algorithm to use for creating file signatures. Required for code signing. SHA-256 is recommended.
    pub fn digest_algorithm<S: Into<String>>(mut self, digest_algorithm: S) -> Self {
        self.digest_algorithm.replace(digest_algorithm.into());
        self
    }

    /// Set the SHA1 hash of the signing certificate.
    pub fn certificate_thumbprint<S: Into<String>>(mut self, certificate_thumbprint: S) -> Self {
        self.certificate_thumbprint
            .replace(certificate_thumbprint.into());
        self
    }

    /// Set whether to use Time-Stamp Protocol (TSP, a.k.a. RFC 3161) for the timestamp server. Your code signing provider may
    /// use a TSP timestamp server, like e.g. SSL.com does. If so, enable TSP by setting to true.
    pub fn tsp(mut self, tsp: bool) -> Self {
        self.tsp = tsp;
        self
    }

    /// Set server url to use during timestamping.
    pub fn timestamp_url<S: Into<String>>(mut self, timestamp_url: S) -> Self {
        self.timestamp_url.replace(timestamp_url.into());
        self
    }

    /// Set whether to validate a second app installation, blocking the user from installing an older version if set to `false`.
    ///
    /// For instance, if `1.2.1` is installed, the user won't be able to install app version `1.2.0` or `1.1.5`.
    ///
    /// The default value of this flag is `true`.
    pub fn allow_downgrades(mut self, allow: bool) -> Self {
        self.allow_downgrades = allow;
        self
    }
}

/// An enum representing the available verbosity levels of the logger.
#[derive(Deserialize, Serialize)]
#[repr(usize)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Default)]
pub enum LogLevel {
    /// The "error" level.
    ///
    /// Designates very serious errors.
    #[default]
    Error = 1,
    /// The "warn" level.
    ///
    /// Designates hazardous situations.
    Warn,
    /// The "info" level.
    ///
    /// Designates useful information.
    Info,
    /// The "debug" level.
    ///
    /// Designates lower priority information.
    Debug,
    /// The "trace" level.
    ///
    /// Designates very low priority, often extremely verbose, information.
    Trace,
}

/// A binary to package within the final package.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[non_exhaustive]
pub struct Binary {
    /// Path to the binary (without `.exe` on Windows).
    /// If it's relative, it will be resolved from [`Config::out_dir`].
    pub path: PathBuf,
    /// Whether this is the main binary or not
    #[serde(default)]
    pub main: bool,
}

impl Binary {
    /// Creates a new [`Binary`] from a path to the binary (without `.exe` on Windows).
    /// If it's relative, it will be resolved from [`Config::out_dir`].
    pub fn new<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            path: path.into(),
            main: false,
        }
    }

    /// Set the path of the binary.
    pub fn path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.path = path.into();
        self
    }

    /// Set the binary as main binary.
    pub fn main(mut self, main: bool) -> Self {
        self.main = main;
        self
    }
}

/// A path to a resource (with optional glob pattern)
/// or an object of `src` and `target` paths.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum Resource {
    /// Supports glob patterns
    Single(String),
    /// An object descriping the src file or directory
    /// and its target location in the final package.
    Mapped {
        /// The src file or directory, supports glob patterns.
        src: String,
        /// A relative path from the root of the final package.
        ///
        /// If `src` is a glob, this will always be treated as a directory
        /// where all globbed files will be placed under.
        target: PathBuf,
    },
}

/// Describes a shell command to be executed when a CLI hook is triggered.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum HookCommand {
    /// Run the given script with the default options.
    Script(String),
    /// Run the given script with custom options.
    ScriptWithOptions {
        /// The script to execute.
        script: String,
        /// The working directory.
        dir: Option<String>,
    },
}

/// The packaging config.
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Config {
    /// The JSON schema for the config.
    ///
    /// Setting this field has no effect, this just exists so
    /// we can parse the JSON correctly when it has `$schema` field set.
    #[serde(rename = "$schema")]
    schema: Option<String>,
    /// The app name, this is just an identifier that could be used
    /// to filter which app to package using `--packages` cli arg when there is multiple apps in the
    /// workspace or in the same config.
    ///
    /// This field resembles, the `name` field in `Cargo.toml` or `package.json`
    ///
    /// If `unset`, the CLI will try to auto-detect it from `Cargo.toml` or
    /// `package.json` otherwise, it will keep it unset.
    pub(crate) name: Option<String>,
    /// Whether this config is enabled or not. Defaults to `true`.
    #[serde(default = "default_true")]
    pub(crate) enabled: bool,
    /// The package's product name, for example "My Awesome App".
    #[serde(default, alias = "product-name", alias = "product_name")]
    pub product_name: String,
    /// The package's version.
    #[serde(default)]
    pub version: String,
    /// The binaries to package.
    #[serde(default)]
    pub binaries: Vec<Binary>,
    /// The application identifier in reverse domain name notation (e.g. `com.packager.example`).
    /// This string must be unique across applications since it is used in some system configurations.
    /// This string must contain only alphanumeric characters (A-Z, a-z, and 0-9), hyphens (-),
    /// and periods (.).
    #[cfg_attr(feature = "schema", schemars(regex(pattern = r"^[a-zA-Z0-9-\.]*$")))]
    pub identifier: Option<String>,
    /// The command to run before starting to package an application.
    ///
    /// This runs only once.
    #[serde(alias = "before-packaging-command", alias = "before_packaging_command")]
    pub before_packaging_command: Option<HookCommand>,
    /// The command to run before packaging each format for an application.
    ///
    /// This will run multiple times depending on the formats specifed.
    #[serde(
        alias = "before-each-package-command",
        alias = "before_each_package_command"
    )]
    pub before_each_package_command: Option<HookCommand>,
    /// The logging level.
    #[serde(alias = "log-level", alias = "log_level")]
    pub log_level: Option<LogLevel>,
    /// The packaging formats to create, if not present, [`PackageFormat::platform_default`] is used.
    pub formats: Option<Vec<PackageFormat>>,
    /// The directory where the generated packages will be placed.
    ///
    /// If [`Config::binaries_dir`] is not set, this is also where the [`Config::binaries`] exist.
    #[serde(default, alias = "out-dir", alias = "out_dir")]
    pub out_dir: PathBuf,
    /// The directory where the [`Config::binaries`] exist.
    ///
    /// Defaults to [`Config::out_dir`].
    #[serde(default, alias = "binaries-dir", alias = "binaries_dir")]
    pub binaries_dir: Option<PathBuf>,
    /// The target triple we are packaging for.
    ///
    /// Defaults to the current OS target triple.
    #[serde(alias = "target-triple", alias = "target_triple")]
    pub target_triple: Option<String>,
    /// The package's description.
    pub description: Option<String>,
    /// The app's long description.
    #[serde(alias = "long-description", alias = "long_description")]
    pub long_description: Option<String>,
    /// The package's homepage.
    pub homepage: Option<String>,
    /// The package's authors.
    #[serde(default)]
    pub authors: Option<Vec<String>>,
    /// The app's publisher. Defaults to the second element in [`Config::identifier`](Config::identifier) string.
    /// Currently maps to the Manufacturer property of the Windows Installer.
    pub publisher: Option<String>,
    /// A path to the license file.
    #[serde(alias = "license-file", alias = "license_file")]
    pub license_file: Option<PathBuf>,
    /// The app's copyright.
    pub copyright: Option<String>,
    /// The app's category.
    pub category: Option<AppCategory>,
    /// The app's icon list. Supports glob patterns.
    pub icons: Option<Vec<String>>,
    /// The file associations
    #[serde(alias = "file-associations", alias = "file_associations")]
    pub file_associations: Option<Vec<FileAssociation>>,
    /// Deep-link protocols.
    #[serde(alias = "deep-link-protocols", alias = "deep_link_protocols")]
    pub deep_link_protocols: Option<Vec<DeepLinkProtocol>>,
    /// The app's resources to package. This a list of either a glob pattern, path to a file, path to a directory
    /// or an object of `src` and `target` paths. In the case of using an object,
    /// the `src` could be either a glob pattern, path to a file, path to a directory,
    /// and the `target` is a path inside the final resources folder in the installed package.
    ///
    /// ## Format-specific:
    ///
    /// - **[PackageFormat::Nsis] / [PackageFormat::Wix]**: The resources are placed next to the executable in the root of the packager.
    /// - **[PackageFormat::Deb]**: The resources are placed in `usr/lib` of the package.
    pub resources: Option<Vec<Resource>>,
    /// Paths to external binaries to add to the package.
    ///
    /// The path specified should not include `-<target-triple><.exe>` suffix,
    /// it will be auto-added when by the packager when reading these paths,
    /// so the actual binary name should have the target platform's target triple appended,
    /// as well as `.exe` for Windows.
    ///
    /// For example, if you're packaging an external binary called `sqlite3`, the packager expects
    /// a binary named `sqlite3-x86_64-unknown-linux-gnu` on linux,
    /// and `sqlite3-x86_64-pc-windows-gnu.exe` on windows.
    ///
    /// If you are building a universal binary for MacOS, the packager expects
    /// your external binary to also be universal, and named after the target triple,
    /// e.g. `sqlite3-universal-apple-darwin`. See
    /// <https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary>
    #[serde(alias = "external-binaries", alias = "external_binaries")]
    pub external_binaries: Option<Vec<PathBuf>>,
    /// Windows-specific configuration.
    pub windows: Option<WindowsConfig>,
    /// MacOS-specific configuration.
    pub macos: Option<MacOsConfig>,
    /// Linux-specific configuration
    pub linux: Option<LinuxConfig>,
    /// Debian-specific configuration.
    pub deb: Option<DebianConfig>,
    /// AppImage configuration.
    pub appimage: Option<AppImageConfig>,
    /// Pacman configuration.
    pub pacman: Option<PacmanConfig>,
    /// WiX configuration.
    pub wix: Option<WixConfig>,
    /// Nsis configuration.
    pub nsis: Option<NsisConfig>,
    /// Dmg configuration.
    pub dmg: Option<DmgConfig>,
}

impl Config {
    /// Creates a new [`ConfigBuilder`].
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }

    /// Returns the [windows](Config::windows) specific configuration.
    pub fn windows(&self) -> Option<&WindowsConfig> {
        self.windows.as_ref()
    }

    /// Returns the [macos](Config::macos) specific configuration.
    pub fn macos(&self) -> Option<&MacOsConfig> {
        self.macos.as_ref()
    }

    /// Returns the [linux](Config::linux) specific configuration.
    pub fn linux(&self) -> Option<&LinuxConfig> {
        self.linux.as_ref()
    }

    /// Returns the [nsis](Config::nsis) specific configuration.
    pub fn nsis(&self) -> Option<&NsisConfig> {
        self.nsis.as_ref()
    }

    /// Returns the [wix](Config::wix) specific configuration.
    pub fn wix(&self) -> Option<&WixConfig> {
        self.wix.as_ref()
    }

    /// Returns the [debian](Config::deb) specific configuration.
    pub fn deb(&self) -> Option<&DebianConfig> {
        self.deb.as_ref()
    }

    /// Returns the [appimage](Config::appimage) specific configuration.
    pub fn appimage(&self) -> Option<&AppImageConfig> {
        self.appimage.as_ref()
    }

    /// Returns the [pacman](Config::pacman) specific configuration.
    pub fn pacman(&self) -> Option<&PacmanConfig> {
        self.pacman.as_ref()
    }

    /// Returns the [dmg](Config::dmg) specific configuration.
    pub fn dmg(&self) -> Option<&DmgConfig> {
        self.dmg.as_ref()
    }

    /// Returns the target triple of this config, if not set, fallsback to the current OS target triple.
    pub fn target_triple(&self) -> String {
        self.target_triple.clone().unwrap_or_else(|| {
            util::target_triple().expect("Failed to detect current target triple")
        })
    }

    /// Returns the architecture for the package to be built (e.g. "arm", "x86" or "x86_64").
    pub fn target_arch(&self) -> crate::Result<&str> {
        let target = self.target_triple();
        Ok(if target.starts_with("x86_64") {
            "x86_64"
        } else if target.starts_with('i') {
            "x86"
        } else if target.starts_with("arm") {
            "arm"
        } else if target.starts_with("aarch64") {
            "aarch64"
        } else if target.starts_with("universal") {
            "universal"
        } else {
            return Err(crate::Error::UnexpectedTargetTriple(target));
        })
    }

    /// Returns the path to the specified binary.
    pub fn binary_path(&self, binary: &Binary) -> PathBuf {
        if binary.path.is_absolute() {
            binary.path.clone()
        } else {
            self.binaries_dir().join(&binary.path)
        }
    }

    /// Returns the package identifier. Defaults an empty string.
    pub fn identifier(&self) -> &str {
        self.identifier.as_deref().unwrap_or("")
    }

    /// Returns the package publisher.
    /// Defaults to the second element in [`Config::identifier`](Config::identifier()).
    pub fn publisher(&self) -> String {
        self.publisher.clone().unwrap_or_else(|| {
            self.identifier()
                .split('.')
                .nth(1)
                .unwrap_or(self.identifier())
                .into()
        })
    }

    /// Returns the out dir. Defaults to the current directory.
    pub fn out_dir(&self) -> PathBuf {
        if self.out_dir.as_os_str().is_empty() {
            return std::env::current_dir().expect("failed to resolve cwd");
        }

        if !self.out_dir.exists() {
            fs::create_dir_all(&self.out_dir).expect("failed to create output directory");
        }
        dunce::canonicalize(&self.out_dir).unwrap_or_else(|_| self.out_dir.clone())
    }

    /// Returns the binaries dir. Defaults to [`Self::out_dir`] if [`Self::binaries_dir`] is not set.
    pub fn binaries_dir(&self) -> PathBuf {
        if let Some(path) = &self.binaries_dir {
            dunce::canonicalize(path).unwrap_or_else(|_| path.clone())
        } else {
            self.out_dir()
        }
    }

    /// Returns the main binary.
    pub fn main_binary(&self) -> crate::Result<&Binary> {
        self.binaries
            .iter()
            .find(|bin| bin.main)
            .ok_or_else(|| crate::Error::MainBinaryNotFound)
    }

    /// Returns a mutable reference to the main binary.
    pub fn main_binary_mut(&mut self) -> crate::Result<&mut Binary> {
        self.binaries
            .iter_mut()
            .find(|bin| bin.main)
            .ok_or_else(|| crate::Error::MainBinaryNotFound)
    }

    /// Returns the main binary name.
    pub fn main_binary_name(&self) -> crate::Result<String> {
        self.binaries
            .iter()
            .find(|bin| bin.main)
            .map(|b| b.path.file_stem().unwrap().to_string_lossy().into_owned())
            .ok_or_else(|| crate::Error::MainBinaryNotFound)
    }

    /// Returns all icons path.
    pub fn icons(&self) -> crate::Result<Option<Vec<PathBuf>>> {
        let Some(patterns) = &self.icons else {
            return Ok(None);
        };
        let mut paths = Vec::new();
        for pattern in patterns {
            for icon_path in glob::glob(pattern)? {
                paths.push(icon_path?);
            }
        }
        Ok(Some(paths))
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ResolvedResource {
    pub src: PathBuf,
    pub target: PathBuf,
}

impl Config {
    #[inline]
    pub(crate) fn resources_from_dir(
        src_dir: &Path,
        target_dir: &Path,
    ) -> crate::Result<Vec<ResolvedResource>> {
        let mut out = Vec::new();
        for entry in walkdir::WalkDir::new(src_dir) {
            let entry = entry?;
            let path = entry.path();
            if path.is_file() {
                let relative = path.relative_to(src_dir)?.to_path("");
                let src = dunce::canonicalize(path)
                    .map_err(|e| Error::IoWithPath(path.to_path_buf(), e))?;
                let resource = ResolvedResource {
                    src,
                    target: target_dir.join(relative),
                };
                out.push(resource);
            }
        }
        Ok(out)
    }

    #[inline]
    pub(crate) fn resources_from_glob(glob: &str) -> crate::Result<Vec<ResolvedResource>> {
        let mut out = Vec::new();
        for src in glob::glob(glob)? {
            let src = src?;
            let src = dunce::canonicalize(&src).map_err(|e| Error::IoWithPath(src, e))?;
            let target = PathBuf::from(src.file_name().unwrap_or_default());
            out.push(ResolvedResource { src, target })
        }
        Ok(out)
    }

    pub(crate) fn resources(&self) -> crate::Result<Vec<ResolvedResource>> {
        if let Some(resources) = &self.resources {
            let mut out = Vec::new();
            for r in resources {
                match r {
                    Resource::Single(src) => {
                        let src_dir = PathBuf::from(src);
                        if src_dir.is_dir() {
                            let target_dir = Path::new(src_dir.file_name().unwrap_or_default());
                            out.extend(Self::resources_from_dir(&src_dir, target_dir)?);
                        } else {
                            out.extend(Self::resources_from_glob(src)?);
                        }
                    }
                    Resource::Mapped { src, target } => {
                        let src_path = PathBuf::from(src);
                        let target_dir = sanitize_path(target);
                        if src_path.is_dir() {
                            out.extend(Self::resources_from_dir(&src_path, &target_dir)?);
                        } else if src_path.is_file() {
                            let src = dunce::canonicalize(&src_path)
                                .map_err(|e| Error::IoWithPath(src_path, e))?;
                            out.push(ResolvedResource {
                                src,
                                target: sanitize_path(target),
                            });
                        } else {
                            let globbed_res = Self::resources_from_glob(src)?;
                            let retargetd_res = globbed_res.into_iter().map(|mut r| {
                                r.target = target_dir.join(r.target);
                                r
                            });
                            out.extend(retargetd_res);
                        }
                    }
                }
            }

            Ok(out)
        } else {
            Ok(vec![])
        }
    }

    #[allow(unused)]
    pub(crate) fn find_ico(&self) -> crate::Result<Option<PathBuf>> {
        let icon = self
            .icons()?
            .as_ref()
            .and_then(|icons| {
                icons
                    .iter()
                    .find(|i| PathBuf::from(i).extension().and_then(|s| s.to_str()) == Some("ico"))
                    .or_else(|| {
                        icons.iter().find(|i| {
                            PathBuf::from(i).extension().and_then(|s| s.to_str()) == Some("png")
                        })
                    })
            })
            .map(PathBuf::from);
        Ok(icon)
    }

    #[allow(unused)]
    pub(crate) fn copy_resources(&self, path: &Path) -> crate::Result<()> {
        for resource in self.resources()? {
            let dest = path.join(resource.target);
            fs::create_dir_all(
                dest.parent()
                    .ok_or_else(|| crate::Error::ParentDirNotFound(dest.to_path_buf()))?,
            )?;
            fs::copy(&resource.src, &dest)
                .map_err(|e| Error::CopyFile(resource.src.clone(), dest.clone(), e))?;
        }
        Ok(())
    }

    #[allow(unused)]
    pub(crate) fn copy_external_binaries(&self, path: &Path) -> crate::Result<Vec<PathBuf>> {
        let mut paths = Vec::new();
        if let Some(external_binaries) = &self.external_binaries {
            let cwd = std::env::current_dir()?;
            let target_triple = self.target_triple();
            for src in external_binaries {
                let file_name = src
                    .file_name()
                    .ok_or_else(|| crate::Error::FailedToExtractFilename(src.clone()))?
                    .to_string_lossy();
                #[cfg(windows)]
                let src = src.with_file_name(format!("{file_name}-{target_triple}.exe"));
                #[cfg(not(windows))]
                let src = src.with_file_name(format!("{file_name}-{target_triple}"));
                #[cfg(windows)]
                let dest = path.join(format!("{file_name}.exe"));
                #[cfg(not(windows))]
                let dest = path.join(&*file_name);
                fs::copy(&src, &dest).map_err(|e| Error::CopyFile(src.clone(), dest.clone(), e))?;
                paths.push(dest);
            }
        }

        Ok(paths)
    }
}

fn sanitize_path<P: AsRef<Path>>(path: P) -> PathBuf {
    let mut dest = PathBuf::new();
    for c in path.as_ref().components() {
        if let std::path::Component::Normal(s) = c {
            dest.push(s)
        }
    }
    dest
}

fn default_true() -> bool {
    true
}