Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/directory_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
- name: Update DIRECTORY.md
run: |
python .github/workflows/scripts/build_directory_md.py
cargo run --manifest-path=.github/workflows/scripts/build_directory/Cargo.toml
- name: Commit DIRECTORY.md
run: |
git add DIRECTORY.md
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/scripts/build_directory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "build_directory"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
124 changes: 124 additions & 0 deletions .github/workflows/scripts/build_directory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::{
error::Error,
fs,
path::{Path, PathBuf},
};

static URL_BASE: &str = "https://github.com/TheAlgorithms/Rust/blob/master";

fn good_filepaths(top_dir: &Path) -> Result<Vec<String>, Box<dyn Error>> {
let mut good_fs = Vec::new();
if top_dir.is_dir() {
for entry in fs::read_dir(top_dir)? {
let entry = entry?;
let path = entry.path();
if entry.file_name().to_str().unwrap().starts_with('.')
|| entry.file_name().to_str().unwrap().starts_with('_')
{
continue;
}
if path.is_dir() {
let mut other = good_filepaths(&path)?;
good_fs.append(&mut other);
} else if entry.file_name().to_str().unwrap().ends_with(".rs")
&& entry.file_name().to_str().unwrap() != "mod.rs"
{
good_fs.push(
path.into_os_string()
.into_string()
.unwrap()
.split_at(2)
.1
.to_string(),
);
}
}
}
good_fs.sort();
Ok(good_fs)
}

fn md_prefix(indent_count: usize) -> String {
if indent_count > 0 {
format!("{}*", " ".repeat(indent_count))
} else {
"\n##".to_string()
}
}

fn print_path(old_path: String, new_path: String) -> (String, String) {
let old_parts = old_path
.split(std::path::MAIN_SEPARATOR)
.collect::<Vec<&str>>();
let mut result = String::new();
for (count, new_part) in new_path.split(std::path::MAIN_SEPARATOR).enumerate() {
if count + 1 > old_parts.len() || old_parts[count] != new_part {
println!("{} {}", md_prefix(count), to_title(new_part));
result.push_str(format!("{} {}\n", md_prefix(count), to_title(new_part)).as_str());
}
}
(new_path, result)
}

pub fn build_directory_md(top_dir: &Path) -> Result<String, Box<dyn Error>> {
let mut old_path = String::from("");
let mut result = String::new();
for filepath in good_filepaths(top_dir)? {
let mut filepath = PathBuf::from(filepath);
let filename = filepath.file_name().unwrap().to_owned();
filepath.pop();
let filepath = filepath.into_os_string().into_string().unwrap();
if filepath != old_path {
let path_res = print_path(old_path, filepath);
old_path = path_res.0;
result.push_str(path_res.1.as_str());
}
let url = format!("{}/{}", old_path, filename.to_string_lossy());
let url = get_addr(&url);
let indent = old_path.matches(std::path::MAIN_SEPARATOR).count() + 1;
let filename = to_title(filename.to_str().unwrap().split('.').collect::<Vec<&str>>()[0]);
println!("{} [{}]({})", md_prefix(indent), filename, url);
result.push_str(format!("{} [{}]({})\n", md_prefix(indent), filename, url).as_str());
}
Ok(result)
}

fn to_title(name: &str) -> String {
let mut change = true;
name.chars()
.map(move |letter| {
if change && !letter.is_numeric() {
change = false;
letter.to_uppercase().next().unwrap()
} else if letter == '_' {
change = true;
' '
} else {
if letter.is_numeric() || !letter.is_alphanumeric() {
change = true;
}
letter
}
})
.collect::<String>()
}

fn get_addr(addr: &str) -> String {
if cfg!(windows) {
format!("{}/{}", URL_BASE, switch_backslash(addr))
} else {
format!("{}/{}", URL_BASE, addr)
}
}

// Function that changes '\' to '/' (for Windows builds only)
fn switch_backslash(addr: &str) -> String {
addr.chars()
.map(|mut symbol| {
if symbol == '\\' {
symbol = '/';
}
symbol
})
.collect::<String>()
}
17 changes: 17 additions & 0 deletions .github/workflows/scripts/build_directory/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::{fs::File, io::Write, path::Path};

use build_directory::build_directory_md;
fn main() -> Result<(), std::io::Error> {
let mut file = File::create("DIRECTORY.md").unwrap(); // unwrap for panic

match build_directory_md(Path::new(".")) {
Ok(buf) => {
file.write_all("# List of all files\n".as_bytes())?;
file.write_all(buf.as_bytes())?;
}
Err(err) => {
panic!("Error while creating string: {err}");
}
}
Ok(())
}
51 changes: 0 additions & 51 deletions .github/workflows/scripts/build_directory_md.py

This file was deleted.