public override void ExecuteCommandImpl(object sender, EventArgs args)
        {
            var project = projectManager.SelectedProject;

            string selectedMethodName     = projectManager.MethodName;
            string serverMethodFolderPath = projectManager.ServerMethodFolderPath;
            string selectedFolderPath     = projectManager.SelectedFolderPath;
            string projectConfigPath      = projectManager.ProjectConfigPath;

            var        projectConfiguration = projectConfigurationManager.Load(projectConfigPath);
            MethodInfo methodInformation    = projectConfiguration.MethodInfos.FirstOrDefault(m => m.MethodName == selectedMethodName);

            if (methodInformation == null)
            {
                throw new Exception($"Configurations for the {selectedMethodName} method not found.");
            }

            var view       = dialogFactory.GetCreateCodeItemView(this.codeProviderFactory.GetCodeItemProvider(project.CodeModel.Language), projectConfiguration.UseVSFormatting);
            var viewResult = view.ShowDialog();

            if (viewResult?.DialogOperationResult != true)
            {
                return;
            }

            string codeItemPath = selectedFolderPath.Substring(serverMethodFolderPath.IndexOf(serverMethodFolderPath) + serverMethodFolderPath.Length);

            codeItemPath = Path.Combine(codeItemPath, viewResult.FileName);

            if (methodInformation.PartialClasses.Contains(codeItemPath, StringComparer.InvariantCultureIgnoreCase) ||
                methodInformation.ExternalItems.Contains(codeItemPath, StringComparer.InvariantCultureIgnoreCase))
            {
                throw new Exception($"Code item already exists.");
            }

            ICodeProvider codeProvider = codeProviderFactory.GetCodeProvider(project.CodeModel.Language, projectConfiguration);
            CodeInfo      codeItemInfo = codeProvider.CreateCodeItemInfo(methodInformation, viewResult.FileName, viewResult.SelectedCodeType, viewResult.SelectedElementType, viewResult.IsUseVSFormattingCode);

            projectManager.AddItemTemplateToProjectNew(codeItemInfo, true, 0);

            if (viewResult.SelectedCodeType == CodeType.Partial)
            {
                methodInformation.PartialClasses.Add(codeItemInfo.Path);
            }
            else if (viewResult.SelectedCodeType == CodeType.External)
            {
                methodInformation.ExternalItems.Add(codeItemInfo.Path);
            }

            projectConfiguration.UseVSFormatting = viewResult.IsUseVSFormattingCode;
            projectConfigurationManager.Save(projectManager.ProjectConfigPath, projectConfiguration);
        }
Пример #2
0
        public void ExecuteCommandImpl_ShouldAddPartialClassesAndSaveConfigfile()
        {
            //Arange
            string  partialfileName = "testPartialfileName";
            string  methodName      = "methodName";
            Project selectedProject = Substitute.For <Project>();

            selectedProject.CodeModel.Language.Returns("C#");

            this.projectManager.MethodName.Returns(methodName);
            this.projectManager.SelectedProject.Returns(selectedProject);
            this.projectManager.ProjectConfigPath.Returns("ProjectConfigPath");
            MethodInfo testMethodInfo = new MethodInfo()
            {
                MethodName     = methodName,
                PartialClasses = new List <string>()
            };

            IProjectConfiguraiton projectConfiguraiton = Substitute.For <IProjectConfiguraiton>();

            projectConfiguraiton.MethodInfos.Returns(new List <MethodInfo>()
            {
                testMethodInfo
            });

            this.projectConfigurationManager.Load(Arg.Any <string>()).Returns(projectConfiguraiton);

            CreateCodeItemViewAdaper createCodeItemViewAdaper = new CreateCodeItemViewAdaper(partialfileName, MethodPlugin.Code.CodeType.Partial, true);

            this.dialogFactory.GetCreateCodeItemView(Arg.Any <ICodeItemProvider>(), Arg.Any <bool>()).Returns(createCodeItemViewAdaper);

            ICodeProvider codeProvider = Substitute.For <ICodeProvider>();
            CodeInfo      codeItemInfo = new CodeInfo()
            {
                Code = "code",
                Path = @"path\testPartialfileName.cs"
            };

            codeProviderFactory.GetCodeProvider("C#", projectConfiguraiton).Returns(codeProvider);
            codeProvider.CreateCodeItemInfo(testMethodInfo, partialfileName, Arg.Any <MethodPlugin.Code.CodeType>(), Arg.Any <CodeElementType>(), Arg.Any <bool>()).Returns(codeItemInfo);

            //Act
            this.createCodeItemCmd.ExecuteCommandImpl(null, null);

            //Assert
            Assert.IsTrue(testMethodInfo.PartialClasses.Contains(codeItemInfo.Path));
            this.projectConfigurationManager.Received().Save("ProjectConfigPath", projectConfiguraiton);
        }