public void TestSimpleStringResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("Value1", "value for 1", "#MessageId = 1");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass2");

            builder2.Add("Value2", "value for 2", "#MessageId = 2");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                    using (TempFile resx2 = TempFile.FromExtension(".resx"))
                    {
                        builder1.BuildResX(resx1.TempPath);
                        builder2.BuildResX(resx2.TempPath);
                        Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                    }

                string contents = mc.ReadAllText();

                Assert.IsTrue(contents.Contains("\r\nvalue for 1\r\n.\r\n"));
                Assert.IsTrue(contents.Contains("\r\nvalue for 2\r\n.\r\n"));
            }
        }
Example #2
0
        public void TestProjectResXWithBadAssemblyInfo()
        {
            /* Bad assembly info... */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat + "[InvalidAttribute]");
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));
                                }
        }
        public void TestFormatWithSuffixResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("WhatsUp(string s, int i)", "Format!%0\n\tstring={0},\n.\r\n\tnumber={1}.", "#messageId=23");
            builder1.Add(".EventMessageFormat", "Suffix\nTest {0} Format");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string contents = mc.ReadAllText();
                Assert.IsTrue(contents.Contains(@"
Format%!%%0%n
	string=%2,%n
%.%n
	number=%3.%n
Suffix%n
Test %1 Format
"));
            }
        }
Example #4
0
        public void TestCreateMessageAssembly()
        {
            /*
             * This is the most simple form of message generation in which we generate a complete dll with
             * the message resource (and optional versioning).  The dll includes the TestResXClass1.Constants.cs
             * and the TestResXClass1.InstallUtil.cs as generated in the example TestGenerateWin32Resource().
             */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile dllout = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.dll")))
                        {
                            asminfo.WriteAllText(AsminfoFormat);

                            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                            builder1.Add(".AutoLog", true);
                            builder1.Add(".EventSource", "HelloWorld");
                            builder1.Add("Value1", "value for 1", "#MessageId = 1");
                            builder1.BuildResX(resx1.TempPath);

                            Generators.Commands.ResXtoMessageDll(dllout.TempPath, new string[] { resx1.TempPath },
                                                                 tmp.TempPath, asminfo.TempPath, "TestNamespace", Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")), "/debug-");

                            Assert.IsTrue(dllout.Exists);
                        }
        }
        public void TestResXToMcWithCategoryFacilityAndSource()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            //The name of the event source to register, can be qualified with log: "Log-Name/Event-Source"
            builder.Add(".EventSource", "Application/YourAppName");

            //OPTIONAL (default=0): The category id and name for the category of the message, should be unique for this ResX file
            builder.Add(".EventCategory", 0x0F, "MyCategory");

            //OPTIONAL (default=0): The facility code (256-2047) and name to define for these messages
            builder.Add(".Facility", 258, "MyFacility");
            builder.Add("SimpleText", "Message Text", "#MessageId=1");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string contents = mc.ReadAllText();
                Assert.IsTrue(contents.Contains("\r\nMessage Text\r\n."));

                //Facility defined
                Assert.IsTrue(new Regex(@"FACILITY_MYFACILITY\s*=\s*0x102").IsMatch(contents));
                //Category defined
                Assert.IsTrue(new Regex(@"SymbolicName\s*=\s*CATEGORY_MYCATEGORY").IsMatch(contents) && contents.Contains("\r\nMyCategory\r\n.\r\n"));
                //Facility used
                Assert.IsTrue(new Regex(@"Facility\s*=\s*FACILITY_MYFACILITY").IsMatch(contents));
                //Message defined
                Assert.IsTrue(contents.Contains("\r\nMessage Text\r\n."));
            }
        }
