slow-cheetah
slow-cheetah copied to clipboard
Support MSbuild variables in transforms?
Can you provide a better explanation for this? Most likely this is an issue with XDT, the engine behind the SlowCheetah transformations.
Ooops, misclicked. Is there a way to insert a variable into a transformation? For example set some attribute value to $(ToolsPath)
That functionality is currently not supported. SlowCheetah uses XDT as its transformation engine, and as far as I know this isn't supported so this would have to be done in a pre process of the transform file. I'll add the appropriate tags and track the demand for this.
XDT itself doesn't support this, but XDT has extensibility. I think you could create a custom XDT transform but I'm not sure how you would get the MSBuild properties and values. Instead of this I think it would be better to have a transform file which is processed by MSBuild to update the MSBuild tokens and then run XDT on that.
@sayedihashimi I see, thank you. Actually I've decided to go that exact way
public class EnvironmentValueReplace : Transform
{
private readonly IDictionary envVars = System.Environment.GetEnvironmentVariables();
protected override void Apply()
{
var xmlAttributeCollection = TargetNode.Attributes;
if (xmlAttributeCollection == null)
{
return;
}
foreach (XmlAttribute attr in xmlAttributeCollection)
{
if (string.IsNullOrWhiteSpace(attr.Value))
{
continue;
}
var newVal = Replace(attr.Value);
attr.Value = newVal;
}
TargetNode.Value = Replace(TargetNode.Value);
}
private string Replace(string value)
{
MatchEvaluator evaluator = (m) =>
{
if (m.Success && m.Groups[1].Success && envVars.Contains(m.Groups[1].Value))
{
return System.Environment.GetEnvironmentVariable(m.Groups[1].Value);
}
return m.Value;
};
var newVal = Regex.Replace(value, @"\$\([^\)]+\)", evaluator, RegexOptions.Compiled);
return newVal;
}
}