cargo_packager_utils/
lib.rs

1// Copyright 2023-2023 CrabNebula Ltd.
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! # cargo-packager-utils
6//!
7//! Contain reusable components of the cargo-packager ecosystem.
8
9use std::fmt::Display;
10
11pub mod current_exe;
12
13// NOTE: When making changes to this enum,
14// make sure to also update in updater and resource-resolver bindings if needed
15/// Types of supported packages by [`cargo-packager`](https://docs.rs/cargo-packager).
16#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
18#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
19#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
20#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
21#[cfg_attr(feature = "clap", value(rename_all = "lowercase"))]
22#[non_exhaustive]
23pub enum PackageFormat {
24    /// All available package formats for the current platform.
25    ///
26    /// See [`PackageFormat::platform_all`]
27    #[cfg(feature = "cli")]
28    All,
29    /// The default list of package formats for the current platform.
30    ///
31    /// See [`PackageFormat::platform_default`]
32    #[cfg(feature = "cli")]
33    Default,
34    /// The macOS application bundle (.app).
35    App,
36    /// The macOS DMG package (.dmg).
37    Dmg,
38    /// The Microsoft Software Installer (.msi) through WiX Toolset.
39    Wix,
40    /// The NSIS installer (.exe).
41    Nsis,
42    /// The Linux Debian package (.deb).
43    Deb,
44    /// The Linux AppImage package (.AppImage).
45    AppImage,
46    /// The Linux Pacman package (.tar.gz and PKGBUILD)
47    Pacman,
48}
49
50impl Display for PackageFormat {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(f, "{}", self.short_name())
53    }
54}
55
56impl PackageFormat {
57    /// Maps a short name to a [PackageFormat].
58    /// Possible values are "deb", "pacman", "appimage", "dmg", "app", "wix", "nsis".
59    pub fn from_short_name(name: &str) -> Option<PackageFormat> {
60        // Other types we may eventually want to support: apk.
61        match name {
62            "app" => Some(PackageFormat::App),
63            "dmg" => Some(PackageFormat::Dmg),
64            "wix" => Some(PackageFormat::Wix),
65            "nsis" => Some(PackageFormat::Nsis),
66            "deb" => Some(PackageFormat::Deb),
67            "appimage" => Some(PackageFormat::AppImage),
68            _ => None,
69        }
70    }
71
72    /// Gets the short name of this [PackageFormat].
73    pub fn short_name(&self) -> &'static str {
74        match *self {
75            #[cfg(feature = "cli")]
76            PackageFormat::All => "all",
77            #[cfg(feature = "cli")]
78            PackageFormat::Default => "default",
79            PackageFormat::App => "app",
80            PackageFormat::Dmg => "dmg",
81            PackageFormat::Wix => "wix",
82            PackageFormat::Nsis => "nsis",
83            PackageFormat::Deb => "deb",
84            PackageFormat::AppImage => "appimage",
85            PackageFormat::Pacman => "pacman",
86        }
87    }
88
89    /// Gets the list of the possible package types on the current OS.
90    ///
91    /// - **macOS**: App, Dmg
92    /// - **Windows**: Nsis, Wix
93    /// - **Linux**: Deb, AppImage, Pacman
94    pub fn platform_all() -> &'static [PackageFormat] {
95        &[
96            #[cfg(target_os = "macos")]
97            PackageFormat::App,
98            #[cfg(target_os = "macos")]
99            PackageFormat::Dmg,
100            #[cfg(target_os = "windows")]
101            PackageFormat::Wix,
102            #[cfg(target_os = "windows")]
103            PackageFormat::Nsis,
104            #[cfg(any(
105                target_os = "linux",
106                target_os = "dragonfly",
107                target_os = "freebsd",
108                target_os = "netbsd",
109                target_os = "openbsd"
110            ))]
111            PackageFormat::Deb,
112            #[cfg(any(
113                target_os = "linux",
114                target_os = "dragonfly",
115                target_os = "freebsd",
116                target_os = "netbsd",
117                target_os = "openbsd"
118            ))]
119            PackageFormat::AppImage,
120            #[cfg(any(
121                target_os = "linux",
122                target_os = "dragonfly",
123                target_os = "freebsd",
124                target_os = "netbsd",
125                target_os = "openbsd"
126            ))]
127            PackageFormat::Pacman,
128        ]
129    }
130
131    /// Returns the default list of targets this platform
132    ///
133    /// - **macOS**: App, Dmg
134    /// - **Windows**: Nsis
135    /// - **Linux**: Deb, AppImage, Pacman
136    pub fn platform_default() -> &'static [PackageFormat] {
137        &[
138            #[cfg(target_os = "macos")]
139            PackageFormat::App,
140            #[cfg(target_os = "macos")]
141            PackageFormat::Dmg,
142            #[cfg(target_os = "windows")]
143            PackageFormat::Nsis,
144            #[cfg(any(
145                target_os = "linux",
146                target_os = "dragonfly",
147                target_os = "freebsd",
148                target_os = "netbsd",
149                target_os = "openbsd"
150            ))]
151            PackageFormat::Deb,
152            #[cfg(any(
153                target_os = "linux",
154                target_os = "dragonfly",
155                target_os = "freebsd",
156                target_os = "netbsd",
157                target_os = "openbsd"
158            ))]
159            PackageFormat::AppImage,
160            #[cfg(any(
161                target_os = "linux",
162                target_os = "dragonfly",
163                target_os = "freebsd",
164                target_os = "netbsd",
165                target_os = "openbsd"
166            ))]
167            PackageFormat::Pacman,
168        ]
169    }
170
171    /// Gets a number representing priority which used to sort package types
172    /// in an order that guarantees that if a certain package type
173    /// depends on another (like Dmg depending on MacOsBundle), the dependency
174    /// will be built first
175    ///
176    /// The lower the number, the higher the priority
177    pub fn priority(&self) -> u32 {
178        match self {
179            #[cfg(feature = "cli")]
180            PackageFormat::All => 0,
181            #[cfg(feature = "cli")]
182            PackageFormat::Default => 0,
183            PackageFormat::App => 0,
184            PackageFormat::Wix => 0,
185            PackageFormat::Nsis => 0,
186            PackageFormat::Deb => 0,
187            PackageFormat::AppImage => 0,
188            PackageFormat::Pacman => 0,
189            PackageFormat::Dmg => 1,
190        }
191    }
192}