Example #6
0
        public void TestGenerateWin32Resource()
        {
            /*
             * Required: This generation type is used to embed the message resource into the current assembly
             * by using a custom Win32Resource setting in the project, for this example you would use:
             *     <Win32Resource>Resources\TestResXClass1.res</Win32Resource>
             *
             * FYI: You must manually add this to the project or else VStudio will erase <ApplicationIcon>
             * The generated res file contains the <ApplicationIcon> as well as assembly version information
             * from the "AssemblyInfo.cs" file included in the project.  If you want to manually supply the
             * assembly info you can point it to a specific assemblyInfo.cs file, or to a dll to extract it
             * from.
             *
             * Optionally you may include "Resources\TestResXClass1.Constants.cs" to define constants for all
             * hresults, facilities, and categories that are defined.
             *
             * Optionally you may include "Resources\TestResXClass1.InstallUtil.cs" to define an installer for
             * the event log registration.  If you need modifications to this installer, simply copy and paste
             * and use it for a starting point.
             */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat);
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                                    Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                                    Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")).Contains("1.2.3.4"));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
                                }
        }
        public void TestDuplicateHResult()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("TestName", "value", "#messageId=23,Severity=Error");
            builder1.Add("TestException", "value", "#messageId=23");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }
            }
        }
        public void TestBuildMcFromResX()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("Testing", "test value 1", "#MessageId=42");

            using (TempDirectory intermediateFiles = new TempDirectory())
                using (TempFile mctxt = TempFile.FromExtension(".mc"))
                {
                    using (TempFile resx1 = TempFile.FromExtension(".resx"))
                    {
                        builder1.BuildResX(resx1.TempPath);
                        Generators.Commands.ResXtoMc(mctxt.TempPath, new string[] { resx1.TempPath });
                    }

                    string mcexe = TestResourceBuilder.FindExe("mc.exe");

                    using (ProcessRunner mc = new ProcessRunner(mcexe, "-U", "{0}", "-r", "{1}", "-h", "{1}"))
                    {
                        mc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, mcexe); };
                        Assert.AreEqual(0, mc.RunFormatArgs(mctxt.TempPath, intermediateFiles.TempPath), "mc.exe failed.");
                    }

                    string rcfile = Path.Combine(intermediateFiles.TempPath, Path.GetFileNameWithoutExtension(mctxt.TempPath) + ".rc");
                    Assert.IsTrue(File.Exists(rcfile));
                    Assert.IsTrue(File.Exists(Path.ChangeExtension(rcfile, ".h")));
                    Assert.IsTrue(File.Exists(Path.Combine(intermediateFiles.TempPath, "MSG00409.bin")));

                    string rcexe = Path.Combine(Path.GetDirectoryName(mcexe), "rc.exe");
                    if (!File.Exists(rcexe))
                    {
                        rcexe = TestResourceBuilder.FindExe("rc.exe");
                    }

                    using (ProcessRunner rc = new ProcessRunner(rcexe, "{0}"))
                    {
                        rc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, rcexe); };
                        Assert.AreEqual(0, rc.RunFormatArgs(rcfile), "rc.exe failed.");
                    }

                    string resfile = Path.ChangeExtension(rcfile, ".res");
                    Assert.IsTrue(File.Exists(resfile));
                    Assert.IsTrue(File.ReadAllText(resfile).Contains("\0t\0e\0s\0t\0 \0v\0a\0l\0u\0e\0 \01"));
                }
        }
        public void TestEmptyResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("Ignored_Lack_of_MessageId", "");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string contents = mc.ReadAllText();
                string[] lines = contents.TrimEnd().Split('\n');
                //no messages:
                Assert.AreEqual(";// MESSAGES", lines[lines.Length - 1].Trim());
            }
        }
        public void TestDuplicateNameInFiles()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("DuplicateName", "value", "#messageId=23");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder2.Add("DuplicateName", "value", "#messageId=24");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                    using (TempFile resx2 = TempFile.FromExtension(".resx"))
                    {
                        builder1.BuildResX(resx1.TempPath);
                        builder2.BuildResX(resx2.TempPath);
                        Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                    }
            }
        }
        public void TestEmptyResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add("Ignored_Lack_of_MessageId", "");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string   contents = mc.ReadAllText();
                string[] lines    = contents.TrimEnd().Split('\n');
                //no messages:
                Assert.AreEqual(";// MESSAGES", lines[lines.Length - 1].Trim());
            }
        }
