Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set-ShieldsIoBadge replaces whole line (until last ')') #131

Open
DEberhardt opened this issue Jul 2, 2022 · 1 comment
Open

Set-ShieldsIoBadge replaces whole line (until last ')') #131

DEberhardt opened this issue Jul 2, 2022 · 1 comment

Comments

@DEberhardt
Copy link

Hello,

first of all, thank you for this, this module helps A LOT - made more progress in the last two weeks learning and understanding the build process, runners, etc. than in the last two years!

I want to utilize Set-ShieldsIoBadge and I have a little table that contains the badges, looks like this:

Component Status

General Documentation - GitHub PowerShell Gallery - TeamsFunctions
Release Release Downloads
Build Build BuildWorkflow Issues
Functions Public Private Live RC BETA ALPHA
Pester Result Passed Failed Skipped NotRun CodeCoverage

after running updates against them, it looks like this:

Component Status

General Documentation - GitHub PowerShell Gallery - TeamsFunctions
Release Release Downloads
Build [Build
Functions Public
Pester Result

Looking into Set-ShieldsIoBadge, I find that the match string is finding too much:

$ReadmeContent = (Get-Content $Path)
$ReadmeContent = $ReadmeContent -replace "!\[$($Subject)\].+\)", "![$($Subject)](https://img.shields.io/badge/$Subject-$Status$Percent-$Color.svg)"
$ReadmeContent | Set-Content -Path $Path

# running this manually to see what matches$RM =  Get-Content Readme.md
❯ $Subject = "Public"$RM -match "!\[$($Subject)\].+\)"
| Functions | ![Public](https://img.shields.io/badge/Public-0-blue.svg) ![Private](https://img.shields.io/badge/Private-0-grey.svg) ![Live](https://img.shields.io/badge/Live-0-blue.svg) ![RC](https://img.shields.io/badge/RC-0-green.svg) ![BETA](https://img.shields.io/badge/BETA-3-yellow.svg) ![ALPHA](https://img.shields.io/badge/ALPHA-4-orange.svg)               

It looks like it replaces the whole line until the (last?) ')' rather than the next one found.
Playing around with Regex and Lazy operators, but cannot find a suitable match that would reduce the match to the actual.

Is there a way to do this please?

Thank you,
David

@DEberhardt
Copy link
Author

Found something that could work:

      $ReadmeContent = (Get-Content $Path)
      #$ReadmeContent = $ReadmeContent -replace "!\[$($Subject)\].+\)", "![$($Subject)](https://img.shields.io/badge/$Subject-$Status$Percent-$Color.svg)"
      $Pattern = "!\[$($Subject)\].+?\)"
      $String = "![$($Subject)](https://img.shields.io/badge/$Subject-$Status$Percent-$Color.svg)"

      $ReadmeContent = $ReadmeContent | ForEach-Object { 
        if ( ([regex]::Matches($_, $Pattern).success) ) { $_ -replace $Pattern, $String } else { $_ } 
      }
      $ReadmeContent | Set-Content -Path $Path

for my testing, it did leave the rest alone and just altered the matching string only

DEberhardt added a commit to DEberhardt/BuildHelpers that referenced this issue Jul 2, 2022
Adding surgical precision to Badge replace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant