Skip to content

Commit 513cad9

Browse files
authored
Weaver: Fixes attributes corruption during weaving (#47)
1 parent cceeda9 commit 513cad9

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

Weaver/Xtensive.Orm.Weaver/ProcessorContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public sealed class ProcessorContext : IDisposable
2424

2525
public bool TranformationPerformed { get; set; }
2626

27+
public bool ShouldAddReferenceToOrm { get; set; }
28+
2729
public IList<WeavingTask> WeavingTasks { get; set; }
2830

2931
public ModuleDefinition TargetModule { get; set; }
@@ -54,6 +56,7 @@ public ProcessorContext()
5456
References = new ReferenceRegistry();
5557
PersistentTypes = new List<TypeInfo>();
5658
AssemblyChecker = new AssemblyChecker();
59+
ShouldAddReferenceToOrm = false;
5760
}
5861
}
5962
}

Weaver/Xtensive.Orm.Weaver/Stages/ImportReferencesStage.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created: 2013.08.20
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Linq;
910
using Mono.Cecil;
1011

@@ -115,12 +116,15 @@ private bool TryGetOrmAssembly(ProcessorContext context, out AssemblyNameReferen
115116
assemblyNameReference = context.TargetModule.AssemblyReferences
116117
.FirstOrDefault(r => comparer.Equals(r.FullName, WellKnown.OrmAssemblyFullName));
117118

118-
if (assemblyNameReference!=null)
119+
if (assemblyNameReference != null) {
119120
return true;
121+
}
120122

121-
// check whether project had reference to Xtensive.Orm assembly.
122-
if (!context.Configuration.ReferencedAssemblies.Reverse().Any(ra => ra.EndsWith(WellKnown.OrmAssemblyShortName + ".dll", StringComparison.InvariantCultureIgnoreCase)))
123+
// check whether project had reference to Xtensive.Orm assembly
124+
// but omitted during project compilation as not used.
125+
if (!IsOrmReferenceOmmitedByCompiler(context.Configuration.ReferencedAssemblies)) {
123126
return false;
127+
}
124128

125129
// if target module has't got reference to the assembly and there was reference to the assembly in project
126130
// then probably some of target module referenses references the assembly.
@@ -131,8 +135,27 @@ private bool TryGetOrmAssembly(ProcessorContext context, out AssemblyNameReferen
131135
.SelectMany(m => m.AssemblyReferences)
132136
.FirstOrDefault(ar => comparer.Equals(ar.FullName, WellKnown.OrmAssemblyFullName));
133137

134-
if (assemblyNameReference!=null)
138+
if (assemblyNameReference != null) {
139+
context.ShouldAddReferenceToOrm = true;
135140
return true;
141+
}
142+
return false;
143+
}
144+
145+
private bool IsOrmReferenceOmmitedByCompiler(IList<string> projectReferences)
146+
{
147+
if (projectReferences.Count == 0) {
148+
return false;
149+
}
150+
var lastItemIndex = projectReferences.Count - 1;
151+
152+
// Orm assembly tends to be at the end of the list
153+
string searchCreteria = WellKnown.OrmAssemblyShortName + ".dll";
154+
for (int i = lastItemIndex; i >= 0; i--) {
155+
if (projectReferences[i].EndsWith(searchCreteria, StringComparison.OrdinalIgnoreCase)) {
156+
return true;
157+
}
158+
}
136159
return false;
137160
}
138161

Weaver/Xtensive.Orm.Weaver/Stages/SaveAssemblyStage.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
// Created: 2013.08.19
66

77
using System.IO;
8-
using System.Reflection;
98
using Mono.Cecil;
10-
using Mono.Cecil.Cil;
119

1210
namespace Xtensive.Orm.Weaver.Stages
1311
{
@@ -27,22 +25,29 @@ public override ActionResult Execute(ProcessorContext context)
2725
var outputIsInput = inputFile==outputFile;
2826

2927
if (context.SkipProcessing) {
30-
if (!outputIsInput)
28+
if (!outputIsInput) {
3129
FileHelper.CopyWithPdb(context, inputFile, outputFile);
30+
}
3231
return ActionResult.Success;
3332
}
3433

35-
if (!context.TranformationPerformed && context.Configuration.MakeBackup && outputIsInput)
34+
if (!context.TranformationPerformed && context.Configuration.MakeBackup && outputIsInput) {
3635
FileHelper.CopyWithPdb(context, inputFile, FileHelper.GetBackupFile(inputFile));
36+
}
37+
38+
if (context.ShouldAddReferenceToOrm) {
39+
context.TargetModule.AssemblyReferences.Add(context.References.OrmAssembly);
40+
}
3741

3842
var writerParameters = new WriterParameters {
3943
WriteSymbols = configuration.ProcessDebugSymbols
4044
};
4145

4246
var strongNameKey = configuration.StrongNameKey;
4347
if (!string.IsNullOrEmpty(strongNameKey)) {
44-
if (File.Exists(strongNameKey))
48+
if (File.Exists(strongNameKey)) {
4549
writerParameters.StrongNameKeyBlob = File.ReadAllBytes(strongNameKey);
50+
}
4651
else {
4752
context.Logger.Write(MessageCode.ErrorStrongNameKeyIsNotFound);
4853
return ActionResult.Failure;

0 commit comments

Comments
 (0)