|
| 1 | +use std::{error::Error, fs::{self}, path::{Path}}; |
| 2 | + |
| 3 | +use zed_extension_api::{self as zed, settings::LspSettings, LanguageServerId}; |
| 4 | + |
| 5 | +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 6 | + |
| 7 | +struct Odoo { |
| 8 | + cached_binary_path: Option<String>, |
| 9 | +} |
| 10 | + |
| 11 | +impl Odoo { |
| 12 | + |
| 13 | + fn language_server_binary_path( |
| 14 | + &mut self, |
| 15 | + language_server_id: &LanguageServerId, |
| 16 | + _worktree: &zed::Worktree, |
| 17 | + ) -> Result<String, Box<dyn Error>> { |
| 18 | + |
| 19 | + if let Some(path) = &self.cached_binary_path { |
| 20 | + if fs::metadata(path).is_ok_and(|stat| stat.is_file()) { |
| 21 | + return Ok(path.clone()); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + zed::set_language_server_installation_status( |
| 26 | + language_server_id, |
| 27 | + &zed::LanguageServerInstallationStatus::CheckingForUpdate, |
| 28 | + ); |
| 29 | + |
| 30 | + let version_dir = format!("{}", VERSION); |
| 31 | + fs::create_dir_all(version_dir.clone()).map_err(|err| format!("failed to create version directory: {err}"))?; |
| 32 | + |
| 33 | + let release = zed::github_release_by_tag_name( |
| 34 | + "odoo/odoo-ls", |
| 35 | + VERSION, |
| 36 | + )?; |
| 37 | + |
| 38 | + let asset_name = format!("odoo-{}-{}.zip", Odoo::platform(), VERSION); |
| 39 | + |
| 40 | + let asset = release.assets.iter().find(|asset| asset.name == asset_name).ok_or_else( |
| 41 | + || format!("Odoo: No asset found for asset name {}", asset_name) |
| 42 | + )?; |
| 43 | + |
| 44 | + let asset_typeshed = release.assets.iter().find(|asset| asset.name == "typeshed.zip").ok_or_else( |
| 45 | + || format!("Odoo: No asset found for asset name {}", "typeshed.zip") |
| 46 | + )?; |
| 47 | + |
| 48 | + let mut exe_name = String::from("odoo_ls_server"); |
| 49 | + if cfg!(windows) { |
| 50 | + exe_name += ".exe"; |
| 51 | + } |
| 52 | + |
| 53 | + let binary_path = format!( |
| 54 | + "{version_dir}/{bin_name}", |
| 55 | + bin_name = match zed::current_platform().0 { |
| 56 | + zed::Os::Windows => format!("{}.exe", "odoo_ls_server"), |
| 57 | + zed::Os::Mac | zed::Os::Linux => "odoo_ls_server".to_string(), |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + if !fs::metadata(&binary_path).is_ok_and(|stat| stat.is_file()) { |
| 62 | + zed::set_language_server_installation_status( |
| 63 | + language_server_id, |
| 64 | + &zed::LanguageServerInstallationStatus::Downloading, |
| 65 | + ); |
| 66 | + |
| 67 | + let path_typeshed = Path::new(&version_dir).join("typeshed"); |
| 68 | + if path_typeshed.exists() { |
| 69 | + fs::remove_dir_all(path_typeshed)?; |
| 70 | + } |
| 71 | + |
| 72 | + zed::download_file(&asset.download_url, &version_dir, zed::DownloadedFileType::Zip) |
| 73 | + .map_err(|err| format!("failed to download file: {err}"))?; |
| 74 | + |
| 75 | + zed::download_file(&asset_typeshed.download_url, &version_dir, zed::DownloadedFileType::Zip) |
| 76 | + .map_err(|err| format!("failed to download file: {err}"))?; |
| 77 | + |
| 78 | + zed::make_file_executable(&binary_path)?; |
| 79 | + |
| 80 | + let entries = fs::read_dir(".") |
| 81 | + .map_err(|err| format!("failed to list working directory {err}"))?; |
| 82 | + for entry in entries { |
| 83 | + let entry = entry.map_err(|err| format!("failed to load directory entry {err}"))?; |
| 84 | + if entry.file_name().to_str() != Some(&version_dir) { |
| 85 | + fs::remove_dir_all(entry.path()).ok(); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + self.cached_binary_path = Some(binary_path.clone()); |
| 91 | + |
| 92 | + Ok(binary_path) |
| 93 | + } |
| 94 | + |
| 95 | + fn platform() -> &'static str { |
| 96 | + let (platform, arch) = zed::current_platform(); |
| 97 | + match (platform, arch) { |
| 98 | + (zed::Os::Linux, zed::Architecture::X8664) if cfg!(target_env = "musl") => "alpine-x64", |
| 99 | + (zed::Os::Linux, zed::Architecture::Aarch64) if cfg!(target_env = "musl") => "alpine-arm64", |
| 100 | + (zed::Os::Linux, zed::Architecture::X8664) => "linux-x64", |
| 101 | + (zed::Os::Linux, zed::Architecture::Aarch64) => "linux-arm64", |
| 102 | + (zed::Os::Windows, zed::Architecture::X8664) => "win32-x64", |
| 103 | + (zed::Os::Windows, zed::Architecture::Aarch64) => "win32-arm64", |
| 104 | + (zed::Os::Mac, zed::Architecture::X8664) => "darwin-x64", |
| 105 | + (zed::Os::Mac, zed::Architecture::Aarch64) => "darwin-arm64", |
| 106 | + (_os, arch) => { |
| 107 | + // fallback |
| 108 | + println!("Odoo: Warning: Unsupported platform {platform:?}-{arch:?}"); |
| 109 | + Box::leak(format!("unknown").into_boxed_str()) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl zed::Extension for Odoo { |
| 116 | + |
| 117 | + fn new() -> Self { |
| 118 | + Self { |
| 119 | + cached_binary_path: None |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn language_server_command( |
| 124 | + &mut self, |
| 125 | + language_server_id: &LanguageServerId, |
| 126 | + worktree: &zed::Worktree, |
| 127 | + ) -> Result<zed::Command, String> { |
| 128 | + Ok(zed::Command { |
| 129 | + command: self.language_server_binary_path(language_server_id, worktree).map_err(|e| e.to_string())?, |
| 130 | + args: vec![], |
| 131 | + env: vec![], |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | + fn language_server_initialization_options( |
| 136 | + &mut self, |
| 137 | + server_id: &LanguageServerId, |
| 138 | + worktree: &zed_extension_api::Worktree, |
| 139 | + ) -> Result<Option<zed_extension_api::serde_json::Value>, String> { |
| 140 | + let settings = LspSettings::for_worktree(server_id.as_ref(), worktree) |
| 141 | + .ok() |
| 142 | + .and_then(|lsp_settings| lsp_settings.initialization_options.clone()) |
| 143 | + .unwrap_or_default(); |
| 144 | + Ok(Some(settings)) |
| 145 | + } |
| 146 | + |
| 147 | + fn language_server_workspace_configuration( |
| 148 | + &mut self, |
| 149 | + server_id: &LanguageServerId, |
| 150 | + worktree: &zed_extension_api::Worktree, |
| 151 | + ) -> Result<Option<zed_extension_api::serde_json::Value>, String> { |
| 152 | + let settings = LspSettings::for_worktree(server_id.as_ref(), worktree) |
| 153 | + .ok() |
| 154 | + .and_then(|lsp_settings| lsp_settings.settings.clone()) |
| 155 | + .unwrap_or_default(); |
| 156 | + Ok(Some(settings)) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +zed::register_extension!(Odoo); |
0 commit comments