Ejemplo n.º 1
0
        private void ExecuteInitPs1(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) &&
                        _scriptExecutor.TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                    {
                        var request = new ScriptExecutionRequest(scriptPath, installPath, identity, project: null);

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

                        return;
                    }
                }

                _scriptExecutor.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.WriteToActivityLog(ex);
            }
        }
        void RunScriptInternal(RunInitScriptParams message)
        {
            var version  = NuGet.Versioning.NuGetVersion.Parse(message.PackageVersion);
            var identity = new PackageIdentity(message.PackageId, version);

            var request = new ScriptExecutionRequest(
                message.ScriptPath,
                message.InstallPath,
                identity,
                null);

            try {
                InvokePowerShellInternal(
                    request.BuildCommand(),
                    request.BuildInput()
                    );
            } catch (Exception ex) {
                if (message.ThrowOnFailure)
                {
                    throw;
                }

                Log(LogLevel.Warning, ex.Message);
            }
        }
Ejemplo n.º 3
0
        private async Task ExecuteScriptCoreAsync(ScriptExecutionRequest request)
        {
            var console = OutputConsoleProvider.CreatePowerShellConsole();
            var host    = await Host.GetValueAsync();

            // Host.Execute calls powershell's pipeline.Invoke and blocks the calling thread
            // to switch to powershell pipeline execution thread. In order not to block the UI thread,
            // go off the UI thread. This is important, since, switches to UI thread,
            // using SwitchToMainThreadAsync will deadlock otherwise
            await Task.Run(() => host.Execute(console, request.BuildCommand(), request.BuildInput()));
        }
Ejemplo n.º 4
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);
            }
        }