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        if let Some(build_id) = build_id {
 94            version.pre = semver::Prerelease::new(&build_id).expect("Invalid build identifier");
 95        }
 96        if let Some(sha) = commit_sha {
 97            version.build = semver::BuildMetadata::new(&sha.0).expect("Invalid build metadata");
 98        }
 99
100        version
101    }
102
103    /// Returns the global version number.
104    pub fn global(cx: &App) -> Version {
105        if cx.has_global::<GlobalAppVersion>() {
106            cx.global::<GlobalAppVersion>().0.clone()
107        } else {
108            Version::new(0, 0, 0)
109        }
110    }
111}
112
113/// A Zed release channel.
114#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
115pub enum ReleaseChannel {
116    /// The development release channel.
117    ///
118    /// Used for local debug builds of Zed.
119    #[default]
120    Dev,
121
122    /// The Nightly release channel.
123    Nightly,
124
125    /// The Preview release channel.
126    Preview,
127
128    /// The Stable release channel.
129    Stable,
130}
131
132struct GlobalReleaseChannel(ReleaseChannel);
133
134impl Global for GlobalReleaseChannel {}
135
136/// Initializes the release channel.
137pub fn init(app_version: Version, cx: &mut App) {
138    cx.set_global(GlobalAppVersion(app_version));
139    cx.set_global(GlobalReleaseChannel(*RELEASE_CHANNEL))
140}
141
142/// Initializes the release channel for tests that rely on fake release channel.
143pub fn init_test(app_version: Version, release_channel: ReleaseChannel, cx: &mut App) {
144    cx.set_global(GlobalAppVersion(app_version));
145    cx.set_global(GlobalReleaseChannel(release_channel))
146}
147
148impl ReleaseChannel {
149    /// Returns the global [`ReleaseChannel`].
150    pub fn global(cx: &App) -> Self {
151        cx.global::<GlobalReleaseChannel>().0
152    }
153
154    /// Returns the global [`ReleaseChannel`], if one is set.
155    pub fn try_global(cx: &App) -> Option<Self> {
156        cx.try_global::<GlobalReleaseChannel>()
157            .map(|channel| channel.0)
158    }
159
160    /// Returns whether we want to poll for updates for this [`ReleaseChannel`]
161    pub fn poll_for_updates(&self) -> bool {
162        !matches!(self, ReleaseChannel::Dev)
163    }
164
165    /// Returns the display name for this [`ReleaseChannel`].
166    pub fn display_name(&self) -> &'static str {
167        match self {
168            ReleaseChannel::Dev => "Zed Dev",
169            ReleaseChannel::Nightly => "Zed Nightly",
170            ReleaseChannel::Preview => "Zed Preview",
171            ReleaseChannel::Stable => "Zed",
172        }
173    }
174
175    /// Returns the programmatic name for this [`ReleaseChannel`].
176    pub fn dev_name(&self) -> &'static str {
177        match self {
178            ReleaseChannel::Dev => "dev",
179            ReleaseChannel::Nightly => "nightly",
180            ReleaseChannel::Preview => "preview",
181            ReleaseChannel::Stable => "stable",
182        }
183    }
184
185    /// Returns the application ID that's used by Wayland as application ID
186    /// and WM_CLASS on X11.
187    /// This also has to match the bundle identifier for Zed on macOS.
188    pub fn app_id(&self) -> &'static str {
189        match self {
190            ReleaseChannel::Dev => "dev.zed.Zed-Dev",
191            ReleaseChannel::Nightly => "dev.zed.Zed-Nightly",
192            ReleaseChannel::Preview => "dev.zed.Zed-Preview",
193            ReleaseChannel::Stable => "dev.zed.Zed",
194        }
195    }
196
197    /// Returns the query parameter for this [`ReleaseChannel`].
198    pub fn release_query_param(&self) -> Option<&'static str> {
199        match self {
200            Self::Dev => None,
201            Self::Nightly => Some("nightly=1"),
202            Self::Preview => Some("preview=1"),
203            Self::Stable => None,
204        }
205    }
206}
207
208/// Error indicating that release channel string does not match any known release channel names.
209#[derive(Copy, Clone, Debug, Hash, PartialEq)]
210pub struct InvalidReleaseChannel;
211
212impl FromStr for ReleaseChannel {
213    type Err = InvalidReleaseChannel;
214
215    fn from_str(channel: &str) -> Result<Self, Self::Err> {
216        Ok(match channel {
217            "dev" => ReleaseChannel::Dev,
218            "nightly" => ReleaseChannel::Nightly,
219            "preview" => ReleaseChannel::Preview,
220            "stable" => ReleaseChannel::Stable,
221            _ => return Err(InvalidReleaseChannel),
222        })
223    }
224}