This commit is contained in:
Djkáťo 2024-04-17 21:48:50 +02:00
parent efe3ef6b59
commit f9f26edb83
2 changed files with 166 additions and 123 deletions

View file

@ -47,7 +47,7 @@ for i in ${redis_apl_target_paths[*]}; do
echo "copying redis_apl.ts to ./all_apps/$i" echo "copying redis_apl.ts to ./all_apps/$i"
cp -f "$REDIS_APL_PATH" "./all_apps/$i" cp -f "$REDIS_APL_PATH" "./all_apps/$i"
# always copies next to saleor-app.ts, so let's add some files to that file too # always copies next to saleor-app.ts, so let's add some files to that file too
rg -l "switch \(process.env.APL\)" -t ts find . -name "saleor-app.ts" -exec sed "/switch/ r $CURR_PWD/changes/case_redisapl.ts" {} \;
done done
for i in ${app_paths[*]}; do for i in ${app_paths[*]}; do

View file

@ -1,134 +1,177 @@
use std::{ use std::{
io, fs::{self, File},
path::{Path, PathBuf}, io::{self, BufRead, BufReader},
process::Command, path::PathBuf,
str::FromStr,
}; };
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
fn main() -> Result<(), io::Error> { fn main() -> Result<(), io::Error> {
let snippets = get_snippets().unwrap(); let args: Vec<_> = std::env::args().collect();
let actions = get_actions().unwrap(); if args.len() < 2 {
let changes = get_changes() std::process::exit(1)
.unwrap() }
.into_iter()
.map(|c| Box::new(c))
.collect::<Vec<_>>();
for change in changes { let file = File::open(&args[1])?;
let mut current_cumulative_path = std::env::current_dir()?; // let contents = fs::read_to_string(&file)?;
current_cumulative_path.push("all_apps"); let reader = BufReader::new(file);
current_cumulative_path.push(&change.folder);
process_change(&change, &current_cumulative_path); let mut first_half = "".to_owned();
if let Some(sub) = change.sub { let mut second_half = "".to_owned();
iter_sub(sub) let mut found_switch = false;
for line in reader.lines() {
let Ok(line) = line else { continue };
match found_switch {
true => first_half.push_str(&line),
false => second_half.push_str(&line),
}
if line.contains("switch") {
found_switch = true;
} }
} }
first_half.push_str(&fs::read_to_string("./changes/snippets/case_redisapl.ts")?);
first_half = r#"import { RedisAPL } from "./redis_apl"
"#
.to_owned()
+ &first_half;
let final_content = first_half + &second_half;
// fs::write(file, final_content)?;
println!("{}", final_content);
Ok(()) Ok(())
} }
pub fn process_change(
change: &Box<Change>,
current_cumulative_path: &PathBuf,
) -> Result<(), io::Error> {
if let Some(actions) = &change.actions {
for action in actions {
let mut current_file = current_cumulative_path.clone();
current_file.push(&action.target);
let command;
match action.action_type {
ActionType::Create => Command::new("ln").args(["-s", current_file]),
ActionType::Insert => {}
}
}
}
Ok(())
}
pub fn iter_sub(sub: Vec<Box<Change>>) { // use std::{
for change in sub { // io,
process_change(&change); // path::{Path, PathBuf},
if let Some(further_sub) = change.sub { // process::Command,
iter_sub(further_sub) // str::FromStr,
} // };
} //
} // use serde::{Deserialize, Serialize};
// use walkdir::WalkDir;
pub fn get_changes() -> Result<Vec<Change>, std::io::Error> { //
Ok(serde_json::from_str(&std::fs::read_to_string( // fn main() -> Result<(), io::Error> {
"./changes/changes.json", // let snippets = get_snippets().unwrap();
)?)?) // let actions = get_actions().unwrap();
} // let changes = get_changes()
// .unwrap()
pub fn get_snippets() -> Result<Vec<Snippet>, std::io::Error> { // .into_iter()
let mut snippets = vec![]; // .map(|c| Box::new(c))
for entry in WalkDir::new("./changes/snippets").into_iter().flatten() { // .collect::<Vec<_>>();
if entry.path().is_file() { //
if let Some(name) = entry.path().file_stem() { // for change in changes {
if let Some(name) = name.to_str() { // let mut current_cumulative_path = std::env::current_dir()?;
snippets.push(Snippet { // current_cumulative_path.push("all_apps");
id: SnippetId(name.to_owned()), // current_cumulative_path.push(&change.folder);
value: std::fs::read_to_string(entry.path())?, //
}) // process_change(&change, &current_cumulative_path);
} // if let Some(sub) = change.sub {
}; // iter_sub(sub)
} // }
} // }
Ok(snippets) //
} // Ok(())
// }
pub fn get_actions() -> Result<Vec<NamedActions>, std::io::Error> { // pub fn process_change(
Ok(serde_json::from_str(&std::fs::read_to_string( // change: &Box<Change>,
"./changes/actions.json", // current_cumulative_path: &PathBuf,
)?)?) // ) -> Result<(), io::Error> {
} // if let Some(actions) = &change.actions {
// for action in actions {
#[derive(Serialize, Deserialize, Debug)] // let mut current_file = current_cumulative_path.clone();
pub struct Change { // current_file.push(&action.target);
pub folder: String, // let command;
pub sub: Option<Vec<Box<Self>>>, // match action.action_type {
pub actions_by_ref: Option<Vec<ActionsId>>, // ActionType::Create => Command::new("ln").args(["-s", current_file]),
pub actions: Option<Vec<Action>>, // ActionType::Insert => {}
} // }
// }
#[derive(Serialize, Deserialize, Debug)] // }
pub struct NamedActions { // Ok(())
pub name: ActionsId, // }
pub actions: Vec<Action>, //
} // pub fn iter_sub(sub: Vec<Box<Change>>) {
// for change in sub {
#[derive(Serialize, Deserialize, Debug)] // process_change(&change);
pub struct Action { // if let Some(further_sub) = change.sub {
pub target: String, // iter_sub(further_sub)
pub action_type: ActionType, // }
pub snippets: Vec<ActionStep>, // }
} // }
//
#[derive(Serialize, Deserialize, Debug)] // pub fn get_changes() -> Result<Vec<Change>, std::io::Error> {
pub struct ActionStep { // Ok(serde_json::from_str(&std::fs::read_to_string(
pub after: Option<String>, // "./changes/changes.json",
pub snippet: SnippetId, // )?)?)
} // }
//
#[derive(Serialize, Deserialize, Debug)] // pub fn get_snippets() -> Result<Vec<Snippet>, std::io::Error> {
pub struct Snippet { // let mut snippets = vec![];
id: SnippetId, // for entry in WalkDir::new("./changes/snippets").into_iter().flatten() {
value: String, // if entry.path().is_file() {
} // if let Some(name) = entry.path().file_stem() {
// if let Some(name) = name.to_str() {
#[derive(Serialize, Deserialize, Debug)] // snippets.push(Snippet {
pub enum ActionType { // id: SnippetId(name.to_owned()),
Insert, // value: std::fs::read_to_string(entry.path())?,
Create, // })
} // }
// };
#[derive(Serialize, Deserialize, Debug, PartialEq)] // }
#[serde(transparent)] // }
pub struct SnippetId(pub String); // Ok(snippets)
// }
#[derive(Serialize, Deserialize, Debug, PartialEq)] //
#[serde(transparent)] // pub fn get_actions() -> Result<Vec<NamedActions>, std::io::Error> {
pub struct ActionsId(pub String); // Ok(serde_json::from_str(&std::fs::read_to_string(
// "./changes/actions.json",
// )?)?)
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub struct Change {
// pub folder: String,
// pub sub: Option<Vec<Box<Self>>>,
// pub actions_by_ref: Option<Vec<ActionsId>>,
// pub actions: Option<Vec<Action>>,
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub struct NamedActions {
// pub name: ActionsId,
// pub actions: Vec<Action>,
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub struct Action {
// pub target: String,
// pub action_type: ActionType,
// pub snippets: Vec<ActionStep>,
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub struct ActionStep {
// pub after: Option<String>,
// pub snippet: SnippetId,
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub struct Snippet {
// id: SnippetId,
// value: String,
// }
//
// #[derive(Serialize, Deserialize, Debug)]
// pub enum ActionType {
// Insert,
// Create,
// }
//
// #[derive(Serialize, Deserialize, Debug, PartialEq)]
// #[serde(transparent)]
// pub struct SnippetId(pub String);
//
// #[derive(Serialize, Deserialize, Debug, PartialEq)]
// #[serde(transparent)]
// pub struct ActionsId(pub String);