public async Task RunAsync()

in tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Program.cs [127:257]


        public async Task<ExitCondition> RunAsync(
            string organization,
            string project,
            string prefix,
            string path,
            string endpoint,
            string repository,
            string branch,
            string agentPool,
            string convention,
            int[] variableGroups,
            IEnumerable<string> serviceConnections,
            string devOpsPath,
            bool whatIf,
            bool open,
            bool destroy,
            bool noSchedule,
            bool setManagedVariables,
            bool overwriteTriggers,
            CancellationToken cancellationToken)
        {
            try
            {
                logger.LogDebug("Creating context.");

                // Fall back to a form of prefix if DevOps path is not specified
                var devOpsPathValue = string.IsNullOrEmpty(devOpsPath) ? $"\\{prefix}" : devOpsPath;

                var context = new PipelineGenerationContext(
                    this.logger,
                    organization,
                    project,
                    endpoint,
                    repository,
                    branch,
                    agentPool,
                    variableGroups,
                    devOpsPathValue,
                    prefix,
                    whatIf,
                    noSchedule,
                    setManagedVariables,
                    overwriteTriggers
                    );

                var pipelineConvention = GetPipelineConvention(convention, context);
                var components = ScanForComponents(path, pipelineConvention.SearchPattern);

                if (components.Count() == 0)
                {
                    logger.LogWarning("No components were found.");
                    return ExitCondition.NoComponentsFound;
                }

                logger.LogInformation("Found {0} components", components.Count());

                if (HasPipelineDefinitionNameDuplicates(pipelineConvention, components))
                {
                    return ExitCondition.DuplicateComponentsFound;
                }

                var definitions = new List<BuildDefinition>();
                foreach (var component in components)
                {
                    logger.LogInformation("Processing component '{0}' in '{1}'.", component.Name, component.Path);
                    if (destroy)
                    {
                        var definition = await pipelineConvention.DeleteDefinitionAsync(component, cancellationToken);
                    }
                    else
                    {
                        var definition = await pipelineConvention.CreateOrUpdateDefinitionAsync(component, cancellationToken);

                        if (open)
                        {
                            OpenBrowser(definition.GetWebUrl());
                        }

                        definitions.Add(definition);
                    }
                }

                var serviceConnectionObjects = await context.GetServiceConnectionsAsync(serviceConnections, cancellationToken);

                foreach (var serviceConnection in serviceConnectionObjects)
                {
                    // Get set of permissions for the service connection
                    JsonNode pipelinePermissions = await context.GetPipelinePermissionsAsync(serviceConnection.Id, cancellationToken);

                    var pipelines = pipelinePermissions["pipelines"].AsArray();
                    var pipelineIdsWithPermissions = new HashSet<int>(pipelines.Select(p => p["id"].GetValue<int>()));

                    int definitionsToAdd = 0;
                    foreach (var definition in definitions)
                    {
                        // Check this pipeline has permissions
                        if (!pipelineIdsWithPermissions.Contains(definition.Id))
                        {
                            pipelines.Add(
                                new JsonObject
                                {
                                    ["id"] = definition.Id,
                                    ["authorized"] = true,
                                    ["authorizedBy"] = null,
                                    ["authorizedOn"] = null
                                }
                            );

                            definitionsToAdd++;
                        }
                    }

                    logger.LogInformation("'{0}' pipelines already have permissions to service connection '{1}'. Need to grant permission to '{2}' more.", pipelineIdsWithPermissions.Count, serviceConnection.Id, definitionsToAdd);

                    if (definitionsToAdd > 0)
                    {
                        logger.LogInformation("Granting permissions for '{0}' definitions to service connection '{1}'.", definitionsToAdd, serviceConnection.Id);
                        // Update the permissions if we added anything
                        await context.UpdatePipelinePermissionsAsync(serviceConnection.Id, pipelinePermissions, cancellationToken);
                    }
                }

                return ExitCondition.Success;

            }
            catch (Exception ex)
            {
                logger.LogCritical(ex, "BOOM! Something went wrong, try running with --debug.");
                return ExitCondition.Exception;
            }
        }