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 ( SerializeDeclarationsCommand 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 SerializeDeclarationsCommand : CommandBase
23+ {
24+ private readonly RubberduckParserState _state ;
25+ private readonly IComProjectSerializationProvider _serializationProvider ;
26+ private readonly IComLibraryProvider _comLibraryProvider ;
27+
28+ public SerializeDeclarationsCommand ( 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+ foreach ( var reference in project . References . Select ( lib => new ReferenceInfo ( lib ) ) )
53+ {
54+ var library = _comLibraryProvider . LoadTypeLibrary ( reference . FullPath ) ;
55+ if ( library == null )
56+ {
57+ Logger . Warn ( $ "Could not load library { reference . FullPath } for serialization.") ;
58+ continue ;
59+ }
60+
61+ var type = new ComProject ( library , reference . FullPath ) ;
62+ if ( ! toSerialize . ContainsKey ( type . Guid ) )
63+ {
64+ toSerialize . Add ( type . Guid , type ) ;
65+ }
66+ }
67+ }
68+
69+ foreach ( var library in toSerialize . Values )
70+ {
71+ Logger . Warn ( $ "Serializing { library . Path } .") ;
72+ _serializationProvider . SerializeProject ( library ) ;
73+ }
74+ }
75+ }
76+ }
0 commit comments