Skip to content

Commit

Permalink
generate launch.json for vscode debug (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
listentodella authored Aug 5, 2024
1 parent 515a611 commit cacdd4e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 7 additions & 0 deletions src/cli/init_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ pub struct InitArgs {

#[arg(long, help = "Configure for use with a Softdevice (NRF only).")]
pub softdevice: Option<Softdevice>,

#[arg(
long,
help = "Generate config files for vscode.",
default_value_t = false
)]
pub vscode: bool,
}
25 changes: 25 additions & 0 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::{
time::Duration,
};

use serde_json::Value;

pub struct Init {
pb: ProgressBar,
}
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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::<Value>(&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()))?;

Expand Down
44 changes: 44 additions & 0 deletions src/templates/launch.json.template
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

0 comments on commit cacdd4e

Please sign in to comment.