@@ -8,6 +8,7 @@ namespace Nerdbank.GitVersioning.Tool
88 using System . Reflection ;
99 using System . Threading ;
1010 using System . Threading . Tasks ;
11+ using Microsoft . Build . Construction ;
1112 using Newtonsoft . Json ;
1213 using NuGet . Common ;
1314 using NuGet . Configuration ;
@@ -16,7 +17,6 @@ namespace Nerdbank.GitVersioning.Tool
1617 using NuGet . Protocol . Core . Types ;
1718 using NuGet . Resolver ;
1819 using Validation ;
19- using MSBuild = Microsoft . Build . Evaluation ;
2020
2121 internal class Program
2222 {
@@ -246,18 +246,12 @@ private static ExitCodes OnInstallCommand(string versionJsonRoot, string version
246246
247247 // Create/update the Directory.Build.props file in the directory of the version.json file to add the NB.GV package.
248248 string directoryBuildPropsPath = Path . Combine ( versionJsonRoot , "Directory.Build.props" ) ;
249- MSBuild . Project propsFile ;
250- if ( File . Exists ( directoryBuildPropsPath ) )
251- {
252- propsFile = new MSBuild . Project ( directoryBuildPropsPath ) ;
253- }
254- else
255- {
256- propsFile = new MSBuild . Project ( ) ;
257- }
249+ ProjectRootElement propsFile = File . Exists ( directoryBuildPropsPath )
250+ ? ProjectRootElement . Open ( directoryBuildPropsPath )
251+ : ProjectRootElement . Create ( directoryBuildPropsPath ) ;
258252
259253 const string PackageReferenceItemType = "PackageReference" ;
260- if ( ! propsFile . GetItemsByEvaluatedInclude ( PackageId ) . Any ( i => i . ItemType == "PackageReference" ) )
254+ if ( ! propsFile . Items . Any ( i => i . ItemType == PackageReferenceItemType && i . Include == PackageId ) )
261255 {
262256 // Validate given sources
263257 foreach ( var source in sources )
@@ -279,14 +273,15 @@ private static ExitCodes OnInstallCommand(string versionJsonRoot, string version
279273 return ExitCodes . PackageIdNotFound ;
280274 }
281275
282- propsFile . AddItem (
276+ var item = propsFile . AddItem (
283277 PackageReferenceItemType ,
284278 PackageId ,
285279 new Dictionary < string , string >
286280 {
287281 { "Version" , packageVersion } ,
288282 { "PrivateAssets" , "all" } ,
289283 } ) ;
284+ item . Condition = "!Exists('packages.config')" ;
290285
291286 propsFile . Save ( directoryBuildPropsPath ) ;
292287 }
0 commit comments