Skip to content

Commit 3087db5

Browse files
committed
control vscode settings.json from init
1 parent 10b1256 commit 3087db5

File tree

5 files changed

+60
-63
lines changed

5 files changed

+60
-63
lines changed

.vscode/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
settings.json

.vscode/settings.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

crates/xtask/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"
2222
glob = "0.3"
2323
toml = "0.8"
24+
tera = "1.20"
25+
json_comments = "0.2"

crates/xtask/src/main.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use anyhow::*;
1+
use anyhow::{Context, *};
22
use clap::Parser;
33
use itertools::Itertools;
4+
use json_comments::StripComments;
45
use log::*;
5-
use serde::Serialize;
6+
use serde::{Deserialize, Serialize};
67
use std::{
78
collections::{HashMap, HashSet},
89
ffi::{OsStr, OsString},
@@ -1357,8 +1358,59 @@ impl Xtasks {
13571358
Some(Path::new(".")),
13581359
)?;
13591360

1361+
// create .vscode settings
1362+
// read from templates at compile time
1363+
let vscode_settings = include_str!("../templates/settings.json.tera");
1364+
let mut tera = tera::Tera::default();
1365+
let mut context = tera::Context::new();
1366+
context.insert("dir", &Self::workspace_dir(&app_settings)?);
1367+
let templated_settings = tera.render_str(vscode_settings, &context)?;
1368+
let templated_settings_json = Self::read_json_with_comments(templated_settings.as_bytes())
1369+
.with_context(|| "reading templated vscode settings")?;
1370+
let vscode_dir = Self::relative_workspace_dir(&app_settings, ".vscode")?;
1371+
std::fs::create_dir_all(&vscode_dir)?;
1372+
let vscode_settings_path = vscode_dir.join("settings.json");
1373+
1374+
// if the file already exists, merge the settings otherwise create it
1375+
info!(
1376+
"Merging vscode settings at {:?}. With overrides generated by template.",
1377+
vscode_settings_path
1378+
);
1379+
if vscode_settings_path.exists() {
1380+
let existing_settings = std::fs::read_to_string(&vscode_settings_path)?;
1381+
let mut existing_settings = Self::read_json_with_comments(existing_settings.as_bytes())
1382+
.with_context(|| "reading existing vscode settings file")?;
1383+
Self::merge_json(templated_settings_json, &mut existing_settings);
1384+
let merged_settings = serde_json::to_string_pretty(&existing_settings)?;
1385+
std::fs::write(&vscode_settings_path, merged_settings)?;
1386+
} else {
1387+
std::fs::write(&vscode_settings_path, templated_settings)?;
1388+
}
1389+
13601390
Ok(())
13611391
}
1392+
1393+
fn read_json_with_comments(bytes: &[u8]) -> Result<serde_json::Value> {
1394+
let stripped = StripComments::new(bytes);
1395+
let mut reader = serde_json::Deserializer::from_reader(stripped);
1396+
let value = serde_json::Value::deserialize(&mut reader)?;
1397+
Ok(value)
1398+
}
1399+
1400+
/// Override the target json file with some overrides. Will replace values if they already exist, or insert them otherwise.
1401+
fn merge_json(overrides: serde_json::Value, target: &mut serde_json::Value) {
1402+
if let (serde_json::Value::Object(overrides), serde_json::Value::Object(target)) =
1403+
(overrides, target)
1404+
{
1405+
for (key, value) in overrides {
1406+
// simply replace
1407+
info!("Replacing json key: {} with value: {}", key, value);
1408+
target.insert(key.clone(), value.clone());
1409+
}
1410+
} else {
1411+
warn!("Could not merge json, overrides and target are not objects");
1412+
}
1413+
}
13621414
}
13631415

13641416
/// Because we are likely already runnnig in the context of a cargo invocation,
@@ -1400,7 +1452,7 @@ fn try_main() -> Result<()> {
14001452

14011453
fn main() {
14021454
if let Err(e) = try_main() {
1403-
eprintln!("{}", e);
1455+
eprintln!("{:?}", e);
14041456
std::process::exit(1);
14051457
}
14061458
}
Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
{
2-
"lldb.displayFormat": "auto",
3-
"lldb.showDisassembly": "never",
4-
"lldb.dereferencePointers": true,
5-
"lldb.consoleMode": "commands",
6-
"[rust]": {
7-
"editor.formatOnSave": true,
8-
"editor.formatOnSaveMode": "file",
9-
"editor.defaultFormatter": "rust-lang.rust-analyzer"
10-
},
112
"rust-analyzer.rustc.source": "discover",
123
"rust-analyzer.linkedProjects": [
13-
"./crates/bevy_api_gen/Cargo.toml",
4+
"{{ dir }}/crates/bevy_api_gen/Cargo.toml",
145
"Cargo.toml"
156
],
167
"rust-analyzer.check.invocationStrategy": "per_workspace",
17-
"rust-analyzer.check.invocationLocation": "workspace",
188
"rust-analyzer.check.overrideCommand": [
19-
"cargo xtask check"
20-
],
21-
"rust-analyzer.cargo.buildScripts.overrideCommand": [
22-
"cargo xtask check"
23-
],
24-
"rust-analyzer.showUnlinkedFileNotification": false,
25-
"rust-analyzer.runnables.extraTestBinaryArgs": [
26-
"--show-output"
27-
],
28-
"rust-analyzer.runnables.extraArgs": [
29-
"--profile=release-with-debug"
9+
"{{ dir }}/check.sh"
3010
]
3111
}

0 commit comments

Comments
 (0)