diff --git a/Cargo.toml b/Cargo.toml index b75ea89..e02bed8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ clap = { version = "4.4.11", features = ["derive"] } indicatif = "0.17.7" open = "5.0.1" probe-rs = "0.24.0" +serde_json = "1.0.122" termimad = "0.29.1" diff --git a/src/cli/init_args.rs b/src/cli/init_args.rs index ebd4e79..44ddd5e 100644 --- a/src/cli/init_args.rs +++ b/src/cli/init_args.rs @@ -18,4 +18,11 @@ pub struct InitArgs { #[arg(long, help = "Configure for use with a Softdevice (NRF only).")] pub softdevice: Option, + + #[arg( + long, + help = "Generate config files for vscode.", + default_value_t = false + )] + pub vscode: bool, } diff --git a/src/init.rs b/src/init.rs index 2dc463e..02dd180 100644 --- a/src/init.rs +++ b/src/init.rs @@ -17,6 +17,8 @@ use std::{ time::Duration, }; +use serde_json::Value; + pub struct Init { pb: ProgressBar, } @@ -53,6 +55,9 @@ impl Init { self.create_project(&args.name)?; self.init_config(&chip, &probe_target_name)?; + if args.vscode { + self.init_debug_config(&chip, &probe_target_name, &args.name)?; + } self.init_toolchain(&chip)?; if !matches!(&chip.family, Family::ESP(_)) { self.init_embed(&probe_target_name)?; @@ -103,6 +108,26 @@ impl Init { } } + fn init_debug_config(&self, chip: &Chip, name: &str, project_name: &str) -> Result<(), Error> { + fs::create_dir_all(".vscode").map_err(|_| Error::CreateFolder(".vscode".into()))?; + + let contents = include_str!("templates/launch.json.template").to_string(); + let mut contents = + serde_json::from_str::(&contents).expect("failed to convert to json"); + + // update chip name + contents["configurations"][0]["chip"] = Value::String(name.to_string()); + + // update target binary name + let target = format!("target/{}/debug/{}", chip.target, project_name); + contents["configurations"][0]["coreConfigs"][0]["programBinary"] = Value::String(target); + + self.create_file( + ".vscode/launch.json", + serde_json::to_string_pretty(&contents).unwrap().as_str(), + ) + } + fn init_config(&self, chip: &Chip, name: &str) -> Result<(), Error> { fs::create_dir_all(".cargo").map_err(|_| Error::CreateFolder(".cargo".into()))?; diff --git a/src/templates/launch.json.template b/src/templates/launch.json.template new file mode 100644 index 0000000..500deed --- /dev/null +++ b/src/templates/launch.json.template @@ -0,0 +1,44 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "probe-rs-debug", + "request": "launch", + "name": "probe_rs", + "cwd": "${workspaceFolder}", + "runtimeExecutable": "probe-rs", + "runtimeArgs": ["dap-server"], + "chip": "chip_name", + "flashingConfig": { + "flashingEnabled": true, + "haltAfterReset": false, + "formatOptions": { + } + }, + "coreConfigs": [ + { + "coreIndex": 0, + "rttEnabled": true, + "rttChannelFormats": [ + { + "channelNumber": 0, + "dataFormat": "String", + "showTimestamps": true + }, + { + "channelNumber": 1, + "dataFormat": "BinaryLE" + } + ], + "programBinary": "path_to_your_binary", + "svdFile": "" + } + ], + "env": { + "RUST_LOG": "info" + }, + "consoleLogLevel": "Console" + } + ] +} +