Otis wrote:
A derived class from DotnetTemplateEngine, which checks the taskcache and selects the right templateID, then calls its base class? (if none, it exists) ?
Great, it saves me a few lines. Here's my code if anyone's interested (in this case I decided to check against the project custom properties rather than the task cache, but it's a simple change):
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Xml;
using SD.LLBLGen.Pro.GeneratorCore;
using SD.LLBLGen.Pro.ApplicationCore;
using SD.LLBLGen.Pro.ApplicationCore.Templates;
using SD.LLBLGen.Pro.LptParser;
namespace Mozaic.WebGen.TaskPerformers {
public class ConditionalLPTGenerator : DotNetTemplateEngine {
#region Class Member Declarations
private bool _performResult;
#endregion
#region Class Property Declarations
/// <summary>
/// Reflects the result of the already executed <see cref="Perform"/> method. Should return false when
/// <see cref="Perform"/> hasn't been called yet. Other tasks can use this property to check what the result
/// was of a given task.
/// </summary>
public override bool PerformResult {
get { return _performResult;}
}
#endregion
public ConditionalLPTGenerator() {
}
public override void Perform(IGenerator executingGenerator, ITask taskDefinition) {
Perform(executingGenerator, taskDefinition, null);
}
public override void Perform(IGenerator executingGenerator, ITask taskDefinition, Hashtable parameters) {
if(parameters==null) {
throw new GeneratorAbortException("No parameters have been specified. Aborting generator.", taskDefinition);
}
if(parameters.Count <= 0) {
throw new GeneratorAbortException("No parameters have been specified. Aborting generator.", taskDefinition);
}
if(!parameters.ContainsKey("conditionalProperty")) {
throw new GeneratorAbortException("Mandatory parameter 'conditionalProperty' not found. Aborting generator.", taskDefinition);
}
string conditionalProperty = ((ITaskParameter)parameters["conditionalProperty"]).Value;
string propertyToCompare = string.Empty;
string compareValue = string.Empty;
bool negate = false;
if (executingGenerator.ProjectDefinition.CustomProperties.ContainsKey(conditionalProperty)) {
propertyToCompare = executingGenerator.ProjectDefinition.CustomProperties[conditionalProperty] as string;
}
if(parameters.ContainsKey("compareValue")) {
compareValue = ((ITaskParameter)parameters["compareValue"]).Value;
}
else {
propertyToCompare = propertyToCompare.ToLower();
compareValue = "true";
}
if(parameters.ContainsKey("negate")) {
string value=((ITaskParameter)parameters["negate"]).Value;
negate=(value.ToLower(CultureInfo.InvariantCulture)=="true");
}
if ((propertyToCompare == compareValue) != negate) {
base.Perform(executingGenerator,taskDefinition, parameters);
}
else {
base.LogLine("Conditional task was skipped.", "SettingsCreator", true, true);
base.AddNewLogNode(taskDefinition.TaskLogNode, LogNodeType.ActionDescription, "Conditional task was skipped. Conditions not met.");
}
_performResult = true;
}
}
}
Usage is simple. You pass in all of the normal parameters for the task, in addition you have to pass "conditionalProperty" which is the key for your custom property. By default it checks for a "true" value, but you have the option of passing in an expected value "compareValue", and you can also pass in "negate" if you want to ... negate the statement