Skip to content

Commit

Permalink
Add bin section (#62)
Browse files Browse the repository at this point in the history
* build: Add bin section

* docs: Update changelog

* fix: Update path for embassy main

* feat: Allow replacing 2 variables in the next line

* fix: Update diagram.json
  • Loading branch information
SergioGasquez authored Nov 28, 2024
1 parent 4c1f792 commit 252262e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Update the resulting binary name (#62)

### Fixed

Expand Down
60 changes: 45 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ static OPTIONS: &[GeneratorOptionItem] = &[
display_name: "Adds support for Wokwi simulation using VS Code Wokwi extension.",
enables: &[],
disables: &[],
chips: &[],
chips: &[
Chip::Esp32,
Chip::Esp32c3,
Chip::Esp32c6,
Chip::Esp32h2,
Chip::Esp32s2,
Chip::Esp32s3,
],
}),
GeneratorOptionItem::Option(GeneratorOption {
name: "dev-container",
Expand Down Expand Up @@ -267,9 +274,20 @@ fn main() -> Result<(), Box<dyn Error>> {
"xtensa".to_string()
});

let wokwi_devkit = match args.chip {
Chip::Esp32 => "board-esp32-devkit-c-v4",
Chip::Esp32c2 => "",
Chip::Esp32c3 => "board-esp32-c3-devkitm-1",
Chip::Esp32c6 => "board-esp32-c6-devkitc-1",
Chip::Esp32h2 => "board-esp32-h2-devkitm-1",
Chip::Esp32s2 => "board-esp32-s2-devkitm-1",
Chip::Esp32s3 => "board-esp32-s3-devkitc-1",
};

let mut variables = vec![
("project-name".to_string(), args.name.clone()),
("mcu".to_string(), args.chip.to_string()),
("wokwi-board".to_string(), wokwi_devkit.to_string()),
];

variables.push(("rust_target".to_string(), args.chip.target().to_string()));
Expand Down Expand Up @@ -319,8 +337,7 @@ fn process_file(
) -> Option<String> {
let mut res = String::new();

let mut replace = None;
let mut replacement = None;
let mut replace: Option<Vec<(String, String)>> = None;
let mut include = vec![true];
let mut first_line = true;

Expand Down Expand Up @@ -373,13 +390,25 @@ fn process_file(
.strip_prefix("#REPLACE ")
.or_else(|| trimmed.strip_prefix("//REPLACE "))
{
let mut split = what.split_terminator(" ");
replace = Some(split.next().unwrap().to_string());
let var = split.next().unwrap().to_string();

// Find the replacement value from the variables map
if let Some((_, value)) = variables.iter().find(|(key, _)| key == &var) {
replacement = Some(value);
let replacements = what
.split(" && ")
.filter_map(|pair| {
let mut parts = pair.split_whitespace();
if let (Some(pattern), Some(var_name)) = (parts.next(), parts.next()) {
if let Some((_, value)) = variables.iter().find(|(key, _)| key == var_name)
{
Some((pattern.to_string(), value.clone()))
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>();

if !replacements.is_empty() {
replace = Some(replacements);
}
// Check if we should include the next line(s)
} else if trimmed.starts_with("#IF ") || trimmed.starts_with("//IF ") {
Expand All @@ -401,22 +430,23 @@ fn process_file(
let mut line = line.to_string();

if trimmed.starts_with("#+") {
line = line.replace("#+", "").to_string();
line = line.replace("#+", "");
}

if trimmed.starts_with("//+") {
line = line.replace("//+", "").to_string();
line = line.replace("//+", "");
}

if let (Some(replace), Some(replacement)) = (replace, replacement) {
line = line.replace(&replace, replacement);
if let Some(replacements) = &replace {
for (pattern, value) in replacements {
line = line.replace(pattern, value);
}
}

res.push_str(&line);
res.push('\n');

replace = None;
replacement = None;
}
}

Expand Down
9 changes: 9 additions & 0 deletions template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ name = "project-name"
version = "0.1.0"
edition = "2021"

[[bin]]
#REPLACE project-name project-name
name = "project-name"
#IF !option("embassy")
path = "./src/bin/main.rs"
#ELSE
#+path = "./src/bin/async_main.rs"
#ENDIF

[dependencies]
esp-backtrace = { version = "0.14.2", features = [
#REPLACE esp32c6 mcu
Expand Down
4 changes: 2 additions & 2 deletions template/diagram.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{
"version": 1,
"editor": "wokwi",
"author": "{{authors}}",
"parts": [
{
"type": "{{ wokwi_board }}",
//REPLACE wokwi-board wokwi-board
"type": "wokwi-board",
"id": "esp",
"top": 0.59,
"left": 0.67,
Expand Down
6 changes: 4 additions & 2 deletions template/wokwi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
[wokwi]
version = 1
gdbServerPort = 3333
elf = "target/{{ rust_target }}/debug/{{ project-name }}"
firmware = "target/{{ rust_target }}/debug/{{ project-name }}"
#REPLACE project-name project-name && rust_target rust_target
elf = "target/rust_target/debug/project-name"
#REPLACE project-name project-name && rust_target rust_target
firmware = "target/rust_target/debug/project-name"
20 changes: 10 additions & 10 deletions xtask/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 252262e

Please sign in to comment.