Ejemplo n.º 1
0
        public void VerifyRegionIncludeWhitespaceFixup2()
        {
            string value    = @"Hello
    #begin foo
value
    #end
There";
            string expected = @"Hello
foo
value
There";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("#begin", "#end", true, false, true, null) };
            EngineConfig         cfg        = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
        public void VerifyTornPageInCloseSeekRegion()
        {
            string value    = @"Hello
    #begin foo
value
value
value
value
value
value
value
value
    #end
There";
            string expected = @"Hello
There";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("#begin", "#end", false, true, true) };
            EngineConfig         cfg        = new EngineConfig(VariableCollection.Environment(), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 28);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 3
0
        public VsTemplateGlobalRunSpec(IParameterSet parameters, IReadOnlyDictionary <string, string> pathMap, IReadOnlyList <string> copyOnly)
        {
            _pathMap = pathMap;
            _special = new Dictionary <IPathMatcher, IRunSpec>(copyOnly.Count);

            foreach (string copyOnlyFile in copyOnly)
            {
                _special[new SpecificFilesMatcher(new[] { copyOnlyFile })] = new NoOpRunSpec();
            }

            string registeredOrganization;

            using (RegistryKey key = Registry.LocalMachine?.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion"))
            {
                registeredOrganization = key?.GetValue("RegisteredOrganization", "")?.ToString() ?? "";
            }

            VariableCollection sys = new VariableCollection(VariableCollection.Environment(true, false, "${0}$"))
            {
                ["$year$"] = DateTime.Now.Year.ToString(),
                ["$registeredorganization$"] = registeredOrganization,
                ["$targetframeworkversion$"] = "4.6",
                ["$machinename$"]            = Environment.MachineName,
                ["$clrVersion$"]             = typeof(VsTemplateGenerator).GetTypeInfo().Assembly.ImageRuntimeVersion,
                ["$registeredorganization$"] = registeredOrganization,
                ["$time$"] = DateTime.Now.ToString("G"),
                ["$specificsolutionname$"] = "",
                ["$webnamespace$"]         = ""
            };

            for (int i = 0; i < 11; ++i)
            {
                sys[$"$guid{i}$"] = Guid.NewGuid();
            }

            VariableCollection vc = new VariableCollection(sys);

            foreach (ITemplateParameter param in parameters.Parameters)
            {
                if (param.Priority != TemplateParameterPriority.Required)
                {
                    vc[$"${param.Name}$"] = param.DefaultValue;
                }
            }

            foreach (KeyValuePair <ITemplateParameter, string> param in parameters.ParameterValues)
            {
                vc[$"${param.Key.Name}$"] = param.Value;
            }

            RootVariableCollection = vc;
        }
Ejemplo n.º 4
0
        public void TestLookAroundMatches()
        {
            string value    = @"aababcabcacc";
            string expected = @"aaba!cabcacc";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Replacement("b".TokenConfigBuilder().OnlyIfBefore("cab").OnlyIfAfter("ba"), "!", null, true) };
            EngineConfig         cfg        = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 5
0
        public void VerifyVariables()
        {
            string value    = @"test %PATH% test";
            string expected = @"test " + Environment.GetEnvironmentVariable("PATH") + " test";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new ExpandVariables() };
            EngineConfig         cfg        = new EngineConfig(VariableCollection.Environment(), "%{0}%");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 6
0
        public void VerifyTinyPageReplacement()
        {
            string value    = @"test value test";
            string expected = @"test foo test";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Replacment("value", "foo") };
            EngineConfig         cfg        = new EngineConfig(VariableCollection.Environment(), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
        public void VerifyRegionExcludeToggle()
        {
            string value    = @"test region value x test region bar";
            string expected = @"test  bar";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("region", "region", false, false, false) };
            EngineConfig         cfg        = new EngineConfig(VariableCollection.Environment(), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 8
0
        public void VerifyRegionStrayEnd()
        {
            string value    = @"test foo value bar foo";
            string expected = @"test   bar ";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("value", "foo", true, false, false, null) };
            EngineConfig         cfg        = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 9
0
        public void TestLongestActualWins()
        {
            string value    = @"foobarbaz";
            string expected = @"testarbaz";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations =
            {
                new Replacement("foob".TokenConfigBuilder().OnlyIfBefore("arbaz"), "test",  null, true),
                new Replacement("foo".TokenConfigBuilder().OnlyIfBefore("barbaz"), "test2", null, true)
            };
            EngineConfig cfg       = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 10
0
        public void TestReadAheadBreaksLookBehinds()
        {
            string value    = @"footbarbaz";
            string expected = @"barbaz";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations =
            {
                new MockOperationProvider(new MockOperation(null,               ReadaheadOneByte, true, Encoding.UTF8.GetBytes("foo"))),
                new Replacement("bar".TokenConfigBuilder().OnlyIfAfter("foot"), "b",              null, true)
            };
            EngineConfig cfg       = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
        public void TestLookBehindCoveringMatchedValueGetsMatched()
        {
            string value    = @"foobarbaz";
            string expected = @"fooba";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations =
            {
                new Replacement("baz".TokenConfigBuilder().OnlyIfAfter("foobar"), "a", null, true),
                new Replacement("bar".TokenConfigBuilder().OnlyIfAfter("foo"),    "b", null, true)
            };
            EngineConfig cfg       = new EngineConfig(_engineEnvironmentSettings, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 12
0
        public void TestLookaroundMatchLengthBehavior()
        {
            string value    = @"background-color:white;
color:white;";
            string expected = @"background-color:blue;
color:red;";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations =
            {
                new Replacement("white".TokenConfigBuilder().OnlyIfBefore(";").OnlyIfAfter("background-color:"), "blue", null, true),
                new Replacement("white".TokenConfigBuilder().OnlyIfBefore(";").OnlyIfAfter("color:"),            "red",  null, true)
            };
            EngineConfig cfg       = new EngineConfig(EnvironmentSettings, VariableCollection.Environment(EnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 13
0
        public void VerifyRegionIncludeWhitespaceFixup()
        {
            string value    = @"test value value x test foo bar";
            string expected = @"testx testbar";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("value".TokenConfig(), "foo".TokenConfig(), true, false, true, null, true) };
            EngineConfig         cfg        = new EngineConfig(_engineEnvironmentSettings.Host.Logger, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 14
0
        public void TestLookAroundsCanBeUsedForInsertion()
        {
            string value    = @"foobaz";
            string expected = @"foobarbaz";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations =
            {
                new Replacement("".TokenConfigBuilder().OnlyIfAfter("foo").OnlyIfBefore("baz"), "bar", null, true)
            };
            EngineConfig cfg       = new EngineConfig(_engineEnvironmentSettings.Host.Logger, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 15
0
        public void VerifyLongStreamNoReplacement()
        {
            Random rnd = new Random();

            byte[] valueBytes = new byte[1024 * 1024];
            rnd.NextBytes(valueBytes);
            ChunkMemoryStream input  = new ChunkMemoryStream(valueBytes, 512);
            ChunkMemoryStream output = new ChunkMemoryStream(1024);

            IOperationProvider[] operations = Array.Empty <IOperationProvider>();
            EngineConfig         cfg        = new EngineConfig(_engineEnvironmentSettings.Host.Logger, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            processor.Run(input, output, 1024);
            Assert.Equal(input.Length, output.Length);

            int file1byte;
            int file2byte;

            do
            {
                file1byte = input.ReadByte();
                file2byte = output.ReadByte();
            }while ((file1byte == file2byte) && (file1byte != -1));
            Assert.Equal(0, file1byte - file2byte);
        }
Ejemplo n.º 16
0
        public void VerifyTinyPageRegion()
        {
            string value    = @"Hello
    #begin foo
value
    #end
There";
            string expected = @"Hello
value
There";

            byte[]       valueBytes = Encoding.UTF8.GetBytes(value);
            MemoryStream input      = new MemoryStream(valueBytes);
            MemoryStream output     = new MemoryStream();

            IOperationProvider[] operations = { new Region("#begin".TokenConfig(), "#end".TokenConfig(), true, true, true, null, true) };
            EngineConfig         cfg        = new EngineConfig(_engineEnvironmentSettings.Host.Logger, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor           processor  = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
Ejemplo n.º 17
0
        public void VerifyLongStreamWithReplacementBeforeAfter()
        {
            string value    = @"test valueA before valueA after valueB valueB";
            string expected = @"test foo before valueA after bar valueB";

            StringBuilder valueBuilder    = new StringBuilder();
            StringBuilder expectedBuilder = new StringBuilder();

            for (int i = 0; i < 1024; i++)
            {
                valueBuilder.Append(value);
                expectedBuilder.Append(expected);
            }
            value    = valueBuilder.ToString();
            expected = expectedBuilder.ToString();

            byte[]            valueBytes = Encoding.UTF8.GetBytes(value);
            ChunkMemoryStream input      = new ChunkMemoryStream(valueBytes, 10);
            ChunkMemoryStream output     = new ChunkMemoryStream(10);

            IOperationProvider[] operations =
            {
                new Replacement("valueA".TokenConfigBuilder().OnlyIfBefore(" before"), "foo", null, true),
                new Replacement("valueB".TokenConfigBuilder().OnlyIfAfter("after "),   "bar", null, true),
            };
            EngineConfig cfg       = new EngineConfig(_engineEnvironmentSettings.Host.Logger, VariableCollection.Environment(_engineEnvironmentSettings), "${0}$");
            IProcessor   processor = Processor.Create(cfg, operations);

            //Changes should be made
            bool changed = processor.Run(input, output, 1000);

            Verify(Encoding.UTF8, output, changed, value, expected);
        }
        private VariableCollection HandleVariables(IParameterSet parameters, JObject data, List <IOperationProvider> result, bool allParameters = false)
        {
            VariableCollection vc = VariableCollection.Root();
            JToken             expandToken;

            if (data.TryGetValue("expand", out expandToken) && expandToken.Type == JTokenType.Boolean && expandToken.ToObject <bool>())
            {
                result?.Add(new ExpandVariables());
            }

            JObject sources        = (JObject)data["sources"];
            string  fallbackFormat = data["fallbackFormat"]?.ToString();
            Dictionary <string, VariableCollection> collections = new Dictionary <string, VariableCollection>();

            foreach (JProperty prop in sources.Properties())
            {
                VariableCollection c      = null;
                string             format = prop.Value.ToString();

                switch (prop.Name)
                {
                case "environment":
                    c = VariableCollection.Environment(format);

                    if (fallbackFormat != null)
                    {
                        c = VariableCollection.Environment(c, fallbackFormat);
                    }
                    break;

                case "user":
                    c = ProduceUserVariablesCollection(parameters, format, allParameters);

                    if (fallbackFormat != null)
                    {
                        VariableCollection d = ProduceUserVariablesCollection(parameters, fallbackFormat, allParameters);
                        d.Parent = c;
                        c        = d;
                    }
                    break;
                }

                collections[prop.Name] = c;
            }

            foreach (JToken order in ((JArray)data["order"]).Children())
            {
                VariableCollection current = collections[order.ToString()];

                VariableCollection tmp = current;
                while (tmp.Parent != null)
                {
                    tmp = tmp.Parent;
                }

                tmp.Parent = vc;
                vc         = current;
            }

            return(vc);
        }