private void Deploy(string name = null, string environment = null, bool watch = false)
{
try
{
IEnumerable<DeploymentSite> sitesToDeploy = null;
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(environment))
sitesToDeploy = _deploymentConfiguration.Sites;
else if (!string.IsNullOrEmpty(name))
sitesToDeploy = _deploymentConfiguration.Sites.Where(p => p.Name == name);
else if (!string.IsNullOrEmpty(environment))
sitesToDeploy = _deploymentConfiguration.Sites.Where(p => p.Environment == environment);
if (sitesToDeploy == null || sitesToDeploy.Count() == 0)
{
Log("Nothing to deploy!", ConsoleColor.Red);
return;
}
Log("Deployment started for {0}", ConsoleColor.White, !string.IsNullOrEmpty(name) ? name.ToUpper() : (!string.IsNullOrEmpty(environment) ? "environment " + environment.ToUpper() : "ALL sites"));
foreach (var site in sitesToDeploy)
{
Log("Deploying {0}...", ConsoleColor.Yellow, site.Name);
using (var context = GetClientContext(site))
{
foreach (var fileConfig in site.Files)
{
Log("... from {0} to {1}", ConsoleColor.DarkGray, fileConfig.Source, fileConfig.Destination);
var requiresPublishing = false;
if (!site.FastMode)
{
var destFolder = context.Web.EnsureFolderPath(fileConfig.Destination);
try
{
var destinationList = context.Web.GetListByUrl(fileConfig.Destination);
context.Load(destinationList, p => p.EnableMinorVersions);
context.ExecuteQuery();
requiresPublishing = destinationList.EnableMinorVersions;
}
catch { }
}
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);
var folderCache = new Dictionary<string, Folder>();
foreach (var localFile in Directory.GetFiles(fileConfig.Source, "*.*", SearchOption.AllDirectories))
{
if (excludeSplit != null)
{
var excludeFile = false;
foreach (var exc in excludeSplit)
{
if (Regex.Match(localFile, exc, RegexOptions.IgnoreCase).Success)
{
excludeFile = true;
break;
}
}
if (excludeFile)
{
Log("...... {0} skipped by exclude pattern", ConsoleColor.DarkYellow, localFile);
continue;
}
}
if (includeSplit != null)
{
var excludeFile = false;
foreach (var inc in includeSplit)
{
if (!Regex.Match(localFile, inc, RegexOptions.IgnoreCase).Success)
{
excludeFile = true;
break;
}
}
if (excludeFile)
{
Log("...... {0} skipped by include pattern", ConsoleColor.DarkYellow, localFile);
continue;
}
}
var filename = Path.GetFileName(localFile);
var localDir = Path.GetDirectoryName(localFile);
localDir = localDir.Replace(fileConfig.Source, "").Replace("\\", "/");
var remoteFolderPath = fileConfig.Destination + localDir;
Folder remoteFolder = null;
if (!folderCache.ContainsKey(remoteFolderPath))
{
remoteFolder = context.Web.EnsureFolderPath(remoteFolderPath);
folderCache.Add(remoteFolderPath, remoteFolder);
}
remoteFolder = folderCache[remoteFolderPath];
var remoteFile = remoteFolder.ServerRelativeUrl + (remoteFolder.ServerRelativeUrl.EndsWith("/") ? string.Empty : "/") + filename;
if (!site.FastMode && fileConfig.Destination != "/")
context.Web.CheckOutFile(remoteFile);
remoteFolder.UploadFile(filename, localFile, true);
if (!site.FastMode && fileConfig.Destination != "/")
context.Web.CheckInFile(remoteFile, CheckinType.MajorCheckIn, "SPDeployment");
if (requiresPublishing)
context.Web.PublishFile(remoteFile, "SPDeployment");
Log("...... {0} deployed successfully", ConsoleColor.DarkGreen, remoteFile);
}
}
}
if (watch)
RegisterWatchTask(site);
}
if (watch)
Log("Completed successfully. Watching for changes...", ConsoleColor.Green);
else
Log("Completed successfully", ConsoleColor.Green);
}
catch (Exception ex)
{
Log("Stop Error: {0}", ConsoleColor.Red, ex.ToString());
Console.ResetColor();
throw new ApplicationException();
}
}