@@ -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 Nerdbank . GitVersioning . LibGit2 ;
1213 using Newtonsoft . Json ;
1314 using NuGet . Common ;
@@ -17,7 +18,6 @@ namespace Nerdbank.GitVersioning.Tool
1718 using NuGet . Protocol . Core . Types ;
1819 using NuGet . Resolver ;
1920 using Validation ;
20- using MSBuild = Microsoft . Build . Evaluation ;
2121
2222 internal class Program
2323 {
@@ -261,18 +261,12 @@ private static ExitCodes OnInstallCommand(string versionJsonRoot, string version
261261
262262 // Create/update the Directory.Build.props file in the directory of the version.json file to add the NB.GV package.
263263 string directoryBuildPropsPath = Path . Combine ( versionJsonRoot , "Directory.Build.props" ) ;
264- MSBuild . Project propsFile ;
265- if ( File . Exists ( directoryBuildPropsPath ) )
266- {
267- propsFile = new MSBuild . Project ( directoryBuildPropsPath ) ;
268- }
269- else
270- {
271- propsFile = new MSBuild . Project ( ) ;
272- }
264+ ProjectRootElement propsFile = File . Exists ( directoryBuildPropsPath )
265+ ? ProjectRootElement . Open ( directoryBuildPropsPath )
266+ : ProjectRootElement . Create ( directoryBuildPropsPath ) ;
273267
274268 const string PackageReferenceItemType = "PackageReference" ;
275- if ( ! propsFile . GetItemsByEvaluatedInclude ( PackageId ) . Any ( i => i . ItemType == "PackageReference" ) )
269+ if ( ! propsFile . Items . Any ( i => i . ItemType == PackageReferenceItemType && i . Include == PackageId ) )
276270 {
277271 // Validate given sources
278272 foreach ( var source in sources )
@@ -294,14 +288,15 @@ private static ExitCodes OnInstallCommand(string versionJsonRoot, string version
294288 return ExitCodes . PackageIdNotFound ;
295289 }
296290
297- propsFile . AddItem (
291+ var item = propsFile . AddItem (
298292 PackageReferenceItemType ,
299293 PackageId ,
300294 new Dictionary < string , string >
301295 {
302296 { "Version" , packageVersion } ,
303297 { "PrivateAssets" , "all" } ,
304298 } ) ;
299+ item . Condition = "!Exists('packages.config')" ;
305300
306301 propsFile . Save ( directoryBuildPropsPath ) ;
307302 }
0 commit comments