|
1 | 1 | use anyhow::{Context, Result, bail}; |
2 | | -use edn_rs::Edn; |
3 | | -use serde::Deserialize; |
| 2 | +use serde::{Deserialize, Serialize}; |
4 | 3 | use std::{ |
5 | 4 | fs, |
6 | 5 | path::{Path, PathBuf}, |
7 | | - str::FromStr, |
8 | 6 | }; |
9 | 7 |
|
10 | | -use crate::bridge; |
| 8 | +use crate::{bridge, editor}; |
11 | 9 |
|
12 | 10 | #[derive(Debug, Deserialize)] |
13 | 11 | pub enum LauncherType { |
@@ -141,42 +139,46 @@ fn create_runner_script( |
141 | 139 | Ok(script_path) |
142 | 140 | } |
143 | 141 |
|
144 | | -pub fn set_default_editor(plugin_root: &Path, launcher_settings: &LauncherSettings) -> Result<()> { |
145 | | - if !plugin_root.exists() { |
146 | | - bail!("plugin root '{}' could not be found", plugin_root.display()); |
147 | | - } |
| 142 | +#[derive(Serialize)] |
| 143 | +struct EditorConfig { |
| 144 | + #[serde(rename = "custom-editor")] |
| 145 | + custom_editor: String, |
148 | 146 |
|
149 | | - let config_dir = if cfg!(target_os = "macos") { |
150 | | - // on macos defold stores prefs.editor_settings in ~/Library/Preferences |
151 | | - dirs::preference_dir().context("could not find pref dir")? |
152 | | - } else { |
153 | | - dirs::config_dir().context("could not find config dir")? |
154 | | - }; |
155 | | - let path = config_dir.join("Defold").join("prefs.editor_settings"); |
| 147 | + #[serde(rename = "open-file")] |
| 148 | + open_file: String, |
156 | 149 |
|
157 | | - if !path.exists() { |
158 | | - bail!( |
159 | | - "prefs.editor_settings file {} could not be found", |
160 | | - path.display() |
161 | | - ); |
162 | | - } |
| 150 | + #[serde(rename = "open-file-at-line")] |
| 151 | + open_file_at_line: String, |
| 152 | +} |
163 | 153 |
|
164 | | - let data = fs::read_to_string(&path)?; |
| 154 | +pub fn set_default_editor( |
| 155 | + port: u16, |
| 156 | + plugin_root: &Path, |
| 157 | + launcher_settings: &LauncherSettings, |
| 158 | +) -> Result<()> { |
| 159 | + if !editor::is_editor_port(port) { |
| 160 | + bail!("No edito was found runnign at {port}"); |
| 161 | + } |
165 | 162 |
|
166 | | - let mut config = Edn::from_str(&data).map_err(|err| anyhow::anyhow!(err.to_string()))?; |
| 163 | + if !plugin_root.exists() { |
| 164 | + bail!("plugin root '{}' could not be found", plugin_root.display()); |
| 165 | + } |
167 | 166 |
|
168 | | - config[":code"][":custom-editor"] = Edn::Str( |
169 | | - create_runner_script(plugin_root, launcher_settings)? |
| 167 | + let config = EditorConfig { |
| 168 | + custom_editor: create_runner_script(plugin_root, launcher_settings)? |
170 | 169 | .to_str() |
171 | 170 | .context("could not convert path to string")? |
172 | 171 | .to_string(), |
173 | | - ); |
174 | | - config[":code"][":open-file"] = Edn::Str("{file}".to_string()); |
175 | | - config[":code"][":open-file-at-line"] = Edn::Str("{file} {line}".to_string()); |
| 172 | + open_file: "{file}".to_string(), |
| 173 | + open_file_at_line: "{file} {line}".to_string(), |
| 174 | + }; |
176 | 175 |
|
177 | | - let config_str = &Edn::to_string(&config); |
| 176 | + let url = format!("http://localhost:{port}/prefs/code"); |
178 | 177 |
|
179 | | - fs::write(path, config_str)?; |
| 178 | + reqwest::blocking::Client::new() |
| 179 | + .post(url) |
| 180 | + .json(&config) |
| 181 | + .send()?; |
180 | 182 |
|
181 | 183 | Ok(()) |
182 | 184 | } |
0 commit comments