Ejemplo n.º 1
0
        private bool ShouldOverwriteFile(ConfigurationElement originalElement, string localPath, DateTime remoteTimestamp)
        {
            var localFileHash = CheckSum.CalculateHashFromFile(localPath);

            if (localFileHash != originalElement.CheckSum)
            {
                return(AskOverwrite(string.Format("{0} was locally modified. Overwrite? (Yes/No/All)", Path.GetFileName(localPath))));
            }
            return(remoteTimestamp != originalElement.ModifiedOn);
        }
Ejemplo n.º 2
0
        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();
        }