Beispiel #1
0
        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);
                        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 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);

                                    Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1",
                                                         typeof(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(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 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);

                                    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 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);

                                    Commands.ProjectResX(csproj.TempPath, @"Resources\TestResXClass1", null, String.Empty, Path.GetDirectoryName(TestResourceBuilder.FindExe("mc.exe")));
                                }
        }
        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);

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

                            Assert.IsTrue(dllout.Exists);
                        }
        }