protected override void Arrange()
            {
                var expectedTemplateString = @"using EdFi.Ods.Xml.XmlMetadata;";

                var templatesByTemplateName = new Dictionary <string, string>
                {
                    { templateName, expectedTemplateString },
                    { "template2", "template2 string" }
                };

                _templateWriterData = new TemplateWriterData
                {
                    Model = new
                    {
                        NamespaceName = "testNamespace",
                        ClassName     = "testClass",
                        InterfaceName = "testInterfaceName"
                    },
                    TemplateSet = new TemplateSet {
                        Name = templateName
                    }
                };

                _mustacheTemplateProvider = Stub <IMustacheTemplateProvider>();

                A.CallTo(() => _mustacheTemplateProvider.GetMustacheTemplates())
                .Returns(templatesByTemplateName);

                _templateWriter = new TemplateWriter(_mustacheTemplateProvider);
            }
        public async Task WriteAsync(TemplateWriterData codeGenTemplateWriterData, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            string template = _templatesByTemplateName.Value[codeGenTemplateWriterData.TemplateSet.Name];

            _logger.Debug($"Rendering {codeGenTemplateWriterData.TemplateSet.Name}.");

            string destinationFolder = Path.GetDirectoryName(codeGenTemplateWriterData.OutputPath);

            if (!Directory.Exists(destinationFolder))
            {
                _logger.Debug($"Creating destination folder {destinationFolder}.");
                Directory.CreateDirectory(destinationFolder);
            }

            string renderedTemplate = await _stubbleRender
                                      .Value
                                      .RenderAsync(template, codeGenTemplateWriterData.Model, _templatesByTemplateName.Value, _renderSettings)
                                      .ConfigureAwait(false);

            await using var streamWriter = new StreamWriter(codeGenTemplateWriterData.OutputPath);

            _logger.Debug($"Writing {codeGenTemplateWriterData.OutputPath}.");

            await streamWriter.WriteAsync(renderedTemplate)
            .ConfigureAwait(false);
        }
Beispiel #3
0
            public void Should_throw_error()
            {
                _container = ContainerHelper.CreateContainer();

                // Arrange
                _templateWriterData = new TemplateWriterData
                {
                    Model = new
                    {
                        NamespaceName = namespaceName,
                        ClassName     = "testClass",
                        InterfaceName = "testInterfaceName"
                    },
                    TemplateSet = new TemplateSet {
                        Name = templateName
                    },
                    OutputPath = $"{Environment.CurrentDirectory}\\Output.cs"
                };

                // Act
                var exception = Assert.ThrowsAsync <KeyNotFoundException>(
                    async() => await _container.Resolve <ITemplateWriter>()
                    .WriteAsync(_templateWriterData, CancellationToken.None));

                // Assert
                exception.Message.ShouldMatch($"The given key '{templateName}' was not present in the dictionary.");
            }
        public async Task ProcessAsync(AssemblyData assemblyData, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            _logger.Info($"Processing started for assembly: {assemblyData.AssemblyName} in folder: {assemblyData.Path}");

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var templateContext = _templateContextProvider.Create(assemblyData);

            foreach (var templateSet in _templateSetProvider.GetTemplatesByName(assemblyData.TemplateSet))
            {
                templateContext.With(templateSet);

                _logger.Debug($"Generating code for template {templateSet.Name}");

                var generator = _generatorProvider.GetGeneratorByDriverName(templateSet.Driver);

                if (generator != null)
                {
                    var templateStopwatch = new Stopwatch();
                    templateStopwatch.Start();

                    var model = generator.Generate(templateContext);

                    _logger.Debug($"Generating template data for template {templateSet.Name}");

                    string outputPath = Path.Combine(assemblyData.Path, templateSet.OutputPath);

                    var codeGenWriterData = new TemplateWriterData
                    {
                        TemplateSet = templateSet,
                        Model       = model,
                        OutputPath  = outputPath
                    };

                    _logger.Debug($"Writing template data for path {outputPath}");

                    await _templateWriter.WriteAsync(codeGenWriterData, cancellationToken)
                    .ConfigureAwait(false);

                    templateStopwatch.Stop();

                    _logger.Debug(
                        $"Code generation for template {templateSet.Name} completed in {templateStopwatch.Elapsed.ToString()}.");
                }
                else
                {
                    _logger.Debug($"TemplateSet model not found for {templateSet.Name}, skipping.");
                }
            }

            stopWatch.Stop();

            _logger.Info($"Processing complete for assembly: {assemblyData.AssemblyName} in {stopWatch.Elapsed.ToString()}.");
        }