1- using System ;
2- using JetBrains . Annotations ;
1+ using JetBrains . Annotations ;
2+ using System ;
33
44namespace BenchmarkDotNet . Jobs
55{
6- public abstract class Argument : IEquatable < Argument >
6+ public abstract class Argument : IEquatable < Argument >
77 {
88 [ PublicAPI ] public string TextRepresentation { get ; }
99
@@ -47,13 +47,17 @@ public MonoArgument(string value) : base(value)
4747 [ PublicAPI ]
4848 public class MsBuildArgument : Argument
4949 {
50- // Characters that need to be escaped.
51- // 1. Space
52- // 2. Comma (Special char that is used for separater for value of `-property:{name}={value}` and `-restoreProperty:{name}={value}`)
53- // 3. Other MSBuild special chars (https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-special-characters?view=vs-2022)
54- private static readonly char [ ] MSBuildCharsToEscape = [ ' ' , ',' , '%' , '$' , '@' , '\' ' , '(' , ')' , ';' , '?' , '*' ] ;
50+ // Specisal chars that need to be wrapped with `\"`.
51+ // 1. Comma char (It's used for separater char for `-property:{name}={value}` and `-restoreProperty:{name}={ value}`)
52+ // 2. MSBuild special chars (https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-special-characters?view=vs-2022)
53+ private static readonly char [ ] MSBuildSpecialChars = [ ',' , '%' , '$' , '@' , '\' ' , '(' , ')' , ';' , '?' , '*' ] ;
5554
56- public MsBuildArgument ( string value ) : base ( value ) { }
55+ private readonly bool escapeArgument ;
56+
57+ public MsBuildArgument ( string value , bool escape = false ) : base ( value )
58+ {
59+ escapeArgument = escape ;
60+ }
5761
5862 /// <summary>
5963 /// Gets the MSBuild argument that is used for build script.
@@ -62,6 +66,9 @@ internal string GetEscapedTextRepresentation()
6266 {
6367 var originalArgument = TextRepresentation ;
6468
69+ if ( ! escapeArgument )
70+ return originalArgument ;
71+
6572 // If entire argument surrounded with double quote, returns original argument.
6673 // In this case. MSBuild special chars must be escaped by user. https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-special-characters
6774 if ( originalArgument . StartsWith ( "\" " ) )
@@ -76,41 +83,27 @@ internal string GetEscapedTextRepresentation()
7683 var key = values [ 0 ] ;
7784 var value = values [ 1 ] ;
7885
79- // If value starts with `\` char. It is expected that the escaped value is specified by the user.
80- if ( value . StartsWith ( "\\ " ) )
86+ // If value starts with `\"`.
87+ // It is expected that the escaped value is specified by the user.
88+ if ( value . StartsWith ( "\\ \" " ) )
8189 return originalArgument ;
8290
83- // If value don't contains special chars. return original value .
84- if ( value . IndexOfAny ( MSBuildCharsToEscape ) < 0 )
85- return originalArgument ;
91+ // If value is wrapped with double quote. Trim leading/trailing double quote .
92+ if ( value . StartsWith ( " \" " ) && value . EndsWith ( " \" " ) )
93+ value = value . Trim ( [ '"' ] ) ;
8694
87- return $ "{ key } ={ GetEscapedValue ( value ) } ";
88- }
95+ // Escape chars that need to escaped when wrapped with escaped double quote (`\"`)
96+ value = value . Replace ( " " , "%20" ) // Space
97+ . Replace ( "\" " , "%22" ) // Double Quote
98+ . Replace ( "\\ " , "%5C" ) ; // BackSlash
8999
90- private static string GetEscapedValue ( string value )
91- {
92- // If value starts with double quote. Trim leading/trailing double quote
93- if ( value . StartsWith ( "\" " ) )
94- value = value . Trim ( [ '"' ] ) ;
100+ // If escaped value doesn't contains MSBuild special char, return original argument.
101+ if ( value . IndexOfAny ( MSBuildSpecialChars ) < 0 )
102+ return originalArgument ;
95103
96- bool isWindows = true ;
97- #if NET
98- isWindows = OperatingSystem . IsWindows ( ) ;
99- #endif
100- if ( isWindows )
101- {
102- // On Windows environment.
103- // Returns double-quoted value. (Command line execution and `.bat` file requires escape double quote with `\`)
104- return $ """
105- \"{ value } \"
106- """ ;
107- }
108-
109- // On non-Windows environment.
110- // Returns value that surround with `'"` and `"'`. See: https://github.com/dotnet/sdk/issues/8792#issuecomment-393756980
111- // It requires escape with `\` when running command with `.sh` file. )
104+ // Return escaped value that is wrapped with escaped double quote (`\"`)
112105 return $ """
113- \'\ "{ value } \"\'
106+ { key } =\ "{ value } \"
114107 """ ;
115108 }
116109 }
0 commit comments