fix(gen-features): keep data files order

This commit is contained in:
Côme ALLART 2021-09-11 20:04:57 +02:00
parent addee8778d
commit 99ccf18160
4 changed files with 1970 additions and 1981 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
//! FIXME discuss about which errors to print and when to panic //! FIXME discuss about which errors to print and when to panic
use std::{collections::HashMap, iter::FilterMap, path::Path, slice::Iter}; use std::{iter::FilterMap, path::Path, slice::Iter};
const SUPPORTED_FAMILIES: [&str; 8] = [ const SUPPORTED_FAMILIES: [&str; 8] = [
"stm32f0", "stm32f0",
@ -89,21 +89,19 @@ fn chip_cores(path: &Path) -> Vec<String> {
/// ///
/// # Panic /// # Panic
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
pub fn embassy_stm32_needed_data( pub fn embassy_stm32_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
names_and_cores: &[(String, Vec<String>)], let mut result = String::new();
) -> HashMap<String, Vec<String>> {
let mut result = HashMap::new();
for (chip_name, cores) in names_and_cores.supported() { for (chip_name, cores) in names_and_cores.supported() {
if cores.len() > 1 { if cores.len() > 1 {
for core_name in cores.iter() { for core_name in cores.iter() {
let key = format!("{}_{}", chip_name, core_name); result += &format!(
let value = vec![format!("stm32-metapac/{}_{}", chip_name, core_name)]; "{chip}_{core} = [ \"stm32-metapac/{chip}_{core}\" ]\n",
result.insert(key, value); chip = chip_name,
core = core_name
);
} }
} else { } else {
let key = chip_name.to_string(); result += &format!("{chip} = [ \"stm32-metapac/{chip}\" ]\n", chip = chip_name);
let value = vec![format!("stm32-metapac/{}", chip_name)];
result.insert(key, value);
} }
} }
result result
@ -116,18 +114,15 @@ pub fn embassy_stm32_needed_data(
/// ///
/// # Panic /// # Panic
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
pub fn stm32_metapac_needed_data( pub fn stm32_metapac_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
names_and_cores: &[(String, Vec<String>)], let mut result = String::new();
) -> HashMap<String, Vec<String>> {
let mut result = HashMap::new();
for (chip_name, cores) in names_and_cores { for (chip_name, cores) in names_and_cores {
if cores.len() > 1 { if cores.len() > 1 {
for core_name in cores { for core_name in cores {
let key = format!("{}_{}", chip_name, core_name); result += &format!("{}_{} = []\n", chip_name, core_name);
result.insert(key, vec![]);
} }
} else { } else {
result.insert(chip_name.clone(), vec![]); result += &format!("{} = []\n", chip_name);
} }
} }
result result
@ -152,13 +147,9 @@ fn split_cargo_toml_contents(contents: &str) -> (&str, &str) {
/// ///
/// # Panic /// # Panic
/// Panics when a separator cound not be not found /// Panics when a separator cound not be not found
pub fn generate_cargo_toml_file( pub fn generate_cargo_toml_file(previous_text: &str, new_contents: &str) -> String {
previous_text: &str,
new_contents: &HashMap<String, Vec<String>>,
) -> String {
let (before, after) = split_cargo_toml_contents(previous_text); let (before, after) = split_cargo_toml_contents(previous_text);
let generated_content = toml::to_string(new_contents).unwrap(); before.to_owned() + SEPARATOR_START + HELP + new_contents + SEPARATOR_END + after
before.to_owned() + SEPARATOR_START + HELP + &generated_content + SEPARATOR_END + after
} }
#[cfg(test)] #[cfg(test)]
@ -203,8 +194,8 @@ a = [\"b\"]
after after
"; ";
let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); let new_contents = String::from("a = [\"b\"]\n");
assert_eq!(generate_cargo_toml_file(initial, &map), expected); assert_eq!(generate_cargo_toml_file(initial, &new_contents), expected);
} }
#[test] #[test]
@ -216,7 +207,7 @@ before
after after
"; ";
let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); let new_contents = String::from("a = [\"b\"]\n");
generate_cargo_toml_file(initial, &map); generate_cargo_toml_file(initial, &new_contents);
} }
} }

View File

@ -1,5 +1,3 @@
use std::collections::HashMap;
use gen_features::{ use gen_features::{
chip_names_and_cores, embassy_stm32_needed_data, generate_cargo_toml_file, chip_names_and_cores, embassy_stm32_needed_data, generate_cargo_toml_file,
stm32_metapac_needed_data, stm32_metapac_needed_data,
@ -21,7 +19,7 @@ fn main() {
/// ///
/// Update the content between "# BEGIN GENERATED FEATURES" and "# END GENERATED FEATURES" /// Update the content between "# BEGIN GENERATED FEATURES" and "# END GENERATED FEATURES"
/// with the given content /// with the given content
fn update_cargo_file(path: &str, new_contents: &HashMap<String, Vec<String>>) { fn update_cargo_file(path: &str, new_contents: &str) {
let previous_text = std::fs::read_to_string(path).unwrap(); let previous_text = std::fs::read_to_string(path).unwrap();
let new_text = generate_cargo_toml_file(&previous_text, new_contents); let new_text = generate_cargo_toml_file(&previous_text, new_contents);
std::fs::write(path, new_text).unwrap(); std::fs::write(path, new_text).unwrap();

File diff suppressed because it is too large Load Diff