Skip to content

Commit

Permalink
Corrected error when multiple files are saved at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
crisfervil committed Nov 22, 2017
1 parent 84eed6c commit be11337
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/XrmCommandBox/Tools/WebResourcesSyncTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -18,7 +19,8 @@ public class WebResourcesSyncTool
private readonly IOrganizationService _crmService;
private readonly ILog _log = LogManager.GetLogger(typeof(ImportTool));

private List<string> _filesToUpdate = new List<string>();
private ConcurrentQueue<string> _filesToUpdate = new ConcurrentQueue<string>();
private ConcurrentQueue<Guid> _webResourcesToPublish = new ConcurrentQueue<Guid>();

public WebResourcesSyncTool(IOrganizationService service)
{
Expand Down Expand Up @@ -57,40 +59,55 @@ public void Run(WebResourcesSyncToolOptions options)
_log.Info($"Done!");
}


private void UpdateFiles()
{
var updatedWebResources = new List<Guid>();
foreach (var fileToUpdate in _filesToUpdate)
while(!_filesToUpdate.IsEmpty)
{
var webResourceName = fileToUpdate.Substring(Environment.CurrentDirectory.Length+1);
webResourceName = webResourceName.Replace("\\","/");
_log.Info($"Updating {webResourceName}...");

var webresourceId = GetId(webResourceName);
if (webresourceId.HasValue)
string fileToUpdate = null;
if(_filesToUpdate.TryDequeue(out fileToUpdate))
{
Update(webresourceId.Value, webResourceName, fileToUpdate);
updatedWebResources.Add(webresourceId.Value);
}
else
{
_log.Info($"{webResourceName} doesn't exist in the environment");
UpdateWebResource(fileToUpdate);
}
}

if (updatedWebResources.Count > 0) {
if (!_webResourcesToPublish.IsEmpty) {
_log.Info("Publishing customizations...");
PublishAllCustomizations(updatedWebResources);
PublishWebResources();
_log.Info("Done. Waiting for more changes....");
}
}

_filesToUpdate.Clear();
private void UpdateWebResource(string fileToUpdate)
{
var webResourceName = fileToUpdate.Substring(Environment.CurrentDirectory.Length + 1);
webResourceName = webResourceName.Replace("\\", "/");
_log.Info($"Updating {webResourceName}...");

var webresourceId = GetId(webResourceName);
if (webresourceId.HasValue)
{
Update(webresourceId.Value, webResourceName, fileToUpdate);
_webResourcesToPublish.Enqueue(webresourceId.Value); // add this to the publish queue
}
else
{
_log.Info($"{webResourceName} doesn't exist in the environment");
}
}

private void PublishAllCustomizations(IEnumerable<Guid> webResourcesIds)
private void PublishWebResources()
{

var webResourcesIds = new List<Guid>();
while (!_webResourcesToPublish.IsEmpty)
{
Guid webResourceId=Guid.Empty;
if(_webResourcesToPublish.TryDequeue(out webResourceId))
{
webResourcesIds.Add(webResourceId);
}
}

var webResourcesXml = webResourcesIds.Select(x => $"<webresource>{{{x}}}</webresource>").ToList();

var importExportXml = $"<importexportxml>" +
Expand Down Expand Up @@ -136,9 +153,8 @@ private void File_Changed(object sender, FileSystemEventArgs e)
_log.Debug($"Change detected: {e.ChangeType} : {e.FullPath}");
if (!_filesToUpdate.Contains(e.FullPath))
{
_filesToUpdate.Add(e.FullPath);
_filesToUpdate.Enqueue(e.FullPath);
}
}

}
}

0 comments on commit be11337

Please sign in to comment.