1
- using System ;
2
- using JetBrains . Annotations ;
1
+ using JetBrains . Annotations ;
2
+ using System ;
3
3
4
4
namespace BenchmarkDotNet . Jobs
5
5
{
6
- public abstract class Argument : IEquatable < Argument >
6
+ public abstract class Argument : IEquatable < Argument >
7
7
{
8
8
[ PublicAPI ] public string TextRepresentation { get ; }
9
9
@@ -47,13 +47,17 @@ public MonoArgument(string value) : base(value)
47
47
[ PublicAPI ]
48
48
public class MsBuildArgument : Argument
49
49
{
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 = [ ',' , '%' , '$' , '@' , '\' ' , '(' , ')' , ';' , '?' , '*' ] ;
55
54
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
+ }
57
61
58
62
/// <summary>
59
63
/// Gets the MSBuild argument that is used for build script.
@@ -62,6 +66,9 @@ internal string GetEscapedTextRepresentation()
62
66
{
63
67
var originalArgument = TextRepresentation ;
64
68
69
+ if ( ! escapeArgument )
70
+ return originalArgument ;
71
+
65
72
// If entire argument surrounded with double quote, returns original argument.
66
73
// In this case. MSBuild special chars must be escaped by user. https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-special-characters
67
74
if ( originalArgument . StartsWith ( "\" " ) )
@@ -76,41 +83,27 @@ internal string GetEscapedTextRepresentation()
76
83
var key = values [ 0 ] ;
77
84
var value = values [ 1 ] ;
78
85
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 ( "\\ \" " ) )
81
89
return originalArgument ;
82
90
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 ( [ '"' ] ) ;
86
94
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
89
99
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 ;
95
103
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 (`\"`)
112
105
return $ """
113
- \'\ "{ value } \"\'
106
+ { key } =\ "{ value } \"
114
107
""" ;
115
108
}
116
109
}
0 commit comments