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
129/// Initializes the release channel for tests that rely on fake release channel.
130pub fn init_test(app_version: SemanticVersion, release_channel: ReleaseChannel, cx: &mut App) {
131    cx.set_global(GlobalAppVersion(app_version));
132    cx.set_global(GlobalReleaseChannel(release_channel))
133}
134
135impl ReleaseChannel {
136    /// Returns the global [`ReleaseChannel`].
137    pub fn global(cx: &App) -> Self {
138        cx.global::<GlobalReleaseChannel>().0
139    }
140
141    /// Returns the global [`ReleaseChannel`], if one is set.
142    pub fn try_global(cx: &App) -> Option<Self> {
143        cx.try_global::<GlobalReleaseChannel>()
144            .map(|channel| channel.0)
145    }
146
147    /// Returns whether we want to poll for updates for this [`ReleaseChannel`]
148    pub fn poll_for_updates(&self) -> bool {
149        !matches!(self, ReleaseChannel::Dev)
150    }
151
152    /// Returns the display name for this [`ReleaseChannel`].
153    pub fn display_name(&self) -> &'static str {
154        match self {
155            ReleaseChannel::Dev => "Zed Dev",
156            ReleaseChannel::Nightly => "Zed Nightly",
157            ReleaseChannel::Preview => "Zed Preview",
158            ReleaseChannel::Stable => "Zed",
159        }
160    }
161
162    /// Returns the programmatic name for this [`ReleaseChannel`].
163    pub fn dev_name(&self) -> &'static str {
164        match self {
165            ReleaseChannel::Dev => "dev",
166            ReleaseChannel::Nightly => "nightly",
167            ReleaseChannel::Preview => "preview",
168            ReleaseChannel::Stable => "stable",
169        }
170    }
171
172    /// Returns the application ID that's used by Wayland as application ID
173    /// and WM_CLASS on X11.
174    /// This also has to match the bundle identifier for Zed on macOS.
175    pub fn app_id(&self) -> &'static str {
176        match self {
177            ReleaseChannel::Dev => "dev.zed.Zed-Dev",
178            ReleaseChannel::Nightly => "dev.zed.Zed-Nightly",
179            ReleaseChannel::Preview => "dev.zed.Zed-Preview",
180            ReleaseChannel::Stable => "dev.zed.Zed",
181        }
182    }
183
184    /// Returns the query parameter for this [`ReleaseChannel`].
185    pub fn release_query_param(&self) -> Option<&'static str> {
186        match self {
187            Self::Dev => None,
188            Self::Nightly => Some("nightly=1"),
189            Self::Preview => Some("preview=1"),
190            Self::Stable => None,
191        }
192    }
193}
194
195/// Error indicating that release channel string does not match any known release channel names.
196#[derive(Copy, Clone, Debug, Hash, PartialEq)]
197pub struct InvalidReleaseChannel;
198
199impl FromStr for ReleaseChannel {
200    type Err = InvalidReleaseChannel;
201
202    fn from_str(channel: &str) -> Result<Self, Self::Err> {
203        Ok(match channel {
204            "dev" => ReleaseChannel::Dev,
205            "nightly" => ReleaseChannel::Nightly,
206            "preview" => ReleaseChannel::Preview,
207            "stable" => ReleaseChannel::Stable,
208            _ => return Err(InvalidReleaseChannel),
209        })
210    }
211}