Operations with strings

Did you know: v0.12.10+ has multiline support for MSBuild expressions via MSBuildComponent:

 #[$(
    [System.TimeSpan]::FromTicks('$(
        [MSBuild]::Subtract(
        $(tNow), 
        $(tBase))
    )')
    .TotalMinutes
    .ToString('0'))]

Concatenation

v0.12.8+ Contains syntactic sugar +=:

$(desc = "Hello ")
$(desc += "world !")

or you can use:

$(desc = "Hello ")
$(desc = $([System.String]::Concat($(desc), "world !")) )

Remove newline characters and other problematic symbols

Some your results may contain a some problematic characters for different functions. In most cases this applies to MSBuild Property Functions.

You can use System.String static methods if you can't manually change your data.

For example:

$(cs.Replace("\r\n", ""))

or with different combination CR/LF (Newline - representations)

$(cs.Replace("\r", "").Replace("\n", ""))

So, if you have a multiline value in your variable projectRev:

#[var cs = Version is a %ver% !] 

#[var projectRev = v1.2
debug 
rev321]

you can use the Replace() method for changing on any compatible sequence, e.g.:

#[var projectRev = $(projectRev.Replace("\r\n", " :: "))]
#[var cs = $(cs.Replace("%ver%", "#[var projectRev]"))]

or as variant:

#[var cs = $(cs.Replace("%ver%", $(projectRev.Replace("\r\n", " :: "))))]

and similar…

note:

  • escape \\r \\n if need
  • use \x00 - \xFF for other chars

as result we have:

Version is a v1.2 :: debug  :: rev321 !

Escape-Sequences

You can use available escape-sequence in SBE-Scripts & MSBuild cores, for example:

$([System.String]::Concat("\r\n"))
$(ver = "1.2.3")
$([System.String]::Format("\t version is a {0}", $(ver)))

What's available ?

Currently used a strictly limited set:

Tricks for the longest string arguments

If you need to pass a long string as argument for some function or method, for example:

#[File sout("cmd", "/C cd \"#[var pDir]bin/#[var cfg]/\" & xcopy *.dll \"#[var nupCIMdir]\bin\" /Y/R/I & xcopy NLog.dll.nlog \"#[var nupCIMdir]\bin\" /Y/R/I")]

The best way to use the User-Variables via MSBuild or SBE-Scripts core (see also UserVariableComponent)

$(src = "$(pDir)bin/$(cfg)/")
$(dir = "$(nupCIMdir)\bin\\")
#[var cim = xcopy NLog.dll.nlog \"$(nupCIMdir)\bin\" ]
...

other tricks

Dynamic evaluation with both engines MSBuild & SBE-Scripts

Not all values from strings may be automatically evaluated beetween different engines.

You also should remeber behaviour of strings for User-variables in MSBuild core

v0.12.6+ Allows evaluation of string arguments with MSBuild engine in File/Function/BuildComponent + some newer. You can also use the MSBuildComponent to force evaluation if still needed.

For example:

#[Func hash.SHA1("$([System.Guid]::NewGuid())")]

For line above:

  • v0.12.5 or less, always returns value - 2A2C9B690E475D713B35BD1FB8A1AB7F214121C6, because the SHA1 method has looked first argument 'as is' - $([System.Guid]::NewGuid())

The result above is not correct if you want evaluate this $([System.Guid]::NewGuid()).

To force evaluation of similar, you should for example, use throught variable with this engine:

#[var myvar = $([System.Guid]::NewGuid())]
...
#[Func hash.SHA1("#[var myvar]")]

compact variant, may be:

#[Func hash.SHA1("#[var _=$([System.Guid]::NewGuid())]#[var _]")]

and similar..

Whitespaces from all script results

Use variables as main container to avoid any spaces if needed.

#[var name = 

    ... all of what you want ...
]

Example:

#[var nul = 

    #["
        Basic example
    "]
    #[var v = 1.2.3]
    #[var log = $(TMP)/v.txt]

    #[($(Configuration) ~= Deb || true)
    {
        #[var tBase     = $([System.DateTime]::Parse('2015/10/01').ToBinary())]
        #[var tNow      = $([System.DateTime]::UtcNow.Ticks)]
        #[var revBuild  = #[$(
                            [System.TimeSpan]::FromTicks('$(
                                [MSBuild]::Subtract(
                                $(tNow), 
                                $(tBase))
                            )')
                            .TotalMinutes
                            .ToString('0'))]]
        
        #[var v = $(v).$([MSBuild]::Modulo($(revBuild), $([System.Math]::Pow(2, 14))))]
    }]

    #[var v = $([System.String]::Format("v{0}\r\n\t", $(v)))]
    #[File write("#[var log]"):> Example #[var v] Generated by vsSolutionBuildEvent]
    #[IO scall("notepad", "#[var log]")]

    $(n = $([System.Math]::Exp('$([MSBuild]::Multiply($([System.Math]::Log(2)), 16))')))

]$(n)

References

MSBuild Property Functions - use any static method or property of these system classes: