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