public bool CreatePartArrayGraph(List <ArrayRange> ranges, MetaGraphNode referenceNode, MetaGraphNode fileNode)
 {
     foreach (var range in ranges)
     {
         var rangeExist = referenceNode.Successors.FirstOrDefault(x => IsObjectEqual(x.filter, range.filter));
         if (rangeExist == null)
         {
             referenceNode.Successors.Add(new MetaGraphNode(range.filter, referenceNode));
             CreatePartMetaGraph(range.properties, referenceNode.Successors[referenceNode.Successors.Count - 1], fileNode);
         }
         else
         {
             CreatePartMetaGraph(range.properties, rangeExist, fileNode);
         }
     }
     return(true);
 }
        public bool DeleteResourceMetaNode(MetaGraphNode referenceNode)
        {
            var preSuccessorNode = referenceNode.Predecessors.FirstOrDefault(x => x.Successors.Count == 1);

            if (preSuccessorNode != null)
            {
                DeleteResourceMetaNode(preSuccessorNode);
                preSuccessorNode.Successors.Remove(referenceNode);
                referenceNode.Predecessors.Remove(preSuccessorNode);
                return(true);
            }
            else
            {
                var lastNode = referenceNode.Predecessors[0];
                lastNode.Successors.Remove(referenceNode);
                referenceNode.Predecessors.Remove(lastNode);
                return(true);
            }
        }
 public bool CreatePartMetaGraph(List <ObjectReference> properties, MetaGraphNode referenceNode, MetaGraphNode fileNode)
 {
     if (properties == null || !properties.Any())
     {
         referenceNode.Successors.Add(fileNode);
         fileNode.Predecessors.Add(referenceNode);
         return(true);
     }
     foreach (var property in properties)
     {
         var propertyExist = referenceNode.Successors.FirstOrDefault(x => x.name == property.name);
         if (propertyExist == null)
         {
             referenceNode.Successors.Add(new MetaGraphNode(property.name, property.type, property.dataType, referenceNode));
             if (property.type == metaPropertyType.array)
             {
                 CreatePartArrayGraph(property.arrayRanges, referenceNode.Successors[referenceNode.Successors.Count - 1], fileNode);
             }
             else
             {
                 CreatePartMetaGraph(property.properties, referenceNode.Successors[referenceNode.Successors.Count - 1], fileNode);
             }
         }
         else
         {
             if (property.type == metaPropertyType.array)
             {
                 CreatePartArrayGraph(property.arrayRanges, propertyExist, fileNode);
             }
             else
             {
                 CreatePartMetaGraph(property.properties, propertyExist, fileNode);
             }
         }
     }
     return(true);
 }
Exemplo n.º 4
0
        public List <CompileResult> BuildProject(string userEmail, string projectId, bool _isDev, int currentCompilerVersion)
        {
            try
            {
                var response                = new List <CompileResult>();
                var api                     = new CompilerAPI();
                var oldPage                 = false;
                var changedKDLOrPartial     = false;
                var filesCompiledCount      = 0;
                var buildStatusUpdateResult = true;
                var dict                    = new Dictionary <string, int>();
                System.Diagnostics.Stopwatch compileStopWatch = new System.Diagnostics.Stopwatch();
                Log.Information("Compilation started");
                dict.Add("LINK", 0);
                //List<BuildError> BuildErrors;
                var userId         = new UserAPI().GetUserId(userEmail);
                var project        = api.GetProjectDetailsApi(userEmail, projectId);
                var forceBuild     = false;
                var skipValidation = false;


                if (project == null)
                {
                    response.Add(new CompileResult {
                        Success = false, ErrorMessages = new List <CompilerError>()
                        {
                            new CompilerError {
                                Message = $"Project with projectid \"{projectId}\" not found."
                            }
                        }
                    });
                    return(response);
                }
                else
                {
                    forceBuild = project.CompilerVersion < currentCompilerVersion;
                }

                if (project.Resources.Any(x => Kitsune.Helper.Constants.ConfigFileRegularExpression.IsMatch(x.SourcePath)))
                {
                    var settings = api.GetProjectResourceDetailsApi(userEmail, projectId, HttpUtility.UrlEncode("/kitsune-settings.json"));
                    if (!string.IsNullOrEmpty(settings.HtmlSourceString))
                    {
                        try
                        {
                            var settingsOb = JsonConvert.DeserializeObject <dynamic>(settings.HtmlSourceString);
                            if (settingsOb.GetType() == typeof(JObject))
                            {
                                var cleanBuild = ((JObject)settingsOb).SelectToken("build.cleanBuild");
                                if (cleanBuild != null && !string.IsNullOrEmpty((string)cleanBuild))
                                {
                                    if ("true" == ((string)cleanBuild).ToLower())
                                    {
                                        forceBuild = true;
                                    }
                                }
                                var syntaxValidation = ((JObject)settingsOb).SelectToken("build.syntax_validation");
                                if (syntaxValidation != null && !string.IsNullOrEmpty((string)syntaxValidation))
                                {
                                    if ("true" == ((string)syntaxValidation).ToLower())
                                    {
                                        skipValidation = false;
                                    }
                                    else
                                    {
                                        skipValidation = true;
                                    }
                                    Console.WriteLine($"skipValidation:{skipValidation}");
                                }
                            }
                        }
                        catch { }
                    }
                }

                project.UserEmail = userEmail;
                project.Resources = project.Resources.Where(x => x.IsStatic == false && x.ResourceType != Kitsune.Models.Project.ResourceType.APPLICATION).ToList();
                var staticFileCount = project.Resources.Count();
                dict.Add("TOTAL", staticFileCount);

                var languageEntity = new KEntity
                {
                    Classes = new List <KClass>()
                };
                if (!string.IsNullOrEmpty(project.SchemaId))
                {
                    languageEntity = new LanguageAPI().GetLanguage(project.SchemaId, userId);
                }
                languageEntity = new KLanguageBase().GetKitsuneLanguage(userEmail, projectId, project, languageEntity, userId);

                #region Handle Project Modules
                var componentEntities = UpdateModuleReference(project);
                #endregion

                var partialPages = api.GetPartialPagesDetailsApi(userEmail, projectId, null);
                GetAuditProjectAndResourcesDetailsResponseModel auditProject = new GetAuditProjectAndResourcesDetailsResponseModel();
                DateTime latestBuildDate = DateTime.MinValue;
                if (project.Version > 1 && !forceBuild)
                {
                    var latestProjectVersion = project.Version - 1;
                    auditProject = api.GetAuditProjectAndResourcesDetails(userEmail, projectId, latestProjectVersion);
                }
                var latestBuild = api.GetLastCompletedBuild(userEmail, projectId);
                if (latestBuild != null && latestBuild.CreatedOn != null)
                {
                    latestBuildDate = latestBuild.CreatedOn;
                }

                var ToBeUpdatedResources = new List <CreateOrUpdateResourceRequestModel>();
                var CompiledResources    = new Dictionary <string, CompileResult>();
                var UncompiledResources  = new List <CompileResourceRequest>();

                //put partial pages first
                var partialResources = project.Resources.FindAll(x => x.PageType == Kitsune.Models.Project.KitsunePageType.PARTIAL);
                if (partialResources.Any())
                {
                    foreach (var resource in partialResources)
                    {
                        project.Resources.Remove(resource);
                        project.Resources.Insert(0, resource);
                    }
                }
                foreach (var page in project.Resources)
                {
                    oldPage = false;
                    var resource = api.GetProjectResourceDetailsApi(userEmail, projectId, HttpUtility.UrlEncode(page.SourcePath));
                    // page.IsStatic = KitsuneCompiler.IsResourceStatic(resource.HtmlSourceString, page.SourcePath);
                    if (!page.IsStatic)
                    {
                        var compiled = false;
                        var document = new HtmlDocument();
                        Console.WriteLine("Page: " + page.SourcePath);
                        var req   = new CompileResourceRequest();
                        var outsd = new CompileResult();
                        if (project.Version > 1 && !forceBuild)
                        {
                            oldPage = auditProject.Resources.Any(x => x.SourcePath == page.SourcePath);
                        }
                        GetFileFromS3RequestModel resourceTreeRequest = new GetFileFromS3RequestModel
                        {
                            SourcePath = string.Format(CompilerConstants.TreeFileName, page.SourcePath),
                            ProjectId  = page.ProjectId,
                            BucketName = project.BucketNames.demo
                        };
                        var resourceTreePresent = api.GetFileFromS3(resourceTreeRequest);
                        if (!changedKDLOrPartial && project.Version > 1 && oldPage && page.UpdatedOn <= latestBuildDate && !string.IsNullOrEmpty(resourceTreePresent))
                        {
                            var samePage = auditProject.Resources.Find(x => x.SourcePath == page.SourcePath);
                            GetFileFromS3RequestModel FileRequest = new GetFileFromS3RequestModel
                            {
                                SourcePath = samePage.SourcePath,
                                ProjectId  = samePage.ProjectId,
                                BucketName = project.BucketNames.demo,
                                Compiled   = true,
                                Version    = samePage.Version
                            };
                            outsd = new CompileResult()
                            {
                                CompiledString = api.GetFileFromS3(FileRequest),
                                Success        = true
                            };
                            req.PageType   = samePage.PageType;
                            req.UrlPattern = samePage.UrlPattern;
                            req.SourcePath = samePage.SourcePath;
                            compiled       = false;
                        }
                        else
                        {
                            req = new CompileResourceRequest {
                                SourcePath = page.SourcePath, FileContent = resource.HtmlSourceString, UserEmail = project.UserEmail, ProjectId = projectId, IsDev = _isDev, IsPublish = true, IsStatic = page.IsStatic, IsDefault = page.IsDefault, ClassName = page.ClassName, UrlPattern = page.UrlPattern, KObject = page.KObject, PageType = page.PageType, ResourceType = page.ResourceType
                            };
                            //Compile config file

                            compileStopWatch.Reset();
                            compileStopWatch.Start();
                            if (skipValidation)
                            {
                                outsd = new CompileResult();
                                outsd.CompiledString = req.FileContent;
                                outsd.PageName       = req.SourcePath;
                                req.UrlPattern       = req.SourcePath;
                                page.UrlPattern      = req.SourcePath;
                                outsd.Success        = true;
                            }
                            else if (Kitsune.Helper.Constants.ConfigFileRegularExpression.IsMatch(req.SourcePath))
                            {
                                outsd = KitsuneCompiler.ValidateJsonConfig(new ValidateConfigRequestModel
                                {
                                    File = new ConfigFile {
                                        Content = req.FileContent
                                    },
                                    UserEmail = req.UserEmail
                                });
                                outsd.CompiledString = req.FileContent;
                                outsd.PageName       = req.SourcePath;
                                req.UrlPattern       = req.SourcePath;
                                page.UrlPattern      = req.SourcePath;
                            }
                            //Compile dynamic html file
                            else
                            {
                                Console.WriteLine($"Compiling : {req.SourcePath}");
                                outsd = KitsuneCompiler.CompileHTML(req, out document, project, languageEntity, partialPages, componentEntities: componentEntities);
                            }

                            if (!outsd.Success)
                            {
                                response.Add(outsd);
                            }
                            else
                            {
                                CompileLog(req.SourcePath, compileStopWatch.ElapsedMilliseconds.ToString());

                                // Console.WriteLine($"'{req.SourcePath}'-{compileStopWatch.ElapsedMilliseconds.ToString()}");
                                ResourceMetaInfo metaInfo = new ResourceMetaInfo();
                                try
                                {
                                    var tagsToIgnore = new List <string>();
                                    if (outsd.CustomVariables != null)
                                    {
                                        tagsToIgnore.AddRange(outsd.CustomVariables.Keys);
                                    }
                                    metaInfo           = ResourceMetaInfoGenerator.MetaInfo(req, outsd.CompiledString, project, languageEntity, tagsToIgnore);
                                    outsd.MetaClass    = metaInfo?.metaClass;
                                    outsd.MetaProperty = metaInfo?.Property;
                                }
                                catch (Exception ex)
                                {
                                    metaInfo = new ResourceMetaInfo
                                    {
                                        IsError = true,
                                        Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                                    };
                                }
                                CompiledResources.Add(string.Format("{0}:{1}", req.ProjectId, req.SourcePath), outsd);
                            }

                            if (!changedKDLOrPartial && oldPage)
                            {
                                var samePage = auditProject.Resources.Find(x => x.SourcePath == page.SourcePath);
                                if (samePage.UrlPattern != req.UrlPattern)
                                {
                                    changedKDLOrPartial = true;
                                }
                                if (samePage.PageType == Kitsune.Models.Project.KitsunePageType.PARTIAL)
                                {
                                    changedKDLOrPartial = true;
                                }
                            }
                            compiled = true;
                        }

                        //Replacing the aws path to the version controlled folder
                        //outsd.CompiledString = KitsuneCompiler.AddAuditAssetsPath(projectId, outsd.CompiledString, project.Version);


                        //Save compiled resource to kitsune resource collection
                        var updatePageRequest = new CreateOrUpdateResourceRequestModel
                        {
                            Errors          = null,
                            FileContent     = req.FileContent,
                            SourcePath      = req.SourcePath,
                            ClassName       = req.ClassName,
                            ProjectId       = req.ProjectId,
                            UserEmail       = req.UserEmail,
                            UrlPattern      = req.UrlPattern,
                            IsStatic        = req.IsStatic,
                            IsDefault       = req.IsDefault,
                            PageType        = req.PageType,
                            KObject         = req.KObject,
                            CustomVariables = outsd.CustomVariables,
                            Offset          = req.Offset
                        };



                        if (compiled)
                        {
                            ToBeUpdatedResources.Add(updatePageRequest);
                            filesCompiledCount += 1;

                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                        else if (!compiled)
                        {
                            UncompiledResources.Add(new CompileResourceRequest
                            {
                                FileContent = resource.HtmlSourceString,
                                SourcePath  = page.SourcePath,
                                IsStatic    = page.IsStatic,
                                IsDefault   = page.IsDefault,
                                UrlPattern  = page.UrlPattern,
                                ClassName   = page.ClassName,
                            });
                        }
                    }
                }
                //Exception operations on uncompiled Pages
                if (!skipValidation && UncompiledResources.Any())
                {
                    if (changedKDLOrPartial)
                    {
                        foreach (var currpage in UncompiledResources)
                        {
                            var document = new HtmlDocument();
                            var req      = new CompileResourceRequest {
                                SourcePath = currpage.SourcePath, FileContent = currpage.FileContent, UserEmail = project.UserEmail, ProjectId = projectId, IsDev = _isDev, IsPublish = true, IsStatic = currpage.IsStatic, IsDefault = currpage.IsDefault, ClassName = currpage.ClassName, UrlPattern = currpage.UrlPattern
                            };

                            compileStopWatch.Reset();
                            compileStopWatch.Start();

                            Console.WriteLine($"Compiling UncompiledResources : {currpage.SourcePath}");

                            var outsd = KitsuneCompiler.CompileHTML(req, out document, project, languageEntity, partialPages, componentEntities: componentEntities);

                            if (!outsd.Success)
                            {
                                response.Add(outsd);
                            }
                            else
                            {
                                CompileLog(currpage.SourcePath, compileStopWatch.ElapsedMilliseconds.ToString());

                                ResourceMetaInfo metaInfo = new ResourceMetaInfo();
                                try
                                {
                                    var tagsToIgnore = new List <string>();
                                    if (outsd.CustomVariables != null)
                                    {
                                        tagsToIgnore.AddRange(outsd.CustomVariables.Keys);
                                    }
                                    metaInfo           = ResourceMetaInfoGenerator.MetaInfo(req, outsd.CompiledString, project, languageEntity, tagsToIgnore);
                                    outsd.MetaClass    = metaInfo?.metaClass;
                                    outsd.MetaProperty = metaInfo?.Property;
                                }
                                catch (Exception ex)
                                {
                                    metaInfo = new ResourceMetaInfo
                                    {
                                        IsError = true,
                                        Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                                    };
                                }
                                CompiledResources.Add(string.Format("{0}:{1}", req.ProjectId, req.SourcePath), outsd);
                            }
                            //outsd.CompiledString = KitsuneCompiler.AddAuditAssetsPath(projectId, outsd.CompiledString, project.Version);
                            ToBeUpdatedResources.Add(new CreateOrUpdateResourceRequestModel
                            {
                                Errors          = null,
                                FileContent     = req.FileContent,
                                SourcePath      = req.SourcePath,
                                ClassName       = req.ClassName,
                                ProjectId       = req.ProjectId,
                                UserEmail       = req.UserEmail,
                                UrlPattern      = req.UrlPattern,
                                IsStatic        = req.IsStatic,
                                IsDefault       = req.IsDefault,
                                PageType        = req.PageType,
                                KObject         = req.KObject,
                                Offset          = req.Offset,
                                CustomVariables = outsd.CustomVariables
                            });
                            filesCompiledCount += 1;
                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var currpage in UncompiledResources)
                        {
                            //SubmittedResources.Add(new CreateOrUpdateResourceRequest
                            //{
                            //    Errors = null,
                            //    FileContent = currpage.HtmlSourceString,
                            //    SourcePath = currpage.SourcePath.Trim().ToUpper(),
                            //    ClassName = currpage.ClassName,
                            //    ProjectId = currpage.ProjectId,
                            //    UserEmail = currpage.UserEmail,
                            //    UrlPattern = currpage.UrlPattern,
                            //    IsStatic = currpage.IsStatic,
                            //    IsDefault = currpage.IsDefault,
                            //    PageType = currpage.PageType,
                            //    KObject = currpage.KObject
                            //});
                            filesCompiledCount += 1;
                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                }

                try
                {
                    dict["LINK"]            = filesCompiledCount;
                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                    {
                        ProjectId = projectId,
                        Compiler  = dict,
                        Stage     = BuildStatus.Compiled
                                    //Error = BuildErrors
                    });
                }
                catch (Exception ex)
                {
                }
                if (!response.Any())
                {
                    List <UrlPatternRegexDetails> UrlPatternRegexList = new List <UrlPatternRegexDetails>();
                    foreach (var resource in project.Resources.Where(x => !string.IsNullOrEmpty(x.UrlPattern)))
                    {
                        var UrlPatternRegex = KitsuneCommonUtils.GenerateUrlPatternRegex(resource.UrlPattern.Substring(resource.UrlPattern.IndexOf("/") + 1), resource.SourcePath);
                        UrlPatternRegexList.Add(new UrlPatternRegexDetails
                        {
                            SourcePath      = resource.SourcePath,
                            UrlPattern      = resource.UrlPattern,
                            UrlPatternRegex = UrlPatternRegex
                        });
                    }
                    var UrlPatternRegexDistinct = UrlPatternRegexList.GroupBy(x => x.UrlPatternRegex).Select(x => new { Regex = x.Key, Count = x.Count() }).Where(x => x.Count > 1).ToList();
                    if (UrlPatternRegexDistinct != null && UrlPatternRegexDistinct.Any())
                    {
                        int regexMatchesCount = 1;
                        var message           = "Url pattern conflict : \n";
                        var pageName          = string.Empty;
                        foreach (var pattern in UrlPatternRegexDistinct)
                        {
                            var result = UrlPatternRegexList.FindAll(x => x.UrlPatternRegex == pattern.Regex);
                            pageName = result?.First()?.SourcePath;
                            var partMessage = string.Join(", ", result.Select(x => x.SourcePath).ToArray());
                            message           += regexMatchesCount.ToString() + ": " + partMessage + "\n";
                            regexMatchesCount += 1;
                        }
                        return(new List <CompileResult> {
                            new CompileResult {
                                ErrorMessages = new List <CompilerError> {
                                    new CompilerError {
                                        Message = message,
                                        LineNumber = 1,
                                        LinePosition = 1
                                    }
                                },
                                Success = false
                            }
                        });
                    }
                    List <ResourcePropertyPosition> AllResourcesMeta = new List <ResourcePropertyPosition>();
                    var metaClassCompletion = true;
                    foreach (var resource in ToBeUpdatedResources)
                    {
                        Console.WriteLine($"Uploading : {resource?.SourcePath}");
                        resource.UrlPatternRegex = UrlPatternRegexList.Find(x => x.SourcePath == resource.SourcePath).UrlPatternRegex;
                        var retryCount = 0;
                        do
                        {
                            var isUploaded = api.CreateOrUpdateResourceApi(resource);
                            if (!isUploaded)
                            {
                                Console.WriteLine($"Upload Failed.. retrying {retryCount}..");
                                retryCount++;
                                if (retryCount >= 3)
                                {
                                    return new List <CompileResult> {
                                               new CompileResult {
                                                   ErrorMessages = new List <CompilerError> {
                                                       new CompilerError {
                                                           Message = string.Format("Faild to save to DB for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                       }
                                                   }, Success = false
                                               }
                                    }
                                }
                                ;
                            }
                            else
                            {
                                retryCount = 0;
                            }
                        } while (retryCount != 0);

                        if (CompiledResources.ContainsKey(string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)))
                        {
                            var currentCompiledResource = CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)];
                            SaveFileContentToS3RequestModel resourceContent = new SaveFileContentToS3RequestModel
                            {
                                ProjectId   = resource.ProjectId,
                                BucketName  = project.BucketNames.demo,
                                SourcePath  = resource.SourcePath,
                                FileContent = currentCompiledResource.CompiledString
                            };
                            var compiledStringResult = api.SaveFileContentToS3(resourceContent);
                            if (compiledStringResult == null)
                            {
                                return new List <CompileResult> {
                                           new CompileResult {
                                               ErrorMessages = new List <CompilerError> {
                                                   new CompilerError {
                                                       Message = string.Format("Faild to save to Demo Bucket for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                   }
                                               }, Success = false
                                           }
                                }
                            }
                            ;
                            //Save compiled (.kc) file
                            if (currentCompiledResource.KitsunePage != null)
                            {
                                var jsonSerializeSettings = new JsonSerializerSettings();

                                jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Auto;
                                string output = JsonConvert.SerializeObject(currentCompiledResource.KitsunePage, Formatting.None, jsonSerializeSettings);

                                resourceContent = new SaveFileContentToS3RequestModel
                                {
                                    ProjectId   = resource.ProjectId,
                                    BucketName  = project.BucketNames.demo,
                                    SourcePath  = $"{resource.SourcePath}.kc",
                                    FileContent = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(output)),
                                    base64      = true
                                };
                                compiledStringResult = api.SaveFileContentToS3(resourceContent);
                                if (compiledStringResult == null)
                                {
                                    return new List <CompileResult> {
                                               new CompileResult {
                                                   ErrorMessages = new List <CompilerError> {
                                                       new CompilerError {
                                                           Message = string.Format("Faild to save to Demo Bucket for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                       }
                                                   }, Success = false
                                               }
                                    }
                                }
                                ;
                            }

                            try
                            {
                                if (Kitsune.Helper.Constants.DynamicFileExtensionRegularExpression.IsMatch(resource.SourcePath.ToLower()))
                                {
                                    if (currentCompiledResource.MetaClass == null)
                                    {
                                        metaClassCompletion = false;
                                        throw new Exception(string.Format("Meta Class is null for resource: {0}", resource.SourcePath));
                                    }
                                    var serializedMetaClass = MetaHelper.ProtoSerializer(CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)].MetaClass);

                                    if (string.IsNullOrEmpty(serializedMetaClass))
                                    {
                                        throw new Exception(string.Format("Meta Class serialization failed for resource: {0}", resource.SourcePath));
                                    }
                                    //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to serialize meta Class for : {0}", resource.SourcePath), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                    resourceContent = new SaveFileContentToS3RequestModel
                                    {
                                        ProjectId   = resource.ProjectId,
                                        BucketName  = project.BucketNames.demo,
                                        SourcePath  = string.Format(CompilerConstants.TreeFileName, resource.SourcePath),
                                        FileContent = serializedMetaClass,
                                        base64      = true
                                    };
                                    var metaClassResult = api.SaveFileContentToS3(resourceContent);
                                    if (metaClassResult == null)
                                    {
                                        throw new Exception(string.Format("Meta Class .k file not saved for resource: {0}", resource.SourcePath));
                                    }
                                    //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to save meta class to Demo Bucket for : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                    metaClassResult = null;

                                    AllResourcesMeta.Add(new ResourcePropertyPosition
                                    {
                                        SourcePath = resource.SourcePath,
                                        UrlPattern = resource.UrlPattern,
                                        Properties = CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)].MetaClass
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                //ToDo: Log MetaInfo Error
                            }
                        }
                    }
                    try
                    {
                        if (AllResourcesMeta.Any() && metaClassCompletion)
                        {
                            MetaGraphNode             metaGraphResult = new MetaGraphNode();
                            GetFileFromS3RequestModel GraphRequest    = new GetFileFromS3RequestModel
                            {
                                SourcePath = string.Format(CompilerConstants.GraphFileName, projectId),
                                ProjectId  = projectId,
                                BucketName = project.BucketNames.demo
                            };
                            var serializedMetaGraph = api.GetFileFromS3(GraphRequest);
                            var metaGraphBuilder    = new MetaGraphBuilder();
                            if (serializedMetaGraph != null)
                            {
                                var metaGraph = MetaHelper.ProtoDeserialize <MetaGraphNode>(serializedMetaGraph);
                                if (metaGraph == null)
                                {
                                    throw new Exception("Meta Graph Deserialization failed");
                                }
                                //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Failed to deserialize Reference Graph", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                metaGraphResult = metaGraphBuilder.CreateGraph(AllResourcesMeta, metaGraph);
                            }
                            else
                            {
                                metaGraphResult = metaGraphBuilder.CreateGraph(AllResourcesMeta);
                            }
                            if (metaGraphResult == null)
                            {
                                throw new Exception("Meta Graph creation failed");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Faild to create reference Graph", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                            var newSerializedMetaGraph = MetaHelper.ProtoSerializer(metaGraphResult);
                            if (string.IsNullOrEmpty(newSerializedMetaGraph))
                            {
                                throw new Exception("Meta Graph serialization failed");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to serialize meta Class for : {0}", projectId), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                            SaveFileContentToS3RequestModel graphContent = new SaveFileContentToS3RequestModel
                            {
                                ProjectId   = projectId,
                                BucketName  = project.BucketNames.demo,
                                SourcePath  = string.Format(CompilerConstants.GraphFileName, projectId),
                                FileContent = newSerializedMetaGraph,
                                base64      = true
                            };
                            var graphSaveResult = api.SaveFileContentToS3(graphContent);
                            if (graphSaveResult == null)
                            {
                                throw new Exception("Meta Graph file not saved");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Faild to save Graph to Demo Bucket", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                        }
                    }
                    catch (Exception ex)
                    {
                        //ToDo: Log MetaInfo Error
                    }

                    //Update the project version

                    if (project.CompilerVersion != currentCompilerVersion)
                    {
                        APIHelpers.UpdateProjectCompilerVersion(userEmail, projectId, currentCompilerVersion);
                    }
                }
                return(response);
            }
            catch (Exception ex)
            {
                Log.Error($"BuildProject : Exception : {ex.Message}, {ex.StackTrace}, {(ex.Data != null ? ex.Data.Count > 0 ? Newtonsoft.Json.JsonConvert.SerializeObject(ex.Data) : "" : "")}");
                return(new List <CompileResult> {
                    new CompileResult {
                        ErrorMessages = new List <CompilerError> {
                            new CompilerError {
                                Message = "BuildProject : Exception :  Something went wrong. Please try again.", LineNumber = 1, LinePosition = 1
                            }
                        }, Success = false
                    }
                });
            }
        }
 public MetaGraphNode CreateGraph(List <ResourcePropertyPosition> ResourceList, MetaGraphNode existingGraph = null)
 {
     try
     {
         parentNode   = new MetaGraphNode(CompilerConstants.GraphParentNode, metaPropertyType.others);
         resourceNode = new MetaGraphNode(CompilerConstants.GraphResourceNode, metaPropertyType.others, parentNode);
         if (existingGraph == null)
         {
             parentNode.Successors.Add(resourceNode);
             foreach (var resource in ResourceList)
             {
                 MetaGraphNode fileNode = new MetaGraphNode(resource.UrlPattern, metaPropertyType.resource, resourceNode);
                 resourceNode.Successors.Add(fileNode);
                 var result = CreatePartMetaGraph(resource.Properties, parentNode, fileNode);
             }
         }
         else
         {
             parentNode   = existingGraph;
             resourceNode = parentNode.Successors.FirstOrDefault(x => x.name == CompilerConstants.GraphResourceNode);
             foreach (var resource in ResourceList)
             {
                 var resNode = resourceNode.Successors.FirstOrDefault(x => x.name == resource.UrlPattern);
                 if (resNode == null)
                 {
                     MetaGraphNode fileNode = new MetaGraphNode(resource.UrlPattern, metaPropertyType.resource, resourceNode);
                     resourceNode.Successors.Add(fileNode);
                     var result = CreatePartMetaGraph(resource.Properties, parentNode, fileNode);
                 }
                 else
                 {
                     List <MetaGraphNode> propertyToRemove = new List <MetaGraphNode>();
                     foreach (var propertyNode in resNode.Predecessors)
                     {
                         if (propertyNode.type <= metaPropertyType.kstring)
                         {
                             if (propertyNode.Successors.Count > 1)
                             {
                                 propertyNode.Successors.Remove(resNode);
                                 propertyToRemove.Add(propertyNode);
                             }
                             else
                             {
                                 DeleteResourceMetaNode(propertyNode);
                                 propertyNode.Successors.Remove(resNode);
                                 propertyToRemove.Add(propertyNode);
                             }
                         }
                     }
                     foreach (var property in propertyToRemove)
                     {
                         resNode.Predecessors.Remove(property);
                     }
                     var result = CreatePartMetaGraph(resource.Properties, parentNode, resNode);
                 }
             }
         }
         return(parentNode);
     }
     catch (Exception ex)
     {
         //throw new Exception(string.Format("Faild to create reference Graph : {0}", ex.Message));
         return(null);
     }
 }