private void fs_Changed(object sender, FileSystemEventArgs e)
{
if (_watcherLastFullPath == e.FullPath && _watcherLastChange.AddSeconds(1) > DateTime.Now)
return;
var fi = new FileInfo(e.FullPath);
if (fi.Attributes.HasFlag(FileAttributes.Hidden))
return;
if (fi.Attributes.HasFlag(FileAttributes.Directory))
return;
_watcherLastFullPath = e.FullPath;
_watcherLastChange = DateTime.Now;
Task.Run(() =>
{
if (!new FileInfo(e.FullPath).Exists)
return;
var dir = new DirectoryInfo(e.FullPath);
Tuple<DeploymentSite, DeploymentFile> sourceFound = null;
while (sourceFound == null && dir != null)
{
var dirParts = dir.FullName.ToUpperInvariant();
if (_registeredSources.ContainsKey(dirParts))
{
sourceFound = _registeredSources[dirParts];
break;
}
dir = dir.Parent;
}
if (sourceFound == null)
return;
var fileConfig = sourceFound.Item2;
var site = sourceFound.Item1;
var localFile = e.FullPath;
string[] excludeSplit = null;
if (!string.IsNullOrEmpty(fileConfig.Exclude))
excludeSplit = fileConfig.Exclude.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] includeSplit = null;
if (!string.IsNullOrEmpty(fileConfig.Include))
includeSplit = fileConfig.Include.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (excludeSplit != null)
{
var excludeFile = false;
foreach (var exc in excludeSplit)
{
if (Regex.Match(localFile, exc, RegexOptions.IgnoreCase).Success)
{
excludeFile = true;
break;
}
}
if (excludeFile)
return;
}
if (includeSplit != null)
{
var excludeFile = false;
foreach (var inc in includeSplit)
{
if (!Regex.Match(localFile, inc, RegexOptions.IgnoreCase).Success)
{
excludeFile = true;
break;
}
}
if (excludeFile)
return;
}
var filename = Path.GetFileName(localFile);
Log("...... Deploying {0}...", ConsoleColor.DarkGray, filename);
var localDir = Path.GetDirectoryName(localFile);
localDir = localDir.Replace(fileConfig.Source, "").Replace("\\", "/");
var remoteFolderPath = fileConfig.Destination + localDir;
using (var context = GetClientContext(site))
{
var remoteFolder = context.Web.EnsureFolderPath(remoteFolderPath);
var remoteFile = remoteFolder.ServerRelativeUrl + (remoteFolder.ServerRelativeUrl.EndsWith("/") ? string.Empty : "/") + filename;
remoteFolder.UploadFile(filename, localFile, true);
Log("...... {0} deployed successfully", ConsoleColor.DarkGreen, remoteFile);
}
});
}