Reading mulltiline XML tag to variable does not work

murarialex's Avatar

murarialex

03 Jun, 2020 02:25 AM

Hi,

Consider the following code:

Directory.build.props

<Project>
  <PropertyGroup>
    <PackageReleaseNotes>
      Alpha release.
    </PackageReleaseNotes>
  </PropertyGroup>
</Project>

Before build script:

before_build:
  - ps: |
        $props = ([xml](Get-Content "Directory.Build.props")).Project.PropertyGroup
        $env:releaseNotes = $props.PackageReleaseNotes

Deploy script:

deploy:
  - provider: GitHub
    tag: $(APPVEYOR_REPO_TAG_NAME)
    description: $(releaseNotes)
    draft: false

If the XML tag "PackageReleaseNotes" is a single liner, everything works perfectly, now if it is multiline, like the example above, the variable is considered null and the output is "$(releaseNotes)" instead of "Alpha release.".

  1. Support Staff 1 Posted by Feodor Fitsner on 03 Jun, 2020 03:45 AM

    Feodor Fitsner's Avatar

    The code looks good to me. Try to output $env:releaseNotes in before_deploy?

  2. 2 Posted by murarialex on 03 Jun, 2020 01:03 PM

    murarialex's Avatar

    Did it, same bahavior.

    When XML tag PackageReleaseNotes is single line it is displayed correctly:
    https://ci.appveyor.com/project/alexmurari/repositive/builds/332911...

    But when the tag is multiline, nothing is displayed (like if the variable is null):
    https://ci.appveyor.com/project/alexmurari/repositive/builds/332911...

    So XML tags with multiline values are really a problem.

  3. Support Staff 3 Posted by Feodor Fitsner on 03 Jun, 2020 05:03 PM

    Feodor Fitsner's Avatar

    OK, now I see what you mean, thanks for additional info!

    Right, environment variables cannot contain new line characters, therefore the following should work in all cases:

    $env:releaseNotes = $props.PackageReleaseNotes.Trim()
    
  4. 4 Posted by murarialex on 03 Jun, 2020 06:56 PM

    murarialex's Avatar

    Thanks for the response!

    It works, but only the first line is preserved.

    If I have something like this:

    <PackageReleaseNotes>
      - Alpha release.
      - Changed Foo
      - Fixed Bar.
    </PackageReleaseNotes>
    

    The variable only has "-Alpha Release".

    There is any way of preserving the full content? It would be really powerful to pass this information to the deploy phase so you could create releases with filled descriptions automatically. Just a wild guess: it supports things like "HTML style line-breaks (br)" or "\n" or something like that to preserve content and line break?

  5. Support Staff 5 Posted by Feodor Fitsner on 03 Jun, 2020 07:04 PM

    Feodor Fitsner's Avatar

    You are right! description supports \n in it that will be replaced with the a real LF during the deployment. You need this:

    $env:releaseNotes = $props.PackageReleaseNotes.Trim().Replace("`n", '\n')
    
  6. 6 Posted by murarialex on 03 Jun, 2020 10:52 PM

    murarialex's Avatar

    Thanks, it works!!

    Only thing is that I can't keep the value inside the tag indented, as the indentation whitespaces are misunderstood and all values ends up with leading whitespaces. But that's a PowerShell limitation and the solution is refrain from indenting the values. No problem at all.

    <Project>
      <PropertyGroup>
        <PackageReleaseNotes>
    - Alpha release.
    - Foo
    - Bar
        </PackageReleaseNotes>
      </PropertyGroup>
    </Project>
    

    Thanks a lot!

  7. Support Staff 7 Posted by Feodor Fitsner on 04 Jun, 2020 05:49 AM

    Feodor Fitsner's Avatar

    You can have as complex PS logic processing the contents of that element as you need.

  8. murarialex closed this discussion on 05 Jun, 2020 11:14 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

02 Jul, 2020 03:24 PM
01 Jul, 2020 08:32 PM
01 Jul, 2020 01:12 PM
30 Jun, 2020 04:26 PM
25 Jun, 2020 05:54 PM