Exemplo n.º 1
0
        public void CalculateMapData(string savePath, NamespaceReferenceInfo namespaceReferenceInfo, string serviceName)
        {
            ProjectInfoBase         project            = LanguageMapBase.GetCurrent.GetActiveProject();
            List <MapDataClassInfo> MapDataClassInfoes = new List <MapDataClassInfo>();
            List <string>           usingsOfClass      = new List <string>();
            string fileName = "";

            foreach (ProjectItemInfoBase projectItem in LanguageMapBase.GetCurrent.GetAllProjectItemsWithoutServices(project.ProjectItemsInfoBase))
            {
                if (projectItem.GetFileCount() == 0)
                {
                    continue;
                }
                fileName = projectItem.GetFileName(0);
                if (Path.GetExtension(fileName).ToLower() == ".java")
                {
                    string dir = Path.GetDirectoryName(fileName);
                    if (File.Exists(Path.Combine(dir, "setting.signalgo")) || !File.Exists(fileName))
                    {
                        continue;
                    }
                    string fileText = File.ReadAllText(fileName, Encoding.UTF8);
                    if (fileText.Contains("ModelMappAttribute(") || fileText.Contains("ModelMapp("))
                    {
                        using (StringReader streamReader = new StringReader(fileText))
                        {
                            string line = "";
                            bool   lineReadClassStarted = false;
                            bool   findStartBlock       = false;
                            bool   canSetBody           = false;
                            int    findEndBlock         = int.MaxValue;

                            MapDataClassInfo mapDataClassInfo = new MapDataClassInfo();
                            StringBuilder    builder          = new StringBuilder();
                            while ((line = streamReader.ReadLine()) != null)
                            {
                                string lineResult = line;
                                //if (lineResult.Trim().StartsWith("using ") && lineResult.Trim().EndsWith(";") && !lineResult.Contains("("))
                                //{
                                //    var uses = GetListOfUsing(lineResult);
                                //    mapDataClassInfo.Usings.AddRange(uses);
                                //    usingsOfClass.AddRange(uses);
                                //}

                                if (findStartBlock && (line.Contains("{") || line.Contains("}")))
                                {
                                    int countPlus = line.Count(x => x == '{') - line.Count(x => x == '}');

                                    if (findEndBlock == int.MaxValue)
                                    {
                                        findEndBlock = countPlus;
                                    }
                                    else
                                    {
                                        findEndBlock += countPlus;
                                    }

                                    if (findEndBlock <= 0)
                                    {
                                        mapDataClassInfo.Body = builder.ToString();
                                        builder.Clear();
                                        MapDataClassInfo find = MapDataClassInfoes.FirstOrDefault(x => x.Name == mapDataClassInfo.Name && (usingsOfClass.Contains(serviceName) || x.ServiceName == serviceName));
                                        if (find != null)
                                        {
                                            find.Body += Environment.NewLine + mapDataClassInfo.Body;
                                        }
                                        else
                                        {
                                            MapDataClassInfoes.Add(mapDataClassInfo);
                                        }

                                        lineReadClassStarted = false;
                                        findStartBlock       = false;
                                        canSetBody           = false;
                                        findEndBlock         = int.MaxValue;
                                        mapDataClassInfo     = new MapDataClassInfo();
                                    }
                                    else
                                    {
                                        if (canSetBody)
                                        {
                                            builder.AppendLine(lineResult);
                                        }
                                        canSetBody = true;
                                    }
                                }
                                else if (lineReadClassStarted && line.Contains(" class "))
                                {
                                    string[] splitInheritance = line.Split(':', ',');
                                    //multiple inheritance
                                    if (splitInheritance.Length > 1)
                                    {
                                        foreach (string item in splitInheritance.Skip(1))
                                        {
                                            Tuple <string, string> nameSpaceAndName = GetNameSpaceAndName(item);
                                            if (!string.IsNullOrEmpty(nameSpaceAndName.Item1))
                                            {
                                                usingsOfClass.Add(nameSpaceAndName.Item1);
                                            }

                                            mapDataClassInfo.Inheritances.Add(nameSpaceAndName.Item2);
                                        }
                                    }
                                    findStartBlock = true;
                                }
                                else if (!lineResult.TrimStart().StartsWith("//") && (lineResult.Contains("ModelMappAttribute(") || lineResult.Contains("ModelMapp(")))
                                {
                                    int length = "ModelMappAttribute(".Length;
                                    int index  = lineResult.IndexOf("ModelMappAttribute(");
                                    if (index == -1)
                                    {
                                        index  = lineResult.IndexOf("ModelMapp(");
                                        length = "ModelMapp(".Length;
                                    }


                                    string[] split = SplitWithIgnoreQuotes(lineResult.Substring(index + length), ",");
                                    foreach (string item in split)
                                    {
                                        if (item.ToLower().Contains("maptotype") || item.Contains("typeof"))
                                        {
                                            Tuple <string, string> nameSpaceAndName = GetNameSpaceAndName(item.Split('=').LastOrDefault());
                                            if (!string.IsNullOrEmpty(nameSpaceAndName.Item1))
                                            {
                                                usingsOfClass.Add(nameSpaceAndName.Item1);
                                                mapDataClassInfo.ServiceName = nameSpaceAndName.Item1;
                                            }

                                            mapDataClassInfo.Name = nameSpaceAndName.Item2.Replace("typeof", "").Replace("(", "").Replace(")", "")
                                                                    .Replace("[", "").Replace("]", "").Trim();
                                        }
                                        else if (item.Contains("IsEnabledNotifyPropertyChangedBaseClass"))
                                        {
                                            if (item.Contains("false"))
                                            {
                                                mapDataClassInfo.IsEnabledNotifyPropertyChangedBaseClass = false;
                                            }
                                        }
                                        else if (item.Contains("IsIncludeInheritances"))
                                        {
                                            if (item.Contains("false"))
                                            {
                                                mapDataClassInfo.IsIncludeInheritances = false;
                                            }
                                        }
                                        else if (item.Contains("IgnoreProperties"))
                                        {
                                            Tuple <string, string> nameSpaceAndName = GetNameSpaceAndName(item.Split('=').LastOrDefault());
                                            Regex           reg     = new Regex("\".*?\"");
                                            MatchCollection matches = reg.Matches(nameSpaceAndName.Item2);
                                            foreach (object str in matches)
                                            {
                                                mapDataClassInfo.IgnoreProperties.Add(str.ToString().Replace("\"", ""));
                                            }
                                        }
                                    }
                                    lineReadClassStarted = true;
                                }
                                else if (canSetBody)
                                {
                                    builder.AppendLine(lineResult);
                                }
                            }
                        }
                    }
                }
            }

            string folder = "";

            EnumNames.Clear();
            EnumNames.AddRange(namespaceReferenceInfo.Enums);

            foreach (IGrouping <string, ClassReferenceInfo> groupInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.ModelLevel || x.Type == ClassReferenceType.InterfaceLevel).GroupBy(x => x.NameSpace))
            {
                folder = Path.Combine(savePath, groupInfo.Key);
                foreach (ClassReferenceInfo modelInfo in groupInfo)
                {
                    //key is full name and value 1 is name space and value 2 is name
                    Dictionary <string, Dictionary <string, string> > namespaces = new Dictionary <string, Dictionary <string, string> >();
                    StringBuilder builderResult = new StringBuilder();
                    builderResult.AppendLine(LanguageMapBase.GetCurrent.GetAutoGeneratedText());
                    builderResult.AppendLine($"package {serviceName}.{modelInfo.NameSpace};");
                    builderResult.AppendLine("*$-SignalGoNameSpaces-!*");
                    fileName = Path.Combine(FixDirectoryName(folder), GetFileNameFromClassName(modelInfo.NormalizedName));
                    GenerateModelClass(modelInfo, "    ", builderResult, MapDataClassInfoes.Where(x => x.Name == modelInfo.NormalizedName).FirstOrDefault(), serviceName, namespaces);
                    StringBuilder nameSpacesResult = new StringBuilder();
                    List <string> added            = new List <string>();
                    foreach (KeyValuePair <string, Dictionary <string, string> > item in namespaces)
                    {
                        foreach (KeyValuePair <string, string> keyValue in item.Value)
                        {
                            string fullName = keyValue.Key;
                            if (fullName.Contains("DontAddOnSignalGoAddreference"))
                            {
                                continue;
                            }
                            if (fullName != "java.util.Date" && fullName != "java.util.List" && fullName != "java.util.Dictionary")
                            {
                                fullName = serviceName + "." + keyValue.Key + ".*";
                            }
                            if (added.Contains(fullName))
                            {
                                continue;
                            }
                            else
                            {
                                added.Add(fullName);
                            }
                            if (keyValue.Key != modelInfo.NameSpace && keyValue.Key != "System")
                            {
                                nameSpacesResult.AppendLine($"import {fullName};");
                            }
                        }
                    }
                    builderResult.Replace("*$-SignalGoNameSpaces-!*", nameSpacesResult.ToString());
                    File.WriteAllText(fileName, builderResult.ToString(), Encoding.ASCII);
                }
            }

            //foreach (ClassReferenceInfo httpClassInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.HttpServiceLevel))
            //{
            //    //key is full name and value 1 is name space and value 2 is name
            //    Dictionary<string, Dictionary<string, string>> namespaces = new Dictionary<string, Dictionary<string, string>>();
            //    StringBuilder builder = new StringBuilder();
            //    builder.AppendLine(LanguageMapBase.GetCurrent.GetAutoGeneratedText());
            //    builder.AppendLine("*$-SignalGoNameSpaces-!*");
            //    //builder.AppendLine("import { List } from 'src/app/SharedComponents/linqts';");
            //    GenerateHttpServiceClass(httpClassInfo, "    ", builder, serviceName, namespaces);
            //    StringBuilder nameSpacesResult = new StringBuilder();

            //    foreach (KeyValuePair<string, Dictionary<string, string>> item in namespaces)
            //    {
            //        foreach (KeyValuePair<string, string> keyValue in item.Value)
            //        {
            //            nameSpacesResult.AppendLine($"import {{{ keyValue.Value }}} from \"./{keyValue.Key}/{keyValue.Value}\"");
            //        }
            //    }

            //    //string result = builder.ToString();
            //    //foreach (string space in namespaces)
            //    //{
            //    //    if (result.Contains(serviceName + "." + space))
            //    //        continue;
            //    //    result = result.Replace(space, serviceName + "." + space);
            //    //}
            //    builder.Replace("*$-SignalGoNameSpaces-!*", nameSpacesResult.ToString());
            //    File.WriteAllText(Path.Combine(savePath, httpClassInfo.ServiceName.Replace("/", "").Replace("\\", "") + "Service.ts"), builder.ToString(), Encoding.UTF8);
            //}


            foreach (IGrouping <string, EnumReferenceInfo> groupInfo in namespaceReferenceInfo.Enums.GroupBy(x => x.NameSpace))
            {
                folder = Path.Combine(savePath, groupInfo.Key);

                foreach (EnumReferenceInfo enumInfo in groupInfo)
                {
                    StringBuilder builderResult = new StringBuilder();
                    builderResult.AppendLine(LanguageMapBase.GetCurrent.GetAutoGeneratedText());

                    fileName = Path.Combine(FixDirectoryName(folder), GetFileNameFromClassName(enumInfo.Name));
                    GenerateModelEnum(serviceName, enumInfo, "    ", builderResult);
                    File.WriteAllText(fileName, builderResult.ToString(), Encoding.ASCII);
                }
            }



            //return builderResult.ToString();
        }