@@ -60,11 +60,14 @@ protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
60
60
newArgs . Add ( $ "/p:ValueName={ _valueName } ") ;
61
61
newArgs . AddRange ( args ) ;
62
62
63
- // Override build target to write out DefineConstants value to a file in the output directory
64
63
Directory . CreateDirectory ( GetBaseIntermediateDirectory ( ) . FullName ) ;
65
- string injectTargetPath = Path . Combine (
64
+ string customAfterDirectoryBuildTargetsPath = Path . Combine (
66
65
GetBaseIntermediateDirectory ( ) . FullName ,
67
- Path . GetFileName ( ProjectFile ) + ".WriteValuesToFile.g.targets" ) ;
66
+ "Custom.After.Directory.Build.targets" ) ;
67
+
68
+ var project = XDocument . Load ( ProjectFile ) ;
69
+
70
+ var ns = project . Root . Name . Namespace ;
68
71
69
72
string linesAttribute ;
70
73
if ( _valueType == ValueType . Property )
@@ -80,35 +83,53 @@ protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
80
83
}
81
84
}
82
85
83
- string propertiesElement = "" ;
86
+ var propertyGroup = project . Root . Elements ( ns + "PropertyGroup" ) . FirstOrDefault ( ) ;
87
+
88
+ if ( propertyGroup == null )
89
+ {
90
+ propertyGroup = new XElement ( ns + "PropertyGroup" ) ;
91
+ project . Root . AddAfterSelf ( propertyGroup ) ;
92
+ }
93
+
94
+ propertyGroup . Add ( new XElement ( ns + "CustomAfterDirectoryBuildTargets" , $ "$(CustomAfterDirectoryBuildTargets);{ customAfterDirectoryBuildTargetsPath } ") ) ;
95
+ propertyGroup . Add ( new XElement ( ns + "CustomAfterMicrosoftCommonCrossTargetingTargets" , $ "$(CustomAfterMicrosoftCommonCrossTargetingTargets);{ customAfterDirectoryBuildTargetsPath } ") ) ;
96
+
97
+ project . Save ( ProjectFile ) ;
98
+
99
+ var customAfterDirectoryBuildTargets = new XDocument ( new XElement ( ns + "Project" ) ) ;
100
+
101
+ var target = new XElement ( ns + "Target" ,
102
+ new XAttribute ( "Name" , TargetName ) ,
103
+ ShouldCompile ? new XAttribute ( "DependsOnTargets" , DependsOnTargets ) : null ) ;
104
+
105
+ customAfterDirectoryBuildTargets . Root . Add ( target ) ;
106
+
84
107
if ( Properties . Count != 0 )
85
108
{
86
- propertiesElement += "<PropertyGroup>\n " ;
109
+ propertyGroup = new XElement ( ns + "PropertyGroup" ) ;
110
+ customAfterDirectoryBuildTargets . Root . Add ( propertyGroup ) ;
111
+
87
112
foreach ( var pair in Properties )
88
113
{
89
- propertiesElement += $ " < { pair . Key } > { pair . Value } </ { pair . Key } > \n " ;
114
+ propertyGroup . Add ( new XElement ( ns + pair . Key , pair . Value ) ) ;
90
115
}
91
- propertiesElement += " </PropertyGroup>" ;
92
116
}
93
117
94
- string injectTargetContents =
95
- $@ "<Project ToolsVersion=`14.0` xmlns=`http://schemas.microsoft.com/developer/msbuild/2003`>
96
- { propertiesElement }
97
- <Target Name=`{ TargetName } ` { ( ShouldCompile ? $ "DependsOnTargets=`{ DependsOnTargets } `" : "" ) } >
98
- <ItemGroup>
99
- <LinesToWrite Include=`{ linesAttribute } `/>
100
- </ItemGroup>
101
- <WriteLinesToFile
102
- File=`bin\$(Configuration)\$(TargetFramework)\{ _valueName } Values.txt`
103
- Lines=`@(LinesToWrite)`
104
- Overwrite=`true`
105
- Encoding=`Unicode`
106
- />
107
- </Target>
108
- </Project>" ;
109
- injectTargetContents = injectTargetContents . Replace ( '`' , '"' ) ;
110
-
111
- File . WriteAllText ( injectTargetPath , injectTargetContents ) ;
118
+ var itemGroup = new XElement ( ns + "ItemGroup" ) ;
119
+ target . Add ( itemGroup ) ;
120
+
121
+ itemGroup . Add (
122
+ new XElement ( ns + "LinesToWrite" ,
123
+ new XAttribute ( "Include" , linesAttribute ) ) ) ;
124
+
125
+ target . Add (
126
+ new XElement ( ns + "WriteLinesToFile" ,
127
+ new XAttribute ( "File" , $@ "bin\$(Configuration)\$(TargetFramework)\{ _valueName } Values.txt") ,
128
+ new XAttribute ( "Lines" , "@(LinesToWrite)" ) ,
129
+ new XAttribute ( "Overwrite" , bool . TrueString ) ,
130
+ new XAttribute ( "Encoding" , "Unicode" ) ) ) ;
131
+
132
+ customAfterDirectoryBuildTargets . Save ( customAfterDirectoryBuildTargetsPath ) ;
112
133
113
134
var outputDirectory = GetValuesOutputDirectory ( _targetFramework ) ;
114
135
outputDirectory . Create ( ) ;
0 commit comments