protected bool InternalCollectProjectClasses(CodeNamespace parentCodeNamespace)
 {
     foreach (EnvDTE.CodeElement ce in parentCodeNamespace.Members)
     {
         if (ce.Kind == EnvDTE.vsCMElement.vsCMElementClass)
         {
             if (ce.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
             {
                 SolutionCodeElement sce = new SolutionCodeElement()
                 {
                     CodeElementName     = ce.Name,
                     CodeElementFullName = ce.FullName,
                     CodeElementRef      = ce
                 };
                 ComboItemsSourceCodeElements.Add(sce);
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             if (ce.Kind == EnvDTE.vsCMElement.vsCMElementNamespace)
             {
                 CodeNamespace cn = (CodeNamespace)ce;
                 if (!InternalCollectProjectClasses(cn))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Exemplo n.º 2
0
 public SelectFolderViewModel(DTE2 dte, ITextTemplating textTemplating, IVsThreadedWaitDialogFactory dialogFactory, SolutionCodeElement selectedDbContext, string rootFolder, string JavaScriptsTmplst, string BatchJavaScriptsTmplst) : base()
 {
     this.T4Folders         = new ObservableCollection <string>();
     this.Dte               = dte;
     this.SelectedDbContext = selectedDbContext;
     this.DialogFactory     = dialogFactory;
     this.TextTemplating    = textTemplating;
     this.T4RootFolder      = Path.Combine(rootFolder, JavaScriptsTmplst);
     this.BatchRootFolder   = Path.Combine(rootFolder, BatchJavaScriptsTmplst);
     this.OnContextChanged  = new ContextChangedService();
 }
        public override void CollectProjectClasses(SolutionProject sp)
        {
            base.CollectProjectClasses(sp);
            if (sp == null)
            {
                return;
            }
            if (sp.ProjectRef == null)
            {
                return;
            }

            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();



            if (sp.ProjectRef.CodeModel == null)
            {
                return;
            }
            foreach (EnvDTE.CodeElement ce in sp.ProjectRef.CodeModel.CodeElements)
            {
                if (ce.Kind == EnvDTE.vsCMElement.vsCMElementClass)
                {
                    if (ce.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
                    {
                        if ((ce as CodeClass).IsDerivedFrom["System.Data.Entity.DbContext"] || (ce as CodeClass).IsDerivedFrom["Microsoft.EntityFrameworkCore.DbContext"])
                        {
                            SolutionCodeElement sce = new SolutionCodeElement()
                            {
                                CodeElementName     = ce.Name,
                                CodeElementFullName = ce.FullName,
                                CodeElementRef      = ce
                            };
                            ComboItemsSourceCodeElements.Add(sce);
                        }
                    }
                }
                else
                {
                    if (ce.Kind == EnvDTE.vsCMElement.vsCMElementNamespace)
                    {
                        EnvDTE.CodeNamespace cn = (EnvDTE.CodeNamespace)ce;
                        InternalCollectProjectClasses(cn);
                    }
                }
            }
        }
        public override void CollectProjectClasses(SolutionProject sp)
        {
            base.CollectProjectClasses(sp);
            if (sp == null)
            {
                return;
            }
            if (sp.ProjectRef == null)
            {
                return;
            }
            if (sp.ProjectRef.CodeModel == null)
            {
                return;
            }
            List <SolutionCodeElement> localList = new List <SolutionCodeElement>();

            foreach (EnvDTE.CodeElement ce in sp.ProjectRef.CodeModel.CodeElements)
            {
                if (ce.Kind == EnvDTE.vsCMElement.vsCMElementClass)
                {
                    if (ce.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
                    {
                        SolutionCodeElement sce = new SolutionCodeElement()
                        {
                            CodeElementName     = ce.Name,
                            CodeElementFullName = ce.FullName,
                            CodeElementRef      = ce
                        };
                        //ComboItemsSourceCodeElements.Add(sce);
                        localList.Add(sce);
                    }
                }
                else
                {
                    if (ce.Kind == EnvDTE.vsCMElement.vsCMElementNamespace)
                    {
                        CodeNamespace cn = (CodeNamespace)ce;
                        InternalCollectProjectClasses(cn, localList);
                    }
                }
            }
            localList.Sort((a, b) => string.Compare(a.CodeElementFullName, b.CodeElementFullName));
            localList.ForEach(ce => ComboItemsSourceCodeElements.Add(ce));
        }
 protected bool InternalCollectProjectClasses(EnvDTE.CodeNamespace parentCodeNamespace)
 {
     foreach (EnvDTE.CodeElement ce in parentCodeNamespace.Members)
     {
         if (ce.Kind == EnvDTE.vsCMElement.vsCMElementClass)
         {
             if (ce.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
             {
                 if ((ce as CodeClass).IsDerivedFrom["System.Data.Entity.DbContext"] || (ce as CodeClass).IsDerivedFrom["Microsoft.EntityFrameworkCore.DbContext"])
                 {
                     //////// additional checkup here
                     SolutionCodeElement sce = new SolutionCodeElement()
                     {
                         CodeElementName     = ce.Name,
                         CodeElementFullName = ce.FullName,
                         CodeElementRef      = ce
                     };
                     ComboItemsSourceCodeElements.Add(sce);
                 }
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             if (ce.Kind == EnvDTE.vsCMElement.vsCMElementNamespace)
             {
                 EnvDTE.CodeNamespace cn = (EnvDTE.CodeNamespace)ce;
                 if (!InternalCollectProjectClasses(cn))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Exemplo n.º 6
0
        public static void UpdateDbContext(DTE2 Dte, Project DestinationProject, SolutionCodeElement SelectedDbContext, DbContextSerializable dbContextSerializable, ModelViewSerializable modelViewSerializable,
                                           string ContextItemViewName, string T4SelectedFolder,
                                           string DestinationProjectRootFolder,
                                           string DestinationFolder,
                                           string DestinationSubFolder,
                                           string FileName, string FileExtension,
                                           string GenerateText)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            if (modelViewSerializable.ViewName == ContextItemViewName)
            {
                dbContextSerializable.CommonStaffs = modelViewSerializable.CommonStaffs;
                if (dbContextSerializable.CommonStaffs == null)
                {
                    dbContextSerializable.CommonStaffs = new List <CommonStaffSerializable>();
                }
                CommonStaffSerializable commonStaffSerializable = dbContextSerializable.CommonStaffs
                                                                  .Where(c => c.FileType == T4SelectedFolder)
                                                                  .FirstOrDefault();
                if (commonStaffSerializable != null)
                {
                    commonStaffSerializable.Extension = FileExtension;
                }
            }
            else
            {
                ModelViewSerializable existedModelViewSerializable =
                    dbContextSerializable.ModelViews.FirstOrDefault(mv => mv.ViewName == modelViewSerializable.ViewName);
                if (modelViewSerializable.CommonStaffs != null)
                {
                    CommonStaffSerializable commonStaffSerializable =
                        modelViewSerializable.CommonStaffs
                        .Where(c => c.FileType == T4SelectedFolder)
                        .FirstOrDefault();
                    if (commonStaffSerializable != null)
                    {
                        commonStaffSerializable.Extension = FileExtension;
                    }
                }
                if (existedModelViewSerializable != null)
                {
                    existedModelViewSerializable.CommonStaffs     = modelViewSerializable.CommonStaffs;
                    existedModelViewSerializable.UIFormProperties = modelViewSerializable.UIFormProperties;
                    existedModelViewSerializable.UIListProperties = modelViewSerializable.UIListProperties;
                }
                else
                {
                    dbContextSerializable.ModelViews.Add(modelViewSerializable);
                }
            }
            string projectName = "";

            if (SelectedDbContext.CodeElementRef != null)
            {
                if (SelectedDbContext.CodeElementRef.ProjectItem != null)
                {
                    projectName =
                        SelectedDbContext.CodeElementRef.ProjectItem.ContainingProject.UniqueName;
                }
            }
            string SolutionDirectory = System.IO.Path.GetDirectoryName(Dte.Solution.FullName);

            if (!string.IsNullOrEmpty(projectName))
            {
                string locFileName = Path.Combine(projectName, SelectedDbContext.CodeElementFullName, "json");
                locFileName = locFileName.Replace("\\", ".");
                locFileName = Path.Combine(SolutionDirectory, locFileName);
                string jsonString = JsonConvert.SerializeObject(dbContextSerializable);
                File.WriteAllText(locFileName, jsonString);
            }
            string FlNm = "";

            if (string.IsNullOrEmpty(DestinationSubFolder))
            {
                FlNm = Path.Combine(
                    DestinationProjectRootFolder,
                    DestinationFolder);
            }
            else
            {
                FlNm = Path.Combine(
                    DestinationProjectRootFolder,
                    DestinationFolder,
                    DestinationSubFolder);
            }
            System.IO.Directory.CreateDirectory(FlNm);
            FlNm = Path.Combine(FlNm, FileName + FileExtension);
            File.WriteAllText(FlNm, GenerateText);
            DestinationProject.ProjectItems.AddFromFile(FlNm);
        }
Exemplo n.º 7
0
        /// <summary>
        /// GetPrimaryKeyProperties returns the list of properties which are used as a Primary Key
        /// SolutionCodeElement.CodeElementRef holds CodeElement object that can be typecasted to CodeProperty
        /// </summary>
        /// <remarks>
        /// the following rules are applied:
        /// if KeyAttribute is found then "Id"-property and "ClassName"+"Id"-property will be ignoried
        /// if KeyAttribute is not found but there is "Id"-property then "ClassName"+"Id"-property will be ignoried
        /// if there is no KeyAttribute, "Id"-property and "ClassName"+"Id"-property then List<SolutionCodeElement> will be returned with Count==0
        /// </remarks>
        protected List <SolutionCodeElement> GetPrimaryKeyProperties(CodeClass cc)
        {
            List <SolutionCodeElement> result = new List <SolutionCodeElement>();
            int currOrder = -1;

            foreach (CodeElement ce in cc.Members)
            {
                currOrder++;
                if (ce.Kind != vsCMElement.vsCMElementProperty)
                {
                    continue;
                }
                CodeProperty cp = ce as CodeProperty;
                if (cp.Access != vsCMAccess.vsCMAccessPublic)
                {
                    continue;
                }
                if (cp.Type == null)
                {
                    continue;
                }
                if (cp.Type.CodeType == null)
                {
                    continue;
                }
                bool isNotMapped = false;
                foreach (CodeElement cea in cp.Attributes)
                {
                    CodeAttribute ca = cea as CodeAttribute;
                    if (ca.FullName.Contains("System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute"))
                    {
                        isNotMapped = true;
                        break;
                    }
                }
                if (isNotMapped)
                {
                    continue;
                }
                CodeTypeRef ctf = cp.Type;
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
                {
                    continue;
                }
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType)
                {
                    continue;
                }

                foreach (CodeElement cea in cp.Attributes)
                {
                    CodeAttribute ca = cea as CodeAttribute;
                    if (ca.FullName.Contains("System.ComponentModel.DataAnnotations.KeyAttribute"))
                    {
                        SolutionCodeElement sce = new SolutionCodeElement()
                        {
                            Order               = currOrder,
                            CodeElementName     = ce.Name,
                            CodeElementFullName = ce.FullName,
                            CodeElementRef      = ce
                        };
                        result.Add(sce);
                        break;
                    }
                }
            }
            if (result.Count > 0)
            {
                return(result);
            }
            foreach (CodeElement ce in cc.Members)
            {
                if (ce.Kind != vsCMElement.vsCMElementProperty)
                {
                    continue;
                }
                CodeProperty cp = ce as CodeProperty;
                if (cp.Access != vsCMAccess.vsCMAccessPublic)
                {
                    continue;
                }
                if (cp.Type == null)
                {
                    continue;
                }
                if (cp.Type.CodeType == null)
                {
                    continue;
                }
                CodeTypeRef ctf = cp.Type;
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
                {
                    continue;
                }
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType)
                {
                    continue;
                }

                if ("Id".Equals(cp.Name, StringComparison.OrdinalIgnoreCase))
                {
                    SolutionCodeElement sce = new SolutionCodeElement()
                    {
                        Order               = currOrder,
                        CodeElementName     = ce.Name,
                        CodeElementFullName = ce.FullName,
                        CodeElementRef      = ce
                    };

                    result.Add(sce);
                    return(result);
                }
            }
            if (result.Count > 0)
            {
                return(result);
            }
            string[] names    = cc.FullName.Split(new char[] { '.' });
            string   typeName = names[names.Length - 1];

            string propName = typeName + "Id";

            foreach (CodeElement ce in cc.Members)
            {
                if (ce.Kind != vsCMElement.vsCMElementProperty)
                {
                    continue;
                }
                CodeProperty cp = ce as CodeProperty;
                if (cp.Access != vsCMAccess.vsCMAccessPublic)
                {
                    continue;
                }
                if (cp.Type == null)
                {
                    continue;
                }
                if (cp.Type.CodeType == null)
                {
                    continue;
                }
                CodeTypeRef ctf = cp.Type;
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
                {
                    continue;
                }
                if (ctf.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType)
                {
                    continue;
                }
                if (propName.Equals(cp.Name, StringComparison.OrdinalIgnoreCase))
                {
                    SolutionCodeElement sce = new SolutionCodeElement()
                    {
                        Order               = currOrder,
                        CodeElementName     = ce.Name,
                        CodeElementFullName = ce.FullName,
                        CodeElementRef      = ce
                    };
                    result.Add(sce);
                    return(result);
                }
            }
            return(result);
        }