Exemplo n.º 1
0
        public static string CalculateMapData(NamespaceReferenceInfo namespaceReferenceInfo, string serviceName)
        {
            Project project = BaseLanguageMap.GetActiveProject();
            List <MapDataClassInfo> MapDataClassInfoes = new List <MapDataClassInfo>();
            List <string>           usingsOfClass      = new List <string>();

            foreach (ProjectItem projectItem in BaseLanguageMap.GetAllProjectItemsWithoutServices(project.ProjectItems))
            {
                if (projectItem.FileCount == 0)
                {
                    continue;
                }
                string fileName = projectItem.FileNames[0];
                if (Path.GetExtension(fileName).ToLower() == ".cs")
                {
                    string dir = Path.GetDirectoryName(fileName);
                    if (File.Exists(Path.Combine(dir, "setting.signalgo")))
                    {
                        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("("))
                                {
                                    string[] uses = CsharpLanguageMap.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 = CsharpLanguageMap.GetNameSpaceAndName(item);
                                            if (!string.IsNullOrEmpty(nameSpaceAndName.Item1))
                                            {
                                                usingsOfClass.Add(nameSpaceAndName.Item1);
                                            }

                                            mapDataClassInfo.Inheritances.Add(nameSpaceAndName.Item2);
                                        }
                                    }
                                    findStartBlock = true;
                                    //builder.AppendLine(lineResult);
                                }
                                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 = CsharpLanguageMap.SplitWithIgnoreQuotes(lineResult.Substring(index + length), ",");
                                    foreach (string item in split)
                                    {
                                        if (item.ToLower().Contains("maptotype") || item.Contains("typeof"))
                                        {
                                            Tuple <string, string> nameSpaceAndName = CsharpLanguageMap.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 = CsharpLanguageMap.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);
                                }
                            }
                        }
                    }
                }
            }


            StringBuilder builderResult = new StringBuilder();

            builderResult.AppendLine(BaseLanguageMap.GetAutoGeneratedText());

            string[] usings = new string[] {
                "System",
                "System.Collections.Generic",
                "System.Net",
                "System.Net.Http.Headers",
                "System.Net.Http",
                "System.Text",
                "System.ComponentModel",
            };
            foreach (var item in usings)
            {
                if (!namespaceReferenceInfo.Usings.Any(x => x.Equals(item, StringComparison.OrdinalIgnoreCase)))
                {
                    namespaceReferenceInfo.Usings.Add(item);
                }
            }
            foreach (string item in namespaceReferenceInfo.Usings)
            {
                if (item.StartsWith("SignalGo.".Trim()))
                {
                    continue;
                }
                builderResult.AppendLine("using " + item + ";");
            }

            usingsOfClass.Add(namespaceReferenceInfo.Name + ".ServerServices");
            usingsOfClass.Add(namespaceReferenceInfo.Name + ".HttpServices");
            //usingsOfClass.Add(namespaceReferenceInfo.Name + ".Models");
            usingsOfClass.Add(namespaceReferenceInfo.Name + ".ClientServices");
            //usingsOfClass.Add(namespaceReferenceInfo.Name + ".Enums");

            foreach (string item in usingsOfClass.Where(x => !namespaceReferenceInfo.Usings.Contains(x)).Distinct())
            {
                builderResult.AppendLine("using " + item + ";");
            }
            builderResult.AppendLine("");


            builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".ServerServices");
            builderResult.AppendLine("{");
            //foreach (ClassReferenceInfo classInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.ServiceLevel))
            //{
            //    GenerateServiceClass(classInfo, "    ", builderResult, true, "ServiceType.ServerService");
            //}
            builderResult.AppendLine("}");
            builderResult.AppendLine("");


            builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".StreamServices");
            builderResult.AppendLine("{");
            //foreach (ClassReferenceInfo classInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.StreamLevel))
            //{
            //    GenerateServiceClass(classInfo, "    ", builderResult, true, "ServiceType.StreamService");
            //}
            builderResult.AppendLine("}");
            builderResult.AppendLine("");

            builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".OneWayServices");
            builderResult.AppendLine("{");
            //foreach (ClassReferenceInfo classInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.OneWayLevel))
            //{
            //    GenerateOneWayServiceClass(classInfo, "    ", builderResult, true, "ServiceType.OneWayService");
            //}
            builderResult.AppendLine("}");
            builderResult.AppendLine("");



            builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".HttpServices");
            builderResult.AppendLine("{");

            builderResult.AppendLine(@"    /// <summary>
    /// reponse of http request
    /// </summary>
    public class HttpClientResponse
    {
        /// <summary>
        /// status
        /// </summary>
        public HttpStatusCode Status { get; set; } = HttpStatusCode.OK;
        /// <summary>
        /// data of response
        /// </summary>
        public string Data { get; set; }
        /// <summary>
        /// response headers
        /// </summary>
        public HttpResponseHeaders ResponseHeaders { get; set; }
    }

    /// <summary>
    /// a parameter data for method call
    /// </summary>
    public class ParameterInfo
    {
        /// <summary>
        /// type of parameter
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// value of parameter
        /// </summary>
        public string Value { get; set; }
    }

    public class SignalGoBlazorHttpClient
    {
        public HttpRequestHeaders RequestHeaders { get; set; } = new HttpRequestMessage().Headers;

        public async Task<HttpClientResponse> PostAsync(string url, ParameterInfo[] parameterInfoes)
        {
            Microsoft.AspNetCore.Blazor.Browser.Http.BrowserHttpMessageHandler browserHttpMessageHandler = new Microsoft.AspNetCore.Blazor.Browser.Http.BrowserHttpMessageHandler();
            using (HttpClient httpClient = new System.Net.Http.HttpClient(browserHttpMessageHandler))
            {
                foreach (KeyValuePair<string, IEnumerable<string>> item in RequestHeaders)
                {
                    httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                }

                MultipartFormDataContent form = new MultipartFormDataContent();
                foreach (ParameterInfo item in parameterInfoes)
                {
                    StringContent jsonPart = new StringContent(item.Value.ToString(), Encoding.UTF8, ""application/json"");
                    jsonPart.Headers.ContentDisposition = new ContentDispositionHeaderValue(""form-data"");
                    jsonPart.Headers.ContentDisposition.Name = item.Name;
                    jsonPart.Headers.ContentType = new MediaTypeHeaderValue(""application/json"");
                    form.Add(jsonPart);
                }

                HttpResponseMessage httpresponse = await httpClient.PostAsync(url, form);
                if (!httpresponse.IsSuccessStatusCode)
                {
                    // Unwrap the response and throw as an Api Exception:
                    throw new Exception(await httpresponse.Content.ReadAsStringAsync());
                }
                else
                {
                    httpresponse.EnsureSuccessStatusCode();
                    return new HttpClientResponse() { Data = await httpresponse.Content.ReadAsStringAsync(), ResponseHeaders = httpresponse.Headers, Status = httpresponse.StatusCode };
                }
            }
        }
    }");
            foreach (ClassReferenceInfo httpClassInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.HttpServiceLevel))
            {
                GenerateHttpServiceClass(httpClassInfo, "    ", builderResult);
            }

            builderResult.AppendLine("}");
            builderResult.AppendLine("");

            //Dictionary<string, string> AddedModels = new Dictionary<string, string>();
            //Dictionary<string, List<ClassReferenceInfo>> NeedToAddModels = new Dictionary<string, List<ClassReferenceInfo>>();

            foreach (IGrouping <string, ClassReferenceInfo> groupInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.ModelLevel).GroupBy(x => x.NameSpace))
            {
                builderResult.AppendLine("namespace " + groupInfo.Key);
                builderResult.AppendLine("{");
                foreach (ClassReferenceInfo modelInfo in groupInfo)
                {
                    CsharpLanguageMap.GenerateModelClass(modelInfo, "    ", builderResult, MapDataClassInfoes.Where(x => x.Name == modelInfo.Name).FirstOrDefault());
                }
                builderResult.AppendLine("}");
                builderResult.AppendLine("");
                //if (AddedModels.ContainsKey(modelInfo.Name))
                //{
                //    var find = NeedToAddModels.Where(x => !x.Value.Any(y => y.Name == modelInfo.Name)).Select(x => x.Value).FirstOrDefault();
                //    if (find != null)
                //        find.Add(modelInfo);
                //    else
                //    {
                //        var list = new List<ClassReferenceInfo>();
                //        NeedToAddModels.Add(nameSpaceName + (NeedToAddModels.Count + 2), list);
                //        list.Add(modelInfo);
                //    }
                //}
                //else
                //{
                //    AddedModels.Add(modelInfo.Name, nameSpaceName);
                //}
            }
            //builderResult.AppendLine("}");
            //builderResult.AppendLine("");

            //add duplicate models name to another name spaces
            //foreach (var item in NeedToAddModels)
            //{
            //    nameSpaceName = item.Key;
            //    builderResult.AppendLine(nameSpaceName);
            //    builderResult.AppendLine("{");
            //    foreach (var modelInfo in item.Value)
            //    {
            //        GenerateModelClass(modelInfo, "    ", builderResult, MapDataClassInfoes.Where(x => x.Name == modelInfo.Name).FirstOrDefault());
            //    }
            //    builderResult.AppendLine("}");
            //    builderResult.AppendLine();
            //}


            builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".ClientServices");
            builderResult.AppendLine("{");
            //foreach (ClassReferenceInfo callbackInfo in namespaceReferenceInfo.Classes.Where(x => x.Type == ClassReferenceType.CallbackLevel))
            //{
            //    GenerateServiceClass(callbackInfo, "    ", builderResult, false, "ServiceType.ClientService");
            //}
            builderResult.AppendLine("}");
            builderResult.AppendLine("");


            //builderResult.AppendLine("namespace " + namespaceReferenceInfo.Name + ".Enums");
            //builderResult.AppendLine("{");
            foreach (IGrouping <string, EnumReferenceInfo> groupInfo in namespaceReferenceInfo.Enums.GroupBy(x => x.NameSpace))
            {
                builderResult.AppendLine("namespace " + groupInfo.Key);
                builderResult.AppendLine("{");
                foreach (EnumReferenceInfo enumInfo in groupInfo)
                {
                    CsharpLanguageMap.GenerateModelEnum(enumInfo, "    ", builderResult);
                }
                builderResult.AppendLine("}");
                builderResult.AppendLine("");
            }
            //builderResult.AppendLine("}");
            //builderResult.AppendLine("");


            return(builderResult.ToString());
        }
