Skip to content

Commit 17303c2

Browse files
committed
Switch to serializing ComProjects. (Recommit of #4414)
1 parent 56f10ec commit 17303c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+793
-966
lines changed

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@
539539
<Compile Include="UI\Command\MenuItems\CommandBars\ReferenceCounterLabelMenuItem.cs" />
540540
<Compile Include="UI\Command\MenuItems\CommandBars\ReparseCommandMenuItem.cs" />
541541
<Compile Include="UI\Command\MenuItems\CommandBars\RubberduckCommandBar.cs" />
542-
<Compile Include="UI\Command\MenuItems\CommandBars\SerializeDeclarationsCommandMenuItem.cs" />
542+
<Compile Include="UI\Command\MenuItems\CommandBars\SerializeProjectsCommandMenuItem.cs" />
543543
<Compile Include="UI\Command\MenuItems\CommandBars\ShowParserErrorsCommandMenuItem.cs" />
544544
<Compile Include="UI\Command\MenuItems\CommandBars\IAppCommandBar.cs" />
545545
<Compile Include="UI\Command\RegexAssistantCommand.cs" />

Rubberduck.Core/UI/Command/MenuItems/CommandBars/SerializeDeclarationsCommandMenuItem.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using NLog;
6+
using Rubberduck.Parsing.ComReflection;
7+
using Rubberduck.Parsing.VBA;
8+
using Rubberduck.VBEditor;
9+
10+
namespace Rubberduck.UI.Command.MenuItems.CommandBars
11+
{
12+
public class SerializeProjectsCommandMenuItem : CommandMenuItemBase
13+
{
14+
public SerializeProjectsCommandMenuItem(SerializeProjectsCommand command) : base(command)
15+
{
16+
}
17+
18+
public override Func<string> Caption { get { return () => "Serialize"; } }
19+
public override string Key => "SerializeProjects";
20+
}
21+
22+
public class SerializeProjectsCommand : CommandBase
23+
{
24+
private readonly RubberduckParserState _state;
25+
private readonly IComProjectSerializationProvider _serializationProvider;
26+
private readonly IComLibraryProvider _comLibraryProvider;
27+
28+
public SerializeProjectsCommand(RubberduckParserState state, IComProjectSerializationProvider serializationProvider, IComLibraryProvider comLibraryProvider)
29+
: base(LogManager.GetCurrentClassLogger())
30+
{
31+
_state = state;
32+
_serializationProvider = serializationProvider;
33+
_comLibraryProvider = comLibraryProvider;
34+
}
35+
36+
protected override bool EvaluateCanExecute(object parameter)
37+
{
38+
return _state.Status == ParserState.Ready;
39+
}
40+
41+
protected override void OnExecute(object parameter)
42+
{
43+
if (!Directory.Exists(_serializationProvider.Target))
44+
{
45+
Directory.CreateDirectory(_serializationProvider.Target);
46+
}
47+
48+
var toSerialize = new Dictionary<Guid, ComProject>();
49+
50+
foreach (var project in _state.ProjectsProvider.Projects().Select(proj => proj.Project))
51+
{
52+
using (var references = project.References)
53+
{
54+
foreach (var reference in references)
55+
{
56+
var info = new ReferenceInfo(reference);
57+
reference.Dispose();
58+
var library = _comLibraryProvider.LoadTypeLibrary(info.FullPath);
59+
if (library == null)
60+
{
61+
Logger.Warn($"Could not load library {info.FullPath} for serialization.");
62+
continue;
63+
}
64+
65+
var type = new ComProject(library, info.FullPath);
66+
if (!toSerialize.ContainsKey(type.Guid))
67+
{
68+
toSerialize.Add(type.Guid, type);
69+
}
70+
}
71+
}
72+
}
73+
74+
foreach (var library in toSerialize.Values)
75+
{
76+
Logger.Warn($"Serializing {library.Path}.");
77+
_serializationProvider.SerializeProject(library);
78+
}
79+
}
80+
}
81+
}

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ private void RegisterConfiguration(IWindsorContainer container, Assembly[] assem
220220
// .LifestyleSingleton()
221221
// .Instance(experimentalTypes));
222222

223-
container.Register(Component.For<IPersistable<SerializableProject>>()
224-
.ImplementedBy<XmlPersistableDeclarations>()
223+
container.Register(Component.For<IComProjectSerializationProvider>()
224+
.ImplementedBy<XmlComProjectSerializer>()
225225
.LifestyleTransient());
226226
container.Register(Component.For(typeof(IPersistanceService<>), typeof(IFilePersistanceService<>))
227227
.ImplementedBy(typeof(XmlPersistanceService<>))
@@ -514,7 +514,7 @@ private Type[] RubberduckCommandBarItems()
514514
typeof(ContextDescriptionLabelMenuItem),
515515
typeof(ReferenceCounterLabelMenuItem),
516516
#if DEBUG
517-
typeof(SerializeDeclarationsCommandMenuItem)
517+
typeof(SerializeProjectsCommandMenuItem)
518518
#endif
519519
};
520520
}

Rubberduck.Parsing/ComReflection/ComAlias.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices.ComTypes;
3+
using System.Runtime.Serialization;
34
using Rubberduck.Parsing.Grammar;
45
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
56
using TYPEFLAGS = System.Runtime.InteropServices.ComTypes.TYPEFLAGS;
67

78
namespace Rubberduck.Parsing.ComReflection
89
{
10+
[DataContract]
11+
[KnownType(typeof(ComBase))]
912
[DebuggerDisplay("{Name} As {TypeName}")]
1013
public class ComAlias : ComBase
1114
{
12-
public string TypeName { get; }
13-
public bool IsHidden { get; }
14-
public bool IsRestricted { get; }
15+
[DataMember(IsRequired = true)]
16+
public string TypeName { get; private set; }
17+
18+
[DataMember(IsRequired = true)]
19+
public bool IsHidden { get; private set; }
20+
21+
[DataMember(IsRequired = true)]
22+
public bool IsRestricted { get; private set; }
1523

1624
public ComAlias(IComBase parent, ITypeLib typeLib, ITypeInfo info, int index, TYPEATTR attributes) : base(parent, typeLib, index)
1725
{

Rubberduck.Parsing/ComReflection/ComBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.Runtime.InteropServices.ComTypes;
4+
using System.Runtime.Serialization;
45
using Rubberduck.Parsing.Symbols;
56
using FUNCDESC = System.Runtime.InteropServices.ComTypes.FUNCDESC;
67

@@ -17,14 +18,26 @@ public interface IComBase
1718
ComProject Project { get; }
1819
}
1920

21+
[DataContract]
2022
public abstract class ComBase : IComBase
2123
{
24+
[DataMember(IsRequired = true)]
2225
public Guid Guid { get; protected set; }
26+
27+
[DataMember(IsRequired = true)]
2328
public int Index { get; protected set; }
29+
30+
[DataMember(IsRequired = true)]
2431
public ComDocumentation Documentation { get; protected set; }
32+
2533
public string Name => Documentation == null ? string.Empty : Documentation.Name;
34+
35+
[DataMember(IsRequired = true)]
2636
public DeclarationType Type { get; protected set; }
37+
38+
[DataMember(IsRequired = true)]
2739
public IComBase Parent { get; protected set; }
40+
2841
public ComProject Project => Parent != null ? Parent.Project : this as ComProject;
2942

3043
protected ComBase(IComBase parent, ITypeLib typeLib, int index)

Rubberduck.Parsing/ComReflection/ComCoClass.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Runtime.InteropServices;
66
using System.Runtime.InteropServices.ComTypes;
7+
using System.Runtime.Serialization;
78
using Rubberduck.Parsing.Symbols;
89
using Rubberduck.VBEditor.Utility;
910
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
@@ -12,15 +13,22 @@
1213

1314
namespace Rubberduck.Parsing.ComReflection
1415
{
16+
[DataContract]
17+
[KnownType(typeof(ComType))]
1518
public class ComCoClass : ComType, IComTypeWithMembers
1619
{
17-
private readonly Dictionary<ComInterface, bool> _interfaces = new Dictionary<ComInterface, bool>();
18-
private readonly List<ComInterface> _events = new List<ComInterface>();
20+
[DataMember(IsRequired = true)]
21+
private Dictionary<ComInterface, bool> _interfaces = new Dictionary<ComInterface, bool>();
1922

20-
public bool IsControl { get; }
23+
[DataMember(IsRequired = true)]
24+
private List<ComInterface> _events = new List<ComInterface>();
25+
26+
[DataMember(IsRequired = true)]
27+
public bool IsControl { get; private set; }
2128

2229
public bool IsExtensible => _interfaces.Keys.Any(i => i.IsExtensible);
2330

31+
[DataMember(IsRequired = true)]
2432
public ComInterface DefaultInterface { get; private set; }
2533

2634
public IEnumerable<ComInterface> EventInterfaces => _events;
@@ -91,6 +99,11 @@ private void GetImplementedInterfaces(ITypeInfo info, TYPEATTR typeAttr)
9199
_interfaces.Add(intface, flags.HasFlag(IMPLTYPEFLAGS.IMPLTYPEFLAG_FRESTRICTED));
92100
}
93101
}
102+
103+
if (DefaultInterface == null)
104+
{
105+
DefaultInterface = VisibleInterfaces.FirstOrDefault();
106+
}
94107
}
95108
}
96109
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
using System.Runtime.InteropServices.ComTypes;
2+
using System.Runtime.Serialization;
23

34
namespace Rubberduck.Parsing.ComReflection
45
{
6+
[DataContract]
57
public class ComDocumentation
68
{
79
public const int LibraryIndex = -1;
10+
[DataMember(IsRequired = true)]
11+
public string Name { get; private set; }
812

9-
public string Name { get; }
10-
public string DocString { get; }
11-
public string HelpFile { get; }
12-
public int HelpContext { get; }
13+
[DataMember(IsRequired = true)]
14+
public string DocString { get; private set; }
15+
16+
[DataMember(IsRequired = true)]
17+
public string HelpFile { get; private set; }
18+
19+
[DataMember(IsRequired = true)]
20+
public int HelpContext { get; private set; }
1321

1422
public ComDocumentation(ITypeLib typeLib, int index)
1523
{
1624
typeLib.GetDocumentation(index, out string name, out string docString, out int helpContext, out string helpFile);
1725
Name = name;
1826
DocString = docString;
1927
HelpContext = helpContext;
20-
HelpFile = helpFile;
28+
HelpFile = helpFile?.Trim('\0');
2129
}
2230

2331
public ComDocumentation(ITypeInfo info, int index)
@@ -26,7 +34,7 @@ public ComDocumentation(ITypeInfo info, int index)
2634
Name = name;
2735
DocString = docString;
2836
HelpContext = helpContext;
29-
HelpFile = helpFile;
37+
HelpFile = helpFile?.Trim('\0');
3038
}
3139
}
3240
}

Rubberduck.Parsing/ComReflection/ComEnumeration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44
using System.Runtime.InteropServices.ComTypes;
5+
using System.Runtime.Serialization;
56
using Rubberduck.Parsing.Symbols;
67
using Rubberduck.VBEditor.Utility;
78
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
89
using VARDESC = System.Runtime.InteropServices.ComTypes.VARDESC;
910

1011
namespace Rubberduck.Parsing.ComReflection
1112
{
13+
[DataContract]
14+
[KnownType(typeof(ComType))]
1215
public class ComEnumeration : ComType
1316
{
14-
public List<ComEnumerationMember> Members { get; }
17+
[DataMember(IsRequired = true)]
18+
public List<ComEnumerationMember> Members { get; private set; }
1519

1620
public ComEnumeration(IComBase parent, ITypeLib typeLib, ITypeInfo info, TYPEATTR attrib, int index) : base(parent, typeLib, attrib, index)
1721
{

Rubberduck.Parsing/ComReflection/ComEnumerationMember.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33
using System.Runtime.InteropServices.ComTypes;
4+
using System.Runtime.Serialization;
45
using VARDESC = System.Runtime.InteropServices.ComTypes.VARDESC;
56

67
namespace Rubberduck.Parsing.ComReflection
78
{
9+
[DataContract]
10+
[KnownType(typeof(ComEnumeration))]
811
[DebuggerDisplay("{Name} = {Value} ({ValueType})")]
912
public class ComEnumerationMember
1013
{
11-
public string Name { get; }
12-
public int Value { get; }
13-
public VarEnum ValueType { get; }
14-
ComEnumeration Parent { get; }
14+
[DataMember(IsRequired = true)]
15+
public string Name { get; private set; }
16+
17+
[DataMember(IsRequired = true)]
18+
public int Value { get; private set; }
19+
20+
[DataMember(IsRequired = true)]
21+
public VarEnum ValueType { get; private set; }
22+
23+
[DataMember(IsRequired = true)]
24+
ComEnumeration Parent { get; set; }
25+
1526
public ComProject Project => Parent?.Project;
1627

1728
public ComEnumerationMember(ComEnumeration parent, ITypeInfo info, VARDESC varDesc)

0 commit comments

Comments
 (0)