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 Git commit SHA that Zed was built at.
 27#[derive(Clone)]
 28pub struct AppCommitSha(pub String);
 29
 30struct GlobalAppCommitSha(AppCommitSha);
 31
 32impl Global for GlobalAppCommitSha {}
 33
 34impl AppCommitSha {
 35    /// Returns the global [`AppCommitSha`], if one is set.
 36    pub fn try_global(cx: &App) -> Option<AppCommitSha> {
 37        cx.try_global::<GlobalAppCommitSha>()
 38            .map(|sha| sha.0.clone())
 39    }
 40
 41    /// Sets the global [`AppCommitSha`].
 42    pub fn set_global(sha: AppCommitSha, cx: &mut App) {
 43        cx.set_global(GlobalAppCommitSha(sha))
 44    }
 45}
 46
 47struct GlobalAppVersion(SemanticVersion);
 48
 49impl Global for GlobalAppVersion {}
 50
 51/// The version of Zed.
 52pub struct AppVersion;
 53
 54impl AppVersion {
 55    /// Initializes the global [`AppVersion`].
 56    ///
 57    /// Attempts to read the version number from the following locations, in order:
 58    /// 1. the `ZED_APP_VERSION` environment variable,
 59    /// 2. the [`AppContext::app_metadata`],
 60    /// 3. the passed in `pkg_version`.
 61    pub fn init(pkg_version: &str) -> SemanticVersion {
 62        if let Ok(from_env) = env::var("ZED_APP_VERSION") {
 63            from_env.parse().expect("invalid ZED_APP_VERSION")
 64        } else {
 65            pkg_version.parse().expect("invalid version in Cargo.toml")
 66        }
 67    }
 68
 69    /// Returns the global version number.
 70    pub fn global(cx: &App) -> SemanticVersion {
 71        if cx.has_global::<GlobalAppVersion>() {
 72            cx.global::<GlobalAppVersion>().0
 73        } else {
 74            SemanticVersion::default()
 75        }
 76    }
 77}
 78
 79/// A Zed release channel.
 80#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
 81pub enum ReleaseChannel {
 82    /// The development release channel.
 83    ///
 84    /// Used for local debug builds of Zed.
 85    #[default]
 86    Dev,
 87
 88    /// The Nightly release channel.
 89    Nightly,
 90
 91    /// The Preview release channel.
 92    Preview,
 93
 94    /// The Stable release channel.
 95    Stable,
 96}
 97
 98struct GlobalReleaseChannel(ReleaseChannel);
 99
100impl Global for GlobalReleaseChannel {}
101
102/// Initializes the release channel.
103pub fn init(app_version: SemanticVersion, cx: &mut App) {
104    cx.set_global(GlobalAppVersion(app_version));
105    cx.set_global(GlobalReleaseChannel(*RELEASE_CHANNEL))
106}
107
108impl ReleaseChannel {
109    /// Returns the global [`ReleaseChannel`].
110    pub fn global(cx: &App) -> Self {
111        cx.global::<GlobalReleaseChannel>().0
112    }
113
114    /// Returns the global [`ReleaseChannel`], if one is set.
115    pub fn try_global(cx: &App) -> Option<Self> {
116        cx.try_global::<GlobalReleaseChannel>()
117            .map(|channel| channel.0)
118    }
119
120    /// Returns whether we want to poll for updates for this [`ReleaseChannel`]
121    pub fn poll_for_updates(&self) -> bool {
122        !matches!(self, ReleaseChannel::Dev)
123    }
124
125    /// Returns the display name for this [`ReleaseChannel`].
126    pub fn display_name(&self) -> &'static str {
127        match self {
128            ReleaseChannel::Dev => "Zed Dev",
129            ReleaseChannel::Nightly => "Zed Nightly",
130            ReleaseChannel::Preview => "Zed Preview",
131            ReleaseChannel::Stable => "Zed",
132        }
133    }
134
135    /// Returns the programmatic name for this [`ReleaseChannel`].
136    pub fn dev_name(&self) -> &'static str {
137        match self {
138            ReleaseChannel::Dev => "dev",
139            ReleaseChannel::Nightly => "nightly",
140            ReleaseChannel::Preview => "preview",
141            ReleaseChannel::Stable => "stable",
142        }
143    }
144
145    /// Returns the application ID that's used by Wayland as application ID
146    /// and WM_CLASS on X11.
147    /// This also has to match the bundle identifier for Zed on macOS.
148    pub fn app_id(&self) -> &'static str {
149        match self {
150            ReleaseChannel::Dev => "dev.zed.Zed-Dev",
151            ReleaseChannel::Nightly => "dev.zed.Zed-Nightly",
152            ReleaseChannel::Preview => "dev.zed.Zed-Preview",
153            ReleaseChannel::Stable => "dev.zed.Zed",
154        }
155    }
156
157    /// Returns the query parameter for this [`ReleaseChannel`].
158    pub fn release_query_param(&self) -> Option<&'static str> {
159        match self {
160            Self::Dev => None,
161            Self::Nightly => Some("nightly=1"),
162            Self::Preview => Some("preview=1"),
163            Self::Stable => None,
164        }
165    }
166}
167
168/// Error indicating that release channel string does not match any known release channel names.
169#[derive(Copy, Clone, Debug, Hash, PartialEq)]
170pub struct InvalidReleaseChannel;
171
172impl FromStr for ReleaseChannel {
173    type Err = InvalidReleaseChannel;
174
175    fn from_str(channel: &str) -> Result<Self, Self::Err> {
176        Ok(match channel {
177            "dev" => ReleaseChannel::Dev,
178            "nightly" => ReleaseChannel::Nightly,
179            "preview" => ReleaseChannel::Preview,
180            "stable" => ReleaseChannel::Stable,
181            _ => return Err(InvalidReleaseChannel),
182        })
183    }
184}