Created
May 18, 2017 05:29
-
-
Save andy-uq/f0ecb1200b8169fdbe8e8e06f0a526cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not yet support lifting the project dependencies to the nuspec.