Skip to content

Instantly share code, notes, and snippets.

@andy-uq
Created May 18, 2017 05:29
Show Gist options
  • Save andy-uq/f0ecb1200b8169fdbe8e8e06f0a526cd to your computer and use it in GitHub Desktop.
Save andy-uq/f0ecb1200b8169fdbe8e8e06f0a526cd to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Namespace>System.Threading.Tasks</Namespace>
</Query>
const string folder = @"C:\Git\_InternalTools\Nephele";
const string assemblyPath = @"bin\debug\net46";
void Main()
{
foreach (var filename in Directory.GetFiles(folder, "*.nuspec", SearchOption.AllDirectories))
{
ProcessNuspec(filename);
}
}
private void ProcessNuspec(string nuspecFilename)
{
var projectFolder = Path.GetDirectoryName(nuspecFilename);
var projectName = Path.GetFileNameWithoutExtension(nuspecFilename);
var csprojFilename = Path.Combine(projectFolder, projectName) + ".csproj";
if (File.Exists(csprojFilename) == false)
{
return;
}
var assemblyFilename = Path.Combine(projectFolder, assemblyPath, projectName) + ".dll";
if (File.Exists(assemblyFilename) == false)
{
Console.WriteLine($"File not found: {assemblyFilename}");
return;
}
UpdateNuspec(nuspecFilename, assemblyFilename);
}
private void UpdateNuspec(string nuspecFilename, string assemblyFilename)
{
var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyFilename);
Console.WriteLine($"Processing {nuspecFilename}");
var nuspec = XDocument.Load(nuspecFilename);
var updatedNuspec = UpdateNuspec(nuspec.XPathSelectElement("package/metadata"), assembly);
if (updatedNuspec == false)
{
Console.WriteLine("No change");
return;
}
Console.WriteLine("Saving updated nuspec");
nuspec.Save(nuspecFilename);
}
private bool UpdateNuspec(XElement nuspec, Assembly assembly)
{
var dirty = false;
var id = assembly.GetName().Name;
var attributes = assembly.GetCustomAttributesData();
dirty |= UpdateNuspec<string>(nuspec, "id", attributes, id);
dirty |= UpdateNuspec<AssemblyTitleAttribute>(nuspec, "title", attributes, id);
dirty |= UpdateNuspec<AssemblyCompanyAttribute>(nuspec, "authors", attributes, id);
dirty |= UpdateNuspec<AssemblyCompanyAttribute>(nuspec, "owners", attributes, id);
dirty |= UpdateNuspec<AssemblyDescriptionAttribute>(nuspec, "description", attributes, id);
dirty |= UpdateNuspec<string>(nuspec, "copyright", attributes, $"Copyright © {GetAttributeValue<AssemblyCompanyAttribute>(attributes, id)} {DateTime.Now.Year}");
return dirty;
}
private bool UpdateNuspec<T>(XElement metadata, string key, IList<CustomAttributeData> attributes, string defaultValue)
{
var node = metadata.Element(key);
if (node == null)
{
return false;
}
var value = GetAttributeValue<T>(attributes, defaultValue);
if (node.Value == value)
{
return false;
}
node.Value = value;
return true;
}
private string GetAttributeValue<T>(IList<CustomAttributeData> attributes, string defaultValue)
{
return attributes.FirstOrDefault(a => a.AttributeType == typeof(T))?.ConstructorArguments[0].Value.ToString() ?? defaultValue;
}
@andy-uq
Copy link
Author

andy-uq commented May 18, 2017

This does not yet support lifting the project dependencies to the nuspec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment