lib.rs

  1//! Provides constructs for the Zed app version and release channel.
  2
  3#![deny(missing_docs)]
  4
  5use std::{env, str::FromStr, sync::LazyLock};
  6
  7use gpui::{App, Global, SemanticVersion};
  8
  9/// stable | dev | nightly | preview
 10pub static RELEASE_CHANNEL_NAME: LazyLock<String> = LazyLock::new(|| {
 11    if cfg!(debug_assertions) {
 12        env::var("ZED_RELEASE_CHANNEL")
 13            .unwrap_or_else(|_| include_str!("../../zed/RELEASE_CHANNEL").trim().to_string())
 14    } else {
 15        include_str!("../../zed/RELEASE_CHANNEL").trim().to_string()
 16    }
 17});
 18
 19#[doc(hidden)]
 20pub static RELEASE_CHANNEL: LazyLock<ReleaseChannel> =
 21    LazyLock::new(|| match ReleaseChannel::from_str(&RELEASE_CHANNEL_NAME) {
 22        Ok(channel) => channel,
 23        _ => panic!("invalid release channel {}", *RELEASE_CHANNEL_NAME),
 24    });
 25
 26/// The app identifier for the current release channel, Windows only.
 27#[cfg(target_os = "windows")]
 28pub fn app_identifier() -> &'static str {
 29    match *RELEASE_CHANNEL {
 30        ReleaseChannel::Dev => "Zed-Editor-Dev",
 31        ReleaseChannel::Nightly => "Zed-Editor-Nightly",
 32        ReleaseChannel::Preview => "Zed-Editor-Preview",
 33        ReleaseChannel::Stable => "Zed-Editor-Stable",
 34    }
 35}
 36
 37/// The Git commit SHA that Zed was built at.
 38#[derive(Clone, Eq, Debug, PartialEq)]
 39pub struct AppCommitSha(String);
 40
 41struct GlobalAppCommitSha(AppCommitSha);
 42
 43impl Global for GlobalAppCommitSha {}
 44
 45impl AppCommitSha {
 46    /// Creates a new [`AppCommitSha`].
 47    pub fn new(sha: String) -> Self {
 48        AppCommitSha(sha)
 49    }
 50
 51    /// Returns the global [`AppCommitSha`], if one is set.
 52    pub fn try_global(cx: &App) -> Option<AppCommitSha> {
 53        cx.try_global::<GlobalAppCommitSha>()
 54            .map(|sha| sha.0.clone())
 55    }
 56
 57    /// Sets the global [`AppCommitSha`].
 58    pub fn set_global(sha: AppCommitSha, cx: &mut App) {
 59        cx.set_global(GlobalAppCommitSha(sha))
 60    }
 61
 62    /// Returns the full commit SHA.
 63    pub fn full(&self) -> String {
 64        self.0.to_string()
 65    }
 66
 67    /// Returns the short (7 character) commit SHA.
 68    pub fn short(&self) -> String {
 69        self.0.chars().take(7).collect()
 70    }
 71}
 72
 73struct GlobalAppVersion(SemanticVersion);
 74
 75impl Global for GlobalAppVersion {}
 76
 77/// The version of Zed.
 78pub struct AppVersion;
 79
 80impl AppVersion {
 81    /// Load the app version from env.
 82    pub fn load(pkg_version: &str) -> SemanticVersion {
 83        if let Ok(from_env) = env::var("ZED_APP_VERSION") {
 84            from_env.parse().expect("invalid ZED_APP_VERSION")
 85        } else {
 86            pkg_version.parse().expect("invalid version in Cargo.toml")
 87        }
 88    }
 89
 90    /// Returns the global version number.
 91    pub fn global(cx: &App) -> SemanticVersion {
 92        if cx.has_global::<GlobalAppVersion>() {
 93            cx.global::<GlobalAppVersion>().0
 94        } else {
 95            SemanticVersion::default()
 96        }
 97    }
 98}
 99
100/// A Zed release channel.
101#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
102pub enum ReleaseChannel {
103    /// The development release channel.
104    ///
105    /// Used for local debug builds of Zed.
106    #[default]
107    Dev,
108
109    /// The Nightly release channel.
110    Nightly,
111
112    /// The Preview release channel.
113    Preview,
114
115    /// The Stable release channel.
116    Stable,
117}
118
119struct GlobalReleaseChannel(ReleaseChannel);
120
121impl Global for GlobalReleaseChannel {}
122
123/// Initializes the release channel.
124pub fn init(app_version: SemanticVersion, cx: &mut App) {
125    cx.set_global(GlobalAppVersion(app_version));
126    cx.set_global(GlobalReleaseChannel(*RELEASE_CHANNEL))
127}
128
129impl ReleaseChannel {
130    /// Returns the global [`ReleaseChannel`].
131    pub fn global(cx: &App) -> Self {
132        cx.global::<GlobalReleaseChannel>().0
133    }
134
135    /// Returns the global [`ReleaseChannel`], if one is set.
136    pub fn try_global(cx: &App) -> Option<Self> {
137        cx.try_global::<GlobalReleaseChannel>()
138            .map(|channel| channel.0)
139    }
140
141    /// Returns whether we want to poll for updates for this [`ReleaseChannel`]
142    pub fn poll_for_updates(&self) -> bool {
143        !matches!(self, ReleaseChannel::Dev)
144    }
145
146    /// Returns the display name for this [`ReleaseChannel`].
147    pub fn display_name(&self) -> &'static str {
148        match self {
149            ReleaseChannel::Dev => "Zed Dev",
150            ReleaseChannel::Nightly => "Zed Nightly",
151            ReleaseChannel::Preview => "Zed Preview",
152            ReleaseChannel::Stable => "Zed",
153        }
154    }
155
156    /// Returns the programmatic name for this [`ReleaseChannel`].
157    pub fn dev_name(&self) -> &'static str {
158        match self {
159            ReleaseChannel::Dev => "dev",
160            ReleaseChannel::Nightly => "nightly",
161            ReleaseChannel::Preview => "preview",
162            ReleaseChannel::Stable => "stable",
163        }
164    }
165
166    /// Returns the application ID that's used by Wayland as application ID
167    /// and WM_CLASS on X11.
168    /// This also has to match the bundle identifier for Zed on macOS.
169    pub fn app_id(&self) -> &'static str {
170        match self {
171            ReleaseChannel::Dev => "dev.zed.Zed-Dev",
172            ReleaseChannel::Nightly => "dev.zed.Zed-Nightly",
173            ReleaseChannel::Preview => "dev.zed.Zed-Preview",
174            ReleaseChannel::Stable => "dev.zed.Zed",
175        }
176    }
177
178    /// Returns the query parameter for this [`ReleaseChannel`].
179    pub fn release_query_param(&self) -> Option<&'static str> {
180        match self {
181            Self::Dev => None,
182            Self::Nightly => Some("nightly=1"),
183            Self::Preview => Some("preview=1"),
184            Self::Stable => None,
185        }
186    }
187}
188
189/// Error indicating that release channel string does not match any known release channel names.
190#[derive(Copy, Clone, Debug, Hash, PartialEq)]
191pub struct InvalidReleaseChannel;
192
193impl FromStr for ReleaseChannel {
194    type Err = InvalidReleaseChannel;
195
196    fn from_str(channel: &str) -> Result<Self, Self::Err> {
197        Ok(match channel {
198            "dev" => ReleaseChannel::Dev,
199            "nightly" => ReleaseChannel::Nightly,
200            "preview" => ReleaseChannel::Preview,
201            "stable" => ReleaseChannel::Stable,
202            _ => return Err(InvalidReleaseChannel),
203        })
204    }
205}