Recommit /repackdrop option support

This commit is contained in:
WMJ 2018-03-05 08:36:16 +08:00
Родитель 279afd3579
Коммит 168e48e6a4
3 изменённых файлов: 77 добавлений и 51 удалений

9
.editorconfig Normal file
Просмотреть файл

@ -0,0 +1,9 @@
[*]
end_of_line = crlf
[*.cs]
indent_style = space
indent_size = 4
csharp_new_line_before_open_brace = all

Просмотреть файл

@ -1,10 +1,11 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 15
VisualStudioVersion = 14.0.25420.1 VisualStudioVersion = 15.0.27130.2036
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4547EE79-6D49-4318-B473-B5BBF73B434C}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4547EE79-6D49-4318-B473-B5BBF73B434C}"
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
build.gradle = build.gradle build.gradle = build.gradle
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection
@ -139,4 +140,7 @@ Global
{6A827942-BEEF-4E49-A3A4-306E21428E85} = {04819B25-ABEA-46F7-90D5-149C8304F67F} {6A827942-BEEF-4E49-A3A4-306E21428E85} = {04819B25-ABEA-46F7-90D5-149C8304F67F}
{EECC6459-78C6-4A08-9039-DE37E75866E6} = {04819B25-ABEA-46F7-90D5-149C8304F67F} {EECC6459-78C6-4A08-9039-DE37E75866E6} = {04819B25-ABEA-46F7-90D5-149C8304F67F}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {54C7CB02-6231-428F-BBD0-113CC9852908}
EndGlobalSection
EndGlobal EndGlobal

Просмотреть файл

@ -119,11 +119,12 @@ namespace ILRepacking
public TypeDefinition Import(TypeDefinition type, Collection<TypeDefinition> col, bool internalize) public TypeDefinition Import(TypeDefinition type, Collection<TypeDefinition> col, bool internalize)
{ {
_logger.Verbose("- Importing " + type); _logger.Verbose("- Importing " + type);
bool hasFilter = String.IsNullOrEmpty(_options.RepackDropAttribute) == false; bool hasFilter = String.IsNullOrEmpty(_options.RepackDropAttribute) == false;
if (hasFilter && ShouldDrop(type)) { if (hasFilter && ShouldDrop(type))
return null; {
} return null;
}
TypeDefinition nt = _repackContext.TargetAssemblyMainModule.GetType(type.FullName); TypeDefinition nt = _repackContext.TargetAssemblyMainModule.GetType(type.FullName);
bool justCreatedType = false; bool justCreatedType = false;
@ -160,55 +161,67 @@ namespace ILRepacking
_repackContext.MappingHandler.StoreRemappedType(type, nt); _repackContext.MappingHandler.StoreRemappedType(type, nt);
// nested types first (are never internalized) // nested types first (are never internalized)
foreach (TypeDefinition nested in type.NestedTypes) { foreach (TypeDefinition nested in type.NestedTypes)
if (hasFilter && ShouldDrop(nested) == false) { {
Import(nested, nt.NestedTypes, false); if (hasFilter == false || ShouldDrop(nested) == false)
} {
} Import(nested, nt.NestedTypes, false);
foreach (FieldDefinition field in type.Fields) { }
if (hasFilter && ShouldDrop(field) == false) { }
CloneTo(field, nt); foreach (FieldDefinition field in type.Fields)
} {
} if (hasFilter == false || ShouldDrop(field) == false)
// methods before fields / events {
foreach (MethodDefinition meth in type.Methods) { CloneTo(field, nt);
if (hasFilter && ShouldDrop(meth) == false) { }
CloneTo(meth, nt, justCreatedType); }
} // methods before fields / events
} foreach (MethodDefinition meth in type.Methods)
foreach (EventDefinition evt in type.Events) { {
if (hasFilter && ShouldDrop(evt) == false) { if (hasFilter == false || ShouldDrop(meth) == false)
CloneTo(evt, nt, nt.Events); {
} CloneTo(meth, nt, justCreatedType);
} }
foreach (PropertyDefinition prop in type.Properties) { }
if (hasFilter && ShouldDrop(prop) == false) { foreach (EventDefinition evt in type.Events)
CloneTo(prop, nt, nt.Properties); {
} if (hasFilter == false || ShouldDrop(evt) == false)
} {
CloneTo(evt, nt, nt.Events);
}
}
foreach (PropertyDefinition prop in type.Properties)
{
if (hasFilter == false || ShouldDrop(prop) == false)
{
CloneTo(prop, nt, nt.Properties);
}
}
return nt; return nt;
} }
private bool ShouldDrop<TMember>(TMember member) where TMember : ICustomAttributeProvider, IMemberDefinition { private bool ShouldDrop<TMember>(TMember member) where TMember : ICustomAttributeProvider, IMemberDefinition
// skip members marked with a custom attribute named as /repackdrop:RepackDropAttribute {
var shouldDrop = member.HasCustomAttributes // skip members marked with a custom attribute named as /repackdrop:RepackDropAttribute
&& member.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.Name == _options.RepackDropAttribute) != null; var shouldDrop = member.HasCustomAttributes
if (shouldDrop) { && member.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.Name == _options.RepackDropAttribute) != null;
_logger.Log("Repack dropped " + typeof(TMember).Name + ": " + member.FullName + " as it was marked with "+ _options.RepackDropAttribute); if (shouldDrop)
} {
return shouldDrop; _logger.Log("Repack dropped " + typeof(TMember).Name + ": " + member.FullName + " as it was marked with " + _options.RepackDropAttribute);
} }
return shouldDrop;
}
// Real stuff below // // Real stuff below //
// These methods are somehow a merge between the clone methods of Cecil 0.6 and the import ones of 0.9 // These methods are somehow a merge between the clone methods of Cecil 0.6 and the import ones of 0.9
// They use Cecil's MetaDataImporter to rebase imported stuff into the new assembly, but then another pass is required // They use Cecil's MetaDataImporter to rebase imported stuff into the new assembly, but then another pass is required
// to clean the TypeRefs Cecil keeps around (although the generated IL would be kind-o valid without, whatever 'valid' means) // to clean the TypeRefs Cecil keeps around (although the generated IL would be kind-o valid without, whatever 'valid' means)
/// <summary> /// <summary>
/// Clones a field to a newly created type /// Clones a field to a newly created type
/// </summary> /// </summary>
private void CloneTo(FieldDefinition field, TypeDefinition nt) private void CloneTo(FieldDefinition field, TypeDefinition nt)
{ {
if (nt.Fields.Any(x => x.Name == field.Name)) if (nt.Fields.Any(x => x.Name == field.Name))
{ {