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 /// Returns the global [`ReleaseChannel`].
158 pub fn global(cx: &App) -> Self {
159 cx.global::<GlobalReleaseChannel>().0
160 }
161
162 /// Returns the global [`ReleaseChannel`], if one is set.
163 pub fn try_global(cx: &App) -> Option<Self> {
164 cx.try_global::<GlobalReleaseChannel>()
165 .map(|channel| channel.0)
166 }
167
168 /// Returns whether we want to poll for updates for this [`ReleaseChannel`]
169 pub fn poll_for_updates(&self) -> bool {
170 !matches!(self, ReleaseChannel::Dev)
171 }
172
173 /// Returns the display name for this [`ReleaseChannel`].
174 pub fn display_name(&self) -> &'static str {
175 match self {
176 ReleaseChannel::Dev => "Zed Dev",
177 ReleaseChannel::Nightly => "Zed Nightly",
178 ReleaseChannel::Preview => "Zed Preview",
179 ReleaseChannel::Stable => "Zed",
180 }
181 }
182
183 /// Returns the programmatic name for this [`ReleaseChannel`].
184 pub fn dev_name(&self) -> &'static str {
185 match self {
186 ReleaseChannel::Dev => "dev",
187 ReleaseChannel::Nightly => "nightly",
188 ReleaseChannel::Preview => "preview",
189 ReleaseChannel::Stable => "stable",
190 }
191 }
192
193 /// Returns the application ID that's used by Wayland as application ID
194 /// and WM_CLASS on X11.
195 /// This also has to match the bundle identifier for Zed on macOS.
196 pub fn app_id(&self) -> &'static str {
197 match self {
198 ReleaseChannel::Dev => "dev.zed.Zed-Dev",
199 ReleaseChannel::Nightly => "dev.zed.Zed-Nightly",
200 ReleaseChannel::Preview => "dev.zed.Zed-Preview",
201 ReleaseChannel::Stable => "dev.zed.Zed",
202 }
203 }
204
205 /// Returns the query parameter for this [`ReleaseChannel`].
206 pub fn release_query_param(&self) -> Option<&'static str> {
207 match self {
208 Self::Dev => None,
209 Self::Nightly => Some("nightly=1"),
210 Self::Preview => Some("preview=1"),
211 Self::Stable => None,
212 }
213 }
214}
215
216/// Error indicating that release channel string does not match any known release channel names.
217#[derive(Copy, Clone, Debug, Hash, PartialEq)]
218pub struct InvalidReleaseChannel;
219
220impl FromStr for ReleaseChannel {
221 type Err = InvalidReleaseChannel;
222
223 fn from_str(channel: &str) -> Result<Self, Self::Err> {
224 Ok(match channel {
225 "dev" => ReleaseChannel::Dev,
226 "nightly" => ReleaseChannel::Nightly,
227 "preview" => ReleaseChannel::Preview,
228 "stable" => ReleaseChannel::Stable,
229 _ => return Err(InvalidReleaseChannel),
230 })
231 }
232}