build_helper/util.rs
1use std::fs::File;
2use std::io::{BufRead, BufReader};
3use std::path::Path;
4
5/// Returns the submodule paths from the `.gitmodules` file in the given directory.
6pub fn parse_gitmodules(target_dir: &Path) -> Vec<String> {
7 let gitmodules = target_dir.join(".gitmodules");
8 assert!(gitmodules.exists(), "'{}' file is missing.", gitmodules.display());
9
10 let file = File::open(gitmodules).unwrap();
11
12 let mut submodules_paths = vec![];
13 for line in BufReader::new(file).lines().map_while(Result::ok) {
14 let line = line.trim();
15 if line.starts_with("path") {
16 let actual_path = line.split(' ').next_back().expect("Couldn't get value of path");
17 submodules_paths.push(actual_path.to_owned());
18 }
19 }
20
21 submodules_paths
22}