From e3e0b16d2a4e08e61e4e94dcf5fe1a61a0be1dc9 Mon Sep 17 00:00:00 2001 From: praveenkuttappan <55455725+praveenkuttappan@users.noreply.github.com> Date: Wed, 15 Jan 2020 15:15:35 -0800 Subject: [PATCH 1/3] New Script to add/update version in change log --- eng/common/Update-Change-Log.ps1 | 125 +++++++++++++++++++++++++++++++ eng/common/Version-Parser.psm1 | 8 ++ 2 files changed, 133 insertions(+) create mode 100644 eng/common/Update-Change-Log.ps1 create mode 100644 eng/common/Version-Parser.psm1 diff --git a/eng/common/Update-Change-Log.ps1 b/eng/common/Update-Change-Log.ps1 new file mode 100644 index 0000000000..11989e146f --- /dev/null +++ b/eng/common/Update-Change-Log.ps1 @@ -0,0 +1,125 @@ +# Note: This script will add or replace version title in change log +param ( + [Parameter(Mandatory = $true)] + [String]$Version, + [Parameter(Mandatory = $true)] + [String]$ChangeLogPath, + [String]$Unreleased = $True, + [String]$ReplaceVersion = $False +) + +# Parameter description +# Version : Version to add or replace in change log +# ChangeLogPath: Path to change log file. If change log path is set to directory then script will probe for change log file in that path +# Unreleased: Default is true. If it is set to false, then today's date will be set in verion title. If it is True then title will show "Unreleased" +# ReplaceVersion: This is useful when replacing current version title with new title.( Helpful to update the title before package release) + + +function Get-ChangelogPath +{ + Param ( + [String]$Path + ) + + # Check if CHANGELOG.md is present in path + $ChangeLogPath = Join-Path -Path $Path -ChildPath "CHANGELOG.md" + if ((Test-Path -Path $ChangeLogPath) -eq $False){ + # Check if change log exists with name HISTORY.md + $ChangeLogPath = Join-Path -Path $Path -ChildPath "HISTORY.md" + if ((Test-Path -Path $ChangeLogPath) -eq $False){ + Write-Host "Change log is not found in path[$Path]" + exit(1) + } + } + + Write-Host "Change log is found at path [$ChangeLogPath]" + return $ChangeLogPath +} + + +function Get-VersionTitle +{ + Param ( + [String]$Version, + [String]$Unreleased + ) + # Generate version title + $newVersionTitle = "## $Version Unreleased" -join [Environment]::NewLine + if ($Unreleased -eq $False){ + $releaseDate = Get-Date -Format "(yyyy-MM-dd)" + $newVersionTitle = "## $Version $releaseDate" -join [Environment]::NewLine + } + return $newVersionTitle +} + + +function Get-NewChangeLog +{ + Param ( + [System.Collections.ArrayList]$ChangelogLines, + [String]$Version, + [String]$Unreleased, + [String]$ReplaceVersion + ) + + # Version Parser module + Import-Module $PSScriptRoot/Version-Parser.psm1 + + # version parameter is to pass new version to add or replace + # Unreleased parameter can be set to False to set today's date instead of "Unreleased in title" + # ReplaceVersion param can be set to true to replace current version title( useful at release time to change title) + + # find index of current version + $Index = 0 + $CurrentTitle = "" + for(; $Index -lt $ChangelogLines.Count; $Index++){ + if (Version-Matches($ChangelogLines[$Index])){ + $CurrentTitle = $ChangelogLines[$Index] + Write-Host "Current Version title: $CurrentTitle" + break + } + } + + # Generate version title + $newVersionTitle = Get-VersionTitle -Version $Version -Unreleased $Unreleased + + # if version is already found and not replacing then nothing to do + if ($ReplaceVersion -eq $False){ + # Check if version is already present in log + if ($CurrentTitle.Contains($Version)){ + Write-Host "Version is already present in change log. Please set ReplaceVersion parameter if current title needs to be updated" + exit(1) + } + + Write-Host "Adding version title $newVersionTitle" + $ChangelogLines.insert($Index, $newVersionTitle) + } + else{ + # Script is executed to replace an existing version title + Write-Host "Replacing current version title to $newVersionTitle" + $ChangelogLines[$index] = $newVersionTitle + } + + return $ChangelogLines +} + + +# Make sure path is valid +if ((Test-Path -Path $ChangeLogPath) -eq $False){ + Write-Host "Change log path is invalid. [$ChangeLogPath]" + exit(1) +} + +# probe change log path if path is directory +if (Test-Path -Path $ChangeLogPath -PathType Container) +{ + $ChangeLogPath = Get-ChangelogPath -Path $ChangeLogPath +} + +# Read current change logs and add/update version +$ChangelogLines = [System.Collections.ArrayList](Get-Content -Path $ChangeLogPath) +$NewContents = Get-NewChangeLog -ChangelogLines $ChangelogLines -Version $Version -Unreleased $Unreleased -ReplaceVersion $ReplaceVersion + +Write-Host "Writing change log to file [$ChangeLogPath]" +Set-Content -Path $ChangeLogPath $NewContents +Write-Host "Version is added/updated in change log" diff --git a/eng/common/Version-Parser.psm1 b/eng/common/Version-Parser.psm1 new file mode 100644 index 0000000000..869f48c777 --- /dev/null +++ b/eng/common/Version-Parser.psm1 @@ -0,0 +1,8 @@ +$RELEASE_TITLE_REGEX = "(?^\#+.*(?\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?))" + +function Version-Matches($line) +{ + return ($line -match $RELEASE_TITLE_REGEX) +} + +Export-ModuleMember -Function 'Version-Matches' \ No newline at end of file From af1ca3e9b3e9954b17136e0e9861aa70d276f40a Mon Sep 17 00:00:00 2001 From: praveenkuttappan <55455725+praveenkuttappan@users.noreply.github.com> Date: Mon, 27 Jan 2020 14:34:34 -0800 Subject: [PATCH 2/3] Fixes as per review comments --- eng/common/Update-Change-Log.ps1 | 32 +++++++++++++++++++++----------- eng/common/Version-Parser.psm1 | 8 -------- 2 files changed, 21 insertions(+), 19 deletions(-) delete mode 100644 eng/common/Version-Parser.psm1 diff --git a/eng/common/Update-Change-Log.ps1 b/eng/common/Update-Change-Log.ps1 index 11989e146f..18bdf78452 100644 --- a/eng/common/Update-Change-Log.ps1 +++ b/eng/common/Update-Change-Log.ps1 @@ -14,6 +14,12 @@ param ( # Unreleased: Default is true. If it is set to false, then today's date will be set in verion title. If it is True then title will show "Unreleased" # ReplaceVersion: This is useful when replacing current version title with new title.( Helpful to update the title before package release) +$RELEASE_TITLE_REGEX = "(?^\#+.*(?\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?))" + +function Version-Matches($line) +{ + return ($line -match $RELEASE_TITLE_REGEX) +} function Get-ChangelogPath { @@ -44,10 +50,10 @@ function Get-VersionTitle [String]$Unreleased ) # Generate version title - $newVersionTitle = "## $Version Unreleased" -join [Environment]::NewLine + $newVersionTitle = "## $Version (Unreleased)" if ($Unreleased -eq $False){ $releaseDate = Get-Date -Format "(yyyy-MM-dd)" - $newVersionTitle = "## $Version $releaseDate" -join [Environment]::NewLine + $newVersionTitle = "## $Version $releaseDate" } return $newVersionTitle } @@ -62,9 +68,6 @@ function Get-NewChangeLog [String]$ReplaceVersion ) - # Version Parser module - Import-Module $PSScriptRoot/Version-Parser.psm1 - # version parameter is to pass new version to add or replace # Unreleased parameter can be set to False to set today's date instead of "Unreleased in title" # ReplaceVersion param can be set to true to replace current version title( useful at release time to change title) @@ -83,15 +86,22 @@ function Get-NewChangeLog # Generate version title $newVersionTitle = Get-VersionTitle -Version $Version -Unreleased $Unreleased + if( $newVersionTitle -eq $CurrentTitle){ + Write-Host "Version is already present in change log" + exit(0) + } + + # if current version tiel already has new version then we should replace title to update it + if ($CurrentTitle.Contains($Version) -and $ReplaceVersion -eq $False){ + Write-Host "Version is already present in title. Updating version title" + $ReplaceVersion = $True + } + # if version is already found and not replacing then nothing to do if ($ReplaceVersion -eq $False){ - # Check if version is already present in log - if ($CurrentTitle.Contains($Version)){ - Write-Host "Version is already present in change log. Please set ReplaceVersion parameter if current title needs to be updated" - exit(1) - } - Write-Host "Adding version title $newVersionTitle" + $ChangelogLines.insert($Index, "") + $ChangelogLines.insert($Index, "") $ChangelogLines.insert($Index, $newVersionTitle) } else{ diff --git a/eng/common/Version-Parser.psm1 b/eng/common/Version-Parser.psm1 deleted file mode 100644 index 869f48c777..0000000000 --- a/eng/common/Version-Parser.psm1 +++ /dev/null @@ -1,8 +0,0 @@ -$RELEASE_TITLE_REGEX = "(?^\#+.*(?\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?))" - -function Version-Matches($line) -{ - return ($line -match $RELEASE_TITLE_REGEX) -} - -Export-ModuleMember -Function 'Version-Matches' \ No newline at end of file From d4f89d1a090f583c73fff81f7723d9a57a744f04 Mon Sep 17 00:00:00 2001 From: praveenkuttappan <55455725+praveenkuttappan@users.noreply.github.com> Date: Tue, 28 Jan 2020 10:44:27 -0800 Subject: [PATCH 3/3] Review comments to stream line parameters --- eng/common/Update-Change-Log.ps1 | 36 +++++++++++--------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/eng/common/Update-Change-Log.ps1 b/eng/common/Update-Change-Log.ps1 index 18bdf78452..0a41f116bb 100644 --- a/eng/common/Update-Change-Log.ps1 +++ b/eng/common/Update-Change-Log.ps1 @@ -1,4 +1,11 @@ # Note: This script will add or replace version title in change log + +# Parameter description +# Version : Version to add or replace in change log +# ChangeLogPath: Path to change log file. If change log path is set to directory then script will probe for change log file in that path +# Unreleased: Default is true. If it is set to false, then today's date will be set in verion title. If it is True then title will show "Unreleased" +# ReplaceVersion: This is useful when replacing current version title with new title.( Helpful to update the title before package release) + param ( [Parameter(Mandatory = $true)] [String]$Version, @@ -8,11 +15,6 @@ param ( [String]$ReplaceVersion = $False ) -# Parameter description -# Version : Version to add or replace in change log -# ChangeLogPath: Path to change log file. If change log path is set to directory then script will probe for change log file in that path -# Unreleased: Default is true. If it is set to false, then today's date will be set in verion title. If it is True then title will show "Unreleased" -# ReplaceVersion: This is useful when replacing current version title with new title.( Helpful to update the title before package release) $RELEASE_TITLE_REGEX = "(?^\#+.*(?\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?))" @@ -21,12 +23,8 @@ function Version-Matches($line) return ($line -match $RELEASE_TITLE_REGEX) } -function Get-ChangelogPath -{ - Param ( - [String]$Path - ) - +function Get-ChangelogPath($Path) +{ # Check if CHANGELOG.md is present in path $ChangeLogPath = Join-Path -Path $Path -ChildPath "CHANGELOG.md" if ((Test-Path -Path $ChangeLogPath) -eq $False){ @@ -43,12 +41,8 @@ function Get-ChangelogPath } -function Get-VersionTitle +function Get-VersionTitle($Version, $Unreleased) { - Param ( - [String]$Version, - [String]$Unreleased - ) # Generate version title $newVersionTitle = "## $Version (Unreleased)" if ($Unreleased -eq $False){ @@ -59,14 +53,8 @@ function Get-VersionTitle } -function Get-NewChangeLog +function Get-NewChangeLog( [System.Collections.ArrayList]$ChangelogLines, $Version, $Unreleased, $ReplaceVersion) { - Param ( - [System.Collections.ArrayList]$ChangelogLines, - [String]$Version, - [String]$Unreleased, - [String]$ReplaceVersion - ) # version parameter is to pass new version to add or replace # Unreleased parameter can be set to False to set today's date instead of "Unreleased in title" @@ -91,7 +79,7 @@ function Get-NewChangeLog exit(0) } - # if current version tiel already has new version then we should replace title to update it + # if current version title already has new version then we should replace title to update it if ($CurrentTitle.Contains($Version) -and $ReplaceVersion -eq $False){ Write-Host "Version is already present in title. Updating version title" $ReplaceVersion = $True