예제 #1
0
        public async Task <List <KeyValuePair <string, string> > > GenerateProjectFiles(GeneratorModel model)
        {
            var name = string.IsNullOrEmpty(model.TemplateShortName) ? DefaultTemplateName : model.TemplateShortName;

            var templateKey = new TemplateKey(name, model.TemplateVersion);

            if (!_mustacheConfig.GetTemplateKeys().Contains(templateKey))
            {
                throw new InvalidDataException($"Template with Name: {name} and Version: {model.TemplateVersion} doesn't exist");
            }

            Dictionary <string, string> dataView;

            using (Timing.Over(_logger, "GetDataView"))
            {
                dataView = await _mustacheConfig.GetDataView(templateKey, model);
            }

            var listOfFiles = new List <KeyValuePair <string, string> >();

            using (Timing.Over(_logger, "Rendering files"))
            {
                foreach (var file in _mustacheConfig.GetFilteredSourceSets(dataView, templateKey))
                {
                    if (file.Name.EndsWith(".csproj"))
                    {
                        var fileName = file.Name.Replace("ReplaceMe", model.ProjectName ?? "SteeltoeExample");
                        var output   = Render(file.Name, file.Text, dataView);
                        listOfFiles.Add(new KeyValuePair <string, string>(fileName, output));
                    }
                    else
                    {
                        var output = Render(file.Name, file.Text, dataView);
                        listOfFiles.Add(new KeyValuePair <string, string>(file.Name, output));
                    }
                }
            }

            return(listOfFiles);
        }
예제 #2
0
        public IEnumerable <SourceFile> GetFilteredSourceSets(Dictionary <string, string> dataView,
                                                              TemplateKey templateKey)
        {
            var settings             = _templateSettings[templateKey];
            var files                = settings.SourceSets;
            var exclusionExpressions = GetInclusionExpressions(dataView, settings.Schema);
            var excludedFiles        = new List <string>();

            foreach (var sourceFile in files)
            {
                if (sourceFile.Name.EndsWith("mustache.json"))
                {
                    excludedFiles.Add(sourceFile.Name);
                    continue;
                }

                // By default everything is included, unless there is a conditional inclusions
                // which causes it to be an exclusion when condition is not met.
                // Explicit inclusions override exclusions by condition not being met
                var explicitInclusions = new List <string>();
                foreach (var exclusionExpression in exclusionExpressions)
                {
                    if (exclusionExpression.IsMatch(sourceFile.Name))
                    {
                        if (exclusionExpression.IsInclusion)
                        {
                            explicitInclusions.Add(sourceFile.Name);
                        }
                        else
                        {
                            excludedFiles.Add(sourceFile.Name);
                        }
                    }
                }

                excludedFiles = excludedFiles.Except(explicitInclusions).ToList();
            }

            return(files.Where(f => excludedFiles.All(e => e != f.Name)));
        }
예제 #3
0
        public async Task <Dictionary <string, string> > GetDataView(TemplateKey templateKey, GeneratorModel model)
        {
            var settings       = _templateSettings[templateKey];
            var mustacheConfig = settings.Schema;

            var dataView = new Dictionary <string, string>();

            using (Timing.Over(_logger, "GetDataView-Rest"))
            {
                foreach (var dep in mustacheConfig.Params)
                {
                    dataView.Add(dep.Name, dep.DefaultValue);
                }

                var dependencies = model.GetDependencies();
                if (dependencies != null)
                {
                    foreach (var dependencyName in dependencies)
                    {
                        var key = mustacheConfig.Params.FirstOrDefault(k => k.Name.ToLower() == dependencyName)?.Name;
                        if (key != null)
                        {
                            dataView[key] = "True";
                        }
                    }
                }

                foreach (var version in mustacheConfig.Versions)
                {
                    dataView.Add(version.Name, version.DefaultValue);
                }

                if (model.SteeltoeVersion != null)
                {
                    const string steeltoeVersionName = "SteeltoeVersion";
                    var          steeltoeVersion     = mustacheConfig.Versions.FirstOrDefault(v => v.Name == steeltoeVersionName);
                    var          validChoice         =
                        steeltoeVersion?.Choices.Any(choice => model.SteeltoeVersion.ToLower() == choice.Choice);
                    if (validChoice == true)
                    {
                        dataView[steeltoeVersionName] = model.SteeltoeVersion.ToLower();
                    }
                    else
                    {
                        throw new InvalidDataException("SteeltoeVersion: InvalidParamValues");
                    }
                }

                if (model.TargetFramework != null)
                {
                    const string targetFrameworkName = "TargetFrameworkVersion";
                    var          targetFramework     = mustacheConfig.Versions.FirstOrDefault(v => v.Name == targetFrameworkName);

                    var validChoice =
                        targetFramework?.Choices.Any(choice => model.TargetFramework.ToLower() == choice.Choice);

                    if (validChoice == true)
                    {
                        dataView[targetFrameworkName] = model.TargetFramework;
                    }
                }

                if (model.ProjectName != null)
                {
                    const string projectNamespaceName = "ProjectNameSpace";
                    if (dataView.ContainsKey(projectNamespaceName))
                    {
                        dataView[projectNamespaceName] = model.ProjectName;
                    }
                }
            }

            using (Timing.Over(_logger, "GetDataView-CalculatedParams"))
            {
                foreach (var(name, expression) in settings.EvaluationExpressions)
                {
                    var result = await expression.EvaluateExpressionAsync(dataView);

                    dataView.Add(name, result);
                }
            }

            return(dataView);
        }
예제 #4
0
 public MustacheConfigSchema GetSchema(TemplateKey templateKey)
 {
     return(_templateSettings[templateKey].Schema);
 }