Inheritance: System.Management.Automation.Host.PSHost, IHostSupportsInteractiveSession
示例#1
0
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue)
            {
                if (_initialized.Value && console.ShowDisclaimerHeader)
                {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else
            {
                try
                {
                    Tuple <RunspaceDispatcher, NuGetPSHost> result = _runspaceManager.GetRunspace(console, _name);
                    _runspace  = result.Item1;
                    _nugetHost = result.Item2;

                    _initialized = true;

                    if (console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    // Hook up solution events
                    _solutionManager.SolutionOpened += (o, e) =>
                    {
                        Task.Factory.StartNew(() =>
                        {
                            UpdateWorkingDirectory();
                            ExecuteInitScripts();
                        },
                                              CancellationToken.None,
                                              TaskCreationOptions.None,
                                              TaskScheduler.Default);
                    };
                    _solutionManager.SolutionClosed      += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectAdded   += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectRenamed += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectRemoved += (o, e) => UpdateWorkingDirectory();

                    // Set available private data on Host
                    SetPrivateDataOnHost(false);
                }
                catch (Exception ex)
                {
                    // catch all exception as we don't want it to crash VS
                    _initialized     = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
示例#2
0
        internal static NuGetPSHost InitializeConsole(
            out Mock<NuGetRawUserInterface> mockRawUI,
            out Mock<NuGetHostUserInterface> mockUI,
            out ConsoleDispatcher dispatcher)
        {

            var console = new Mock<IConsole>();
            console.Setup(o => o.Write("text"));
            console.Setup(o => o.Write("text", null, null));

            var privateWpfConsole = new Mock<IPrivateWpfConsole>();
            dispatcher = new ConsoleDispatcher(privateWpfConsole.Object);
            console.SetupGet(o => o.Dispatcher).Returns(dispatcher);

            var host = new NuGetPSHost("Test")
            {
                ActiveConsole = console.Object
            };

            mockRawUI = new Mock<NuGetRawUserInterface>(host);
            mockRawUI.CallBase = true;

            mockUI = new Mock<NuGetHostUserInterface>(host);
            mockUI.CallBase = true;
            mockUI.SetupGet(o => o.RawUI).Returns(mockRawUI.Object);

            return host;
        }
示例#3
0
        private static Tuple<Runspace, NuGetPSHost> CreateRunspace(IConsole console, string hostName)
        {
            DTE dte = ServiceLocator.GetInstance<DTE>();

            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
            initialSessionState.Variables.Add(
                new SessionStateVariableEntry(
                    "DTE",
                    (DTE2)dte,
                    "Visual Studio DTE automation object",
                    ScopedItemOptions.AllScope | ScopedItemOptions.Constant)
            );

            Tuple<string, object>[] privateData;

            // this is used by the functional tests
            var packageManagerFactory = ServiceLocator.GetInstance<IVsPackageManagerFactory>();
            var pmfTuple = Tuple.Create<string, object>("packageManagerFactory", packageManagerFactory);

            privateData = new Tuple<string, object>[] { pmfTuple };

#if DEBUG
            var recentPackageRepository = ServiceLocator.GetInstance<IRecentPackageRepository>();
            var rprTuple = Tuple.Create<string, object>("recentPackageRepository", recentPackageRepository);

            privateData = new[] { pmfTuple, rprTuple };
#endif

            var host = new NuGetPSHost(hostName, privateData)
            {
                ActiveConsole = console
            };

            var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);
            runspace.ThreadOptions = PSThreadOptions.Default;
            runspace.Open();

            //
            // Set this runspace as DefaultRunspace so I can script DTE events.
            //
            // WARNING: MSDN says this is unsafe. The runspace must not be shared across
            // threads. I need this to be able to use ScriptBlock for DTE events. The
            // ScriptBlock event handlers execute on DefaultRunspace.
            //
            Runspace.DefaultRunspace = runspace;

            return Tuple.Create(runspace, host);
        }
示例#4
0
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue)
            {
                if (_initialized.Value && console.ShowDisclaimerHeader)
                {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else
            {
                try
                {
                    Tuple <RunspaceDispatcher, NuGetPSHost> result = _runspaceManager.GetRunspace(console, _name);
                    _runspace  = result.Item1;
                    _nugetHost = result.Item2;

                    _initialized = true;

                    if (console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    // Hook up solution events
                    _solutionManager.SolutionOpened += (o, e) =>
                    {
                        UpdateWorkingDirectory();
                        ExecuteInitScripts();
                    };
                    _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                }
                catch (Exception ex)
                {
                    // catch all exception as we don't want it to crash VS
                    _initialized     = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
示例#5
0
        private static Tuple <RunspaceDispatcher, NuGetPSHost> CreateRunspace(IConsole console, string hostName)
        {
            DTE dte = ServiceLocator.GetInstance <DTE>();

            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();

            initialSessionState.Variables.Add(
                new SessionStateVariableEntry(
                    "DTE",
                    (DTE2)dte,
                    "Visual Studio DTE automation object",
                    ScopedItemOptions.AllScope | ScopedItemOptions.Constant)
                );

            // this is used by the functional tests
            var packageManagerFactory = ServiceLocator.GetInstance <IVsPackageManagerFactory>();
            var pmfTuple = Tuple.Create <string, object>("packageManagerFactory", packageManagerFactory);

            Tuple <string, object>[] privateData = new Tuple <string, object>[] { pmfTuple };

#if DEBUG
            var recentPackageRepository = ServiceLocator.GetInstance <IRecentPackageRepository>();
            var rprTuple = Tuple.Create <string, object>("recentPackageRepository", recentPackageRepository);

            privateData = new[] { pmfTuple, rprTuple };
#endif

            var host = new NuGetPSHost(hostName, privateData)
            {
                ActiveConsole = console
            };

            var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);
            runspace.ThreadOptions = PSThreadOptions.Default;
            runspace.Open();

            //
            // Set this runspace as DefaultRunspace so I can script DTE events.
            //
            // WARNING: MSDN says this is unsafe. The runspace must not be shared across
            // threads. I need this to be able to use ScriptBlock for DTE events. The
            // ScriptBlock event handlers execute on DefaultRunspace.
            //
            Runspace.DefaultRunspace = runspace;

            return(Tuple.Create(new RunspaceDispatcher(runspace), host));
        }
示例#6
0
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue)
            {
                if (_initialized.Value && console.ShowDisclaimerHeader)
                {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else
            {
                try {
                    Tuple <Runspace, NuGetPSHost> tuple = _runspaceManager.GetRunspace(console, _name);
                    _runspace  = tuple.Item1;
                    _nugetHost = tuple.Item2;

                    _initialized = true;

                    // when initializing host from the dialog, we don't want to execute existing init scripts in the solution, if any.
                    if (console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }

                    _solutionManager.SolutionOpened += (o, e) => {
                        UpdateWorkingDirectory();
                        ExecuteInitScripts();
                    };

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                }
                catch (Exception ex) {
                    // catch all exception as we don't want it to crash VS
                    _initialized     = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
        private static Tuple <RunspaceDispatcher, NuGetPSHost> CreateRunspace(IConsole console, string hostName)
        {
            DTE2 dte = ServiceLocator.GetGlobalService <DTE, DTE2>();

            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();

            initialSessionState.Variables.Add(
                new SessionStateVariableEntry(
                    "DTE",
                    dte,
                    "Visual Studio DTE automation object",
                    ScopedItemOptions.AllScope | ScopedItemOptions.Constant)
                );

            // this is used by the functional tests
            var sourceRepositoryProvider = ServiceLocator.GetComponentModelService <ISourceRepositoryProvider>();
            var solutionManager          = ServiceLocator.GetComponentModelService <ISolutionManager>();
            var sourceRepoTuple          = Tuple.Create <string, object>("SourceRepositoryProvider", sourceRepositoryProvider);
            var solutionManagerTuple     = Tuple.Create <string, object>("VsSolutionManager", solutionManager);

            Tuple <string, object>[] privateData = { sourceRepoTuple, solutionManagerTuple };

            var host = new NuGetPSHost(hostName, privateData)
            {
                ActiveConsole = console
            };

            var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);

            runspace.ThreadOptions = PSThreadOptions.Default;
            runspace.Open();

            //
            // Set this runspace as DefaultRunspace so I can script DTE events.
            //
            // WARNING: MSDN says this is unsafe. The runspace must not be shared across
            // threads. I need this to be able to use ScriptBlock for DTE events. The
            // ScriptBlock event handlers execute on DefaultRunspace.
            //
            Runspace.DefaultRunspace = runspace;

            return(Tuple.Create(new RunspaceDispatcher(runspace), host));
        }
        private static Tuple<RunspaceDispatcher, NuGetPSHost> CreateRunspace(IConsole console, string hostName)
        {
            DTE dte = ServiceLocator.GetInstance<DTE>();

            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
            initialSessionState.Variables.Add(
                new SessionStateVariableEntry(
                    "DTE",
                    (DTE2)dte,
                    "Visual Studio DTE automation object",
                    ScopedItemOptions.AllScope | ScopedItemOptions.Constant)
            );

            // this is used by the functional tests
            var sourceRepositoryProvider = ServiceLocator.GetInstance<ISourceRepositoryProvider>();
            var solutionManager = ServiceLocator.GetInstance<ISolutionManager>();
            var settings = ServiceLocator.GetInstance<ISettings>();
            var sourceRepoTuple = Tuple.Create<string, object>("SourceRepositoryProvider", sourceRepositoryProvider);
            var solutionManagerTuple = Tuple.Create<string, object>("VsSolutionManager", solutionManager);

            Tuple<string, object>[] privateData = new Tuple<string, object>[] { sourceRepoTuple, solutionManagerTuple  };

            var host = new NuGetPSHost(hostName, privateData)
            {
                ActiveConsole = console
            };

            var runspace = RunspaceFactory.CreateRunspace(host, initialSessionState);
            runspace.ThreadOptions = PSThreadOptions.Default;
            runspace.Open();

            //
            // Set this runspace as DefaultRunspace so I can script DTE events.
            //
            // WARNING: MSDN says this is unsafe. The runspace must not be shared across
            // threads. I need this to be able to use ScriptBlock for DTE events. The
            // ScriptBlock event handlers execute on DefaultRunspace.
            //
            Runspace.DefaultRunspace = runspace;

            return Tuple.Create(new RunspaceDispatcher(runspace), host);
        }
 public NuGetRawUserInterface(NuGetPSHost host)
 {
     _host = host;
 }
示例#10
0
        public void Initialize(IConsole console)
        {
            NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
            {
                ActiveConsole = console;
                if (_initialized.HasValue)
                {
                    if (_initialized.Value &&
                        console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }
                }
                else
                {
                    try
                    {
                        var result = _runspaceManager.GetRunspace(console, _name);
                        Runspace   = result.Item1;
                        _nugetHost = result.Item2;

                        _initialized = true;

                        if (console.ShowDisclaimerHeader)
                        {
                            DisplayDisclaimerAndHelpText();
                        }

                        UpdateWorkingDirectory();
                        await ExecuteInitScriptsAsync();

                        // check if PMC console is actually opened, then only hook to solution load/close events.
                        if (console is IWpfConsole)
                        {
                            // Hook up solution events
                            _solutionManager.Value.SolutionOpened += (_, __) => HandleSolutionOpened();
                            _solutionManager.Value.SolutionClosed += (o, e) =>
                            {
                                UpdateWorkingDirectory();

                                DefaultProject = null;

                                NuGetUIThreadHelper.JoinableTaskFactory.Run(CommandUiUtilities.InvalidateDefaultProjectAsync);
                            };
                        }
                        _solutionManager.Value.NuGetProjectAdded   += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.Value.NuGetProjectRenamed += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.Value.NuGetProjectUpdated += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.Value.NuGetProjectRemoved += (o, e) =>
                        {
                            UpdateWorkingDirectoryAndAvailableProjects();
                            // When the previous default project has been removed, _solutionManager.DefaultNuGetProjectName becomes null
                            if (_solutionManager.Value.DefaultNuGetProjectName == null)
                            {
                                // Change default project to the first one in the collection
                                SetDefaultProjectIndex(0);
                            }
                        };
                        // Set available private data on Host
                        SetPrivateDataOnHost(false);

                        StartAsyncDefaultProjectUpdate();
                    }
                    catch (Exception ex)
                    {
                        // catch all exception as we don't want it to crash VS
                        _initialized     = false;
                        IsCommandEnabled = false;
                        ReportError(ex);

                        ExceptionHelper.WriteErrorToActivityLog(ex);
                    }
                }
            });
        }
示例#11
0
 public Commander(NuGetPSHost host)
 {
     _host = host;
 }
 public NuGetRawUserInterface(NuGetPSHost host)
 {
     _host = host;
 }
示例#13
0
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue)
            {
                if (_initialized.Value && console.ShowDisclaimerHeader)
                {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else
            {
                try
                {
                    Tuple<RunspaceDispatcher, NuGetPSHost> result = _runspaceManager.GetRunspace(console, _name);
                    _runspace = result.Item1;
                    _nugetHost = result.Item2;
                    
                    _initialized = true;

                    if (console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    // Hook up solution events
                    _solutionManager.SolutionOpened += (o, e) =>
                    {
                        UpdateWorkingDirectory();
                        ExecuteInitScripts();
                    };
                    _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                }
                catch (Exception ex)
                {
                    // catch all exception as we don't want it to crash VS
                    _initialized = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue)
            {
                if (_initialized.Value && console.ShowDisclaimerHeader)
                {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else
            {
                try
                {
                    Tuple<RunspaceDispatcher, NuGetPSHost> result = _runspaceManager.GetRunspace(console, _name);
                    _runspace = result.Item1;
                    _nugetHost = result.Item2;
                    
                    _initialized = true;

                    if (console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    // Hook up solution events
                    _solutionManager.SolutionOpened += (o, e) =>
                    {
                        Task.Factory.StartNew(() =>
                            {
                                UpdateWorkingDirectory();
                                ExecuteInitScripts();
                            }, 
                            CancellationToken.None,
                            TaskCreationOptions.None,
                            TaskScheduler.Default);
                    };
                    _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectAdded += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectRenamed += (o, e) => UpdateWorkingDirectory();
                    _solutionManager.NuGetProjectRemoved += (o, e) => UpdateWorkingDirectory();

                    // Set available private data on Host
                    SetPrivateDataOnHost(false);
                }
                catch (Exception ex)
                {
                    // catch all exception as we don't want it to crash VS
                    _initialized = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
示例#15
0
        public void Initialize(IConsole console)
        {
            NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
            {
                ActiveConsole = console;
                if (_initialized.HasValue)
                {
                    if (_initialized.Value &&
                        console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }
                }
                else
                {
                    try
                    {
                        var result = _runspaceManager.GetRunspace(console, _name);
                        Runspace   = result.Item1;
                        _nugetHost = result.Item2;

                        _initialized = true;

                        if (console.ShowDisclaimerHeader)
                        {
                            DisplayDisclaimerAndHelpText();
                        }

                        UpdateWorkingDirectory();
                        await ExecuteInitScriptsAsync();

                        // check if PMC console is actually opened, then only hook to solution load/close events.
                        if (console is IWpfConsole)
                        {
                            // Hook up solution events
                            _solutionManager.SolutionOpened += (o, e) =>
                            {
                                _scriptExecutor.Reset();

                                // Solution opened event is raised on the UI thread
                                // Go off the UI thread before calling likely expensive call of ExecuteInitScriptsAsync
                                // Also, it uses semaphores, do not call it from the UI thread
                                Task.Run(delegate
                                {
                                    UpdateWorkingDirectory();
                                    return(ExecuteInitScriptsAsync());
                                });
                            };
                            _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                        }
                        _solutionManager.NuGetProjectAdded   += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.NuGetProjectRenamed += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.NuGetProjectUpdated += (o, e) => UpdateWorkingDirectoryAndAvailableProjects();
                        _solutionManager.NuGetProjectRemoved += (o, e) =>
                        {
                            UpdateWorkingDirectoryAndAvailableProjects();
                            // When the previous default project has been removed, _solutionManager.DefaultNuGetProjectName becomes null
                            if (_solutionManager.DefaultNuGetProjectName == null)
                            {
                                // Change default project to the first one in the collection
                                SetDefaultProjectIndex(0);
                            }
                        };
                        // Set available private data on Host
                        SetPrivateDataOnHost(false);
                    }
                    catch (Exception ex)
                    {
                        // catch all exception as we don't want it to crash VS
                        _initialized     = false;
                        IsCommandEnabled = false;
                        ReportError(ex);

                        ExceptionHelper.WriteErrorToActivityLog(ex);
                    }
                }
            });
        }
 public NuGetHostUserInterface(NuGetPSHost host)
 {
     UtilityMethods.ThrowIfArgumentNull(host);
     _host = host;
 }
示例#17
0
        public void Initialize(IConsole console)
        {
            ActiveConsole = console;

            if (_initialized.HasValue) {
                if (_initialized.Value && console.ShowDisclaimerHeader) {
                    DisplayDisclaimerAndHelpText();
                }
            }
            else {
                try {
                    Tuple<Runspace, NuGetPSHost> tuple = _runspaceManager.GetRunspace(console, _name);
                    _runspace = tuple.Item1;
                    _nugetHost = tuple.Item2;

                    _initialized = true;

                    // when initializing host from the dialog, we don't want to execute existing init scripts in the solution, if any.
                    if (console.ShowDisclaimerHeader) {
                        DisplayDisclaimerAndHelpText();
                    }

                    _solutionManager.SolutionOpened += (o, e) => {
                        UpdateWorkingDirectory();
                        ExecuteInitScripts();
                    };

                    UpdateWorkingDirectory();
                    ExecuteInitScripts();

                    _solutionManager.SolutionClosed += (o, e) => UpdateWorkingDirectory();
                }
                catch (Exception ex) {
                    // catch all exception as we don't want it to crash VS
                    _initialized = false;
                    IsCommandEnabled = false;
                    ReportError(ex);

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
 public NuGetHostUserInterface(NuGetPSHost host)
 {
     UtilityMethods.ThrowIfArgumentNull(host);
     _host = host;
 }
 public Commander(NuGetPSHost host)
 {
     _host = host;
 }