private void DownloadWebTemplates() { Console.WriteLine("Downloading Web Templates"); var directory = CreateDirectory(Options.WebTemplatesDirectory); var metaData = new ConfigurationMetaData(Path.Combine(directory, "WebTemplates.json")); var fetchData = new { adx_websiteid = Website.Id }; var fetchXml = $@" <fetch> <entity name='adx_webtemplate'> <attribute name='adx_websiteid' /> <attribute name='adx_name' /> <attribute name='adx_source' /> <attribute name='modifiedon' /> <filter> <condition attribute='adx_websiteid' operator='eq' value='{fetchData.adx_websiteid}'/> </filter> </entity> </fetch>"; var result = Service.RetrieveMultiple(new FetchExpression(fetchXml)); foreach (var e in result.Entities) { var name = (string)e["adx_name"]; var filename = name + ".liquid"; var path = Path.Combine(directory, filename); var modifiedOn = (DateTime)e["modifiedon"]; string content = (string)e["adx_source"]; var metaDataElement = metaData.GetElementById(e.Id); if (ShouldOverwriteFile(metaDataElement, path, modifiedOn)) { metaDataElement.Type = ConfigurationElement.ElementType.WebTemplate; metaDataElement.Name = name; metaDataElement.FileName = filename; metaDataElement.RecordId = e.Id; metaDataElement.ModifiedOn = modifiedOn; metaDataElement.CheckSum = CheckSum.CalculateHash(content); Console.WriteLine("Saving {0} ({1})", path.ShortenLeft(30), content.ShortenRight(50)); WriteTextFile(path, content); } } metaData.Update(); }
private void DeployWebFiles() { Console.WriteLine("Web Files"); var path = Options.LocalDirectory; path = Path.Combine(path, "WebFiles"); var metaData = new ConfigurationMetaData(Path.Combine(path, "WebFiles.json")); var directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); foreach (var dir in directories) { var relativeDir = dir.Substring(path.Length + 1); Console.WriteLine(relativeDir); var hierarchy = relativeDir.Split('\\'); string pageFilter = ""; foreach (var page in relativeDir.Split('\\')) { pageFilter = string.Format(@" <link-entity name='adx_webpage' from='adx_webpageid' to='adx_parentpageid'> <filter> <condition attribute='adx_name' operator='eq' value='{0}'/> </filter> {1} </link-entity> ", page, pageFilter); } foreach (var file in Directory.GetFiles(dir)) { var fileName = file.Substring(dir.Length + 1); var relativeFileName = file.Substring(path.Length + 1); if (relativeFileName.ToLower() == "webfiles.json") { continue; } var metaDataElement = metaData.GetElementByFilename(relativeFileName); if (metaDataElement.CheckSum == CheckSum.CalculateHashFromFile(file)) { continue; // skip loop } var fetchData = new { adx_websiteid = Website.Id, adx_name = fileName, pageFilter }; var fetchXml = $@" <fetch top='1'> <entity name='annotation'> <attribute name='annotationid' /> <attribute name='documentbody' /> <attribute name='modifiedon' /> <order attribute='modifiedon' descending='true' /> <link-entity name='adx_webfile' from='adx_webfileid' to='objectid'> <attribute name='adx_name' /> <filter> <condition attribute='adx_websiteid' operator='eq' value='{fetchData.adx_websiteid/*2AB10DAB-D681-4911-B881-CC99413F07B6*/}'/> <condition attribute='adx_name' operator='eq' value='{fetchData.adx_name/*logo.jpg*/}'/> </filter> {fetchData.pageFilter} </link-entity> </entity> </fetch>"; var results = Service.RetrieveMultiple(new FetchExpression(fetchXml)); if (results.Entities.Count == 0) { throw new ApplicationException(string.Format("No file found for {0}. Create one first.", fileName)); } else if (results.Entities.Count > 1) { throw new ApplicationException(string.Format("More than one file found ({0}). I really didn't expect this.", fileName)); } else { var target = results.Entities[0]; var modifiedOn = (DateTime)target["modifiedon"]; bool shouldDeploy = true; if (metaDataElement.ModifiedOn < modifiedOn) { shouldDeploy = AskOverwrite(string.Format("The remote file has been changed since the {0} was downloaded. Overwrite anyway? (Yes/No/All)", fileName)); } var content = Convert.ToBase64String(File.ReadAllBytes(file)); if (content == (string)target["documentbody"]) { shouldDeploy = false; } if (shouldDeploy) { Entity entity = new Entity(target.LogicalName, target.Id); entity["documentbody"] = content; Console.WriteLine("Updating {0}", relativeFileName.ShortenLeft(50)); if (!Options.WhatIf) { Service.Update(entity); } entity = Service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(new string[] { "documentbody", "modifiedon" })); metaDataElement.CheckSum = CheckSum.CalculateHash((string)entity["documentbody"]); metaDataElement.ModifiedOn = (DateTime)entity["modifiedon"]; } } } } metaData.Update(); }
private void DeployWebTemplates() { Console.WriteLine("Deploying Web Templates"); var path = Path.Combine(Options.LocalDirectory, Options.WebTemplatesDirectory); var metaData = new ConfigurationMetaData(Path.Combine(path, "WebTemplates.json")); var files = Directory.GetFiles(path); foreach (var file in files) { var fileName = file.Substring(path.Length + 1); if (fileName.ToLower() == "webtemplates.json") { continue; } var webTemplateName = fileName.Replace(".liquid", ""); var fetchData = new { adx_websiteid = Website.Id, adx_name = webTemplateName }; var fetchXml = $@" <fetch> <entity name='adx_webtemplate'> <attribute name='adx_webtemplateid' /> <attribute name='adx_source' /> <attribute name='modifiedon' /> <filter> <condition attribute='adx_websiteid' operator='eq' value='{fetchData.adx_websiteid/*2AB10DAB-D681-4911-B881-CC99413F07B6*/}'/> <condition attribute='adx_name' operator='eq' value='{fetchData.adx_name/*Breadcrumbs*/}'/> </filter> </entity> </fetch>"; var results = Service.RetrieveMultiple(new FetchExpression(fetchXml)); if (results.Entities.Count == 0) { throw new ApplicationException(string.Format("Web Template {0} does not exist. Please create it first.", webTemplateName)); } else if (results.Entities.Count > 1) { throw new ApplicationException(string.Format("Web Template {0} is not a unique name.", webTemplateName)); } var template = results.Entities[0]; var modifiedOn = (DateTime)template["modifiedon"]; var metaDataElement = metaData.GetElementByFilename(fileName); var shouldDeploy = true; var forceDeploy = false; if (metaDataElement.ModifiedOn < modifiedOn) { shouldDeploy = AskOverwrite(string.Format("The remote file has been changed since the {0} was downloaded. Overwrite anyway? (Yes/No/All)", webTemplateName)); forceDeploy = shouldDeploy; } var content = File.ReadAllText(file); if (!forceDeploy && (string)template["adx_source"] == content) { shouldDeploy = false; } else { var entity = new Entity("adx_webtemplate", template.Id); entity["adx_source"] = content; if (!Options.WhatIf) { Console.WriteLine("Updating {0}", webTemplateName.ShortenRight(50)); Service.Update(entity); } entity = Service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(new string[] { "adx_source", "modifiedon" })); metaDataElement.CheckSum = CheckSum.CalculateHash((string)entity["adx_source"]); metaDataElement.ModifiedOn = (DateTime)entity["modifiedon"]; } } metaData.Update(); }