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