headless.rs

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