Exemplo n.º 1
0
        public async Task <bool> ExecuteAsync(
            PackageIdentity identity,
            string installPath,
            string relativeScriptPath,
            DotNetProject project,
            INuGetProjectContext nuGetProjectContext,
            bool throwOnFailure,
            CancellationToken token)
        {
            var scriptPath = Path.Combine(installPath, relativeScriptPath);

            if (File.Exists(scriptPath))
            {
                if (scriptPath.EndsWith(PowerShellScripts.Init, StringComparison.OrdinalIgnoreCase) &&
                    !TryMarkVisited(identity, PackageInitPS1State.FoundAndExecuted))
                {
                    return(true);
                }

                var consoleNuGetProjectContext = nuGetProjectContext as ConsoleHostNuGetProjectContext;
                if (consoleNuGetProjectContext != null &&
                    consoleNuGetProjectContext.IsExecutingPowerShellCommand)
                {
                    var message = new RunScriptParams {
                        ScriptPath     = scriptPath,
                        InstallPath    = installPath,
                        PackageId      = identity.Id,
                        PackageVersion = identity.Version.ToNormalizedString(),
                        Project        = project.CreateProjectInformation(),
                        ThrowOnFailure = throwOnFailure
                    };
                    await RunScriptWithRemotePowerShellConsoleHost(message, nuGetProjectContext, throwOnFailure, token);
                }
                else
                {
                    var logMessage = GettextCatalog.GetString("Executing script file '{0}'...", scriptPath);
                    nuGetProjectContext.Log(MessageLevel.Info, logMessage);

                    var message = new RunInitScriptParams {
                        ScriptPath     = scriptPath,
                        InstallPath    = installPath,
                        PackageId      = identity.Id,
                        PackageVersion = identity.Version.ToNormalizedString(),
                        ThrowOnFailure = throwOnFailure
                    };
                    await RunInitScriptWithRemotePowerShellConsoleHost(message, nuGetProjectContext, throwOnFailure, token);
                }

                return(true);
            }
            else
            {
                if (scriptPath.EndsWith(PowerShellScripts.Init, StringComparison.OrdinalIgnoreCase))
                {
                    TryMarkVisited(identity, PackageInitPS1State.NotFound);
                }
            }
            return(false);
        }