Exemple #1
0
        private void GeneratorJsWebApi(string entity, int i, int count)
        {
            var jsForm        = new List <string>();
            var isDebugForm   = true;
            var isDebugWebApi = true;

            if (GeneratorJson.usetypescriptdeclaration == "true")
            {
                if (!File.Exists($"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.js"))
                {
                    var text = string.Empty;
                    text += "//@ts-check\r\n";
                    text += $"///<reference path=\"{entity}.d.ts\" />\r\n";
                    File.WriteAllText($"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.js", text, System.Text.Encoding.UTF8);
                }
                var fileTypeScriptDeclaration = $"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.d.ts";
                if (File.Exists(fileTypeScriptDeclaration))
                {
                    var lines = File.ReadAllLines(fileTypeScriptDeclaration);
                    if (lines.Count() != 0)
                    {
                        var json    = lines[lines.Length - 1];
                        var comment = SimpleJson.DeserializeObject <CommentIntellisense>(json.Substring("//".Length).Replace("'", "\""));
                        isDebugWebApi = comment.IsDebugWebApi;
                        jsForm        = comment.JsForm;
                        isDebugForm   = comment.IsDebugForm;
                    }
                }
                var parts       = GeneratorJson.rootnamespace.Split(".".ToCharArray());
                var projectName = parts.Length > 1 ? parts[1] : parts[0];
                var jsWebApi    = new JsWebApi(CrmServiceClient.OrganizationServiceProxy, projectName, entity, isDebugWebApi, jsForm, isDebugForm);
                jsWebApi.GeneratorCode();
                var old = string.Empty;
                if (File.Exists(fileTypeScriptDeclaration))
                {
                    old = File.ReadAllText(fileTypeScriptDeclaration).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                }
                var @new = jsWebApi.WebApiCodeTypeScriptDeclaration.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                if (old != @new)
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": Processing ", CliLog.ColorGreen, entity, ".d.ts");
                    if (Utility.CanWriteAllText(fileTypeScriptDeclaration))
                    {
                        File.WriteAllText(fileTypeScriptDeclaration, jsWebApi.WebApiCodeTypeScriptDeclaration, System.Text.Encoding.UTF8);
                    }
                }
                else
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": No change ", CliLog.ColorGreen, entity, ".d.ts");
                }
                var fileWebApi = $"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.webapi.js";
                old = string.Empty;
                if (File.Exists(fileWebApi))
                {
                    old = File.ReadAllText(fileWebApi).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                }
                @new = jsWebApi.WebApiCode.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                if (old != @new)
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": Processing ", CliLog.ColorGreen, entity, ".webapi.js");
                    if (Utility.CanWriteAllText(fileWebApi))
                    {
                        File.WriteAllText(fileWebApi, jsWebApi.WebApiCode, System.Text.Encoding.UTF8);
                    }
                }
                else
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": No change ", CliLog.ColorGreen, entity, ".webapi.js");
                }
            }
            else
            {
                var fileIntellisense = $"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.intellisense.js";
                var lines            = File.ReadAllLines(fileIntellisense);
                var json             = lines[lines.Length - 1];
                var comment          = SimpleJson.DeserializeObject <CommentIntellisense>(json.Substring("//".Length).Replace("'", "\""));
                var parts            = GeneratorJson.rootnamespace.Split(".".ToCharArray());
                var projectName      = parts.Length > 1 ? parts[1] : parts[0];
                var jsWebApi         = new JsWebApi(CrmServiceClient.OrganizationServiceProxy, projectName, entity, comment.IsDebugWebApi, comment.JsForm, comment.IsDebugForm);
                jsWebApi.GeneratorCode();
                var old  = File.ReadAllText(fileIntellisense).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                var @new = jsWebApi.WebApiCodeIntellisense.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                if (old != @new)
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": Processing ", CliLog.ColorGreen, entity, ".intellisense.js");
                    if (Utility.CanWriteAllText(fileIntellisense))
                    {
                        File.WriteAllText(fileIntellisense, jsWebApi.WebApiCodeIntellisense, System.Text.Encoding.UTF8);
                    }
                }
                else
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": No change ", CliLog.ColorGreen, entity, ".intellisense.js");
                }
                var fileWebApi = $"{CurrentDirectory}\\{GeneratorJson.rootfolder}\\{entity}.webapi.js";
                old  = File.ReadAllText(fileWebApi).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                @new = jsWebApi.WebApiCode.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
                if (old != @new)
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": Processing ", CliLog.ColorGreen, entity, ".webapi.js");
                    if (Utility.CanWriteAllText(fileWebApi))
                    {
                        File.WriteAllText(fileWebApi, jsWebApi.WebApiCode, System.Text.Encoding.UTF8);
                    }
                }
                else
                {
                    CliLog.WriteLine(CliLog.ColorCyan, string.Format("{0,0}|{1," + count.ToString().Length + "}", "", i) + ": No change ", CliLog.ColorGreen, entity, ".webapi.js");
                }
            }
        }
        private void GeneratorJsWebApi(string entity, int i, int count)
        {
            var jsForm        = new List <string>();
            var isDebugForm   = true;
            var isDebugWebApi = true;

            if (!File.Exists($"{currentDirectory}\\{json.rootfolder}\\{entity}.js"))
            {
                var text = string.Empty;
                text += "//@ts-check\r\n";
                text += $"///<reference path=\"{entity}.d.ts\" />\r\n";
                Utility.ForceWriteAllText($"{currentDirectory}\\{json.rootfolder}\\{entity}.js", text);
            }
            var fileTypeScriptDeclaration = $"{currentDirectory}\\{json.rootfolder}\\{entity}.d.ts";

            if (File.Exists(fileTypeScriptDeclaration))
            {
                var lines = File.ReadAllLines(fileTypeScriptDeclaration);
                if (lines.Count() != 0)
                {
                    var json    = lines[lines.Length - 1];
                    var comment = SimpleJson.DeserializeObject <CommentTypeScriptDeclaration>(json.Substring("//".Length).Replace("'", "\""));
                    isDebugWebApi = comment.IsDebugWebApi;
                    jsForm        = comment.JsForm;
                    isDebugForm   = comment.IsDebugForm;
                }
            }
            var parts       = json.rootnamespace.Split(".".ToCharArray());
            var projectName = parts.Length > 1 ? parts[1] : parts[0];

            if (json.debug.Length > 0)
            {
                isDebugWebApi = json.debug == "yes" ? true : false;
            }
            var jsWebApi = new JsWebApi(XrmHelper.GetIOrganizationService(crmServiceClient), projectName, entity, isDebugWebApi, jsForm, isDebugForm);

            jsWebApi.GeneratorCode();
            var old = string.Empty;

            if (File.Exists(fileTypeScriptDeclaration))
            {
                old = File.ReadAllText(fileTypeScriptDeclaration).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
            }
            var @new = jsWebApi.WebApiCodeTypeScriptDeclaration.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);

            if (old != @new)
            {
                CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorGreen, string.Format("{0,0}{1," + count.ToString().Length + "}", "", i) + ": ", CliLog.ColorMagenta, "Processing ", CliLog.ColorGreen, entity, ".d.ts");
                Utility.ForceWriteAllText(fileTypeScriptDeclaration, jsWebApi.WebApiCodeTypeScriptDeclaration);
            }
            else
            {
                CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorGreen, string.Format("{0,0}{1," + count.ToString().Length + "}", "", i) + ": ", CliLog.ColorMagenta, "No change ", CliLog.ColorGreen, entity, ".d.ts");
            }
            var fileWebApi = $"{currentDirectory}\\{json.rootfolder}\\{entity}.webapi.js";

            old = string.Empty;
            if (File.Exists(fileWebApi))
            {
                old = File.ReadAllText(fileWebApi).Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
            }
            @new = jsWebApi.WebApiCode.Replace(" ", string.Empty).Replace("\r\n", string.Empty).Replace("\t", string.Empty);
            if (old != @new)
            {
                CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorGreen, string.Format("{0,0}{1," + count.ToString().Length + "}", "", i) + ": ", CliLog.ColorMagenta, "Processing ", CliLog.ColorGreen, entity, ".webapi.js");
                Utility.ForceWriteAllText(fileWebApi, jsWebApi.WebApiCode);
            }
            else
            {
                CliLog.WriteLine(CliLog.ColorWhite, "|", CliLog.ColorGreen, string.Format("{0,0}{1," + count.ToString().Length + "}", "", i) + ": ", CliLog.ColorMagenta, "No change ", CliLog.ColorGreen, entity, ".webapi.js");
            }
        }