Exemplo n.º 2
0
        public static string DownloadService(string servicePath, AddReferenceConfigInfo config)
        {
            string fullFilePath = "";

            if (config.ServiceType == 0)
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(config.ServiceUrl);
                webRequest.ContentType = "SignalGo Service Reference";
                webRequest.Headers.Add("servicenamespace", config.ServiceNameSpace);
                webRequest.Headers.Add("selectedLanguage", config.LanguageType.ToString());
                WebResponse response = webRequest.GetResponse();
                if (response.ContentLength <= 0)
                {
                    throw new Exception("Url ContentLength is not set!");
                }
                else if (response.Headers["Service-Type"] == null || response.Headers["Service-Type"] != "SignalGoServiceType")
                {
                    throw new Exception("Url file type is not support!");
                }
                Stream stream = response.GetResponseStream();

                using (MemoryStream streamWriter = new MemoryStream())
                {
                    streamWriter.SetLength(0);
                    byte[] bytes = new byte[1024 * 10];
                    while (streamWriter.Length != response.ContentLength)
                    {
                        int readCount = stream.Read(bytes, 0, bytes.Length);
                        if (readCount <= 0)
                        {
                            break;
                        }
                        streamWriter.Write(bytes, 0, readCount);
                    }
                    string json = Encoding.UTF8.GetString(streamWriter.ToArray());
                    //var namespaceReferenceInfo = (NamespaceReferenceInfo)JsonConvert.DeserializeObject(json, typeof(NamespaceReferenceInfo), new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Converters = new List<JsonConverter>() { new DataExchangeConverter(LimitExchangeType.IncomingCall) { Server = null, Client = null, IsEnabledReferenceResolver = true, IsEnabledReferenceResolverForArray = true } }, Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
                    NamespaceReferenceInfo namespaceReferenceInfo = (NamespaceReferenceInfo)JsonConvert.DeserializeObject(json, typeof(NamespaceReferenceInfo), new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore
                    });

                    if (config.LanguageType == 0)
                    {
                        fullFilePath = Path.Combine(servicePath, "Reference.cs");
                        File.WriteAllText(fullFilePath, CsharpLanguageMap.CalculateMapData(namespaceReferenceInfo, config), Encoding.UTF8);
                    }
                    else if (config.LanguageType == 1)
                    {
                        var oldPath = Path.Combine(servicePath, "OldAngular");
                        var newPath = Path.Combine(servicePath, "NewAngular");
                        fullFilePath = Path.Combine(oldPath, "Reference.ts");
                        if (!Directory.Exists(oldPath))
                        {
                            Directory.CreateDirectory(oldPath);
                        }
                        TypeScriptLanguageMap typeScriptLanguageMap = new TypeScriptLanguageMap();
                        File.WriteAllText(fullFilePath, typeScriptLanguageMap.CalculateMapData(oldPath, namespaceReferenceInfo, config.ServiceNameSpace), Encoding.UTF8);

                        AngularTypeScriptLanguageMap angularTypeScriptLanguageMap = new AngularTypeScriptLanguageMap();
                        angularTypeScriptLanguageMap.CalculateMapData(newPath, namespaceReferenceInfo, config.ServiceNameSpace);

                        MessageBox.Show("Angular sources are compiled in OldAngular and NewAngular folder");
                    }
                    else if (config.LanguageType == 2)
                    {
                        fullFilePath = Path.Combine(servicePath, "Reference.cs");
                        File.WriteAllText(fullFilePath, BlazorLanguageMap.CalculateMapData(namespaceReferenceInfo, config.ServiceNameSpace), Encoding.UTF8);
                    }
                }
            }
            else
            {
                if (config.LanguageType > 0)
                {
                    throw new NotSupportedException("this language for this type not supported now!");
                }
                CsharpWebService.XMLToCsharp xmlCsharp = new CsharpWebService.XMLToCsharp();
                xmlCsharp.Generate(config.ServiceUrl);
                string csharpCode = xmlCsharp.GeneratesharpCode();
                fullFilePath = Path.Combine(servicePath, "Reference.cs");
                StringBuilder builder = new StringBuilder();
                builder.AppendLine($"namespace {config.ServiceNameSpace}");
                builder.AppendLine("{");
                builder.AppendLine(csharpCode);
                builder.AppendLine("}");
                File.WriteAllText(fullFilePath, builder.ToString(), Encoding.UTF8);
            }
            return(fullFilePath);
        }