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