protected override async Task InitializeAsync(CancellationToken cancel,
                                                      IProgress <Microsoft.VisualStudio.Shell.ServiceProgressData> progress)
        {
            Instance = this;
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            AutoBuilding = GetAutoBuilding();

            object value = null;

            {
                Shell = await GetServiceAsync(typeof(IVsShell)) as IVsShell;

                if (Shell == null)
                {
                    throw new PackageInitializationException("Error initializing Shell");
                }
                Shell.GetProperty((int)__VSSPROPID.VSSPROPID_IsInCommandLineMode, out value);
                CommandLineMode = (bool)value;
            }

            if (!CommandLineMode)
            {
                InstallDirectory   = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                ResourcesDirectory = Path.Combine(InstallDirectory, "Resources");

                DTE2 = await GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;

                if (DTE2 == null)
                {
                    throw new PackageInitializationException("Error initializing DTE2");
                }

                DTEEvents = DTE.Events.DTEEvents;

                IVsSolution = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution;

                if (IVsSolution == null)
                {
                    throw new PackageInitializationException("Error initializing IVsSolution");
                }

                IVsSolution4 = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution4;

                if (IVsSolution4 == null)
                {
                    throw new PackageInitializationException("Error initializing IVsSolution4");
                }

                UIShell = await GetServiceAsync(typeof(SVsUIShell)) as IVsUIShell;

                if (UIShell == null)
                {
                    throw new PackageInitializationException("Error initializing UIShell");
                }

                MonitorSelection = await GetServiceAsync(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;

                if (MonitorSelection == null)
                {
                    throw new PackageInitializationException("Error initializing MonitorSelection");
                }

                //
                // Update the InstallDir this was previously used in project imports, but is still usefull if you
                // need to detect the extension install dir.
                //
                Registry.SetValue(IceBuilderKey, string.Format("InstallDir.{0}", DTE.Version), InstallDirectory,
                                  RegistryValueKind.String);

                Assembly assembly = null;
                if (DTE.Version.StartsWith("14.0"))
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2015.dll"));
                }
                else if (DTE.Version.StartsWith("15.0"))
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2017.dll"));
                }
                else
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2019.dll"));
                }
                var factory = assembly.GetType("IceBuilder.ProjectHelperFactoryI").GetConstructor(new Type[] { }).Invoke(
                    new object[] { }) as IVsProjectHelperFactory;

                VCUtil        = factory.VCUtil;
                NuGet         = factory.NuGet;
                ProjectHelper = factory.ProjectHelper;

                ProjectFactoryHelperInstance.Init(VCUtil, NuGet, ProjectHelper);

                NuGet.OnNugetBatchEnd(PackageInstalled);

                RunningDocumentTableEventHandler = new RunningDocumentTableEventHandler(
                    await GetServiceAsync(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable);

                Builder = new Builder(await GetServiceAsync(typeof(SVsBuildManagerAccessor)) as IVsBuildManagerAccessor2);

                //
                // Subscribe to solution events.
                //
                SolutionEventHandler = new SolutionEventHandler();
                SolutionEventHandler.BeginTrack();

                DocumentEventHandler = new DocumentEventHandler(
                    await GetServiceAsync(typeof(SVsTrackProjectDocuments)) as IVsTrackProjectDocuments2);
                DocumentEventHandler.BeginTrack();

                BuildEvents = DTE2.Events.BuildEvents;
                BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
                BuildEvents.OnBuildDone  += BuildEvents_OnBuildDone;
            }

            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            RegisterProjectFactory(new ProjectFactory());
            RegisterProjectFactory(new ProjectFactoryOld());

            value = null;
            IVsSolution.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out value);
            if ((bool)value)
            {
                RunningDocumentTableEventHandler.BeginTrack();
                InitializeProjects(DTEUtil.GetProjects());
            }
        }
Exemplo n.º 2
0
        protected override void Initialize()
        {
            base.Initialize();
            Instance = this;

            AutoBuilding = GetAutoBuilding();

            {
                Shell = GetService(typeof(IVsShell)) as IVsShell;
                object value;
                Shell.GetProperty((int)__VSSPROPID.VSSPROPID_IsInCommandLineMode, out value);
                CommandLineMode = (bool)value;
            }

            RegisterProjectFactory(new ProjectFactory());
            RegisterProjectFactory(new ProjectFactoryOld());

            if (!CommandLineMode)
            {
                InstallDirectory   = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                ResourcesDirectory = Path.Combine(InstallDirectory, "Resources");

                DTE2             = (EnvDTE80.DTE2)GetService(typeof(EnvDTE.DTE));
                DTEEvents        = DTE.Events.DTEEvents;
                IVsSolution      = GetService(typeof(SVsSolution)) as IVsSolution;
                IVsSolution4     = GetService(typeof(SVsSolution)) as IVsSolution4;
                UIShell          = Instance.GetService(typeof(SVsUIShell)) as IVsUIShell;
                MonitorSelection = GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;

                //
                // Update the InstallDir this was previously used in project imports, but is still usefull if you
                // need to detect the extension install dir.
                //
                Registry.SetValue(IceBuilderKey, string.Format("InstallDir.{0}", DTE.Version), InstallDirectory,
                                  RegistryValueKind.String);

                Assembly assembly = null;
                if (DTE.Version.StartsWith("11.0"))
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2012.dll"));
                }
                else if (DTE.Version.StartsWith("12.0"))
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2013.dll"));
                }
                else if (DTE.Version.StartsWith("14.0"))
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2015.dll"));
                }
                else
                {
                    assembly = Assembly.LoadFrom(Path.Combine(ResourcesDirectory, "IceBuilder.VS2017.dll"));
                }
                var factory = assembly.GetType("IceBuilder.ProjectHelperFactoryI").GetConstructor(new Type[] { }).Invoke(
                    new object[] { }) as IVsProjectHelperFactory;

                VCUtil        = factory.VCUtil;
                NuGet         = factory.NuGet;
                ProjectHelper = factory.ProjectHelper;

                ProjectFactoryHelperInstance.Init(factory.VCUtil, factory.NuGet, factory.ProjectHelper);

                NuGet.OnNugetBatchEnd(PackageInstalled);

                RunningDocumentTableEventHandler = new RunningDocumentTableEventHandler(
                    GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable);

                Builder = new Builder(GetService(typeof(SVsBuildManagerAccessor)) as IVsBuildManagerAccessor2);

                //
                // Subscribe to solution events.
                //
                SolutionEventHandler = new SolutionEventHandler();
                SolutionEventHandler.BeginTrack();

                DocumentEventHandler = new DocumentEventHandler(
                    GetService(typeof(SVsTrackProjectDocuments)) as IVsTrackProjectDocuments2);
                DocumentEventHandler.BeginTrack();

                BuildEvents = DTE2.Events.BuildEvents;
                BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
                BuildEvents.OnBuildDone  += BuildEvents_OnBuildDone;
            }
        }