Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MSbuild variables in transforms? #237

Open
xumix opened this issue Mar 22, 2017 · 5 comments
Open

Support MSbuild variables in transforms? #237

xumix opened this issue Mar 22, 2017 · 5 comments

Comments

@xumix
Copy link

xumix commented Mar 22, 2017

No description provided.

@davilimap
Copy link
Collaborator

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

@xumix
Copy link
Author

xumix commented Mar 22, 2017

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

@davilimap
Copy link
Collaborator

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.

@sayedihashimi
Copy link
Owner

sayedihashimi commented Mar 29, 2017

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.

@xumix
Copy link
Author

xumix commented Mar 30, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants