1use client::{Client, ProxySettings, UserStore};
2use collections::HashMap;
3use extension::ExtensionHostProxy;
4use fs::RealFs;
5use gpui::http_client::read_proxy_from_env;
6use gpui::{App, AppContext, Entity};
7use gpui_tokio::Tokio;
8use language::LanguageRegistry;
9use language_extension::LspAccess;
10use node_runtime::{NodeBinaryOptions, NodeRuntime};
11use project::{Project, project_settings::ProjectSettings};
12use release_channel::{AppCommitSha, AppVersion};
13use reqwest_client::ReqwestClient;
14use settings::{Settings, SettingsStore};
15use std::path::PathBuf;
16use std::sync::{Arc, Mutex};
17use util::ResultExt as _;
18
19/// Headless subset of `workspace::AppState`.
20pub struct EpAppState {
21 pub languages: Arc<LanguageRegistry>,
22 pub client: Arc<Client>,
23 pub user_store: Entity<UserStore>,
24 pub fs: Arc<dyn fs::Fs>,
25 pub node_runtime: NodeRuntime,
26 pub project_cache: ProjectCache,
27}
28
29#[derive(Default)]
30pub struct ProjectCache(Mutex<HashMap<String, Entity<Project>>>);
31
32impl ProjectCache {
33 pub fn insert(&self, repository_url: String, project: Entity<Project>) {
34 self.0.lock().unwrap().insert(repository_url, project);
35 }
36
37 pub fn get(&self, repository_url: &String) -> Option<Entity<Project>> {
38 self.0.lock().unwrap().get(repository_url).cloned()
39 }
40}
41
42pub fn init(cx: &mut App) -> EpAppState {
43 let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
44
45 let app_version = AppVersion::load(
46 env!("ZED_PKG_VERSION"),
47 option_env!("ZED_BUILD_ID"),
48 app_commit_sha,
49 );
50 release_channel::init(app_version.clone(), cx);
51 gpui_tokio::init(cx);
52
53 let settings_store = SettingsStore::new(cx, &settings::default_settings());
54 cx.set_global(settings_store);
55
56 // Set User-Agent so we can download language servers from GitHub
57 let user_agent = format!(
58 "Zeta CLI/{} ({}; {})",
59 app_version,
60 std::env::consts::OS,
61 std::env::consts::ARCH
62 );
63 let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
64 let proxy_url = proxy_str
65 .as_ref()
66 .and_then(|input| input.parse().ok())
67 .or_else(read_proxy_from_env);
68 let http = {
69 let _guard = Tokio::handle(cx).enter();
70
71 ReqwestClient::proxy_and_user_agent(proxy_url, &user_agent)
72 .expect("could not start HTTP client")
73 };
74 cx.set_http_client(Arc::new(http));
75
76 let client = Client::production(cx);
77 cx.set_http_client(client.http_client());
78
79 let git_binary_path = None;
80 let fs = Arc::new(RealFs::new(
81 git_binary_path,
82 cx.background_executor().clone(),
83 ));
84
85 let mut languages = LanguageRegistry::new(cx.background_executor().clone());
86 languages.set_language_server_download_dir(paths::languages_dir().clone());
87 let languages = Arc::new(languages);
88
89 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
90
91 extension::init(cx);
92
93 let (mut tx, rx) = watch::channel(None);
94 cx.observe_global::<SettingsStore>(move |cx| {
95 let settings = &ProjectSettings::get_global(cx).node;
96 let options = NodeBinaryOptions {
97 allow_path_lookup: !settings.ignore_system_version,
98 allow_binary_download: true,
99 use_paths: settings.path.as_ref().map(|node_path| {
100 let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
101 let npm_path = settings
102 .npm_path
103 .as_ref()
104 .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
105 (
106 node_path.clone(),
107 npm_path.unwrap_or_else(|| {
108 let base_path = PathBuf::new();
109 node_path.parent().unwrap_or(&base_path).join("npm")
110 }),
111 )
112 }),
113 };
114 tx.send(Some(options)).log_err();
115 })
116 .detach();
117 let node_runtime = NodeRuntime::new(client.http_client(), None, rx);
118
119 let extension_host_proxy = ExtensionHostProxy::global(cx);
120
121 debug_adapter_extension::init(extension_host_proxy.clone(), cx);
122 language_extension::init(LspAccess::Noop, extension_host_proxy, languages.clone());
123 language_model::init(client.clone(), cx);
124 language_models::init(user_store.clone(), client.clone(), cx);
125 languages::init(languages.clone(), fs.clone(), node_runtime.clone(), cx);
126 prompt_store::init(cx);
127 terminal_view::init(cx);
128
129 let project_cache = ProjectCache::default();
130
131 EpAppState {
132 languages,
133 client,
134 user_store,
135 fs,
136 node_runtime,
137 project_cache,
138 }
139}