Skip to content

Instantly share code, notes, and snippets.

@andy-uq
Created May 18, 2017 21:35
Show Gist options
  • Save andy-uq/0c4a1949a653fd546991e0c2630225b7 to your computer and use it in GitHub Desktop.
Save andy-uq/0c4a1949a653fd546991e0c2630225b7 to your computer and use it in GitHub Desktop.
Updates a nuspec to reflect the references in the csproj
foreach (var csproj in Directory.GetFiles(@"C:\Git\_InternalTools\Nephele", "*.csproj", SearchOption.AllDirectories))
{
var nuspecFileInfo = new FileInfo(Path.ChangeExtension(csproj, ".nuspec"));
if (!nuspecFileInfo.Exists)
continue;
Console.WriteLine($"Inspecting {nuspecFileInfo.Name}");
var xNuspec = XDocument.Load(nuspecFileInfo.FullName);
var xMetaData = xNuspec.XPathSelectElement("package/metadata");
var xDependencies = xMetaData.XPathSelectElement("dependencies");
bool dirty = false;
var xcsProj = XDocument.Load(csproj);
var xPackageReferences = xcsProj.XPathSelectElements("/Project/ItemGroup/PackageReference[not(PrivateAssets[text()='all'])]");
var xProjectReferences = xcsProj.XPathSelectElements("/Project/ItemGroup/ProjectReference");
if (xPackageReferences.Any() || xProjectReferences.Any())
{
if (xDependencies == null)
{
xDependencies = new XElement("dependencies");
xMetaData.Add(xDependencies);
Console.WriteLine("> Added dependencies node");
dirty = true;
}
foreach (var xReference in xPackageReferences)
{
string name = (string )xReference.Attribute("Include");
string version = (string )xReference.Attribute("Version");
var xDependency = xDependencies.XPathSelectElement($"dependency[@id='{name}']");
if (xDependency == null)
{
xDependency = new XElement("dependency", new XAttribute("id", name), new XAttribute("version", version));
xDependencies.Add(xDependency);
Console.WriteLine($"> Added dependency {name} {version}");
dirty = true;
continue;
}
var xVersion = xDependency.Attribute("version");
if ((string)xVersion != version)
{
xVersion.Value = version;
Console.WriteLine($"> Updated dependency {name} {version}");
dirty = true;
}
}
foreach (var xReference in xProjectReferences)
{
var internalNuspec = Path.ChangeExtension(Path.Combine(Path.GetDirectoryName(csproj), (string)xReference.Attribute("Include")), ".nuspec");
if (!File.Exists(internalNuspec))
{
continue;
}
string name = System.IO.Path.GetFileNameWithoutExtension((string)xReference.Attribute("Include"));
var xDependency = xDependencies.XPathSelectElement($"dependency[@id='{name}']");
if (xDependency == null)
{
xDependency = new XElement("dependency", new XAttribute("id", name), new XAttribute("version", "$version$"));
xDependencies.Add(xDependency);
Console.WriteLine($"> Added internal dependency {name}");
dirty = true;
continue;
}
var xVersion = xDependency.Attribute("version");
if ((string)xVersion != "$version$")
{
xVersion.Value = "$version$";
Console.WriteLine($"> Updated dependency {name} to internal");
dirty = true;
}
}
}
bool IsPackageReference(string name, string version) => xcsProj.XPathSelectElement($"/Project/ItemGroup/PackageReference[@Include='{name}' and @Version='{version}' and not(PrivateAssets[text()='all'])]") != null;
bool IsProjectReference(string name, string version) => version == "$version$"
&& xcsProj.XPathSelectElements($"/Project/ItemGroup/ProjectReference")
.Select(x => (string)x.Attribute("Include"))
.Select(x => System.IO.Path.GetFileNameWithoutExtension(x))
.Contains(name, StringComparer.OrdinalIgnoreCase);
if (xDependencies != null)
{
foreach (var xDependency in xDependencies.XPathSelectElements("dependency").ToList())
{
string name = (string)xDependency.Attribute("id");
string version = (string)xDependency.Attribute("version");
if (IsPackageReference(name, version) || IsProjectReference(name, version))
continue;
Console.WriteLine($"> Removed dependency {name} {version}");
xDependency.Remove();
dirty = true;
}
if (!xDependencies.Elements().Any())
{
xDependencies.Remove();
Console.WriteLine($"> Dropped all dependencies");
dirty = true;
}
}
if (dirty)
{
xNuspec.Save(nuspecFileInfo.FullName);
Console.WriteLine($"Updated {nuspecFileInfo.FullName}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment