示例#1
0
        public async Task <SyntaxTree> AddNewContext(NewDbContextTemplateModel dbContextTemplateModel)
        {
            if (dbContextTemplateModel == null)
            {
                throw new ArgumentNullException(nameof(dbContextTemplateModel));
            }

            var templateName = "NewLocalDbContext.cshtml";
            var templatePath = _filesLocator.GetFilePath(templateName, TemplateFolders);

            Contract.Assert(File.Exists(templatePath));

            var templateContent = File.ReadAllText(templatePath);
            var templateResult  = await _templatingService.RunTemplateAsync(templateContent, dbContextTemplateModel);

            if (templateResult.ProcessingException != null)
            {
                throw new InvalidOperationException(string.Format(
                                                        MessageStrings.TemplateProcessingError,
                                                        templatePath,
                                                        templateResult.ProcessingException.Message));
            }

            var newContextContent = templateResult.GeneratedText;

            var sourceText = SourceText.From(newContextContent);

            return(CSharpSyntaxTree.ParseText(sourceText));
        }
        public async Task AddFileFromTemplateAsync(string outputPath, string templateName,
                                                   [NotNull] IEnumerable <string> templateFolders,
                                                   [NotNull] object templateModel)
        {
            ExceptionUtilities.ValidateStringArgument(outputPath, "outputPath");
            ExceptionUtilities.ValidateStringArgument(templateName, "templateName");

            var templatePath = _filesLocator.GetFilePath(templateName, templateFolders);

            if (string.IsNullOrEmpty(templatePath))
            {
                throw new InvalidOperationException(string.Format(
                                                        "Template file {0} not found within search paths {1}",
                                                        templateName,
                                                        string.Join(";", templateFolders)));
            }

            Contract.Assert(File.Exists(templatePath));
            var templateContent = File.ReadAllText(templatePath);

            var templateResult = await _templatingService.RunTemplateAsync(templateContent, templateModel);

            if (templateResult.ProcessingException != null)
            {
                throw new InvalidOperationException(string.Format(
                                                        "There was an error running the template {0}: {1}",
                                                        templatePath,
                                                        templateResult.ProcessingException.Message));
            }

            using (var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(templateResult.GeneratedText)))
            {
                await AddFileHelper(outputPath, sourceStream);
            }
        }
示例#3
0
        public async Task AddFileFromTemplateAsync(string outputPath, string templateName,
                                                   IEnumerable <string> templateFolders,
                                                   object templateModel)
        {
            if (templateFolders == null)
            {
                throw new ArgumentNullException(nameof(templateFolders));
            }

            ExceptionUtilities.ValidateStringArgument(outputPath, "outputPath");
            ExceptionUtilities.ValidateStringArgument(templateName, "templateName");

            var templatePath = _filesLocator.GetFilePath(templateName, templateFolders);

            if (string.IsNullOrEmpty(templatePath))
            {
                throw new InvalidOperationException(string.Format(
                                                        MessageStrings.TemplateFileNotFound,
                                                        templateName,
                                                        string.Join(";", templateFolders)));
            }

            Debug.Assert(_fileSystem.FileExists(templatePath));
            var templateContent = _fileSystem.ReadAllText(templatePath);

            var templateResult = await _templatingService.RunTemplateAsync(templateContent, templateModel);

            if (templateResult.ProcessingException != null)
            {
                throw new InvalidOperationException(string.Format(
                                                        MessageStrings.TemplateProcessingError,
                                                        templatePath,
                                                        templateResult.ProcessingException.Message));
            }

            using (var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(templateResult.GeneratedText)))
            {
                await AddFileHelper(outputPath, sourceStream);
            }
        }