/// <summary> /// Fills the export snippet placeholders /// </summary> /// <param name="code">Code to fill</param> /// <param name="exportObject">Export object</param> /// <param name="flexFieldObject">Flex field object</param> /// <returns>Updated code</returns> private async Task <string> FillExportSnippetPlaceholders(string code, IExportSnippetExportable exportObject, FlexFieldObject flexFieldObject) { HashSet <string> usedExportSnippets = new HashSet <string>(); Dictionary <string, List <ExportSnippetFunction> > renderedFunctions = new Dictionary <string, List <ExportSnippetFunction> >(); List <ObjectExportSnippet> exportSnippets = await _cachedDbAccess.GetObjectExportSnippetsByObject(exportObject.Id); code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasAnyCodeSnippet_Start, Placeholder_HasAnyCodeSnippet_End, exportSnippets.Count > 0); code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNotAnyCodeSnippet_Start, Placeholder_HasNotAnyCodeSnippet_End, exportSnippets.Count == 0); code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_HasCodeSnippet_Start, Placeholder_HasCodeSnippet_End, (m) => { string snippetName = m.Groups[1].Value; snippetName = snippetName.ToLowerInvariant(); return(exportSnippets.Any(e => e.SnippetName.ToLowerInvariant() == snippetName)); }); code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_HasNotCodeSnippet_Start, Placeholder_HasNotCodeSnippet_End, (m) => { string snippetName = m.Groups[1].Value; snippetName = snippetName.ToLowerInvariant(); return(!exportSnippets.Any(e => e.SnippetName.ToLowerInvariant() == snippetName)); }); code = await ExportUtil.BuildPlaceholderRegex(ExportConstants.ExportSnippetRegex, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async(m) => { string snippetName = m.Groups[2].Value; snippetName = snippetName.ToLowerInvariant(); if (!usedExportSnippets.Contains(snippetName)) { usedExportSnippets.Add(snippetName); } return(await ExportCodeSnippetContent(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject, m.Groups[1].Value)); }); code = await ExportUtil.RenderPlaceholderIfFuncTrueAsync(code, Placeholder_ExportSnippet_HasCodeSnippet_Additional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_Additional_Functions_End, async (m) => { string snippetName = m.Groups[1].Value; snippetName = snippetName.ToLowerInvariant(); return(await HasCodeSnippetAdditionalFunctions(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject)); }); code = await ExportUtil.RenderPlaceholderIfFuncTrueAsync(code, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_End, async (m) => { string snippetName = m.Groups[1].Value; snippetName = snippetName.ToLowerInvariant(); return(!(await HasCodeSnippetAdditionalFunctions(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject))); }); code = await ExportUtil.BuildPlaceholderRegex(Placeholder_ExportSnippet_AdditionalFuntions, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async(m) => { string snippetName = m.Groups[2].Value; snippetName = snippetName.ToLowerInvariant(); return(await ExportCodeSnippetAdditionalFunctions(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject, m.Groups[1].Value)); }); ValidateAllExportSnippetsUsed(usedExportSnippets, exportSnippets); return(code); }