Skip to content

Commit

Permalink
feat: add support for udpating readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Nov 10, 2024
1 parent 924a885 commit e498a8e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
4 changes: 2 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ runs:
using: docker
image: ghcr.io/alexandre-abrioux/github-profile-toolbox:latest
args:
- --config=${{ inputs.config }}
- --readme=${{ inputs.readme }}
- --config=/github/workspace/${{ inputs.config }}
- --readme=/github/workspace/${{ inputs.readme }}
29 changes: 27 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -101,6 +108,24 @@ fn generate_img_tag(slug: &str) -> String {
format!("[<img align=\"left\" alt=\"{title}\" src=\"https://img.shields.io/badge/-{title}-{hex}?logo={slug}&logoColor={background}\">](#)")
}

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("<!-- START_SECTION:toolbox -->")
.expect("Could not find START_SECTION comment in README file");
let toolbox_section_end_bytes = readme_content
.find("<!-- STOP_SECTION:toolbox -->")
.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("<!-- START_SECTION:toolbox -->\n");
readme_content_updated.push_str("<!-- Generated by github-profile-toolbox GitHub action -->\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;
Expand Down
71 changes: 68 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ mod tests {

#[test]
fn should_generate_toolbox() -> Result<(), Box<dyn std::error::Error>> {
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
Expand All @@ -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|
|-|-|
|[<img align=\"left\" alt=\"JetBrains\" src=\"https://img.shields.io/badge/-JetBrains-000000?logo=jetbrains&logoColor=white\">](#)|[<img align=\"left\" alt=\"JavaScript\" src=\"https://img.shields.io/badge/-JavaScript-F7DF1E?logo=javascript&logoColor=black\">](#)|
Expand All @@ -44,4 +44,69 @@ mod tests {

Ok(())
}

#[test]
fn should_update_readme_file() -> Result<(), Box<dyn std::error::Error>> {
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
<!-- START_SECTION:toolbox --><!-- STOP_SECTION: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
<!-- START_SECTION:toolbox -->
<!-- Generated by github-profile-toolbox GitHub action -->
|ides|languages|
|-|-|
|[<img align=\"left\" alt=\"JetBrains\" src=\"https://img.shields.io/badge/-JetBrains-000000?logo=jetbrains&logoColor=white\">](#)|[<img align=\"left\" alt=\"JavaScript\" src=\"https://img.shields.io/badge/-JavaScript-F7DF1E?logo=javascript&logoColor=black\">](#)|
|[<img align=\"left\" alt=\"Neovim\" src=\"https://img.shields.io/badge/-Neovim-57A143?logo=neovim&logoColor=white\">](#)|[<img align=\"left\" alt=\"C++\" src=\"https://img.shields.io/badge/-C++-00599C?logo=cplusplus&logoColor=white\">](#)|
<!-- STOP_SECTION:toolbox -->
## Other things
Lorem ipsum dolor sit amet
",
));

Ok(())
}

#[test]
fn should_handle_missing_section() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}

0 comments on commit e498a8e

Please sign in to comment.