1- param ( [string ]$tag , [switch ]$updatelog )
2- # -tag [new_tag], new tag shall be in format ^\d+\.\d+\.\d+$ and greater than
3- # the current one
4- # -updatelog update CHANGELOG.md if this switch used
5-
1+ # !/usr/bin/env pwsh
2+ param ( [string ]$tag , [string ]$basetag , [switch ]$updatelog , [switch ]$h )
3+ # -tag [new_tag], new tag shall be in format of ^\d+\.\d+\.\d+$ and greater than
4+ # the current one or $basetag
5+ # -basetag [basetag name], specify the base tag(which shall be in format of ^\d+\.\d+\.\d+$),
6+ # default to be the latest number format tag name
7+ # -updatelog, update CHANGELOG.md if this switch used
8+ # -h, show help info
9+ function faild_exit {
10+ Write-Host
11+ Write-Host " Orz :( bump version failed!"
12+ Write-Host " we will revert the modification by command git checkout . "
13+ Write-Host
14+ & git checkout .
15+ exit 1
16+ }
617# get old version info from file, esp from build.bat
718function get_old_version {
819 param ([string ]$filePath )
@@ -25,7 +36,6 @@ function get_old_version{
2536 $version [' str' ] = $version [" major" ] + " ." + $version [" minor" ] + " ." + $version [" patch" ];
2637 return $version ;
2738}
28-
2939# parse new version info
3040function parse_new_version {
3141 param ([string ]$new_version )
@@ -37,26 +47,10 @@ function parse_new_version {
3747 }
3848 return $version ;
3949}
40-
41- # update bat file with param filePath, and tags
42- function update_bat_file {
43- param ( [string ]$filePath , $old_version , $new_version )
44- $old_major , $old_minor , $old_patch = $old_version [' major' ], $old_version [" minor" ], $old_version [" patch" ];
45- $new_major , $new_minor , $new_patch = $new_version [' major' ], $new_version [" minor" ], $new_version [" patch" ];
46- $fileContent = Get-Content - Path $filePath ;
47- $fileContent = $fileContent -replace " VERSION_MAJOR=$old_major " , " VERSION_MAJOR=$new_major " ;
48- $fileContent = $fileContent -replace " VERSION_MINOR=$old_minor " , " VERSION_MINOR=$new_minor " ;
49- $fileContent = $fileContent -replace " VERSION_PATCH=$old_patch " , " VERSION_PATCH=$new_patch " ;
50- Set-Content - Path $filePath - Value $fileContent - Encoding UTF8;
51- }
52-
5350# update change log, work like clog-cli
5451function update_changelog {
5552 param ( [string ]$new_tag , [string ]$old_tag )
56- # get the current the previous release tag and current release tag
57- $tags = git tag -- sort= creatordate | Select-String - Pattern ' ^\d+\.\d+\.\d+$' | ForEach-Object { $_.ToString () };
58- $latestTag = $tags [-1 ];
59- $commits = git log " $latestTag ..HEAD" -- pretty= format:" %s ([%an](https://github.com/rime/weasel/commit/%H))" ;
53+ $commits = git log " $old_tag ..HEAD" -- pretty= format:" %s ([%an](https://github.com/rime/weasel/commit/%H))" ;
6054 # group commit logs
6155 $groupedCommits = @ {
6256 build = @ (); ci = @ (); fix = @ (); feat = @ (); docs = @ (); style = @ ();
@@ -75,8 +69,8 @@ function update_changelog {
7569 commit = " Commits" ;
7670 };
7771 foreach ($commit in $commits ) {
78- if ($commit -match " ^(build|ci|fix|feat|docs|style|refactor|test|chore):" ) {
79- $prefix = $matches [1 ] ;
72+ if ($commit -match " ^((?i)( build|ci|fix|feat|docs|style|refactor|test|chore)(\([^\)]+\))? ):" ) {
73+ $prefix = $matches [2 ].ToLower() ;
8074 $groupedCommits [$prefix ] += $commit ;
8175 } else {
8276 $groupedCommits [" commit" ] += $commit ;
@@ -99,57 +93,102 @@ function update_changelog {
9993 $contentAdd += $changelog ;
10094 Write-Host " `n " + $contentAdd + " `n "
10195 $fileContent = $contentAdd + " `n " + $fileContent ;
102- $fileContent | Out-File - FilePath " CHANGELOG.md" - Encoding UTF8;
96+ $fileContent | Out-File - FilePath " CHANGELOG.md" - NoNewline - Encoding UTF8;
10397}
104-
105- # update appcast file, with regex patterns
106- function update_appcast_file {
98+ # replace string with regex pat
99+ function replace_str {
107100 param ( [string ]$filePath , [string ]$pat_orig , [string ]$pat_replace )
108- $fileContent = Get-Content - Path $filePath ;
109- $fileContent = $fileContent -replace $pat_orig , $pat_replace ;
110- Set-Content - Path $filePath - Value $fileContent ;
101+ $fileContent = Get-Content - Path $filePath - Raw;
102+ $fileContent = [regex ]::Replace($fileContent , $pat_orig , $pat_replace )
103+ $fileContent | Out-File - FilePath $filePath - NoNewline;
104+ }
105+ # update xml file
106+ function update_xml {
107+ param ([string ]$filePath , [string ]$tag )
108+ $CurrentCulture = [System.Globalization.CultureInfo ]::InvariantCulture
109+ $localTime = Get-Date
110+ $currentDateTime = $localTime.ToString (" ddd, dd MMM yyyy HH:mm:ss zz00" )
111+ replace_str - filePath " $filePath " - pat_orig " \d+\.\d+\.\d+" - pat_replace $tag
112+ replace_str - filePath " $filePath " `
113+ - pat_orig " (?<=<sparkle:releaseNotesLink>)[^>]*(?=</sparkle:releaseNotesLink>)" `
114+ - pat_replace " http://rime.github.io/release/weasel/"
115+ replace_str - filePath " $filePath " `
116+ - pat_orig " (?<=<pubDate>).*?(?=</pubDate>)" - pat_replace " $currentDateTime "
117+ Write-Host " $filePath updated"
118+ }
119+ # update bat file
120+ function update_bat {
121+ param ([string ]$filePath , [string ]$tag )
122+ $new_version = parse_new_version - new_version $tag
123+ replace_str - filePath $filePath - pat_orig " (?<=VERSION_MAJOR=)\d+" $new_version [' major' ]
124+ replace_str - filePath $filePath - pat_orig " (?<=VERSION_MINOR=)\d+" $new_version [' minor' ]
125+ replace_str - filePath $filePath - pat_orig " (?<=VERSION_PATCH=)\d+" $new_version [' patch' ]
126+ Write-Host " $filePath updated"
127+ }
128+ # test if cwd is weasel root
129+ function is_weasel_root () {
130+ return (Test-Path - Path " weasel.sln" ) -and (Test-Path - Path " .git" )
111131}
112132# ##############################################################################
113133# program now started
134+ if ($h -or (-not $tag )) {
135+ $info = @ (
136+ " -tag [new_tag]`n`t new tag shall be in format of ^\d+\.\d+\.\d+$ and greater than the current one or `$ basetag" ,
137+ " -basetag [basetag name]`n`t specify the base tag(which shall be in format of ^\d+\.\d+\.\d+$), default to be the latest number format tag name" ,
138+ " -updatelog`n`t update CHANGELOG.md if this switch used" ,
139+ " -h`n`t show help info"
140+ )
141+ Write-Host ($info -join " `n " );
142+ exit 0 ;
143+ }
144+ if (-not (is_weasel_root)) {
145+ Write-Host " Current directory is: $ ( Get-Location ) , it's not the weasel root directory"
146+ Write-Host " please run `` update/bump_version.ps1`` with parameters under the weasel root in Powershell" ;
147+ Write-Host " or run `` pwsh update/bump_version.ps1`` with parameters under weasel root in shell if yor're running Mac OS or Linux"
148+ exit 1 ;
149+ }
114150# tag name not match rule, exit
115151if (-not ($tag -match ' ^\d+\.\d+\.\d+$' )) {
116152 Write-Host " tag name not match rule '^\d+\.\d+\.\d+$'" ;
117153 Write-Host " please recheck it" ;
118- exit ;
154+ exit 1 ;
119155}
120156# get old version
121157$old_version = get_old_version - filePath " build.bat" ;
122- $old_major , $old_minor , $old_patch = $old_version [' major' ], $old_version [" minor" ], $old_version [" patch" ];
123158# get new version
124159$new_version = parse_new_version - new_version $tag
160+ if ($basetag -eq " " ) {
161+ $basetag = $old_version [" str" ]
162+ }
163+ if ($basetag -notmatch " ^\d+\.\d+\.\d+$" ) {
164+ Write-Host " basetag format is not correct, it shall be in format of '^\d+\.\d+\.\d+$'"
165+ Write-Host " please recheck it" ;
166+ exit 1 ;
167+ }
168+
125169# check new version is greater than the current one
126170if ($tag -eq $old_version [" str" ]) {
127171 Write-Host " the new tag: $tag is the same with the old one:" $old_version [" str" ];
128- exit ;
172+ exit 1 ;
129173} elseif ($tag -lt $old_version [" str" ]) {
130174 Write-Host " $tag is older version than " $old_version [' str' ]" , please recheck your target version." ;
131- exit ;
175+ exit 1 ;
132176}
133- Write-Host " bump version to $tag ... "
134- # get date-time string in english date-time format
135- $CurrentCulture = [System.Globalization.CultureInfo ]::InvariantCulture
136- $currentDateTime = (Get-Date ).ToString(" ddd, dd MMM yyyy HH:mm:ss K" , $CurrentCulture )
137-
138- # update appcast files
139- update_appcast_file - filePath " update/appcast.xml" - pat_orig " $old_major \.$old_minor \.$old_patch " - pat_replace $tag
140- update_appcast_file - filePath " update/appcast.xml" - pat_orig " <pubDate>.*?</pubDate>" - pat_replace " <pubDate>$currentDateTime </pubDate>"
141- Write-Host " update/appcast.xml updated"
142- update_appcast_file - filePath " update/testing-appcast.xml" - pat_orig " $old_major \.$old_minor \.$old_patch " - pat_replace $tag
143- update_appcast_file - filePath " update/testing-appcast.xml" - pat_orig " <pubDate>.*?</pubDate>" - pat_replace " <pubDate>$currentDateTime </pubDate>"
144- Write-Host " update/testing-appcast.xml updated"
145- # update bat files
146- update_bat_file - filePath " build.bat" - old_version $old_version - new_version $new_version
147- Write-Host " build.bat updated"
148- update_bat_file - filePath " xbuild.bat" - old_version $old_version - new_version $new_version
149- Write-Host " xbuild.bat updated"
177+ Write-Host " Bumping version from $ ( $old_version [' str' ]) to $tag ..."
178+ # update xml files
179+ update_xml - filePath " update/appcast.xml" - tag " $tag " ;
180+ if ($? ) { } else { faild_exit }
181+ update_xml - filePath " update/testing-appcast.xml" - tag " $tag " ;
182+ if ($? ) { } else { faild_exit }
183+ # update bat files
184+ update_bat - filePath " build.bat" - tag " $tag " ;
185+ if ($? ) { } else { faild_exit }
186+ update_bat - filePath " xbuild.bat" - tag " $tag " ;
187+ if ($? ) { } else { faild_exit }
150188# update CHANGELOG.md
151189if ($updatelog ) {
152- update_changelog - new_tag $tag - old_tag " $old_major .$old_minor .$old_patch "
190+ update_changelog - new_tag $tag - old_tag $basetag ;
191+ if ($? ) { } else { faild_exit }
153192 & git add CHANGELOG.md
154193 Write-Host " CHANGELOG.md updated"
155194}
0 commit comments