Last active
July 6, 2023 01:41
-
-
Save sravan-s/096c3d442539b2a9434bfb23c53a5c2c to your computer and use it in GitHub Desktop.
Rust source file to print list of given files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rust source file to print list of given files | |
// In cargo.toml add following dependencies | |
// [dependencies] | |
// walkdir = "2.3.3" | |
// serde = "1.0.166" | |
// serde_yaml = "0.9.22" | |
// Build the project using cargo build | |
// Move the executable to the `uikit/src` folder | |
// For source_files, run ./executable --extentions=js,jsx --exclude-path=__test,stories,mock | |
// For story_files, run ./executable --extentions=stories.js,stories.jsx --exclude-path=__test | |
// For test_files, run ./executable --extentions=spec.js,spec.jsx,mock.js --exclude-path=__stories | |
use std::{collections::BTreeMap, error::Error, fs::{File}, io::BufWriter, env}; | |
use walkdir::WalkDir; | |
const EXTENTIONS_ARG: &str = "--extentions="; | |
const DEFAULT_EXTENTIONS: &str = "js,jsx"; | |
const EXCLUDE_PATH_ARG: &str = "--exclude-path="; | |
const DEFAULT_EXCLUDE_PATH: &str = "__test,stories,mock"; | |
fn is_valid_file(path: &str, extentions: Vec<String>, excluded_paths: Vec<String>) -> bool { | |
let valid_path = extentions.iter().any(|ext| path.ends_with(ext)); | |
if !valid_path { | |
return false; | |
} | |
let invalid_path = excluded_paths.iter().any(|ext| path.contains(ext)); | |
if invalid_path { | |
return false; | |
} | |
return true; | |
} | |
// --extentions=js,jsx | |
// --exclude-path=__test,stories,mock | |
fn main() -> Result<(), Box<dyn Error>> { | |
// Reading arguments | |
let args: Vec<String> = env::args().collect(); | |
let mut extentions: Vec<String> = String::from(DEFAULT_EXTENTIONS).split(",").map(|x| x.to_string()).collect(); | |
let mut excluded_paths: Vec<String> = String::from(DEFAULT_EXCLUDE_PATH).split(",").map(|x| x.to_string()).collect(); | |
for arg in args.clone() { | |
if arg.starts_with(EXTENTIONS_ARG) { | |
extentions = arg | |
.split(EXTENTIONS_ARG) | |
.collect::<Vec<&str>>() | |
.get(1) | |
.expect("No arguments provided for --extentions") | |
.split(",") | |
.map(|x| x.to_string()) | |
.collect(); | |
} | |
if arg.starts_with(EXCLUDE_PATH_ARG) { | |
excluded_paths = arg | |
.split(EXCLUDE_PATH_ARG) | |
.collect::<Vec<&str>>() | |
.get(1) | |
.expect("No arguments provided for --extentions") | |
.split(",") | |
.map(|x| x.to_string()) | |
.collect(); | |
} | |
} | |
for e in extentions.clone() { | |
println!("extentions to look for: {}", e); | |
} | |
for e in excluded_paths.clone() { | |
println!("path to exlude: {}", e); | |
} | |
let mut file_name_map = BTreeMap::new(); | |
// Walking directory | |
let files: Vec<_> = WalkDir::new(".") | |
.follow_links(true) | |
.into_iter() | |
.filter_map(|e| e.ok()) | |
.filter(|entry| { | |
let f_name = entry.file_name().to_string_lossy(); | |
is_valid_file(&f_name, extentions.clone(), excluded_paths.clone()) | |
}).collect(); | |
// Adding files to BTreeMap | |
for file in files { | |
let f_name = file.file_name().to_string_lossy().into_owned(); | |
let f_path = file.path().to_string_lossy().into_owned(); | |
file_name_map.insert(f_path, f_name); | |
} | |
// Writing files to list.yml | |
let yaml = serde_yaml::to_string(&file_name_map)?; | |
let file = File::create("list.yml")?; | |
let writer = BufWriter::new(file); | |
serde_yaml::to_writer(writer, &yaml)?; | |
println!("Finished writing to file list.yml"); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment