XML : Update Version Based on Schema Using Powershell Regular Expressions

So I'm trying to write a Powershell script that will update the version in an xml file for a couple of web projects using a schema in another xml file (Web.config). Let me illustrate what I mean.

Versions are 4 period-delimited numbers (1.0.0.5, 1.0.1.0, etc). I want to automate the incrementation of these on deployment according to a schema that looks like /[0-2].[0-2].[0-2].[0-2]/. So essentially the same, except that the values for each place can only be 0, 1 or 2. Values have the following meaning...

0 - do not change version number in this position

1 - increment version number in this position

2 - replace version number in this position with

So for example if I had a version number of 1.0.0.8 and a schema of 0.0.1.2, I'd want to transform the version number to 1.0.1.0

This is the Powershell function I have to do this...

  FUNCTION Increment-Version($path, $incrementSchema)  {      # PATH IS PATH TO WEB PROJECT ROOT, INCREMENTSCHEMA IS THE SCHEMA        #GET CURRENT APP VERSION      $projXml = New-Object XML      $projXml.Load($path)      $projXml.AppVersions.AppVersion.Version -match '(\d+)\.(\d+)\.(\d+)\.(\d+)'      #VERSION MATCH IS NOW THE MATCHES VARIABLE FOR THE CURRENT PROJECT VERSION      $versionMatch = $matches        #EXTRACT SCHEMA      $incrementSchema -match  '(\d+)\.(\d+)\.(\d+)\.(\d+)'      #SCHEMAMATCH IS NOW THE INCREMENT SCHEMA MATCH OBJECT       $schemaMatch = $matches        #ITERATE THROUGH ALL POSITIONS IN VERSION AND SCHEMA, THEY HAVE THE SAME       for ($i = 1; $i -lt 5; $i++)       {          switch ($schemaMatch[$i])          {              0 {                  #HERE I WANT TO LEAVE THE $ITH POSITION OF THE VERSION ALONE              }              1 {                  #HERE I WANT OT INCREMENT THE $ITH POSITION OF THE VERISON BY 1               }              2 {                  #HERE I WANT TO SET $ITH POSITION OF VERSION TO 0              }              default {}          }      }        # CODE TO SAVE VERSION XML FILE, ETC  }    

I'm a little lost on how to implement the case logic, what I need to do is replace the relevant position of the version according to what schema number is found at the same index.

No comments:

Post a Comment