Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: how to split, do math and format

Tags:

powershell

I am looking for a 1-liner that looks something like this:

(gitversion | ConvertFrom-Json).SemVer.split('.') | [string]::Format("{0}.{1}.{2}",$_[0],$_[1] + 1,$_[2]) | git tag $_

So, right now I have 0.1.0 and I want a 1 liner that will map it to: 0.2.0

I am sure a guru in PS can make fast work of this

like image 738
Christian Bongiorno Avatar asked Sep 26 '26 16:09

Christian Bongiorno


1 Answers

You can read in the entire json, use the Major, Minor and Patch number of the version and update whichever one you are interested in... patch, minor or major.

I am using foreach-object to access all the properties of the json even though there is only one available. 1-liner:

gitversion | ConvertFrom-Json | % { "$($_.Major).$($_.Minor + 1).$($_.Patch)" } | % { git tag $_ }

How can % / ForEach-Object work with multiple repositories

If there is only one repository at the path you are running gitversion, it will always give you one object. (gitversion fails with errors when you run this in directory that doesnt have a git repository.)

I would like to stress that this would work great in your scenario where there is only Major, Minor and Patch version. In cases where the SemVersion is like, "SemVer":"0.1.0", this is fine. But, if your interest is to keep the PreReleaseTag as well in your next version (and not drop the alpha / beta etc tags), you can include the PreReleaseTagWithDash to your output as well. e.g of pre-release tags: "SemVer":"0.1.0-alpha.915"

gitversion | ConvertFrom-Json | % { "$($_.Major).$($_.Minor + 1).$($_.Patch)$($_.PreReleaseTagWithDash)" } | % { git tag $_ }

Sample JSONs of gitversion

  • From dev or other branches, you can expect variations in semVer that includes pre-release tags.
{
  "Major":0,
  "Minor":1,
  "Patch":0,
  "PreReleaseTag":"alpha.915",
  "PreReleaseTagWithDash":"-alpha.915",
  "PreReleaseLabel":"alpha",
  "PreReleaseNumber":915,
  "WeightedPreReleaseNumber":915,
  "BuildMetaData":"",
  "BuildMetaDataPadded":"",
  "FullBuildMetaData":"Branch.dev.Sha.888xxx.xxx888",
  "MajorMinorPatch":"0.1.0",
  "SemVer":"0.1.0-alpha.915",
  "LegacySemVer":"0.1.0-alpha915",
  "LegacySemVerPadded":"0.1.0-alpha0915",
  "AssemblySemVer":"0.1.0.0",
  "AssemblySemFileVer":"0.1.0.0",
  "FullSemVer":"0.1.0-alpha.915",
  "InformationalVersion":"0.1.0-alpha.915+Branch.dev.Sha.888xx.xx888",
  "BranchName":"dev",
  "Sha":"888xx.xx888",
  "ShortSha":"7a1152f",
  "NuGetVersionV2":"0.1.0-alpha0915",
  "NuGetVersion":"0.1.0-alpha0915",
  "NuGetPreReleaseTagV2":"alpha0915",
  "NuGetPreReleaseTag":"alpha0915",
  "VersionSourceSha":"88xxx.xx88",
  "CommitsSinceVersionSource":915,
  "CommitsSinceVersionSourcePadded":"0915",
  "CommitDate":"2019-12-06"
}
  • From master branch, you would normally expect a semver = major.minor.patch.
{
  "Major":0,
  "Minor":1,
  "Patch":0,
  "PreReleaseTag":"",
  "PreReleaseTagWithDash":"",
  "PreReleaseLabel":"",
  "PreReleaseNumber":"",
  "WeightedPreReleaseNumber":"",
  "BuildMetaData":0,
  "BuildMetaDataPadded":"0000",
  "FullBuildMetaData":"0.Branch.master.Sha.9999xxx....xxx999",
  "MajorMinorPatch":"0.1.0",
  "SemVer":"0.1.0",
  "LegacySemVer":"0.1.0",
  "LegacySemVerPadded":"0.1.0",
  "AssemblySemVer":"0.1.0.0",
  "AssemblySemFileVer":"0.1.0.0",
  "FullSemVer":"0.1.0+0",
  "InformationalVersion":"0.1.0+0.Branch.master.Sha.999xxx...xxx999",
  "BranchName":"master",
  "Sha":"86b0be929a84ba7e9b2a463e7dbdc9a3c9325dc1",
  "ShortSha":"86b0be9",
  "NuGetVersionV2":"0.1.0",
  "NuGetVersion":"0.1.0",
  "NuGetPreReleaseTagV2":"",
  "NuGetPreReleaseTag":"",
  "VersionSourceSha":"999xxx...xxx999",
  "CommitsSinceVersionSource":0,
  "CommitsSinceVersionSourcePadded":"0000",
  "CommitDate":"2018-02-18"
}

NOTE: I have not tested this with multiple git repositories in one location. This solution will work if there is only repository at the path you are running.

like image 187
Jawad Avatar answered Sep 28 '26 10:09

Jawad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!