Esempio n. 1
0
        private static void ApplyStepOverrides(JobsTemplateReference reference, JobsTemplate template)
        {
            // Select by job name.
            var byJobNames =
                (reference.JobSelectors ?? new List <JobSelector>(0))
                .Join(inner: (template.Jobs ?? new List <IJob>(0)).Cast <Job>(),
                      outerKeySelector: (JobSelector jobSelector) => jobSelector.Name,
                      innerKeySelector: (Job job) => job.Name,
                      resultSelector: (JobSelector jobSelector, Job job) => new { Selector = jobSelector, Job = job })
                .ToArray();

            // Apply overrides from job selectors.
            foreach (var byJobName in byJobNames)
            {
                ApplyStepOverrides(byJobName.Selector.StepOverrides, byJobName.Job.Steps);
            }

            // Apply unqualified overrides.
            var allStepLists =
                (template.Jobs ?? new List <IJob>(0))
                .Cast <Job>()
                .Select((Job job) => job.Steps ?? new List <IStep>(0))
                .Concat(new[] { template.Steps ?? new List <IStep>(0) })
                .ToArray();

            foreach (List <IStep> stepList in allStepLists)
            {
                ApplyStepOverrides(reference.StepOverrides, stepList);
            }
        }
Esempio n. 2
0
        private void ResolveTemplates(List <IJob> jobs, String defaultRoot, CancellationToken cancellationToken, ref Int32 fileCount)
        {
            for (int i = 0; i < (jobs?.Count ?? 0);)
            {
                if (jobs[i] is JobsTemplateReference)
                {
                    // Load the template.
                    var    reference        = jobs[i] as JobsTemplateReference;
                    String templateFilePath = m_fileProvider.ResolvePath(defaultRoot: defaultRoot, path: reference.Name);
                    PipelineFile <JobsTemplate> templateFile = LoadFile <JobsTemplate, JobsTemplateConverter>(templateFilePath, reference.Parameters, cancellationToken, ref fileCount);
                    JobsTemplate template = templateFile.Object;

                    // Resolve template references within the template.
                    if (template.Jobs != null)
                    {
                        foreach (Job job in template.Jobs)
                        {
                            if (job.Variables != null)
                            {
                                ResolveTemplates(job.Variables, defaultRoot: templateFile.Directory, cancellationToken: cancellationToken, fileCount: ref fileCount);
                            }
                        }
                    }
                    else if (template.Steps != null)
                    {
                        ResolveTemplates(template.Steps, defaultRoot: templateFile.Directory, cancellationToken: cancellationToken, fileCount: ref fileCount);
                    }

                    // Merge the template.
                    ApplyStepOverrides(reference, template);
                    jobs.RemoveAt(i);
                    if (template.Jobs != null)
                    {
                        jobs.InsertRange(i, template.Jobs);
                        i += template.Jobs.Count;
                    }
                    else if (template.Steps != null)
                    {
                        var newJob = new Job {
                            Steps = template.Steps
                        };
                        jobs.Insert(i, newJob);
                        i++;
                    }
                }
                else
                {
                    // Resolve nested template references.
                    var job = jobs[i] as Job;
                    if (job.Variables != null)
                    {
                        ResolveTemplates(job.Variables, defaultRoot, cancellationToken, ref fileCount);
                    }

                    if (job.Steps != null)
                    {
                        ResolveTemplates(job.Steps, defaultRoot, cancellationToken, ref fileCount);
                    }

                    i++;
                }
            }
        }