TransformVariables() public method

public TransformVariables ( string content ) : string
content string
return string
        public void Conditional_Is_Not_Visible_With_True(string condition)
        {
            var target = new VariableResolver(new List<Variable>());
            var result = target.TransformVariables("Hello\n<!--[if not " + condition + "]-->\ntobi!\n<!--[endif]-->");

            Assert.AreEqual("Hello\r\ntobi!", result);
        }
        public int TransformDirectory(string path, Environment targetEnvironment, bool deleteTemplate = true)
        {
            Log.DebugFormat("Transform templates for environment {1} ind {0} {2} deleting templates", path, targetEnvironment.Name, deleteTemplate ? "with" : "without");

            VariableResolver = new VariableResolver(targetEnvironment.Variables);

            int templateCounter = 0;

            foreach (var templateFile in _fileSystem.EnumerateDirectoryRecursive(path, "*.template.*", SearchOption.AllDirectories))
            {
                ++templateCounter;
                Log.Info(string.Format("  Transform template {0}", templateFile));

                var templateText = _fileSystem.ReadFile(templateFile);
                var transformed = VariableResolver.TransformVariables(templateText);

                _fileSystem.OverwriteFile(templateFile.Replace(".template.", ".").Replace(".Template.", "."), transformed);

                if (deleteTemplate)
                {
                    _fileSystem.DeleteFile(templateFile);
                }
            }

            Log.DebugFormat("Transformed {0} template(s) in {1}.", templateCounter, path);

            return templateCounter;
        }
        public void Conditional_Is_Visible_With_False_Variable_Negated(string condition)
        {
            var variableList = new List<Variable>()
            {
                new Variable() {Name = "condition", Value = condition}
            };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello\r\n<!--[if not ${condition}]-->\r\ntobi!\r\n<!--[endif]-->");

            Assert.AreEqual("Hello", result);
        }
        private EnvironmentVariablesDto GetEnvironment(string name, bool fetchVariables)
        {
            using (var session = DocumentStore.OpenSession())
            {
                var environment = session.Query<Environment>().FirstOrDefault(e => e.Name == name);

                if (environment == null)
                {
                    throw HttpError.NotFound("Environment {0} not found.".Fmt(name));
                }

                using (var workspace = new Workspace(FileSystem, ServerSettings))
                {
                    var result = new EnvironmentVariablesDto()
                    {
                        Environment = environment.ToDto()
                    };

                    if (fetchVariables)
                    {
                        workspace.UpdateSources();

                        var provider = new EnvironmentProvider();

                        try
                        {
                            var serializedEnvironment = provider.GetEnvironmentFromFile(Path.Combine(workspace.EnviornmentPath, name + ".xml"));

                            var resolver = new VariableResolver(serializedEnvironment.Variables);

                            result.Variables = new List<VariableDto>();
                            result.Variables.AddRange(serializedEnvironment.Variables.Select(v => new VariableDto()
                            {
                                Name = v.Name,
                                Value = v.Value,
                                Resolved = resolver.TransformVariables(v.Value)
                            }));

                            if (resolver.VariableUsageList.Any(v => v.IsMissingValue))
                            {
                                result.MissingVariables =
                                    new List<string>(resolver.VariableUsageList.Where(v => v.IsMissingValue).Select(v => v.Variable.Name));
                            }
                        }
                        catch (FileNotFoundException e)
                        {
                            result.Variables = new List<VariableDto>();
                            result.Warning = "No xml file found for this environment!";
                        }
                    }

                    return result;
                }
            }
        }
        public void Resolve_With_Default_Value_Set_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "FirstName", Value = "Jack" },
                    new Variable() { Name = "LastName", Value = "Bauer" },
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello ${FirstName=Mila} ${LastName=Kunis}!");

            Assert.AreEqual("Hello Jack Bauer!", result);
        }
        public void Resolve_Variable_In_Variable_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "FirstName", Value = "Mila" },
                    new Variable() { Name = "LastName", Value = "Kunis" },
                    new Variable() { Name = "Fullname", Value = "${FirstName} ${LastName}" },
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello ${Fullname}!");

            Assert.AreEqual("Hello Mila Kunis!", result);
        }
        public void Resolve_Variable_In_Variable_In_Variable_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "A", Value = "A + ${B}" },
                    new Variable() { Name = "B", Value = "B + ${C}" },
                    new Variable() { Name = "C", Value = "C" },
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("${A}");

            Assert.AreEqual("A + B + C", result);
        }
        public void Resolve_Using_Default_Value_Test()
        {
            var variableList = new List<Variable>();

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello ${FirstName=Mila} ${LastName=Kunis}!");

            Assert.AreEqual("Hello Mila Kunis!", result);
        }
        public void Resolve_On_Multiple_Lines_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "FirstName", Value = "Jack" },
                    new Variable() { Name = "LastName", Value = "Bauer" },
                    new Variable() { Name = "SecondLine", Value = "2" },
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables(@"Hello ${FirstName} ${LastName}!\r\nThis is another line:${SecondLine}");

            Assert.AreEqual(@"Hello Jack Bauer!\r\nThis is another line:2", result);
        }
        public void Resolve_One_Variable_Test()
        {
            var variableList = new List<Variable>() { new Variable() { Name = "Var1", Value = "Jack Bauer" } };

             var target = new VariableResolver(variableList);
             var result = target.TransformVariables("Hello ${Var1}!");

             Assert.AreEqual("Hello Jack Bauer!", result);
        }
        public void Resolve_Default_Value_With_Missing_Variable_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "FirstName", Value = "Jack" }
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello ${FullName=$[gugus]}");

            Assert.AreEqual(1, target.VariableUsageList.Count(vu => vu.IsMissingValue));
        }
        public void Multiple_Conditional_Transforming(string condition1, string condition2, string assert)
        {
            var variableList = new List<Variable>()
            {
                new Variable() { Name = "true.condition", Value = "true" },
                new Variable() { Name = "false.condition", Value = "false" },
            };

            var target = new VariableResolver(variableList);

            var result = target.TransformVariables("<!-- [if " + condition1 + "] -->" + System.Environment.NewLine +
                                                   "content 1" + System.Environment.NewLine +
                                                   "<!-- [endif] -->" + System.Environment.NewLine +
                                                   "<!-- [if " + condition2 + "] -->" + System.Environment.NewLine +
                                                   "content 2" + System.Environment.NewLine +
                                                   "<!-- [endif] -->");

            Assert.AreEqual(assert, result);
        }
        public void List_Multiple_Missing_Variables_Test()
        {
            var target = new VariableResolver(new List<Variable>());

            target.TransformVariables("${a} ${b} ${c}");

            Assert.AreEqual(3, target.VariableUsageList.Count(v => v.IsMissingValue));
        }
        public void List_Missing_Variables_Test()
        {
            var variableList = new List<Variable>()
                {
                    new Variable() { Name = "FirstName", Value = "Jack" },
                };

            var target = new VariableResolver(variableList);
            var result = target.TransformVariables("Hello ${FirstName} ${LastName}!");

            Assert.AreEqual(1, target.VariableUsageList.Count(v => v.IsMissingValue));
            Assert.AreEqual("Hello Jack !!Missing variable for LastName!!!", result);
        }
        public void Dont_Resolve_Escaped_Variable()
        {
            var target = new VariableResolver(new List<Variable>());
            var result = target.TransformVariables("Hello _$_{Escaped}!");

            Assert.AreEqual("Hello ${Escaped}!", result);
        }