コード例 #1
0
        public async Task UpgradeSolutionAsync(SolutionAccessor solution)
        {
            _logger.LogInformation("UpgradeSolution");
            if (solution.Metadata == null)
            {
                _logger.LogWarning("Solution metadata not found. Cannot upgrade.");
                return;
            }
            _logger.LogInformation($"Current version: {solution.Metadata.Version}");
            var latestVersion = (await _packageFeed.GetVersionsAsync(PackageName)).First();

            _logger.LogInformation($"Latest version: {latestVersion}");
            if (solution.Metadata.Version == latestVersion)
            {
                _logger.LogWarning("Using latest version. Nothing to upgrade");
                return;
            }

            var solutionsPath             = Path.Combine(StudioDirectoryPath, "Solutions");
            var latestPackageSolutionPath = Path.Combine(solutionsPath, $"{solution.Namespace}.{latestVersion}");

            if (Directory.Exists(latestPackageSolutionPath))
            {
                Directory.Delete(latestPackageSolutionPath, true);
            }
            var currentPackageSolutionPath = Path.Combine(solutionsPath, $"{solution.Namespace}.{solution.Metadata.Version}");

            if (Directory.Exists(currentPackageSolutionPath))
            {
                Directory.Delete(currentPackageSolutionPath, true);
            }

            await InitSolutionAsync(solution.Namespace, latestVersion, latestPackageSolutionPath);

            var latestPackageSolution = new SolutionAccessor(latestPackageSolutionPath);

            await CopyEnumsAsync(solution, latestPackageSolution);
            await CopyEntitiesAsync(solution, latestPackageSolution);

            await InitSolutionAsync(solution.Namespace, solution.Metadata.Version, currentPackageSolutionPath);

            var currentPackageSolution = new SolutionAccessor(currentPackageSolutionPath);

            await CopyEnumsAsync(solution, currentPackageSolution);
            await CopyEntitiesAsync(solution, currentPackageSolution);

            var upgradeRenames = await GetUpgradeRenames(currentPackageSolution, latestPackageSolution);

            RenameInSolution(solution, upgradeRenames);

            var templateResultRenames = await GetTemplateResultRenames(currentPackageSolution, latestPackageSolution);

            var renames = upgradeRenames.Concat(templateResultRenames).ToList();

            _logger.LogInformation($"Merging changes to {solution.Path}");
            await _fileMerge.MergeDirectoryAsync(currentPackageSolutionPath, latestPackageSolutionPath, solution.Path,
                                                 renames);

            _logger.LogInformation($"Completed");
        }
コード例 #2
0
        private async Task CopyEnumsAsync(SolutionAccessor fromSolution, SolutionAccessor toSolution)
        {
            var fromEnums = await fromSolution.GetEnumsAsync();

            var toEnums = await toSolution.GetEnumsAsync();

            foreach (var @enum in fromEnums)
            {
                var toEnum = toEnums.FirstOrDefault(e => e.Id == @enum.Id);
                if (toEnum == null)
                {
                    await toSolution.CreateEnumAsync(@enum);

                    var newEnum = (await toSolution.GetEnumsAsync())
                                  .FirstOrDefault(e => e.Id == @enum.Id);
                    await GenerateEnumAsync(toSolution, newEnum);
                    await GenerateEnumViewAsync(toSolution, new View(), newEnum);
                }
                else
                {
                    await toSolution.UpdateEnumAsync(@enum);

                    var newEnum = (await toSolution.GetEnumsAsync())
                                  .FirstOrDefault(e => e.Id == @enum.Id);
                    await UpgradeEnumAsync(toSolution, toEnum, newEnum);
                    await UpgradeEnumViewAsync(toSolution, new View(), toEnum, newEnum);
                }
            }
        }
コード例 #3
0
        private async Task CopyEntitiesAsync(SolutionAccessor fromSolution, SolutionAccessor toSolution)
        {
            var fromEntities = await fromSolution.GetEntitiesAsync();

            fromEntities = SortByDependency(fromEntities).ToList();
            var toEntities = await toSolution.GetEntitiesAsync();

            foreach (var entity in fromEntities)
            {
                var toEntity = toEntities.FirstOrDefault(e => e.Id == entity.Id);
                if (toEntity == null)
                {
                    await toSolution.CreateEntityAsync(entity);

                    var newEntity = (await toSolution.GetEntitiesAsync())
                                    .FirstOrDefault(e => e.Id == entity.Id);
                    await GenerateEntityAsync(toSolution, newEntity);
                    await GenerateViewAsync(toSolution, new View(), newEntity);
                }
                else
                {
                    await toSolution.UpdateEntityAsync(entity);

                    var newEntity = (await toSolution.GetEntitiesAsync())
                                    .FirstOrDefault(e => e.Id == entity.Id);
                    await UpgradeEntityAsync(toSolution, toEntity, newEntity);
                    await UpgradeViewAsync(toSolution, new View(), toEntity, newEntity);
                }
            }
        }
コード例 #4
0
        internal ProjectAccessor(SolutionAccessor solution, string path, string projectFile)
        {
            Solution = solution;
            Path     = path;
            var info = new FileInfo(projectFile);

            Namespace = Regex.Replace(info.Name, "\\.csproj$", "");
        }
コード例 #5
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);
        }
コード例 #6
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);
            }
        }
コード例 #7
0
 public EnumViewModel(View view, Models.Enum @enum, SolutionAccessor solution)
 {
     View     = view;
     Enum     = @enum;
     Solution = solution;
 }
コード例 #8
0
 public EntityModel(Entity entity, SolutionAccessor solution)
 {
     Entity   = entity;
     Solution = solution;
 }
コード例 #9
0
 public ViewModel(View view, Entity entity, SolutionAccessor solution)
 {
     View     = view;
     Entity   = entity;
     Solution = solution;
 }
コード例 #10
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);
        }
コード例 #11
0
        private string ProcessTemplate(CodeTemplate template, SolutionAccessor solutionAccessor, object model)
        {
            var templatePath = solutionAccessor.GetTemplatePath(template);

            return(_templateEngine.ProcessTemplate(templatePath, model));
        }
コード例 #12
0
 private void RenameInSolution(SolutionAccessor solution, IList <(string Left, string Right)> renames)
コード例 #13
0
 public EnumModel(Models.Enum @enum, SolutionAccessor solution)
 {
     Enum     = @enum;
     Solution = solution;
 }