Пример #1
0
        protected override sealed void ProcessRecord()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            try
            {
                // Record NuGetCmdlet executed
                NuGetPowerShellUsage.RaiseNuGetCmdletExecutedEvent();

                ProcessRecordCore();
            }
            catch (Exception ex)
            {
                ExceptionHelper.WriteErrorToActivityLog(ex);

                // unhandled exceptions should be terminating
                ErrorHandler.HandleException(ex, terminating: true);
            }
            finally
            {
                UnsubscribeEvents();
            }

            stopWatch.Stop();

            // Log total time elapsed except for Tab command
            if (!IsLoggingTimeDisabled)
            {
                LogCore(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_TotalTime, stopWatch.Elapsed));
            }
        }
Пример #2
0
        private async Task ExecuteInitPs1Async(string installPath, PackageIdentity identity)
        {
            try
            {
                var toolsPath = Path.Combine(installPath, "tools");
                if (Directory.Exists(toolsPath))
                {
                    AddPathToEnvironment(toolsPath);

                    var scriptPath = Path.Combine(toolsPath, PowerShellScripts.Init);
                    if (File.Exists(scriptPath))
                    {
                        NuGetPowerShellUsage.RaiseInitPs1LoadEvent(isPMC: _activeConsole is IWpfConsole);

                        if (_scriptExecutor.Value.TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                        {
                            // always execute init script on a background thread
                            await TaskScheduler.Default;

                            var request = new ScriptExecutionRequest(scriptPath, installPath, identity, project: null);

                            Runspace.Invoke(
                                request.BuildCommand(),
                                request.BuildInput(),
                                outputResults: true);

                            return;
                        }
                    }
                }

                _scriptExecutor.Value.TryMarkVisited(identity, PackageInitPS1State.NotFound);
            }
            catch (Exception ex)
            {
                // If execution of an init.ps1 scripts fails, do not let it crash our console.
                ReportError(ex);

                ExceptionHelper.WriteErrorToActivityLog(ex);
            }
        }
Пример #3
0
        private async Task OnSolutionExistsAndFullyLoadedAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            SolutionOpening?.Invoke(this, EventArgs.Empty);

            NuGetPowerShellUsage.RaiseSolutionOpenEvent();

            // although the SolutionOpened event fires, the solution may be only in memory (e.g. when
            // doing File - New File). In that case, we don't want to act on the event.
            if (!await IsSolutionOpenAsync())
            {
                return;
            }

            await EnsureNuGetAndVsProjectAdapterCacheAsync();

            SolutionOpened?.Invoke(this, EventArgs.Empty);

            _solutionOpenedRaised = true;
        }
Пример #4
0
        public bool Execute(IConsole console, string command, params object[] inputs)
        {
            if (console == null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            NuGetPowerShellUsage.RaiseCommandExecuteEvent(isPMC: console is IWpfConsole);

            // since install.ps1/uninstall.ps1 could depend on init scripts, so we need to make sure
            // to run it once for each solution
            NuGetUIThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await ExecuteInitScriptsAsync();
            });

            NuGetEventTrigger.Instance.TriggerEvent(NuGetEvent.PackageManagerConsoleCommandExecutionBegin);
            ActiveConsole = console;

            string fullCommand;

            if (ComplexCommand.AddLine(command, out fullCommand) &&
                !string.IsNullOrEmpty(fullCommand))
            {
                // create a new token source with each command since CTS aren't usable once cancelled.
                _tokenSource = new CancellationTokenSource();
                _token       = _tokenSource.Token;
                return(ExecuteHost(fullCommand, command, inputs));
            }

            return(false); // constructing multi-line command
        }
Пример #5
0
        public void Initialize(IConsole console)
        {
            NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
            {
                ActiveConsole = console;
                if (_initialized.HasValue)
                {
                    if (_initialized.Value &&
                        console.ShowDisclaimerHeader)
                    {
                        DisplayDisclaimerAndHelpText();
                    }
                }
                else
                {
                    try
                    {
                        bool _isPmc = console is IWpfConsole;
                        var result  = _runspaceManager.GetRunspace(console, _name);
                        Runspace    = result.Item1;
                        _nugetHost  = result.Item2;

                        _initialized = true;

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

                        UpdateWorkingDirectory();

                        if (!PowerShellLoaded)
                        {
                            var telemetryEvent = new PowerShellLoadedEvent(isPmc: _isPmc, psVersion: Runspace.PSVersion.ToString());
                            TelemetryActivity.EmitTelemetryEvent(telemetryEvent);
                            PowerShellLoaded = true;
                        }

                        NuGetPowerShellUsage.RaisePowerShellLoadEvent(isPMC: _isPmc);

                        await ExecuteInitScriptsAsync();

                        // check if PMC console is actually opened, then only hook to solution load/close events.
                        if (_isPmc)
                        {
                            // 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);
                    }
                }
            });
        }
Пример #6
0
        private void OnBeforeClosing()
        {
            NuGetPowerShellUsage.RaiseSolutionCloseEvent();

            SolutionClosing?.Invoke(this, EventArgs.Empty);
        }
 void ConsoleContainer_Loaded(object sender, RoutedEventArgs e)
 {
     NuGetPowerShellUsage.RaisePmcWindowsLoadEvent(isLoad: true);
 }