Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilson0x4d/7560750 to your computer and use it in GitHub Desktop.
Save wilson0x4d/7560750 to your computer and use it in GitHub Desktop.
Microsoft.BCL.Build 1.0.13
public class ValidatePackageReferences : Task
{
// Methods
public override bool Execute()
{
if (string.IsNullOrEmpty(this.ReferencingProjectPackagesConfig) && string.IsNullOrEmpty(this.ReferencingProject))
{
this.LogWarning("BCLBUILD1001", Resources.Warning_AllProjectsMissingPackageReference, new object[] { Path.GetFileName(this.ReferencedProject) });
return true;
}
if (!File.Exists(this.ReferencedProjectPackagesConfig))
{
base.Log.LogError(Resources.Error_NoPackagesConfig, new object[] { this.ReferencedProjectPackagesConfig });
return true;
}
if (!File.Exists(this.ReferencingProjectPackagesConfig))
{
base.Log.LogError(Resources.Error_NoPackagesConfig, new object[] { this.ReferencingProjectPackagesConfig });
return true;
}
IEnumerable<Package> packages = this.GetPackages(this.ReferencedProjectPackagesConfig);
if (!packages.Any<Package>())
{
base.Log.LogError(Resources.Error_EmptyPackagesConfig, new object[] { this.ReferencedProjectPackagesConfig });
return true;
}
IEnumerable<Package> source = this.GetPackages(this.ReferencingProjectPackagesConfig);
if (!source.Any<Package>())
{
base.Log.LogError(Resources.Error_EmptyPackagesConfig, new object[] { this.ReferencingProjectPackagesConfig });
return true;
}
using (IEnumerator<Package> enumerator = (from p in packages
where this.ShouldValidatePackage(p.Id)
select p).GetEnumerator())
{
while (enumerator.MoveNext())
{
Func<Package, bool> predicate = null;
Func<Package, bool> func2 = null;
Package referencedProjectPackage = enumerator.Current;
if (predicate == null)
{
predicate = p => (p.Id != null) && (p.Id == referencedProjectPackage.Id);
}
IEnumerable<Package> enumerable3 = source.Where<Package>(predicate);
if (!enumerable3.Any<Package>())
{
this.LogWarning("BCLBUILD1002", Resources.Warning_MissingPackageReference, new object[] { referencedProjectPackage });
}
else
{
if (func2 == null)
{
func2 = p => p.Version.CompareTo(referencedProjectPackage.Version) >= 0;
}
if (!enumerable3.Any<Package>(func2))
{
this.LogWarning("BCLBUILD1003", Resources.Warning_PackageUpgradeNeeded, new object[] { referencedProjectPackage.Id, referencedProjectPackage.Version });
}
}
}
}
return true;
}
private IEnumerable<Package> GetPackages(string packagesConfig)
{
XElement element = XDocument.Load(packagesConfig).Element("packages");
if (element == null)
{
return Enumerable.Empty<Package>();
}
return (from p in element.Elements("package") select new Package(p));
}
private void LogWarning(string keyword, string format, params object[] args)
{
if (this.TreatWarningsAsErrors)
{
base.Log.LogError(null, null, keyword, null, 0, 0, 0, 0, format, args);
}
else
{
base.Log.LogWarning(null, null, keyword, null, 0, 0, 0, 0, format, args);
}
}
private bool ShouldValidatePackage(string packageId)
{
if ((this.Packages != null) && (this.Packages.Length != 0))
{
return this.Packages.Contains<string>(packageId, StringComparer.OrdinalIgnoreCase);
}
return true;
}
// Properties
public string[] Packages { get; set; }
public string ReferencedProject { get; set; }
public string ReferencedProjectPackagesConfig { get; set; }
public string ReferencingProject { get; set; }
public string ReferencingProjectPackagesConfig { get; set; }
public bool TreatWarningsAsErrors { get; set; }
}
Collapse Methods
public class EnsureBindingRedirects : Task
{
// Fields
private static readonly XNamespace asmNamespace = XNamespace.Get("urn:schemas-microsoft-com:asm.v1");
// Methods
private IEnumerable<DependentAssembly> CreateDependentAssembliesForReferences()
{
foreach (ITaskItem iteratorVariable0 in this.References)
{
string metadata = iteratorVariable0.GetMetadata("FullPath");
if (!File.Exists(metadata))
{
throw new FileNotFoundException("Referenced assembly does not exist.", metadata);
}
AssemblyName assemblyName = AssemblyName.GetAssemblyName(metadata);
string iteratorVariable3 = ((assemblyName.CultureInfo == null) || string.IsNullOrEmpty(assemblyName.CultureInfo.Name)) ? "neutral" : assemblyName.CultureInfo.Name;
StringBuilder iteratorVariable4 = assemblyName.GetPublicKeyToken().Aggregate<byte, StringBuilder>(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b));
string iteratorVariable5 = iteratorVariable0.GetMetadata("MinVersion");
BindingRedirect iteratorVariable6 = new BindingRedirect {
Min = string.IsNullOrEmpty(iteratorVariable5) ? new Version(0, 0, 0, 0) : Version.Parse(iteratorVariable5),
Max = assemblyName.Version,
Target = assemblyName.Version
};
DependentAssembly iteratorVariable8 = new DependentAssembly {
Name = assemblyName.Name,
Culture = iteratorVariable3,
PublicKeyToken = iteratorVariable4.ToString(),
Redirect = iteratorVariable6
};
yield return iteratorVariable8;
}
}
private XElement CreateDependentAssemblyElement(DependentAssembly dependentAssembly)
{
XElement element = new XElement((XName) (asmNamespace + "assemblyIdentity"), new XAttribute("name", dependentAssembly.Name));
if (!string.IsNullOrEmpty(dependentAssembly.PublicKeyToken))
{
element.Add(new XAttribute("publicKeyToken", dependentAssembly.PublicKeyToken));
}
if (!string.IsNullOrEmpty(dependentAssembly.Culture))
{
element.Add(new XAttribute("culture", dependentAssembly.Culture));
}
return new XElement((XName) (asmNamespace + "dependentAssembly"), new object[] { element, new XElement((XName) (asmNamespace + "bindingRedirect"), new object[] { new XAttribute("oldVersion", dependentAssembly.Redirect.Range), new XAttribute("newVersion", dependentAssembly.Redirect.Max) }) });
}
private static XElement EnsureElement(XElement parent, XName name)
{
XElement content = parent.Element(name);
if (content == null)
{
content = new XElement(name);
parent.Add(content);
}
return content;
}
public override bool Execute()
{
Func<DependentAssembly, XElement> selector = null;
XDocument document = LoadAppConfig(this.SourceAppConfigPath);
if ((this.References != null) && (this.References.Length > 0))
{
XElement parent = EnsureElement(document.Root, "runtime");
if (selector == null)
{
selector = dependentAssembly => this.CreateDependentAssemblyElement(dependentAssembly);
}
IEnumerable<XElement> source = this.CreateDependentAssembliesForReferences().Select<DependentAssembly, XElement>(selector);
if (source.Any<XElement>())
{
EnsureElement(parent, (XName) (asmNamespace + "assemblyBinding")).AddFirst(source);
}
}
string directoryName = Path.GetDirectoryName(this.DestinationAppConfigPath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
document.Save(this.DestinationAppConfigPath);
return true;
}
private static XDocument LoadAppConfig(string appConfigPath)
{
XDocument document;
if (string.IsNullOrEmpty(appConfigPath))
{
document = new XDocument(new XDeclaration("1.0", "utf-8", "true"), new object[] { new XElement("configuration") });
}
else
{
document = XDocument.Load(appConfigPath, LoadOptions.SetLineInfo);
}
if ((document.Root == null) || (document.Root.Name != "configuration"))
{
ThrowXmlException("App.config must have root configuration element.", null, document.Root);
}
return document;
}
private static void ThrowXmlException(string message, Exception inner, IXmlLineInfo info)
{
throw new XmlException(message, inner, info.LineNumber, info.LinePosition);
}
// Properties
[Output, Required]
public string DestinationAppConfigPath { get; set; }
public ITaskItem[] References { get; set; }
public string SourceAppConfigPath { get; set; }
// Nested Types
[CompilerGenerated]
private sealed class <CreateDependentAssembliesForReferences>d__6 : IEnumerable<EnsureBindingRedirects.DependentAssembly>, IEnumerable, IEnumerator<EnsureBindingRedirects.DependentAssembly>, IEnumerator, IDisposable
{
// Fields
private int <>1__state;
private EnsureBindingRedirects.DependentAssembly <>2__current;
public EnsureBindingRedirects <>4__this;
public int <>7__wrap10;
public ITaskItem[] <>7__wrapf;
public EnsureBindingRedirects.BindingRedirect <>g__initLocal2;
public EnsureBindingRedirects.DependentAssembly <>g__initLocal3;
private int <>l__initialThreadId;
public AssemblyName <assemblyName>5__9;
public string <culture>5__a;
public string <minVersion>5__c;
public StringBuilder <publicKeyToken>5__b;
public EnsureBindingRedirects.BindingRedirect <redirect>5__d;
public ITaskItem <reference>5__7;
public string <referencePath>5__8;
// Methods
[DebuggerHidden]
public <CreateDependentAssembliesForReferences>d__6(int <>1__state)
{
this.<>1__state = <>1__state;
this.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId;
}
private void <>m__Finallye()
{
this.<>1__state = -1;
}
private bool MoveNext()
{
bool flag;
try
{
switch (this.<>1__state)
{
case 0:
this.<>1__state = -1;
this.<>1__state = 1;
this.<>7__wrapf = this.<>4__this.References;
this.<>7__wrap10 = 0;
goto Label_0226;
case 2:
this.<>1__state = 1;
this.<>7__wrap10++;
goto Label_0226;
default:
goto Label_023F;
}
Label_0049:
this.<reference>5__7 = this.<>7__wrapf[this.<>7__wrap10];
this.<referencePath>5__8 = this.<reference>5__7.GetMetadata("FullPath");
if (!File.Exists(this.<referencePath>5__8))
{
throw new FileNotFoundException("Referenced assembly does not exist.", this.<referencePath>5__8);
}
this.<assemblyName>5__9 = AssemblyName.GetAssemblyName(this.<referencePath>5__8);
this.<culture>5__a = ((this.<assemblyName>5__9.CultureInfo == null) || string.IsNullOrEmpty(this.<assemblyName>5__9.CultureInfo.Name)) ? "neutral" : this.<assemblyName>5__9.CultureInfo.Name;
if (EnsureBindingRedirects.CS$<>9__CachedAnonymousMethodDelegate5 == null)
{
EnsureBindingRedirects.CS$<>9__CachedAnonymousMethodDelegate5 = new Func<StringBuilder, byte, StringBuilder>(EnsureBindingRedirects.<CreateDependentAssembliesForReferences>b__4);
}
this.<publicKeyToken>5__b = this.<assemblyName>5__9.GetPublicKeyToken().Aggregate<byte, StringBuilder>(new StringBuilder(), EnsureBindingRedirects.CS$<>9__CachedAnonymousMethodDelegate5);
this.<minVersion>5__c = this.<reference>5__7.GetMetadata("MinVersion");
this.<>g__initLocal2 = new EnsureBindingRedirects.BindingRedirect();
this.<>g__initLocal2.Min = string.IsNullOrEmpty(this.<minVersion>5__c) ? new Version(0, 0, 0, 0) : Version.Parse(this.<minVersion>5__c);
this.<>g__initLocal2.Max = this.<assemblyName>5__9.Version;
this.<>g__initLocal2.Target = this.<assemblyName>5__9.Version;
this.<redirect>5__d = this.<>g__initLocal2;
this.<>g__initLocal3 = new EnsureBindingRedirects.DependentAssembly();
this.<>g__initLocal3.Name = this.<assemblyName>5__9.Name;
this.<>g__initLocal3.Culture = this.<culture>5__a;
this.<>g__initLocal3.PublicKeyToken = this.<publicKeyToken>5__b.ToString();
this.<>g__initLocal3.Redirect = this.<redirect>5__d;
this.<>2__current = this.<>g__initLocal3;
this.<>1__state = 2;
return true;
Label_0226:
if (this.<>7__wrap10 < this.<>7__wrapf.Length)
{
goto Label_0049;
}
this.<>m__Finallye();
Label_023F:
flag = false;
}
fault
{
this.System.IDisposable.Dispose();
}
return flag;
}
[DebuggerHidden]
IEnumerator<EnsureBindingRedirects.DependentAssembly> IEnumerable<EnsureBindingRedirects.DependentAssembly>.GetEnumerator()
{
if ((Thread.CurrentThread.ManagedThreadId == this.<>l__initialThreadId) && (this.<>1__state == -2))
{
this.<>1__state = 0;
return this;
}
return new EnsureBindingRedirects.<CreateDependentAssembliesForReferences>d__6(0) { <>4__this = this.<>4__this };
}
[DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator()
{
return this.System.Collections.Generic.IEnumerable<Roxel.BuildTasks.EnsureBindingRedirects.DependentAssembly>.GetEnumerator();
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
void IDisposable.Dispose()
{
switch (this.<>1__state)
{
case 1:
case 2:
this.<>m__Finallye();
return;
}
}
// Properties
EnsureBindingRedirects.DependentAssembly IEnumerator<EnsureBindingRedirects.DependentAssembly>.Current
{
[DebuggerHidden]
get
{
return this.<>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return this.<>2__current;
}
}
}
private sealed class BindingRedirect
{
// Properties
public Version Max { get; set; }
public Version Min { get; set; }
public string Range
{
get
{
if (!(this.Min == this.Max))
{
return (this.Min + "-" + this.Max);
}
return this.Min.ToString();
}
}
public Version Target { get; set; }
}
private sealed class DependentAssembly
{
// Properties
public string Culture { get; set; }
public string Name { get; set; }
public string PublicKeyToken { get; set; }
public EnsureBindingRedirects.BindingRedirect Redirect { get; set; }
}
}
Collapse Methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment