Skip to content

Instantly share code, notes, and snippets.

@jraps20
Last active July 19, 2018 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jraps20/1f86cb8a8a620c36130cf7de5304b741 to your computer and use it in GitHub Desktop.
Save jraps20/1f86cb8a8a620c36130cf7de5304b741 to your computer and use it in GitHub Desktop.
TDS Post Deploy Action to publish items via publishing queue instead of native solution
[Description("Publishes deployed items after deployment using publishing queue.\nPublishing targets are specified in the Parameter as a comma separated list.")]
public class PublishDeployedItemsWithPublishingQueue : IPostDeployAction
{
/// <summary>
/// Borrowed directly from HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.BuiltIn.PublishDeployedItems, HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor
/// </summary>
private static Database[] GetPublishingTargetDatabases(string parameters)
{
if (string.IsNullOrEmpty(parameters))
{
throw new InvalidOperationException("Please specify a publishing target in the parameter");
}
var source = (from t in parameters.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
join p in PublishManager.GetPublishingTargets(Database.GetDatabase("master")) on t.Trim().ToLower() equals p.Name.ToLower()
select Database.GetDatabase(p["Target Database"]) into d
where d != null
select d).Distinct().ToArray();
if (!source.Any())
{
throw new InvalidOperationException("Could not locate publishing target databases");
}
return source;
}
/// <summary>
/// Borrowed directly from HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.BuiltIn.PublishDeployedItems, HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor
/// </summary>
private static Language[] GetPublishingTargetLanguages(Database[] publishingTargetDatabases)
{
if (publishingTargetDatabases == null)
throw new ArgumentNullException(nameof(publishingTargetDatabases));
var source = new List<Language>();
foreach (var database in publishingTargetDatabases)
{
source.AddRange(LanguageManager.GetLanguages(database));
}
return source.Distinct().ToArray();
}
public void RunPostDeployAction(XDocument deployedItems, IPostDeployActionHost host, string parameter)
{
Log.Info("Running TDS Post Deploy Action :: PublishDeployedItemsWithQueue", this);
var publishingTargetDatabases = GetPublishingTargetDatabases(parameter);
var publishingTargetLanguages = GetPublishingTargetLanguages(publishingTargetDatabases);
var publishingCandidates = new List<PublishingCandidate>();
var sourceDb = Database.GetDatabase("master");
PostDeployActionSupport.ExecuteOnAllDeployedItems(deployedItems, delegate (XElement deployedItemInPackage, Guid deployedItemId, string databaseName)
{
if (databaseName == "core")
return;
if (databaseName != "master")
Log.Info(":: PublishDeployedItemsWithQueue, unknown database -> " + databaseName, this);
publishingCandidates.AddRange(
PublishUtils.CreatePublishingCandidatesFromItemId(sourceDb, publishingTargetDatabases, publishingTargetLanguages, new ID(deployedItemId))
);
});
Log.Info(":: PublishDeployedItemsWithQueue, targetDatabases", this);
foreach (var db in publishingTargetDatabases)
{
Log.Info(":: PublishDeployedItemsWithQueue, targetDatabase -> " + db.Name, this);
}
Log.Info(":: PublishDeployedItemsWithQueue, targetLanguages", this);
foreach (var lang in publishingTargetLanguages)
{
Log.Info(":: PublishDeployedItemsWithQueue, targetLanguage -> " + lang.Name, this);
}
Log.Info(":: PublishDeployedItemsWithQueue, number of publishing candidates -> " + publishingCandidates.Count, this);
PublishUtils.ProcessCandidates(sourceDb, publishingTargetDatabases, publishingTargetLanguages, publishingCandidates);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment