From e498a8e2166669781b5d8eb0dfaddfe16ef88656 Mon Sep 17 00:00:00 2001 From: Alexandre ABRIOUX Date: Sun, 10 Nov 2024 17:03:49 +0100 Subject: [PATCH] feat: add support for udpating readme file --- action.yaml | 4 +-- src/main.rs | 29 +++++++++++++++++++-- tests/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 97 insertions(+), 7 deletions(-) diff --git a/action.yaml b/action.yaml index 65a56c8..0d1bddb 100644 --- a/action.yaml +++ b/action.yaml @@ -17,5 +17,5 @@ runs: using: docker image: ghcr.io/alexandre-abrioux/github-profile-toolbox:latest args: - - --config=${{ inputs.config }} - - --readme=${{ inputs.readme }} \ No newline at end of file + - --config=/github/workspace/${{ inputs.config }} + - --readme=/github/workspace/${{ inputs.readme }} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a462b97..b607c7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,20 @@ struct Args { /// Path to the configuration file #[arg(short, long)] config: String, + /// Path to the README file to update + #[arg(short, long)] + readme: String, } fn main() { let args = Args::parse(); let contents = fs::read_to_string(&args.config).expect("Configuration file not found"); - let markdown = generate_toolbox(&contents); - print!("{}", markdown); + let toolbox_markdown = generate_toolbox(&contents); + if args.readme.is_empty() { + print!("{}", toolbox_markdown); + return; + } + update_readme(&args.readme, &toolbox_markdown); } fn generate_toolbox(contents: &String) -> String { @@ -101,6 +108,24 @@ fn generate_img_tag(slug: &str) -> String { format!("[\"{title}\"](#)") } +fn update_readme(readme_path: &String, toolbox_markdown: &String) { + let readme_content = fs::read_to_string(&readme_path).expect("Unable to read README file"); + let toolbox_section_start_bytes = readme_content + .find("") + .expect("Could not find START_SECTION comment in README file"); + let toolbox_section_end_bytes = readme_content + .find("") + .expect("Could not find STOP_SECTION comment in README file"); + let mut readme_content_updated = String::new(); + readme_content_updated.push_str(&readme_content[0..toolbox_section_start_bytes]); + readme_content_updated.push_str("\n"); + readme_content_updated.push_str("\n"); + readme_content_updated.push_str(&toolbox_markdown); + readme_content_updated + .push_str(&readme_content[toolbox_section_end_bytes..readme_content.len()]); + fs::write(&readme_path, readme_content_updated).expect("Unable to write to README file"); +} + #[cfg(test)] mod tests { use crate::generate_toolbox; diff --git a/tests/main.rs b/tests/main.rs index b520fe8..6135ac4 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -19,8 +19,8 @@ mod tests { #[test] fn should_generate_toolbox() -> Result<(), Box> { - let file = assert_fs::NamedTempFile::new("config.yml")?; - file.write_str( + let config_file = assert_fs::NamedTempFile::new("config.yaml")?; + config_file.write_str( "tools: ides: - jetbrains @@ -33,7 +33,7 @@ mod tests { )?; let mut cmd = Command::cargo_bin(BIN_NAME)?; - cmd.arg("--config").arg(file.path()); + cmd.arg("--config").arg(config_file.path()); cmd.assert().success().stdout(predicate::eq("|ides|languages| |-|-| |[\"JetBrains\"](#)|[\"JavaScript\"](#)| @@ -44,4 +44,69 @@ mod tests { Ok(()) } + + #[test] + fn should_update_readme_file() -> Result<(), Box> { + let config_file = assert_fs::NamedTempFile::new("config.yaml")?; + let readme_file = assert_fs::NamedTempFile::new("README.md")?; + config_file.write_str( + "tools: + ides: + - jetbrains + - neovim + languages: + - javascript + - cplusplus", + )?; + readme_file.write_str( + "# Title + +## Toolbox + + +## Other things +Lorem ipsum dolor sit amet +", + )?; + + let mut cmd = Command::cargo_bin(BIN_NAME)?; + cmd.arg("--config").arg(config_file.path()); + cmd.arg("--readme").arg(readme_file.path()); + cmd.assert().success(); + + readme_file.assert(predicate::eq( + "# Title + +## Toolbox + + +|ides|languages| +|-|-| +|[\"JetBrains\"](#)|[\"JavaScript\"](#)| +|[\"Neovim\"](#)|[\"C++\"](#)| + + +## Other things +Lorem ipsum dolor sit amet +", + )); + + Ok(()) + } + + #[test] + fn should_handle_missing_section() -> Result<(), Box> { + let config_file = assert_fs::NamedTempFile::new("config.yaml")?; + let readme_file = assert_fs::NamedTempFile::new("README.md")?; + config_file.write_str("tools:\n ides:\n - jetbrains")?; + readme_file.write_str("")?; + + let mut cmd = Command::cargo_bin(BIN_NAME)?; + cmd.arg("--config").arg(config_file.path()); + cmd.arg("--readme").arg(readme_file.path()); + cmd.assert().failure().stderr(predicate::str::contains( + "Could not find START_SECTION comment in README file", + )); + Ok(()) + } }