|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Get info about the current commit |
| 4 | +most_recent_tag=$(git describe --tags --match="v*" --abbrev=0) |
| 5 | +commits_since_tag=$(git rev-list $most_recent_tag..HEAD | wc -l | awk '{$1=$1};1') |
| 6 | +sha=$(git log -1 --format=%H) |
| 7 | +branch=$(git rev-parse --abbrev-ref HEAD) |
| 8 | + |
| 9 | +# A regex for extracting data from a version number: major, minor, patch, |
| 10 | +# [prerelease] |
| 11 | +REGEX='v(\d+)\.(\d+)\.(\d+)(-.*)?' |
| 12 | + |
| 13 | +# Extract the data from the version number |
| 14 | +major=$(echo $most_recent_tag | perl -pe "s|$REGEX|\1|" ) |
| 15 | +minor=$(echo $most_recent_tag | perl -pe "s|$REGEX|\2|" ) |
| 16 | +patch=$(echo $most_recent_tag | perl -pe "s|$REGEX|\3|" ) |
| 17 | +prerelease=$(echo $most_recent_tag | perl -pe "s|$REGEX|\4|" ) |
| 18 | + |
| 19 | +# Create the version strings we'll write into the AssemblyInfo files |
| 20 | +OutputAssemblyVersion="$major.$minor.$patch.$commits_since_tag" |
| 21 | +OutputAssemblyInformationalVersion="$major.$minor.$patch$prerelease+$commits_since_tag.Branch.$branch.Sha.$sha" |
| 22 | +OutputAssemblyFileVersion="$major.$minor.$patch.$commits_since_tag" |
| 23 | + |
| 24 | +# Calculate the semver from the version (should be the same as the version, but |
| 25 | +# just in case) |
| 26 | +SemVer="v$major.$minor.$patch$prerelease" |
| 27 | + |
| 28 | +# If there are any commits since the current tag, add that note |
| 29 | +if [ "$commits_since_tag" -gt 0 ]; then |
| 30 | + SemVer="$Semver+$commits_since_tag" |
| 31 | +fi |
| 32 | + |
| 33 | +# Update the AssemblyInfo.cs files |
| 34 | +for infoFile in $(find . -name "AssemblyInfo.cs"); do |
| 35 | + perl -pi -e "s/AssemblyVersion\(\".*\"\)/AssemblyVersion(\"$OutputAssemblyVersion\")/" $infoFile |
| 36 | + perl -pi -e "s/AssemblyInformationalVersion\(\".*\"\)/AssemblyInformationalVersion(\"$OutputAssemblyInformationalVersion\")/" $infoFile |
| 37 | + perl -pi -e "s/AssemblyFileVersion\(\".*\"\)/AssemblyFileVersion(\"$OutputAssemblyFileVersion\")/" $infoFile |
| 38 | +done |
| 39 | + |
| 40 | +# If we're running in GitHub Workflows, output our calculated SemVer |
| 41 | +if [[ -n $GITHUB_OUTPUT ]]; then |
| 42 | + echo "SemVer=$SemVer" >> "$GITHUB_OUTPUT" |
| 43 | +fi |
| 44 | + |
| 45 | +# Log our SemVer |
| 46 | +echo $SemVer |
0 commit comments