Example #12
0
        public void TestProjectResXVersionByAssembly()
        {
            /* Demonstrates versioning from an existing assembly... */
            using (TempDirectory tmp = new TempDirectory())
                using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
                    using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
                        using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
                            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
                                using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
                                {
                                    asminfo.WriteAllText(AsminfoFormat);
                                    csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                                    manifest.WriteAllText(AppManifest);

                                    TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                                    builder1.Add(".AutoLog", true);
                                    builder1.Add(".EventSource", "HelloWorld");
                                    builder1.Add("Value1", "value for 1", "#MessageId = 1");
                                    builder1.BuildResX(resx1.TempPath);

                                    using (Stream s = appico.Open())
                                        Properties.Resources.App.Save(s);

                                    Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1",
                                                                    typeof(Generators.Commands).Assembly.Location, String.Empty,
                                                                    Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                                    Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                                    Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc"))
                                                  .Contains(typeof(Generators.Commands).Assembly.GetName().Version.ToString()));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                                    Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
                                }
        }
        public void TestDuplicateCategorySameIdDifferentName()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder1.Add(".EventCategory", 1, "MyCategory");
            builder1.Add("name1", "value", "#messageId=23");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            builder2.Add(".EventCategory", 1, "NotMyCategory");
            builder2.Add("name2", "value", "#messageId=24");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                    using (TempFile resx2 = TempFile.FromExtension(".resx"))
                    {
                        builder1.BuildResX(resx1.TempPath);
                        builder2.BuildResX(resx2.TempPath);
                        Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                    }
            }
        }
        public void TestSimpleStringResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("Value1", "value for 1", "#MessageId = 1");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass2");
            builder2.Add("Value2", "value for 2", "#MessageId = 2");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                using (TempFile resx2 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    builder2.BuildResX(resx2.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                }

                string contents = mc.ReadAllText();

                Assert.IsTrue(contents.Contains("\r\nvalue for 1\r\n.\r\n"));
                Assert.IsTrue(contents.Contains("\r\nvalue for 2\r\n.\r\n"));
            }
        }
        public void TestFormatWithSuffixResXToMc()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("WhatsUp(string s, int i)", "Format!%0\n\tstring={0},\n.\r\n\tnumber={1}.", "#messageId=23");
            builder1.Add(".EventMessageFormat", "Suffix\nTest {0} Format");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string contents = mc.ReadAllText();
                Assert.IsTrue(contents.Contains(@"
Format%!%%0%n
	string=%2,%n
%.%n
	number=%3.%n
Suffix%n
Test %1 Format
"));
            }
        }
        public void TestDuplicateHResult()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("TestName", "value", "#messageId=23,Severity=Error");
            builder1.Add("TestException", "value", "#messageId=23");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }
            }
        }
        public void TestDuplicateNameInFiles()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("DuplicateName", "value", "#messageId=23");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder2.Add("DuplicateName", "value", "#messageId=24");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                using (TempFile resx2 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    builder2.BuildResX(resx2.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                }
            }
        }
        public void TestDuplicateCategorySameIdDifferentName()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add(".EventCategory", 1, "MyCategory");
            builder1.Add("name1", "value", "#messageId=23");
            TestResourceBuilder builder2 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder2.Add(".EventCategory", 1, "NotMyCategory");
            builder2.Add("name2", "value", "#messageId=24");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                using (TempFile resx2 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    builder2.BuildResX(resx2.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath, resx2.TempPath });
                }
            }
        }
        public void TestResXToMcWithCategoryFacilityAndSource()
        {
            TestResourceBuilder builder = new TestResourceBuilder("TestNamespace", "TestResXClass1");

            //The name of the event source to register, can be qualified with log: "Log-Name/Event-Source"
            builder.Add(".EventSource", "Application/YourAppName");

            //OPTIONAL (default=0): The category id and name for the category of the message, should be unique for this ResX file 
            builder.Add(".EventCategory", 0x0F, "MyCategory");

            //OPTIONAL (default=0): The facility code (256-2047) and name to define for these messages
            builder.Add(".Facility", 258, "MyFacility");
            builder.Add("SimpleText", "Message Text", "#MessageId=1");

            using (TempFile mc = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mc.TempPath, new string[] { resx1.TempPath });
                }

                string contents = mc.ReadAllText();
                Assert.IsTrue(contents.Contains("\r\nMessage Text\r\n."));

                //Facility defined
                Assert.IsTrue(new Regex(@"FACILITY_MYFACILITY\s*=\s*0x102").IsMatch(contents));
                //Category defined
                Assert.IsTrue(new Regex(@"SymbolicName\s*=\s*CATEGORY_MYCATEGORY").IsMatch(contents) && contents.Contains("\r\nMyCategory\r\n.\r\n"));
                //Facility used
                Assert.IsTrue(new Regex(@"Facility\s*=\s*FACILITY_MYFACILITY").IsMatch(contents));
                //Message defined
                Assert.IsTrue(contents.Contains("\r\nMessage Text\r\n."));
            }
        }
        public void TestProjectResXVersionByAssembly()
        {
            /* Demonstrates versioning from an existing assembly... */
            using (TempDirectory tmp = new TempDirectory())
            using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
            using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
            using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
            using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
            {
                asminfo.WriteAllText(AsminfoFormat);
                csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                manifest.WriteAllText(AppManifest);

                TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                builder1.Add(".AutoLog", true);
                builder1.Add(".EventSource", "HelloWorld");
                builder1.Add("Value1", "value for 1", "#MessageId = 1");
                builder1.BuildResX(resx1.TempPath);

                using (Stream s = appico.Open())
                    Properties.Resources.App.Save(s);

                Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1",
                    typeof(Generators.Commands).Assembly.Location, String.Empty, 
                    Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc"))
                    .Contains(typeof(Generators.Commands).Assembly.GetName().Version.ToString()));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
            }
        }
        public void TestCreateMessageAssembly()
        {
            /* 
             * This is the most simple form of message generation in which we generate a complete dll with
             * the message resource (and optional versioning).  The dll includes the TestResXClass1.Constants.cs
             * and the TestResXClass1.InstallUtil.cs as generated in the example TestGenerateWin32Resource().
             */
            using (TempDirectory tmp = new TempDirectory())
            using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
            using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
            using (TempFile dllout = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.dll")))
            {
                asminfo.WriteAllText(AsminfoFormat);

                TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                builder1.Add(".AutoLog", true);
                builder1.Add(".EventSource", "HelloWorld");
                builder1.Add("Value1", "value for 1", "#MessageId = 1");
                builder1.BuildResX(resx1.TempPath);

                Generators.Commands.ResXtoMessageDll(dllout.TempPath, new string[] { resx1.TempPath },
                    tmp.TempPath, asminfo.TempPath, "TestNamespace", Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")), "/debug-");

                Assert.IsTrue(dllout.Exists);
            }
        }
        public void TestProjectResXWithBadAssemblyInfo()
        {
            /* Bad assembly info... */
            using (TempDirectory tmp = new TempDirectory())
            using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
            using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
            using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
            using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
            {
                asminfo.WriteAllText(AsminfoFormat + "[InvalidAttribute]");
                csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                manifest.WriteAllText(AppManifest);

                TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                builder1.Add(".AutoLog", true);
                builder1.Add(".EventSource", "HelloWorld");
                builder1.Add("Value1", "value for 1", "#MessageId = 1");
                builder1.BuildResX(resx1.TempPath);

                using (Stream s = appico.Open())
                    Properties.Resources.App.Save(s);

                Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));
            }
        }
        public void TestGenerateWin32Resource()
        {
            /* 
             * Required: This generation type is used to embed the message resource into the current assembly
             * by using a custom Win32Resource setting in the project, for this example you would use:
             *     <Win32Resource>Resources\TestResXClass1.res</Win32Resource>
             *     
             * FYI: You must manually add this to the project or else VStudio will erase <ApplicationIcon>
             * The generated res file contains the <ApplicationIcon> as well as assembly version information
             * from the "AssemblyInfo.cs" file included in the project.  If you want to manually supply the
             * assembly info you can point it to a specific assemblyInfo.cs file, or to a dll to extract it
             * from.
             * 
             * Optionally you may include "Resources\TestResXClass1.Constants.cs" to define constants for all
             * hresults, facilities, and categories that are defined.
             * 
             * Optionally you may include "Resources\TestResXClass1.InstallUtil.cs" to define an installer for
             * the event log registration.  If you need modifications to this installer, simply copy and paste
             * and use it for a starting point.
             */
            using (TempDirectory tmp = new TempDirectory())
            using (TempFile asminfo = TempFile.Attach(Path.Combine(tmp.TempPath, "AssemblyInfo.cs")))
            using (TempFile resx1 = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.resx")))
            using (TempFile csproj = TempFile.Attach(Path.Combine(tmp.TempPath, "TestResXClass1.csproj")))
            using (TempFile manifest = TempFile.Attach(Path.Combine(tmp.TempPath, "App.manifest")))
            using (TempFile appico = TempFile.Attach(Path.Combine(tmp.TempPath, "App.ico")))
            {
                asminfo.WriteAllText(AsminfoFormat);
                csproj.WriteAllText(String.Format(ProjFormat, Path.GetFileName(resx1.TempPath)));
                manifest.WriteAllText(AppManifest);

                TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
                builder1.Add(".AutoLog", true);
                builder1.Add(".EventSource", "HelloWorld");
                builder1.Add("Value1", "value for 1", "#MessageId = 1");
                builder1.BuildResX(resx1.TempPath);

                using (Stream s = appico.Open())
                    Properties.Resources.App.Save(s);

                Generators.Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));

                Assert.IsTrue(Directory.Exists(Path.Combine(tmp.TempPath, "Resources")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\MSG00409.bin")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.mc")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.h")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")));
                Assert.IsTrue(File.ReadAllText(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.rc")).Contains("1.2.3.4"));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.res")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.Constants.cs")));
                Assert.IsTrue(File.Exists(Path.Combine(tmp.TempPath, @"Resources\TestResXClass1.InstallUtil.cs")));
            }
        }
        public void TestBuildMcFromResX()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("Testing", "test value 1", "#MessageId=42");

            using (TempDirectory intermediateFiles = new TempDirectory())
            using (TempFile mctxt = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Generators.Commands.ResXtoMc(mctxt.TempPath, new string[] { resx1.TempPath });
                }

                string mcexe = TestResourceBuilder.FindExe("mc.exe");

                using (ProcessRunner mc = new ProcessRunner(mcexe, "-U", "{0}", "-r", "{1}", "-h", "{1}"))
                {
                    mc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, mcexe); };
                    Assert.AreEqual(0, mc.RunFormatArgs(mctxt.TempPath, intermediateFiles.TempPath), "mc.exe failed.");
                }

                string rcfile = Path.Combine(intermediateFiles.TempPath, Path.GetFileNameWithoutExtension(mctxt.TempPath) + ".rc");
                Assert.IsTrue(File.Exists(rcfile));
                Assert.IsTrue(File.Exists(Path.ChangeExtension(rcfile, ".h")));
                Assert.IsTrue(File.Exists(Path.Combine(intermediateFiles.TempPath, "MSG00409.bin")));

                string rcexe = Path.Combine(Path.GetDirectoryName(mcexe), "rc.exe");
                if (!File.Exists(rcexe))
                    rcexe = TestResourceBuilder.FindExe("rc.exe");

                using (ProcessRunner rc = new ProcessRunner(rcexe, "{0}"))
                {
                    rc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, rcexe); };
                    Assert.AreEqual(0, rc.RunFormatArgs(rcfile), "rc.exe failed.");
                }

                string resfile = Path.ChangeExtension(rcfile, ".res");
                Assert.IsTrue(File.Exists(resfile));
                Assert.IsTrue(File.ReadAllText(resfile).Contains("\0t\0e\0s\0t\0 \0v\0a\0l\0u\0e\0 \01"));
            }
        }