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"
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
rg -l "switch \(process.env.APL\)" -t ts
find . -name "saleor-app.ts" -exec sed "/switch/ r $CURR_PWD/changes/case_redisapl.ts" {} \;
done
for i in ${app_paths[*]}; do

View file

@ -1,134 +1,177 @@
use std::{
io,
path::{Path, PathBuf},
process::Command,
str::FromStr,
fs::{self, File},
io::{self, BufRead, BufReader},
path::PathBuf,
};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
fn main() -> Result<(), io::Error> {
let snippets = get_snippets().unwrap();
let actions = get_actions().unwrap();
let changes = get_changes()
.unwrap()
.into_iter()
.map(|c| Box::new(c))
.collect::<Vec<_>>();
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 {
std::process::exit(1)
}
for change in changes {
let mut current_cumulative_path = std::env::current_dir()?;
current_cumulative_path.push("all_apps");
current_cumulative_path.push(&change.folder);
let file = File::open(&args[1])?;
// let contents = fs::read_to_string(&file)?;
let reader = BufReader::new(file);
process_change(&change, &current_cumulative_path);
if let Some(sub) = change.sub {
iter_sub(sub)
let mut first_half = "".to_owned();
let mut second_half = "".to_owned();
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(())
}
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>>) {
for change in sub {
process_change(&change);
if let Some(further_sub) = change.sub {
iter_sub(further_sub)
}
}
}
pub fn get_changes() -> Result<Vec<Change>, std::io::Error> {
Ok(serde_json::from_str(&std::fs::read_to_string(
"./changes/changes.json",
)?)?)
}
pub fn get_snippets() -> Result<Vec<Snippet>, std::io::Error> {
let mut snippets = vec![];
for entry in WalkDir::new("./changes/snippets").into_iter().flatten() {
if entry.path().is_file() {
if let Some(name) = entry.path().file_stem() {
if let Some(name) = name.to_str() {
snippets.push(Snippet {
id: SnippetId(name.to_owned()),
value: std::fs::read_to_string(entry.path())?,
})
}
};
}
}
Ok(snippets)
}
pub fn get_actions() -> Result<Vec<NamedActions>, std::io::Error> {
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);
// use std::{
// io,
// path::{Path, PathBuf},
// process::Command,
// str::FromStr,
// };
//
// use serde::{Deserialize, Serialize};
// use walkdir::WalkDir;
//
// fn main() -> Result<(), io::Error> {
// let snippets = get_snippets().unwrap();
// let actions = get_actions().unwrap();
// let changes = get_changes()
// .unwrap()
// .into_iter()
// .map(|c| Box::new(c))
// .collect::<Vec<_>>();
//
// for change in changes {
// let mut current_cumulative_path = std::env::current_dir()?;
// current_cumulative_path.push("all_apps");
// current_cumulative_path.push(&change.folder);
//
// process_change(&change, &current_cumulative_path);
// if let Some(sub) = change.sub {
// iter_sub(sub)
// }
// }
//
// 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>>) {
// for change in sub {
// process_change(&change);
// if let Some(further_sub) = change.sub {
// iter_sub(further_sub)
// }
// }
// }
//
// pub fn get_changes() -> Result<Vec<Change>, std::io::Error> {
// Ok(serde_json::from_str(&std::fs::read_to_string(
// "./changes/changes.json",
// )?)?)
// }
//
// pub fn get_snippets() -> Result<Vec<Snippet>, std::io::Error> {
// let mut snippets = vec![];
// for entry in WalkDir::new("./changes/snippets").into_iter().flatten() {
// if entry.path().is_file() {
// if let Some(name) = entry.path().file_stem() {
// if let Some(name) = name.to_str() {
// snippets.push(Snippet {
// id: SnippetId(name.to_owned()),
// value: std::fs::read_to_string(entry.path())?,
// })
// }
// };
// }
// }
// Ok(snippets)
// }
//
// pub fn get_actions() -> Result<Vec<NamedActions>, std::io::Error> {
// 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);