0

The following two inline yaml-pipeline powershell scripts:

- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "2.0.0.0", '$(major).$(minor).$(patch).0'} | set-content -path $(versionHeader)
  displayName: 'Update build number in header file'

- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "20200101000000", (get-date -f 'yyyyMMddhhmmss')} | set-content -path $(versionHeader)
  displayName: 'Update date in header file'

are meant to take these two lines

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.0.0")]                                   
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200101000000")]

and turn them into these two lines (i.e. put new values in the quotes)

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.185.0")] 
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200724013502")]

(The replacement values vary)

And either script works fine by itself. But when I try to use both scripts, one after the other, the second value comes out messed up.

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.209.0")]                                // correct
[assembly: MyApp.Net.Attributes.BuildDateAttribute("202.0.209.000000")] // ?????

Obviously they are somehow interfering with each other but I don't know how. Can someone tell me what I'm doing wrong?

Joe
  • 5,394
  • 3
  • 23
  • 54
  • I should say that I just found that if I reverse the order of the two scripts, it appears to work as desired. So I guess my problem is "solved" But I would still like to understand what is wrong with the other order – Joe Jul 24 '20 at 16:45

1 Answers1

1

The problem is in powershell's -replace - it interprets wildcards, and it happens that 20200101000000 matches 2.0.0.0:

PS> "20200101000000" -replace "2.0.0.0", "zzzzzz"
20zzzzzz00000
qbik
  • 5,502
  • 2
  • 27
  • 33
  • Thank you for your explanation. Do you know if there is some option to replace that can force it to treat things literally and *not* use wildcards? – Joe Dec 15 '20 at 07:30
  • 1
    I don't think `-replace` has something like that, but you can use the built-in [String.Replace](https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-5.0), like this: `"20200101000000".Replace("2.0.0.0", "zzzzzz")`. This will not interpret wildcards. – qbik Dec 17 '20 at 22:08