public TemplateMetadata GetMetadata(AppConfiguration.Template template)
        {
            TemplateMetadata metadata = null;

            var metadataFile = GetMetadataFile(template);

            if (metadataFile != null && !string.IsNullOrEmpty(metadataFile.Content))
            {
                try
                {
                    if (metadataFile.Name.EndsWith(DotNetTemplateMetadata.FILE_NAME, StringComparison.InvariantCultureIgnoreCase))
                    {
                        metadata = new DotNetTemplateMetadataMapper().Map(DotNetTemplateMetadata.FromJson(metadataFile.Content));
                    }
                    else
                    {
                        metadata = JsonSerializer.Deserialize <TemplateMetadata>(metadataFile.Content);
                    }
                }
                catch (Exception ex)
                {
                    var error = $"`{metadataFile.Name}` in `{template.SourceUrl}` must be in JSON";
                    Console.WriteLine(error + Environment.NewLine + ex.ToString());
                    throw new TemplateException(error, ex);
                }

                // Make sure the tags have keys. Names can be used to substitute keys.
                if (metadata.Tags != null)
                {
                    foreach (var tag in metadata.Tags.Where(x => string.IsNullOrEmpty(x.Key)))
                    {
                        tag.Key = tag.Name;
                    }
                    metadata.Tags = metadata.Tags.Where(x => !string.IsNullOrEmpty(x.Key));
                }

                if (metadata.ConditionalTags != null)
                {
                    foreach (var tag in metadata.ConditionalTags.Where(x => string.IsNullOrEmpty(x.Key)))
                    {
                        tag.Key = tag.Name;
                    }
                    metadata.ConditionalTags = metadata.ConditionalTags.Where(x => !string.IsNullOrEmpty(x.Key));
                }

                if (metadata.ComputedTags != null)
                {
                    metadata.ComputedTags = metadata.ComputedTags.Where(x => !string.IsNullOrEmpty(x.Key));
                }
            }

            return(metadata);
        }
        private TemplateFile GetMetadataFile(AppConfiguration.Template template)
        {
            var templateSource = _templateSources.FirstOrDefault(x => string.Equals(x.SourceType, template?.SourceType, StringComparison.InvariantCultureIgnoreCase));

            if (templateSource != null)
            {
                if (template.TemplateType?.ToLower() == "dotnet")
                {
                    return(templateSource.GetFile(DotNetTemplateMetadata.FILE_NAME, template.SourceUrl, template.SourceDirectory + DotNetTemplateMetadata.FILE_PATH, template.SourceBranch));
                }
                else
                {
                    return(templateSource.GetFile(TemplateMetadata.FILE_NAME, template.SourceUrl, template.SourceDirectory, template.SourceBranch));
                }
            }

            return(null);
        }
예제 #3
0
        public ActionResult <TemplateMetadata> Get([FromServices] ITemplateMetadataReader templateReader, [FromBody] AppConfiguration.Template template)
        {
            TemplateMetadata metadata = null;

            try
            {
                metadata = templateReader.GetMetadata(template);
            }
            catch (TemplateException ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Content(ex.Message));
            }

            metadata ??= new TemplateMetadata();
            if (template != null)
            {
                var tags = metadata.Tags?.ToList() ?? new List <Tag>();

                if (!tags.Any(x => x.Name == TemplateMetadataReader.PROJECT_NAME || x.Key == TemplateMetadataReader.PROJECT_NAME_KEY))
                {
                    tags.Insert(0, new Tag
                    {
                        Key             = TemplateMetadataReader.PROJECT_NAME_KEY,
                        Name            = TemplateMetadataReader.PROJECT_NAME,
                        DefaultValue    = TemplateMetadataReader.DEFAULT_PROJECT_NAME,
                        ValidationRegex = @"^[\w\-. ]+$",
                        ValidationError = "Must be a valid filename",
                    });
                }

                metadata.Tags = tags;
            }

            return(metadata);
        }
예제 #4
0
        public ProjectMetadata BuildProjectMetadata(Dictionary <string, string> formData, TemplateMetadata metadata, AppConfiguration.Template template)
        {
            var tags            = _templateReader.GetTags(metadata);
            var conditionalTags = _templateReader.GetConditionalTags(metadata);

            foreach (var key in formData.Keys.Where(x => tags.ContainsKey(x)))
            {
                string value = formData[key]?.ToString();
                try
                {
                    if (tags[key] is int)
                    {
                        tags[key] = int.TryParse(value, out int intValue) ? intValue : tags[key];
                    }
                    else if (tags[key] is float)
                    {
                        tags[key] = float.TryParse(value, out float floatValue) ? floatValue : tags[key];
                    }
                    else
                    {
                        tags[key] = value;
                    }
                }
                catch (Exception)
                {
                    tags[key] = value;
                }
            }

            foreach (var key in formData.Keys.Where(x => conditionalTags.ContainsKey(x)))
            {
                if (bool.TryParse(formData[key]?.ToString(), out bool value))
                {
                    conditionalTags[key] = value;
                }
            }

            var nonComputedTags = tags.ToDictionary(x => x.Key, x => x.Value)
                                  .Union(conditionalTags.ToDictionary(x => x.Key, x => (object)x.Value))
                                  .ToDictionary(x => x.Key, x => x.Value);

            var computedTags     = _templateReader.GetComputedTags(metadata, nonComputedTags);
            var boolComputedTags = computedTags.Where(x => x.Value is bool).ToDictionary(x => x.Key, x => (bool)x.Value);
            var booleanTags      = conditionalTags.Union(boolComputedTags).ToDictionary(x => x.Key, x => x.Value);

            var allTags = nonComputedTags
                          .Union(computedTags.ToDictionary(x => x.Key, x => x.Value))
                          .ToDictionary(x => x.Key, x => x.Value);

            string projectName = TemplateMetadataReader.DEFAULT_PROJECT_NAME;

            if (nonComputedTags.ContainsKey(TemplateMetadataReader.PROJECT_NAME_KEY))
            {
                projectName = nonComputedTags[TemplateMetadataReader.PROJECT_NAME_KEY].ToString();
            }
            else
            {
                var projectNameTag = metadata.Tags.FirstOrDefault(x => x.Name == TemplateMetadataReader.PROJECT_NAME);
                if (projectNameTag != null)
                {
                    projectName = nonComputedTags[projectNameTag.Key].ToString();
                }
            }

            return(new ProjectMetadata
            {
                ProjectName = projectName,
                TemplateType = metadata.TemplateType,
                TemplateSourceType = template.SourceType,
                TemplateSourceUrl = template.SourceUrl,
                TemplateSourceDirectory = template.SourceDirectory,
                TemplateSourceBranch = template.SourceBranch,
                FilesToExclude = _templateReader.GetFilesToExclude(metadata, booleanTags),
                Tags = allTags,
                TagRegexes = _templateReader.GetTagRegexes(metadata)
            });
        }