Exemplo n.º 1
0
        private async Task CreateFileAsync(CodeTemplate template, object model, SolutionAccessor solution, IGenerationObject generationObject)
        {
            var filePath = GetTemplateFilePath(template, solution, generationObject);
            var content  = ProcessTemplate(template, solution, model);

            if (!string.IsNullOrEmpty(template.InsertAfter))
            {
                var fileContent = await ReadFileAsync(filePath);

                if (!fileContent.Contains(content))
                {
                    fileContent = fileContent.Replace(template.InsertAfter + Environment.NewLine,
                                                      template.InsertAfter + Environment.NewLine + content + Environment.NewLine);

                    await WriteFileAsync(filePath, fileContent);
                }
            }
            else
            {
                await WriteFileAsync(filePath, content);
            }
        }
Exemplo n.º 2
0
        private async Task UpgradeFileAsync(CodeTemplate template, object oldModel, object newModel, SolutionAccessor solution, IGenerationObject oldGenerationObject, IGenerationObject newGenerationObject)
        {
            var oldContent  = ProcessTemplate(template, solution, oldModel);
            var newContent  = ProcessTemplate(template, solution, newModel);
            var oldFilePath = GetTemplateFilePath(template, solution, oldGenerationObject);
            var newFilePath = GetTemplateFilePath(template, solution, newGenerationObject);

            if (!File.Exists(oldFilePath))
            {
                _logger.LogWarning($"File {oldFilePath} is not found. Skip upgrade");
                return;
            }
            var oldFileContent = await ReadFileAsync(oldFilePath);

            var newFileContent = _merge.Merge(oldContent, newContent, oldFileContent);

            if (oldFilePath != newFilePath)
            {
                File.Delete(oldFilePath);
            }
            if (oldFileContent == newFileContent && oldFilePath == newFilePath)
            {
                return;
            }
            await WriteFileAsync(newFilePath, newFileContent);
        }
Exemplo n.º 3
0
        private string GetTemplateFilePath(CodeTemplate template, SolutionAccessor solution, IGenerationObject generationObject)
        {
            var dir           = FindDirectory(solution.Path, template.FilePath);
            var fileName      = generationObject.ProcessTemplate(template.FileName);
            var filePath      = Path.Combine(dir, fileName);
            var fileDirectory = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(fileDirectory))
            {
                Directory.CreateDirectory(fileDirectory);
            }
            return(filePath);
        }