slow-cheetah icon indicating copy to clipboard operation
slow-cheetah copied to clipboard

Support MSbuild variables in transforms?

Open xumix opened this issue 7 years ago • 5 comments

xumix avatar Mar 22 '17 16:03 xumix

Can you provide a better explanation for this? Most likely this is an issue with XDT, the engine behind the SlowCheetah transformations.

davilimap avatar Mar 22 '17 17:03 davilimap

Ooops, misclicked. Is there a way to insert a variable into a transformation? For example set some attribute value to $(ToolsPath)

xumix avatar Mar 22 '17 17:03 xumix

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.

davilimap avatar Mar 28 '17 18:03 davilimap

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 avatar Mar 29 '17 23:03 sayedihashimi

@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;
        }
    }

xumix avatar Mar 30 '17 11:03 xumix