/// <summary> /// 应用扩展项目 /// </summary> /// <param name="opertion">模型对象</param> /// <param name="ProfileName"></param> /// <param name="StereotypeName"></param> private void ApplyStereotype(IElement opertion, string ProfileName, string StereotypeName) { var q = opertion.GetModelStore().ProfileManager.AllProfiles.ToList(); foreach (IProfile ip in q) { if (ip.Name == ProfileName) { foreach (IStereotype istype in ip.Stereotypes) { if (istype.Name == StereotypeName && opertion.ApplicableStereotypes.ToList().Contains(istype)) { var stereotypeInstaces = opertion.AppliedStereotypes.ToList(); int count = 0; foreach (IStereotypeInstance isInstance in stereotypeInstaces) { if (isInstance.Name == istype.Name) { count = count + 1; break; } } if (count == 0) { opertion.ApplyStereotype(istype); } } } } } }
/// <summary> /// 模型是否存在 /// </summary> /// <param name="element">模型对象</param> /// <param name="modeType">模型类型0==类 1==接口 2==包</param> /// <param name="modelName">模型名称</param> /// <returns>模型是否存在</returns> private bool isExistModel(IElement element, int modeType, string modelName) { bool isExistModel = false; var packages = element.GetModelStore().Root.NestedPackages; foreach (IPackage package in packages) { isExistModel = isExistModelInPackage(package, modeType, modelName); if (isExistModel == true) { break; } } return isExistModel; }
/// <summary> /// 通过模型类型移除项目 /// </summary> /// <param name="element">模型对象</param> /// <param name="modeType">模型类型0==类 1==接口 2==包</param> /// <param name="modelName">模型名称</param> private void RemoveProjectItemByType(IElement element, int modeType, string modelName) { EnvDTE80.DTE2 dte = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2; //获得TextTemplateBindings //项目路径 string projectsPath = GetStereotypePropertyValue(element.GetModelStore().Root, "TextTemplateBindings", "ProjectPath"); ; //文件名称 string targetsNames = GetStereotypePropertyValue(element.GetModelStore().Root, "TextTemplateBindings", "TargetName"); //类项目名称 string classProjectName = GetTemplateProjectName(projectsPath, "TelChinaClassTemplate"); //DTO项目名称 string dtoProjectName = GetTemplateProjectName(projectsPath, "TelChinaClassToDTOTemplate"); //接口项目名称 string interfaceProjectName = GetTemplateProjectName(projectsPath, "TelChinaInterfaceTemplate"); //实现项目名称 string contractProjectName = GetTemplateProjectName(projectsPath, "TelChinaImplTemplate"); //类文件集合 List<string> classFileNameList = new List<string>(); //DTO文件集合 List<string> dtoFileNameList = new List<string>(); //类文件集合 List<string> interfaceFileNameList = new List<string>(); //DTO文件集合 List<string> contractFileNameList = new List<string>(); if (modeType == 2) { classFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Package); dtoFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Package); interfaceFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Package); contractFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Package); } else { classFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Class); dtoFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.DTO); interfaceFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Interface); contractFileNameList = GetFileName(targetsNames, modelName, TemplateEnum.Contract); } //取得project var sln = dte.Solution; bool isExistFile = false; if (classFileNameList.Count > 0) { foreach (string fileName in classFileNameList) { isExistFile = IsExistProjectItem(sln, classProjectName, fileName); if (isExistFile) { break; } } } if (dtoFileNameList.Count > 0 && !isExistFile) { foreach (string fileName in dtoFileNameList) { isExistFile = IsExistProjectItem(sln, dtoProjectName, fileName); if (isExistFile) { break; } } } if (interfaceFileNameList.Count > 0 && !isExistFile) { foreach (string fileName in interfaceFileNameList) { isExistFile = IsExistProjectItem(sln, interfaceProjectName, fileName); if (isExistFile) { break; } } } if (contractFileNameList.Count > 0 && !isExistFile) { foreach (string fileName in contractFileNameList) { isExistFile = IsExistProjectItem(sln, contractProjectName, fileName); if (isExistFile) { break; } } } if (!isExistFile) { return; } //移除类 if (classFileNameList.Count > 0) { foreach (string fileName in classFileNameList) { RemoveItemFromSolution(sln, classProjectName, fileName); } } //移除DTO if (dtoFileNameList.Count > 0) { foreach (string fileName in dtoFileNameList) { RemoveItemFromSolution(sln, dtoProjectName, fileName); } } //移除接口 if (interfaceFileNameList.Count > 0) { foreach (string fileName in interfaceFileNameList) { RemoveItemFromSolution(sln, interfaceProjectName, fileName); } } //移除实现 if (contractFileNameList.Count > 0) { foreach (string fileName in contractFileNameList) { RemoveItemFromSolution(sln, contractProjectName, fileName); } } }