11using System ;
22using System . ComponentModel . Composition ;
33using System . Globalization ;
4+ using System . IO ;
5+ using System . Reflection ;
46using System . Runtime . InteropServices ;
57using GitHub . Extensions ;
68using GitHub . Models ;
1214using Microsoft . VisualStudio . Shell ;
1315using Microsoft . VisualStudio . Shell . Interop ;
1416using Octokit ;
17+ using System . Linq ;
1518
1619namespace GitHub . VisualStudio
1720{
18- /// <summary>
19- /// This is the class that implements the package exposed by this assembly.
20- ///
21- /// The minimum requirement for a class to be considered a valid package for Visual Studio
22- /// is to implement the IVsPackage interface and register itself with the shell.
23- /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
24- /// to do it: it derives from the Package class that provides the implementation of the
25- /// IVsPackage interface and uses the registration attributes defined in the framework to
26- /// register itself and its components with the shell.
27- /// </summary>
28- // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
29- // a package.
3021 [ PackageRegistration ( UseManagedResourcesOnly = true ) ]
31- // This attribute is used to register the information needed to show this package
32- // in the Help/About dialog of Visual Studio.
3322 [ InstalledProductRegistration ( "#110" , "#112" , "1.0" , IconResourceID = 400 ) ]
3423 [ Guid ( GuidList . guidGitHubPkgString ) ]
3524 //[ProvideBindingPath]
3625 [ ProvideMenuResource ( "Menus.ctmenu" , 1 ) ]
3726 //[ProvideAutoLoad(UIContextGuids.NoSolution)]
27+ // this is the Git service GUID, so we load whenever it loads
3828 [ ProvideAutoLoad ( "11B8E6D7-C08B-4385-B321-321078CDD1F8" ) ]
3929 [ ProvideToolWindow ( typeof ( GitHubPane ) , Orientation = ToolWindowOrientation . Right , Style = VsDockStyle . Tabbed , Window = EnvDTE . Constants . vsWindowKindSolutionExplorer ) ]
4030 [ ProvideOptionPage ( typeof ( OptionsPage ) , "GitHub for Visual Studio" , "General" , 0 , 0 , supportsAutomation : true ) ]
41- public class GitHubPackage : PackageBase
31+ public class GitHubPackage : Package
4232 {
33+ // list of assemblies to be loaded from the extension installation path
34+ static readonly string [ ] ourAssemblies =
35+ {
36+ "GitHub.Api" ,
37+ "GitHub.App" ,
38+ "GitHub.CredentialManagement" ,
39+ "GitHub.Exports" ,
40+ "GitHub.Exports.Reactive" ,
41+ "GitHub.Extensions" ,
42+ "GitHub.Extensions.Reactive" ,
43+ "GitHub.UI" ,
44+ "GitHub.UI.Reactive" ,
45+ "GitHub.VisualStudio" ,
46+ "GitHub.TeamFoundation" ,
47+ "GitHub.TeamFoundation.14" ,
48+ "GitHub.TeamFoundation.15" ,
49+ "GitHub.VisualStudio.UI" ,
50+ "System.Windows.Interactivity"
51+ } ;
52+
53+ readonly IServiceProvider serviceProvider ;
54+
4355 public GitHubPackage ( )
4456 {
57+ serviceProvider = this ;
4558 }
4659
4760 public GitHubPackage ( IServiceProvider serviceProvider )
48- : base ( serviceProvider )
4961 {
62+ this . serviceProvider = serviceProvider ;
5063 }
5164
52-
5365 protected override void Initialize ( )
5466 {
67+ AppDomain . CurrentDomain . AssemblyResolve += LoadAssemblyFromRunDir ;
68+
5569 base . Initialize ( ) ;
5670
57- var menus = ServiceProvider . GetExportedValue < IMenuProvider > ( ) ;
71+ var menus = serviceProvider . GetExportedValue < IMenuProvider > ( ) ;
5872 foreach ( var menu in menus . Menus )
59- ServiceProvider . AddTopLevelMenuItem ( menu . Guid , menu . CmdId , ( s , e ) => menu . Activate ( ) ) ;
73+ serviceProvider . AddTopLevelMenuItem ( menu . Guid , menu . CmdId , ( s , e ) => menu . Activate ( ) ) ;
6074
6175 foreach ( var menu in menus . DynamicMenus )
62- ServiceProvider . AddDynamicMenuItem ( menu . Guid , menu . CmdId , menu . CanShow , menu . Activate ) ;
76+ serviceProvider . AddDynamicMenuItem ( menu . Guid , menu . CmdId , menu . CanShow , menu . Activate ) ;
77+ }
78+
79+ [ System . Diagnostics . CodeAnalysis . SuppressMessageAttribute ( "Microsoft.Reliability" , "CA2001:AvoidCallingProblematicMethods" ) ]
80+ static Assembly LoadAssemblyFromRunDir ( object sender , ResolveEventArgs e )
81+ {
82+ try
83+ {
84+ var name = new AssemblyName ( e . Name ) ;
85+ if ( ! ourAssemblies . Contains ( name . Name ) )
86+ return null ;
87+ var path = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
88+ var filename = Path . Combine ( path , name . Name + ".dll" ) ;
89+ if ( ! File . Exists ( filename ) )
90+ return null ;
91+ return Assembly . LoadFrom ( filename ) ;
92+ }
93+ catch ( Exception ex )
94+ {
95+ var log = string . Format ( CultureInfo . CurrentCulture ,
96+ "Error occurred loading {0} from {1}.{2}{3}{4}" ,
97+ e . Name ,
98+ Assembly . GetExecutingAssembly ( ) . Location ,
99+ Environment . NewLine ,
100+ ex ,
101+ Environment . NewLine ) ;
102+ VsOutputLogger . Write ( log ) ;
103+ }
104+ return null ;
63105 }
64106 }
65107
@@ -70,7 +112,6 @@ public class GHClient : GitHubClient
70112 public GHClient ( IProgram program )
71113 : base ( program . ProductHeader )
72114 {
73-
74115 }
75116 }
76117}
0 commit comments