@@ -134,7 +134,7 @@ if not needed and play nicely when building incrementally.
134
134
****************************************************************************************************
135
135
NuGetPack: Creates a nuget package.
136
136
**************************************************************************************************** -->
137
- <Target Name =" NuGetPack" DependsOnTargets =" EnsureNuGet;GenerateNuSpecFile;ResolveReferences" Condition =" '$(OutputsOutdated)' == 'true'" >
137
+ <Target Name =" NuGetPack" DependsOnTargets =" EnsureNuGet;GenerateNuSpecFile;ValidateDependencies; ResolveReferences" Condition =" '$(OutputsOutdated)' == 'true'" >
138
138
<PropertyGroup >
139
139
<NuSpecProperties >$(NuSpecProperties.Trim(';'))</NuSpecProperties >
140
140
<NuGetPackOptions Condition =" '$(NuGetPackOptions)' == '' AND '$(NuGetPackSymbols)' != 'true'" >-NoPackageAnalysis</NuGetPackOptions >
@@ -376,4 +376,95 @@ NormalizeNuGetContent: Normalize NuGetContent items full path and destination fo
376
376
<Target Name =" EnsureNuGet" >
377
377
<DownloadNuGet OutputFilename =" $(NuGetExePath)" Condition =" !Exists('$(NuGetExePath)')" />
378
378
</Target >
379
+
380
+ <!--
381
+ ==================================================================================================
382
+ Verifies that dependencies listed in the nuspec match actual packages.config from the project
383
+ ==================================================================================================-->
384
+ <UsingTask TaskName =" ValidateNugetDependencies" TaskFactory =" CodeTaskFactory" AssemblyFile =" $(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
385
+ <ParameterGroup >
386
+ <ProjectReferences Required =" true" ParameterType =" Microsoft.Build.Framework.ITaskItem[]" />
387
+ <NuSpecFile Required =" true" ParameterType =" System.String" />
388
+ </ParameterGroup >
389
+ <Task >
390
+ <Reference Include =" System.Core" />
391
+ <Reference Include =" System.Xml" />
392
+ <Reference Include =" System.Xml.Linq" />
393
+ <Using Namespace =" System" />
394
+ <Using Namespace =" System.IO" />
395
+ <Using Namespace =" System.Net" />
396
+ <Using Namespace =" System.Linq" />
397
+ <Using Namespace =" System.Xml.Linq" />
398
+ <Using Namespace =" Microsoft.Build.Framework" />
399
+ <Using Namespace =" Microsoft.Build.Utilities" />
400
+ <Code Type =" Fragment" Language =" cs" >
401
+ <![CDATA[
402
+ try
403
+ {
404
+ if (ProjectReferences.Length != 1)
405
+ {
406
+ throw new InvalidOperationException("The project must contain exactly one project reference. It contains " + ProjectReferences.Length);
407
+ }
408
+ if(!File.Exists(NuSpecFile))
409
+ {
410
+ throw new InvalidOperationException("The generated nuspec file was not found.");
411
+ }
412
+
413
+ string projectFile = Path.GetFullPath(ProjectReferences[0].ItemSpec);
414
+ string sourceFolder = Path.GetDirectoryName(projectFile);
415
+ string packagesFile = Path.Combine(sourceFolder, "packages.config");
416
+
417
+ var nuspecFile = Path.GetFullPath(NuSpecFile);
418
+ XElement nuspecXml = XElement.Load(nuspecFile);
419
+ XNamespace nuspecNamespace = nuspecXml.Name.Namespace;
420
+ var nuspecDependencyNodes = nuspecXml.Descendants(nuspecNamespace + "dependency");
421
+
422
+ XElement packageConfigXml = XElement.Load(packagesFile);
423
+ XNamespace packageConfigNamespace = packageConfigXml.Name.Namespace;
424
+
425
+ foreach (var dependency in nuspecDependencyNodes)
426
+ {
427
+ var nugetPackage = dependency.Attribute("id").Value;
428
+ var version = dependency.Attribute("version").Value;
429
+
430
+ // skip if it has replacements
431
+ if(version.Contains("$"))
432
+ {
433
+ continue;
434
+ }
435
+
436
+ // check the packages.config for a matching id
437
+ var matchingElement = packageConfigXml.Descendants(packageConfigNamespace + "package")
438
+ .Where(p => p.Attribute("id").Value == nugetPackage).SingleOrDefault();
439
+
440
+ if (matchingElement == null)
441
+ {
442
+ throw new InvalidOperationException(string.Format("The package {0} was defined in the nuspec file {1} but not found in the {2}", nugetPackage, nuspecFile, packagesFile));
443
+ }
444
+
445
+ string packageConfigVersion = matchingElement.Attribute("version").Value;
446
+
447
+ if (packageConfigVersion != version)
448
+ {
449
+ throw new InvalidOperationException(string.Format("The file {0} declares a dependency {1} with a version of {2}. However the file {3} delcares the same package with a version {4}. Update the nuspec file to the correct version.",
450
+ nuspecFile, nugetPackage, version, packagesFile, packageConfigVersion));
451
+ }
452
+ }
453
+
454
+ return true;
455
+ }
456
+ catch (Exception ex)
457
+ {
458
+ Log.LogErrorFromException(ex);
459
+ return false;
460
+ }
461
+ ]]>
462
+ </Code >
463
+ </Task >
464
+ </UsingTask >
465
+
466
+ <Target Name =" ValidateDependencies" >
467
+ <ValidateNugetDependencies ProjectReferences =" @(ProjectReference)" NuSpecFile =" $(GeneratedNuSpec)" />
468
+ </Target >
469
+
379
470
</Project >
0 commit comments