public void DotNetTemplateMetadataMapper_Map(string templateJsonFile)
        {
            var content          = File.ReadAllText(templateJsonFile);
            var templateMetadata = DotNetTemplateMetadata.FromJson(content);
            var result           = new DotNetTemplateMetadataMapper().Map(templateMetadata);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Tags.Count() > 0);
            Assert.IsTrue(result.ConditionalTags.Count() > 0);
            Assert.IsTrue(result.ComputedTags.Count() > 0);
        }
        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);
        }
        public void DotNetTemplateMetadataMapper_MapGeneratedSymbols(string templateJsonFile)
        {
            var content          = File.ReadAllText(templateJsonFile);
            var templateMetadata = DotNetTemplateMetadata.FromJson(content);
            var result           = new DotNetTemplateMetadataMapper().Map(templateMetadata);

            Assert.IsNotNull(result);

            var httpPortConstant = result.ComputedTags.FirstOrDefault(x => x.Key == "HttpPortConstant");

            Assert.AreEqual("5000", httpPortConstant.Expression);

            var httpPortReplacer = result.ComputedTags.FirstOrDefault(x => x.Key == "5000");

            Assert.AreEqual("HttpPort != null ? HttpPort : HttpPortConstant", httpPortReplacer.Expression);

            var gitHubProjectLower = result.ComputedTags.FirstOrDefault(x => x.Key == "GitHubProjectLower");

            Assert.AreEqual("lowerCase(GitHubProject)", gitHubProjectLower.Expression);
        }
Beispiel #4
0
        public TemplateMetadata Map(DotNetTemplateMetadata dotNetMetadata)
        {
            var tags            = new List <Tag>();
            var conditionalTags = new List <ConditionalTag>();
            var computedTags    = new List <ComputedTag>();

            if (!string.IsNullOrWhiteSpace(dotNetMetadata.SourceName))
            {
                tags.Add(new Tag
                {
                    Key          = TemplateMetadataReader.PROJECT_NAME_KEY,
                    Name         = TemplateMetadataReader.PROJECT_NAME,
                    DefaultValue = dotNetMetadata.SourceName,
                    Regex        = dotNetMetadata.SourceName
                });
            }

            foreach (var symbol in dotNetMetadata.Symbols)
            {
                var    generator = symbol.Value;
                string dataType  = generator.Datatype?.ToString();

                if (generator.Type == SymbolType.Parameter)
                {
                    if (string.Compare(dataType, "bool", true) == 0)
                    {
                        conditionalTags.Add(BuildConditionalTag(symbol.Key, generator));
                    }
                    else if (string.Compare(dataType, "choice", true) == 0)
                    {
                        tags.Add(BuildOptionsTag(symbol.Key, generator));
                    }
                    else
                    {
                        tags.Add(BuildInputTag(symbol.Key, generator));
                    }
                }
                else if (generator.Type == SymbolType.Computed)
                {
                    computedTags.Add(BuildComputedTag(symbol.Key, generator));
                }
                else if (generator.Type == SymbolType.Derived)
                {
                    computedTags.AddRange(BuildComputedTagFromDerived(symbol.Key, generator));
                }
                else if (generator.Type == SymbolType.Generated)
                {
                    computedTags.AddRange(BuildComputedTagFromGenerated(symbol.Key, generator));
                }
            }

            foreach (var source in dotNetMetadata.Sources)
            {
                foreach (var modifier in source.Modifiers)
                {
                    ConditionalTag conditionalTag = conditionalTags.Find(x => modifier.Condition.Contains(x.Key));
                    if (conditionalTag != null)
                    {
                        if (modifier.Condition.Contains($"!{conditionalTag.Key}"))
                        {
                            if (modifier.Exclude != null)
                            {
                                conditionalTag.FilesToInclude = GetFilesToExclude(modifier);
                            }
                        }
                        else
                        {
                            if (modifier.Include != null)
                            {
                                conditionalTag.FilesToInclude = GetFilesToInclude(modifier);
                            }
                        }
                    }

                    ComputedTag computedTag = computedTags.Find(x => modifier.Condition.Contains(x.Key));
                    if (computedTag != null)
                    {
                        if (modifier.Condition.Contains($"!{computedTag.Key}"))
                        {
                            if (modifier.Exclude != null)
                            {
                                computedTag.FilesToInclude = GetFilesToExclude(modifier);
                            }
                        }
                        else
                        {
                            if (modifier.Include != null)
                            {
                                computedTag.FilesToInclude = GetFilesToInclude(modifier);
                            }
                        }
                    }
                }
            }

            return(new TemplateMetadata
            {
                TemplateType = "dotnet",
                Tags = tags,
                ConditionalTags = conditionalTags,
                ComputedTags = computedTags
            });
        }