Skip to content

Commit

Permalink
refactor & clean main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rjodinchr committed Dec 3, 2024
1 parent 3edcdd6 commit c14ac94
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,85 +42,81 @@ fn generate_project(

fn generate_projects<'a>(
all_projects: Vec<&'a mut dyn Project>,
mut project_ids_to_write: VecDeque<ProjectId>,
project_ids: Vec<ProjectId>,
) -> Result<(), String> {
let write_all = project_ids_to_write.len() == 0;
let mut projects_not_generated: HashMap<ProjectId, &mut dyn Project> = HashMap::new();
let mut project_ids_to_generate: VecDeque<ProjectId> = VecDeque::new();
for project in all_projects {
if write_all {
project_ids_to_write.push_back(project.get_id());
if project_ids.len() == 0 {
project_ids_to_generate.push_back(project.get_id());
}
projects_not_generated.insert(project.get_id(), project);
}
let mut project_ids_to_generate = project_ids_to_write.clone();
project_ids_to_generate.extend(project_ids);
let project_ids_to_write = project_ids_to_generate.clone();

let mut projects_generated: ProjectsMap = HashMap::new();
while let Some(project_id) = project_ids_to_generate.pop_front() {
if projects_generated.contains_key(&project_id) {
while let Some(project_id) = project_ids_to_generate.pop_front().as_ref() {
if projects_generated.contains_key(project_id) {
continue;
}
let project = projects_not_generated.remove(&project_id).unwrap();
let mut missing_deps: Vec<ProjectId> = Vec::new();
for dep in project.get_project_deps() {
if !projects_generated.contains_key(&dep) {
missing_deps.push(dep);
}
}
let project = projects_not_generated.remove(project_id).unwrap();
let missing_deps: Vec<ProjectId> = project
.get_project_deps()
.into_iter()
.filter(|dep| !projects_generated.contains_key(&dep))
.collect();
if missing_deps.len() > 0 {
let mut deps: Vec<&str> = Vec::new();
project_ids_to_generate.push_front(project_id.clone());
projects_not_generated.insert(project_id, project);
for dep in missing_deps {
deps.push(dep.str());
project_ids_to_generate.push_front(dep);
}
project_ids_to_generate.extend(missing_deps);
project_ids_to_generate.push_back(project.get_id());
projects_not_generated.insert(project.get_id(), project);
continue;
}
generate_project(
project,
!project_ids_to_write.contains(&project_id),
!project_ids_to_write.contains(project_id),
&projects_generated,
)?;
projects_generated.insert(project_id, project);
projects_generated.insert(project.get_id(), project);
}

Ok(())
}

fn parse_args<'a>(
fn parse_args(
executable: &str,
args: &'a Vec<String>,
) -> Result<(&'a str, &'a str, VecDeque<ProjectId>), String> {
args: Vec<String>,
) -> Result<(String, String, Vec<ProjectId>), String> {
let required_args = 3;
if args.len() < required_args {
return error!(format!(
"USAGE: {executable} <android_dir> <{ANDROID_NDK}_dir> [<projects>]"
));
}
let android_dir = &args[1];
let ndk_dir = &args[2];
let android_dir = args[1].clone();
let ndk_dir = args[2].clone();

let ndk_name = ndk_dir.rsplit_once("/").unwrap().1;
if ndk_name != ANDROID_NDK {
print_warn!(format!("Expected '{ANDROID_NDK}' got '{ndk_name}'"));
}

let mut project_ids_to_write: VecDeque<ProjectId> = VecDeque::new();
let mut project_ids: Vec<ProjectId> = Vec::new();
for arg in &args[required_args..] {
match ProjectId::from(arg) {
Some(project_id) => project_ids_to_write.push_back(project_id),
Some(project_id) => project_ids.push(project_id),
None => return error!(format!("Unknown project '{arg}'")),
};
}
Ok((android_dir, ndk_dir, project_ids_to_write))
Ok((android_dir, ndk_dir, project_ids))
}

fn main() -> Result<(), String> {
let args: Vec<String> = std::env::args().collect();
let executable = args[0].rsplit_once("/").unwrap().1;
let (android_dir, ndk_dir, project_ids_to_write) = parse_args(executable, &args)?;
let executable = args[0].clone().rsplit_once("/").unwrap().1.to_string();
let (android_dir, ndk_dir, project_ids) = parse_args(&executable, args)?;

let temp_path = std::env::temp_dir().join(executable);
let temp_path = std::env::temp_dir().join(&executable);
let temp_dir = temp_path.to_str().unwrap();

let mut clvk = clvk::Clvk::default();
Expand All @@ -137,10 +133,10 @@ fn main() -> Result<(), String> {
&mut spirv_headers,
];
for project in all_projects.iter_mut() {
project.init(android_dir, ndk_dir, temp_dir);
project.init(&android_dir, &ndk_dir, temp_dir);
}

if let Err(err) = generate_projects(all_projects, project_ids_to_write) {
if let Err(err) = generate_projects(all_projects, project_ids) {
print_error!(err);
Err(format!("{executable} failed"))
} else {
Expand Down

0 comments on commit c14ac94

Please sign in to comment.