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